Skip to content

Commit

Permalink
Merge pull request #49 from jeffreybakker/feature/extension_6
Browse files Browse the repository at this point in the history
Feature/extension 6
  • Loading branch information
jeffreybakker authored Aug 12, 2017
2 parents 662ed33 + 24b3491 commit 5d0c224
Show file tree
Hide file tree
Showing 25 changed files with 813 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public NewCardBean provideAccess(String authToken, String iBAN, String username)

account.addHolder(holder);
accountRepository.save(account);

Card card = new Card(holder, account, CardUtil.generateCardNumber(cardRepository));
cardRepository.save(card);

Expand Down
79 changes: 79 additions & 0 deletions src/main/java/honours/ing/banq/account/Account.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package honours.ing.banq.account;

import javax.persistence.*;
import java.math.BigDecimal;

/**
* @author Jeffrey Bakker
* @since 7-8-17
*/
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Account {

@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Integer id;

@OneToOne(targetEntity = BankAccount.class, cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
protected BankAccount account;

protected BigDecimal balance;

/**
* @deprecated empty constructor for the Hibernate ORM
*/
@Deprecated
public Account() { }

/**
* Creates a new {@link Account}.
* @param account the bank account to which this account belongs
*/
public Account(BankAccount account) {
this.account = account;
this.balance = BigDecimal.ZERO;
}

/**
* Returns whether the given amount can be payed from the balance on this account.
* @param amount the amount the user wishes to pay
* @return {@code true} if the user can pay the amount
*/
public boolean canPayAmount(BigDecimal amount) {
return balance.subtract(amount).compareTo(BigDecimal.ZERO) >= 0;
}

/**
* Returns the {@link BankAccount} to which this account belongs.
* @return a {@link BankAccount}
*/
public BankAccount getAccount() {
return account;
}

/**
* Adds balance to the balance of the account.
* @param delta the difference in balance
*/
public void addBalance(BigDecimal delta) {
balance = balance.add(delta);
}

/**
* Returns the balance on the account.
* @return the balance
*/
public BigDecimal getBalance() {
return balance;
}

/**
* Returns the ID of the account.
* @return the ID
*/
public Integer getId() {
return id;
}

}
105 changes: 26 additions & 79 deletions src/main/java/honours/ing/banq/account/BankAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ public class BankAccount {
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

private BigDecimal balance;
private BigDecimal interest;
private BigDecimal lowestBalance;
private BigDecimal overdraftLimit;

@ManyToOne(targetEntity = Customer.class)
private Customer primaryHolder;

@ManyToMany(targetEntity = Customer.class)
private List<Customer> holders;

@OneToOne(targetEntity = CheckingAccount.class, cascade = CascadeType.ALL, orphanRemoval = true)
private CheckingAccount checkingAccount;

@OneToOne(targetEntity = SavingAccount.class, cascade = CascadeType.ALL, orphanRemoval = true)
private SavingAccount savingAccount;

/**
* @deprecated empty constructor for spring
*/
Expand All @@ -37,105 +38,51 @@ public BankAccount() { }

/**
* Creates a new {@link BankAccount} with the given primary holder and 0.0 for balance.
* @param primaryHolder
* @param primaryHolder the primary holder of this account
*/
public BankAccount(Customer primaryHolder) {
this.primaryHolder = primaryHolder;
balance = new BigDecimal(0.0);
interest = new BigDecimal(0.0);
lowestBalance = new BigDecimal(0.0);
overdraftLimit = new BigDecimal(0.0);
holders = new ArrayList<>();
}

/**
* Returns the ID of the account.
* @return the ID
*/
public Integer getId() {
return id;
}

/**
* Returns the balance of the account.
* @return the balance
*/
public BigDecimal getBalance() {
return balance;
}

/**
* Adds balance on the account.
* @param delta the difference in balance
*/
public void addBalance(BigDecimal delta) {
balance = balance.add(delta);
if (balance.compareTo(lowestBalance) < 0) {
lowestBalance = balance;
}
}

/**
* Returns whether the amount can be payed from this bank account with respect to the overdraft limit.
* @param amount the amount the user wishes to pay
* @return {@code true} if the account has enough balance
*/
public boolean canPayAmount(BigDecimal amount) {
return balance.subtract(amount).compareTo(overdraftLimit) >= 0;
}

/**
* Adds interest to the interest that will have to be payed at the end of the month.
* @param delta the amount of interest to add
*/
public void addInterest(BigDecimal delta) {
interest = interest.add(delta);
checkingAccount = new CheckingAccount(this);
}

/**
* Returns the amount of interest that will have to be payed at the end of the month.
* @return the amount of interest
* Returns this {@link BankAccount}'s {@link CheckingAccount}.
* @return a {@link CheckingAccount}
*/
public BigDecimal getInterest() {
return interest;
public CheckingAccount getCheckingAccount() {
return checkingAccount;
}

/**
* Resets the amount of interest that still has to be cashed in.
* Adds a {@link SavingAccount} to this {@link BankAccount}.
*/
public void resetInterest() {
interest = new BigDecimal(0.0);
public void addSavingAccount() {
savingAccount = new SavingAccount(this);
}

/**
* Returns the lowest balance on this account since the last reset.
* @return the lowest balance
* Returns this {@link BankAccount}'s {@link SavingAccount}.
* @return a {@link SavingAccount}
*/
public BigDecimal getLowestBalance() {
return lowestBalance;
public SavingAccount getSavingAccount() {
return savingAccount;
}

/**
* Resets the lowest balance to the current balance.
* Removes this {@link BankAccount}'s {@link SavingAccount}.
*/
public void resetLowestBalance() {
lowestBalance = balance;
public void removeSavingAccount() {
savingAccount = null;
}

/**
* Returns the maximum amount of negative balance that this account may have.
* @return the overdraft limit
*/
public BigDecimal getOverdraftLimit() {
return overdraftLimit.multiply(new BigDecimal(-1.0));
}

/**
* Sets the maximum amount of negative balance that this account may have.
* @param overdraftLimit the overdraft limit
* Returns the ID of the account.
* @return the ID
*/
public void setOverdraftLimit(BigDecimal overdraftLimit) {
this.overdraftLimit = overdraftLimit.multiply(new BigDecimal(-1.0));
public Integer getId() {
return id;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,10 @@ Object setOverdraftLimit(
@JsonRpcParam("overdraftLimit") double overdraftLimit)
throws NotAuthorizedError, InvalidParamValueError;

Object openSavingsAccount(@JsonRpcParam("authToken") String authToken, @JsonRpcParam("iBAN") String iBAN)
throws InvalidParamValueError, NotAuthorizedError;

Object closeSavingsAccount(@JsonRpcParam("authToken") String authToken, @JsonRpcParam("iBAN") String iBAN)
throws InvalidParamValueError, NotAuthorizedError;

}
68 changes: 44 additions & 24 deletions src/main/java/honours/ing/banq/account/BankAccountServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,21 @@ public NewAccountBean openAdditionalAccount(String authToken) throws NotAuthoriz
@Transactional
@Override
public Object closeAccount(String authToken, String iBAN) throws NotAuthorizedError, InvalidParamValueError {
BankAccount account = auth.getAuthorizedBankAccount(authToken, iBAN);
Customer customer = auth.getAuthorizedCustomer(authToken);

long accountNumber = IBANUtil.getAccountNumber(iBAN);
BankAccount account = repository.findOne((int) accountNumber);
if (!account.getPrimaryHolder().equals(customer)) {
throw new NotAuthorizedError();
}

if (account == null) {
throw new InvalidParamValueError("Bank account does not exist");
Account savingsAccount = account.getSavingAccount();
if (savingsAccount != null && savingsAccount.getBalance().compareTo(BigDecimal.ZERO) != 0) {
throw new InvalidParamValueError("Account balance needs to be cleared");
}

if (!account.getPrimaryHolder().equals(customer)) {
throw new NotAuthorizedError();
Account checkingAccount = account.getCheckingAccount();
if (checkingAccount != null && checkingAccount.getBalance().compareTo(BigDecimal.ZERO) != 0) {
throw new InvalidParamValueError("Account balance needs to be cleared");
}

List<Card> cards = cardRepository.findByAccount(account);
Expand All @@ -125,46 +129,62 @@ public Object closeAccount(String authToken, String iBAN) throws NotAuthorizedEr

@Override
public OverdraftBean getOverdraftLimit(String authToken, String iBAN) throws NotAuthorizedError, InvalidParamValueError {
Customer customer = auth.getAuthorizedCustomer(authToken);

long accountNumber = IBANUtil.getAccountNumber(iBAN);
BankAccount account = repository.findOne((int) accountNumber);
return new OverdraftBean(
auth.getAuthorizedBankAccount(authToken, iBAN).getCheckingAccount().getOverdraftLimit());
}

if (account == null) {
throw new InvalidParamValueError("Bank account does not exist");
}
@Transactional
@Override
public Object setOverdraftLimit(String authToken, String iBAN, double overdraftLimit) throws NotAuthorizedError, InvalidParamValueError {
Customer customer = auth.getAuthorizedCustomer(authToken);
BankAccount account = auth.getAuthorizedBankAccount(authToken, iBAN);

if (!account.getPrimaryHolder().equals(customer)) {
throw new NotAuthorizedError();
}

return new OverdraftBean(account.getOverdraftLimit());
if (overdraftLimit < 0.0d) {
throw new InvalidParamValueError("The overdraft limit must be a positive value");
}

account.getCheckingAccount().setOverdraftLimit(new BigDecimal(overdraftLimit));
repository.save(account);

return new Object();
}

@Transactional
@Override
public Object setOverdraftLimit(String authToken, String iBAN, double overdraftLimit) throws NotAuthorizedError, InvalidParamValueError {
public Object openSavingsAccount(String authToken, String iBAN) throws InvalidParamValueError, NotAuthorizedError {
Customer customer = auth.getAuthorizedCustomer(authToken);
BankAccount account = auth.getAuthorizedBankAccount(authToken, iBAN);

long accountNumber = IBANUtil.getAccountNumber(iBAN);
BankAccount account = repository.findOne((int) accountNumber);

if (account == null) {
throw new InvalidParamValueError("Bank account does not exist");
if (!account.getPrimaryHolder().equals(customer)) {
throw new NotAuthorizedError();
}

account.addSavingAccount();
repository.save(account);

return new Object();
}

@Override
public Object closeSavingsAccount(String authToken, String iBAN) throws InvalidParamValueError, NotAuthorizedError {
Customer customer = auth.getAuthorizedCustomer(authToken);
BankAccount account = auth.getAuthorizedBankAccount(authToken, iBAN);

if (!account.getPrimaryHolder().equals(customer)) {
throw new NotAuthorizedError();
}

if (overdraftLimit < 0.0d) {
throw new InvalidParamValueError("The overdraft limit must be a positive value");
if (!account.getSavingAccount().getBalance().equals(BigDecimal.ZERO)) {
throw new InvalidParamValueError("Account balance needs to be cleared");
}

account.setOverdraftLimit(new BigDecimal(overdraftLimit));
account.removeSavingAccount();
repository.save(account);

return new Object();
}

}
Loading

0 comments on commit 5d0c224

Please sign in to comment.