#125 Money Transfer Service Review
Perform a code review:
package com.example.demo.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Component
@Service
public class TransferService {
@Value("${accountService.url}")
private String url;
@Value("${transfer.comission=10}")
private double commission;
/**
* REST service for retrieving an account by BIK
*/
private AccountService accountService;
@Autowired
private AccountRepository accountRepository;
@Autowired
public AccountService accountService() {
return new AccountService(url);
}
@Transactional
public void sendMoney(double amount, long accountFrom, long bankBik) {
Account fromAccount = accountRepository.findByBik(bankBik)
.orElseThrow(() -> new IllegalArgumentException("Value not found"));
Account to = accountService.getAccountByBik(bankBikTo);
try {
makeTransfer(accountFrom, amount);
} catch (Exception ex) {
log.info("Could not transfer money");
}
}
@Transactional
private void makeTransfer(long accountFrom, double amount) {
Account accFrom = accountRepository.findByAccountNumber(accountFrom);
Account accTo = accountRepository.findByAccountNumber(toAccountNumber);
accFrom.setBalance(accFrom.getBalance() - amount);
accTo.setBalance(accTo.getBalance() + amount * (1 - commission));
accountRepository.save(accTo);
accountRepository.save(accFrom);
}
}Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor