Skip to content

Commit

Permalink
Merge pull request #47 from jeffreybakker/extension_5
Browse files Browse the repository at this point in the history
Extension 5
  • Loading branch information
jeffreybakker authored Aug 5, 2017
2 parents 34f8a66 + ca63234 commit ea3b9a2
Show file tree
Hide file tree
Showing 24 changed files with 761 additions and 150 deletions.
8 changes: 8 additions & 0 deletions src/main/java/honours/ing/banq/Application.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package honours.ing.banq;

import honours.ing.banq.redirect.RedirectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
Expand All @@ -16,8 +18,14 @@
@EnableWebMvc
public class Application {

private static final Logger logger = LoggerFactory.getLogger(Application.class);

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

public static Logger getLogger() {
return logger;
}

}
91 changes: 78 additions & 13 deletions src/main/java/honours/ing/banq/account/BankAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import honours.ing.banq.customer.Customer;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -17,7 +18,10 @@ public class BankAccount {
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

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

@ManyToOne(targetEntity = Customer.class)
private Customer primaryHolder;
Expand All @@ -28,16 +32,19 @@ public class BankAccount {
/**
* @deprecated empty constructor for spring
*/
public BankAccount() {
}
@Deprecated
public BankAccount() { }

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

Expand All @@ -53,24 +60,82 @@ public Integer getId() {
* Returns the balance of the account.
* @return the balance
*/
public Double getBalance() {
public BigDecimal getBalance() {
return balance;
}

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

/**
* Adds balance on the account.
* @param balance the difference in 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);
}

/**
* Returns the amount of interest that will have to be payed at the end of the month.
* @return the amount of interest
*/
public BigDecimal getInterest() {
return interest;
}

/**
* Resets the amount of interest that still has to be cashed in.
*/
public void resetInterest() {
interest = new BigDecimal(0.0);
}

/**
* Returns the lowest balance on this account since the last reset.
* @return the lowest balance
*/
public BigDecimal getLowestBalance() {
return lowestBalance;
}

/**
* Resets the lowest balance to the current balance.
*/
public void resetLowestBalance() {
lowestBalance = balance;
}

/**
* 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
*/
public void addBalance(Double balance) {
this.balance += balance;
public void setOverdraftLimit(BigDecimal overdraftLimit) {
this.overdraftLimit = overdraftLimit.multiply(new BigDecimal(-1.0));
}

/**
Expand Down
30 changes: 28 additions & 2 deletions src/main/java/honours/ing/banq/account/BankAccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.googlecode.jsonrpc4j.JsonRpcService;
import honours.ing.banq.InvalidParamValueError;
import honours.ing.banq.account.bean.NewAccountBean;
import honours.ing.banq.account.bean.OverdraftBean;
import honours.ing.banq.auth.NotAuthorizedError;

/**
Expand Down Expand Up @@ -55,7 +56,32 @@ NewAccountBean openAccount(
* @throws NotAuthorizedError if the user is not authorized to close the account
* @throws InvalidParamValueError if some of the parameters are invalid, please see the message
*/
Object closeAccount(@JsonRpcParam("authToken") String authToken, @JsonRpcParam("iBAN") String iBAN) throws
NotAuthorizedError, InvalidParamValueError;
Object closeAccount(@JsonRpcParam("authToken") String authToken, @JsonRpcParam("iBAN") String iBAN)
throws NotAuthorizedError, InvalidParamValueError;

/**
* Method that asks the server for the overdraft limit of a bank account.
* @param authToken the authentication token, obtained with {@code getAuthToken}
* @param iBAN the number of the bank account
* @return a dictionary containing the overdraft limit
* @throws NotAuthorizedError if the user is not authorized to see the overdraft limit on this account
* @throws InvalidParamValueError if one of the parameters was invalid
*/
OverdraftBean getOverdraftLimit(@JsonRpcParam("authToken") String authToken, @JsonRpcParam("iBAN") String iBAN)
throws NotAuthorizedError, InvalidParamValueError;

/**
* Method that sets the overdraft limit for a bank account.
* @param authToken the authentication token, obtained with {@code getAuthToken}
* @param iBAN the number of the bank account
* @param overdraftLimit the new overdraft limit of the account
* @return an empty dictionary if successful
* @throws NotAuthorizedError if the user is not authorized to set the overdraft limit on this account
* @throws InvalidParamValueError if one of the parameters was invalid
*/
Object setOverdraftLimit(
@JsonRpcParam("authToken") String authToken, @JsonRpcParam("iBAN") String iBAN,
@JsonRpcParam("overdraftLimit") double overdraftLimit)
throws NotAuthorizedError, InvalidParamValueError;

}
47 changes: 47 additions & 0 deletions src/main/java/honours/ing/banq/account/BankAccountServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import honours.ing.banq.InvalidParamValueError;
import honours.ing.banq.account.bean.NewAccountBean;
import honours.ing.banq.account.bean.OverdraftBean;
import honours.ing.banq.auth.AuthService;
import honours.ing.banq.auth.NotAuthorizedError;
import honours.ing.banq.card.Card;
Expand All @@ -15,6 +16,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
Expand Down Expand Up @@ -114,4 +116,49 @@ public Object closeAccount(String authToken, String iBAN) throws NotAuthorizedEr

return new Object();
}

@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);

if (account == null) {
throw new InvalidParamValueError("Bank account does not exist");
}

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

return new OverdraftBean(account.getOverdraftLimit());
}

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

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();
}

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

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

