TransactionServiceImpl.java
package in.ravikalla.cloudBank.service.UserServiceImpl;
import java.math.BigDecimal;
import java.security.Principal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import in.ravikalla.cloudBank.dao.PrimaryAccountDao;
import in.ravikalla.cloudBank.dao.PrimaryTransactionDao;
import in.ravikalla.cloudBank.dao.RecipientDao;
import in.ravikalla.cloudBank.dao.SavingsAccountDao;
import in.ravikalla.cloudBank.dao.SavingsTransactionDao;
import in.ravikalla.cloudBank.domain.PrimaryAccount;
import in.ravikalla.cloudBank.domain.PrimaryTransaction;
import in.ravikalla.cloudBank.domain.Recipient;
import in.ravikalla.cloudBank.domain.SavingsAccount;
import in.ravikalla.cloudBank.domain.SavingsTransaction;
import in.ravikalla.cloudBank.domain.User;
import in.ravikalla.cloudBank.service.TransactionService;
import in.ravikalla.cloudBank.service.UserService;
@Service
public class TransactionServiceImpl implements TransactionService {
@Autowired
private UserService userService;
@Autowired
private PrimaryTransactionDao primaryTransactionDao;
@Autowired
private SavingsTransactionDao savingsTransactionDao;
@Autowired
private PrimaryAccountDao primaryAccountDao;
@Autowired
private SavingsAccountDao savingsAccountDao;
@Autowired
private RecipientDao recipientDao;
public List<PrimaryTransaction> findPrimaryTransactionList(String username){
User user = userService.findByUsername(username);
List<PrimaryTransaction> primaryTransactionList = user.getPrimaryAccount().getPrimaryTransactionList();
return primaryTransactionList;
}
public List<SavingsTransaction> findSavingsTransactionList(String username) {
User user = userService.findByUsername(username);
List<SavingsTransaction> savingsTransactionList = user.getSavingsAccount().getSavingsTransactionList();
return savingsTransactionList;
}
public void savePrimaryDepositTransaction(PrimaryTransaction primaryTransaction) {
primaryTransactionDao.save(primaryTransaction);
}
public void saveSavingsDepositTransaction(SavingsTransaction savingsTransaction) {
savingsTransactionDao.save(savingsTransaction);
}
public void savePrimaryWithdrawTransaction(PrimaryTransaction primaryTransaction) {
primaryTransactionDao.save(primaryTransaction);
}
public void saveSavingsWithdrawTransaction(SavingsTransaction savingsTransaction) {
savingsTransactionDao.save(savingsTransaction);
}
public void betweenAccountsTransfer(String transferFrom, String transferTo, String amount, PrimaryAccount primaryAccount, SavingsAccount savingsAccount) throws Exception {
if (transferFrom.equalsIgnoreCase("Primary") && transferTo.equalsIgnoreCase("Savings")) {
primaryAccount.setAccountBalance(primaryAccount.getAccountBalance().subtract(new BigDecimal(amount)));
savingsAccount.setAccountBalance(savingsAccount.getAccountBalance().add(new BigDecimal(amount)));
primaryAccountDao.save(primaryAccount);
savingsAccountDao.save(savingsAccount);
Date date = new Date();
PrimaryTransaction primaryTransaction = new PrimaryTransaction(date, "Between account transfer from "+transferFrom+" to "+transferTo, "Account", "Finished", Double.parseDouble(amount), primaryAccount.getAccountBalance(), primaryAccount);
primaryTransactionDao.save(primaryTransaction);
} else if (transferFrom.equalsIgnoreCase("Savings") && transferTo.equalsIgnoreCase("Primary")) {
primaryAccount.setAccountBalance(primaryAccount.getAccountBalance().add(new BigDecimal(amount)));
savingsAccount.setAccountBalance(savingsAccount.getAccountBalance().subtract(new BigDecimal(amount)));
primaryAccountDao.save(primaryAccount);
savingsAccountDao.save(savingsAccount);
Date date = new Date();
SavingsTransaction savingsTransaction = new SavingsTransaction(date, "Between account transfer from "+transferFrom+" to "+transferTo, "Transfer", "Finished", Double.parseDouble(amount), savingsAccount.getAccountBalance(), savingsAccount);
savingsTransactionDao.save(savingsTransaction);
} else {
throw new Exception("Invalid Transfer");
}
}
public List<Recipient> findRecipientList(Principal principal) {
String username = principal.getName();
List<Recipient> recipientList = recipientDao.findAll().stream()
.filter(recipient -> username.equals(recipient.getUser().getUsername()))
.collect(Collectors.toList());
return recipientList;
}
public Recipient saveRecipient(Recipient recipient) {
return recipientDao.save(recipient);
}
public Recipient findRecipientByName(String recipientName) {
return recipientDao.findByName(recipientName);
}
public void deleteRecipientByName(String recipientName) {
recipientDao.deleteByName(recipientName);
}
public void toSomeoneElseTransfer(Recipient recipient, String accountType, String amount, PrimaryAccount primaryAccount, SavingsAccount savingsAccount) {
if (accountType.equalsIgnoreCase("Primary")) {
primaryAccount.setAccountBalance(primaryAccount.getAccountBalance().subtract(new BigDecimal(amount)));
primaryAccountDao.save(primaryAccount);
Date date = new Date();
PrimaryTransaction primaryTransaction = new PrimaryTransaction(date, "Transfer to recipient "+recipient.getName(), "Transfer", "Finished", Double.parseDouble(amount), primaryAccount.getAccountBalance(), primaryAccount);
primaryTransactionDao.save(primaryTransaction);
} else if (accountType.equalsIgnoreCase("Savings")) {
savingsAccount.setAccountBalance(savingsAccount.getAccountBalance().subtract(new BigDecimal(amount)));
savingsAccountDao.save(savingsAccount);
Date date = new Date();
SavingsTransaction savingsTransaction = new SavingsTransaction(date, "Transfer to recipient "+recipient.getName(), "Transfer", "Finished", Double.parseDouble(amount), savingsAccount.getAccountBalance(), savingsAccount);
savingsTransactionDao.save(savingsTransaction);
}
}
}