#125 Money Transfer Service Review

Companies:
Bastion
Themes:
SpringSpring BootTransactionsCode Review

Perform a code review:

Java
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

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in