return new Object();
}

}
24 changes: 24 additions & 0 deletions src/main/java/honours/ing/banq/account/bean/OverdraftBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package honours.ing.banq.account.bean;

import java.math.BigDecimal;

/**
* @author jeffrey
* @since 5-8-17
*/
public class OverdraftBean {

private double overdraftLimit;

public OverdraftBean(BigDecimal overdraftLimit) {
this.overdraftLimit = overdraftLimit.doubleValue();
}

public double getOverdraftLimit() {
return overdraftLimit;
}

public void setOverdraftLimit(double overdraftLimit) {
this.overdraftLimit = overdraftLimit;
}
}
4 changes: 2 additions & 2 deletions src/main/java/honours/ing/banq/auth/AuthServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import honours.ing.banq.card.CardRepository;
import honours.ing.banq.customer.Customer;
import honours.ing.banq.customer.CustomerRepository;
import honours.ing.banq.time.TimeServiceImpl;
import honours.ing.banq.util.IBANUtil;
import honours.ing.banq.time.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -57,7 +57,7 @@ public AuthToken getAuthToken(String username, String password) throws Authentic

Authentication auth = null;
Calendar exp = Calendar.getInstance();
exp.setTime(TimeUtil.getDate());
exp.setTime(TimeServiceImpl.getCurDate());
exp.add(Calendar.SECOND, VALIDITY);

while (auth == null) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/honours/ing/banq/auth/Authentication.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package honours.ing.banq.auth;

import honours.ing.banq.customer.Customer;
import honours.ing.banq.time.TimeUtil;
import honours.ing.banq.time.TimeServiceImpl;

import javax.persistence.*;
import java.util.Date;
Expand Down Expand Up @@ -50,7 +50,7 @@ public Date getExpiration() {
}

public boolean hasExpired() {
return TimeUtil.currentTimeMillis() >= expiration.getTime();
return TimeServiceImpl.currentTimeMillis() >= expiration.getTime();
}

}
6 changes: 3 additions & 3 deletions src/main/java/honours/ing/banq/card/Card.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import honours.ing.banq.account.BankAccount;
import honours.ing.banq.customer.Customer;
import honours.ing.banq.time.TimeUtil;
import honours.ing.banq.time.TimeServiceImpl;

import javax.persistence.*;
import java.util.Calendar;
Expand Down Expand Up @@ -51,7 +51,7 @@ public Card(Customer holder, BankAccount account, String cardNumber) {
pin = generatePin();

Calendar expiration = Calendar.getInstance();
expiration.setTimeInMillis(TimeUtil.currentTimeMillis() + DURABILITY);
expiration.setTimeInMillis(TimeServiceImpl.currentTimeMillis() + DURABILITY);
expirationDate = expiration.getTime();

failedAttempts = 0;
Expand All @@ -78,7 +78,7 @@ public String getPin() {
}

public boolean hasExpired() {
return expirationDate.getTime() >= TimeUtil.getDate().getTime();
return expirationDate.getTime() >= TimeServiceImpl.getCurDate().getTime();
}

public Date getExpirationDate() {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/honours/ing/banq/event/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package honours.ing.banq.event;

/**
* @author jeffrey
* @since 5-8-17
*/
public interface Event {

void execute(long time);
long nextIteration(long lastIteration);

}
Loading

0 comments on commit ea3b9a2

Please sign in to comment.