Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dillon Connolly assessment #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions src/main/java/com/abc/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Calendar;
import java.util.Date;

public class Account {

Expand All @@ -11,47 +13,77 @@ public class Account {

private final int accountType;
public List<Transaction> transactions;
private Date lastDeposit;
private Date lastWithdrawal;

public Account(int accountType) {
this.accountType = accountType;
this.transactions = new ArrayList<Transaction>();
lastDeposit = lastWithdrawal = DateProvider.getInstance().now();
}

public void deposit(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("amount must be greater than zero");
} else {
transactions.add(new Transaction(amount));
lastDeposit = DateProvider.getInstance().now();
}
}

//Just made this to test the date variables. If they are recording properly then that suggests that the maxi-function operates correctly.
public String getDepositDate(){
return lastDeposit.toString();
}

public String getWithdrawalDate(){
return lastWithdrawal.toString();
}
//Remove after testing. This is just to test the maxi-savings over 10 days
public void setWithdrawlDate(Date date){
lastWithdrawal = date;
}

public void withdraw(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("amount must be greater than zero");
} else {
transactions.add(new Transaction(-amount));
lastWithdrawal = DateProvider.getInstance().now();
}
}
//I added in the accrue daily rates. This method will now return just the interest for 1 day rather than a year.
//In the future the program needs a timer so that everyday at a given time this method is called and the daily interest
//is added to each customer's account.

public double interestEarned() {
double amount = sumTransactions();
switch(accountType){
case SAVINGS:
if (amount <= 1000)
return amount * 0.001;
//0.01% for first 1000 per-annum
return amount * (0.001/365);
else
return 1 + (amount-1000) * 0.002;
//0.2% after first 1000 per-annum
return 1 + (amount-1000) * (0.002/365);
// case SUPER_SAVINGS:
// if (amount <= 4000)
// return 20;
case MAXI_SAVINGS:
if (amount <= 1000)
Date currentDate = DateProvider.getInstance().now();
//5% per annum after 10 days without withdrawing
if(currentDate.compareTo(lastWithdrawal)>=10){
return amount * (0.05/365);
}
//.1% per annum if the customer withdrew in the past 10 days
return amount * (0.001/365);
//Previous code for the MAXI_SAVINGS account interest calculation.
/*if (amount <= 1000)
return amount * 0.02;
if (amount <= 2000)
return 20 + (amount-1000) * 0.05;
return 70 + (amount-2000) * 0.1;
return 70 + (amount-2000) * 0.1;*/
default:
return amount * 0.001;
return amount * (0.001/365);
}
}

Expand All @@ -69,5 +101,5 @@ private double checkIfTransactionsExist(boolean checkAll) {
public int getAccountType() {
return accountType;
}

}
6 changes: 4 additions & 2 deletions src/main/java/com/abc/Bank.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

public class Bank {
private List<Customer> customers;

private int idCount = 0;

public Bank() {
customers = new ArrayList<Customer>();
}
Expand Down Expand Up @@ -36,7 +37,8 @@ public double totalInterestPaid() {

public String getFirstCustomer() {
try {
customers = null;
//Setting customers to null will erase the customer list.
//customers = null;
return customers.get(0).getName();
} catch (Exception e){
e.printStackTrace();
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/com/abc/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,26 @@

public class Customer {
private String name;
//It's important to have a customer id so if two customers named "John Smith" come along you can differentiate between them.
//Ideally you would use a hash function to create a unique id or maybe a list of all customers and set the id equal to their
//position in the list.
private int customer_id;

private List<Account> accounts;

public Customer(String name) {
public Customer(String name, int id) {
this.name = name;
this.accounts = new ArrayList<Account>();
this.customer_id = id;
}

public String getName() {
return name;
}

public int getCustomer_id(){
return customer_id;
}

public Customer openAccount(Account account) {
accounts.add(account);
Expand Down Expand Up @@ -75,4 +85,15 @@ private String statementForAccount(Account a) {
private String toDollars(double d){
return String.format("$%,.2f", abs(d));
}

//To transfer between accounts just withdraw from account 1 and deposit that amount in account 2.
public void transferBtwnAccounts(Account transferFrom, Account transferTo, double amount){
if(amount <= 0){
throw new IllegalArgumentException("amount must be greater than zero");
}
else{
transferFrom.withdraw(amount);
transferTo.deposit(amount);
}
}
}
1 change: 1 addition & 0 deletions src/main/java/com/abc/DateProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public static DateProvider getInstance() {
public Date now() {
return Calendar.getInstance().getTime();
}

}
4 changes: 2 additions & 2 deletions src/main/java/com/abc/Transaction.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.abc;

import java.util.Calendar;
//import java.util.Calendar;
import java.util.Date;

public class Transaction {
Expand All @@ -12,5 +12,5 @@ public Transaction(double amount) {
this.amount = amount;
this.transactionDate = DateProvider.getInstance().now();
}

}
39 changes: 27 additions & 12 deletions src/test/java/com/abc/BankTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
package com.abc;

import org.junit.Test;
import java.util.Calendar;
import java.util.Date;

import static org.junit.Assert.assertEquals;

public class BankTest {
private static final double DOUBLE_DELTA = 1e-15;
int customer_id =0;

@Test
public void firstCustomer() {
Bank bank = new Bank();
assertEquals("Error",bank.getFirstCustomer());

Customer john = new Customer("John", customer_id++);
john.openAccount(new Account(Account.SAVINGS));
bank.addCustomer(john);

assertEquals("John", bank.getFirstCustomer());
}

@Test
public void customerSummary() {
Bank bank = new Bank();
Customer john = new Customer("John");
Customer john = new Customer("John",customer_id++);
john.openAccount(new Account(Account.CHECKING));
bank.addCustomer(john);

Expand All @@ -21,34 +36,34 @@ public void customerSummary() {
public void checkingAccount() {
Bank bank = new Bank();
Account checkingAccount = new Account(Account.CHECKING);
Customer bill = new Customer("Bill").openAccount(checkingAccount);
Customer bill = new Customer("Bill", customer_id++).openAccount(checkingAccount);
bank.addCustomer(bill);

checkingAccount.deposit(100.0);

assertEquals(0.1, bank.totalInterestPaid(), DOUBLE_DELTA);
assertEquals((100 *(.001/365)), bank.totalInterestPaid(), DOUBLE_DELTA);
}

@Test
public void savings_account() {
Bank bank = new Bank();
Account checkingAccount = new Account(Account.SAVINGS);
bank.addCustomer(new Customer("Bill").openAccount(checkingAccount));
Account savingsAccount = new Account(Account.SAVINGS);
bank.addCustomer(new Customer("Bill", customer_id++).openAccount(savingsAccount));

checkingAccount.deposit(1500.0);
savingsAccount.deposit(1500.0);

assertEquals(2.0, bank.totalInterestPaid(), DOUBLE_DELTA);
assertEquals((1 + (1500-1000) * (0.002/365)), bank.totalInterestPaid(), DOUBLE_DELTA);
}

@Test
public void maxi_savings_account() {
Bank bank = new Bank();
Account checkingAccount = new Account(Account.MAXI_SAVINGS);
bank.addCustomer(new Customer("Bill").openAccount(checkingAccount));
Account maxiAccount = new Account(Account.MAXI_SAVINGS);
bank.addCustomer(new Customer("Bill", customer_id++).openAccount(maxiAccount));

checkingAccount.deposit(3000.0);
maxiAccount.deposit(3000.0);

assertEquals(170.0, bank.totalInterestPaid(), DOUBLE_DELTA);
assertEquals((3000*(.001/365)), bank.totalInterestPaid(), DOUBLE_DELTA);
}

//need to test maxi saving for 10+ days.
}
27 changes: 22 additions & 5 deletions src/test/java/com/abc/CustomerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
import static org.junit.Assert.assertEquals;

public class CustomerTest {
int customer_id = 0;
private static final double DOUBLE_DELTA = 1e-15;

@Test //Test customer statement generation
public void testApp(){

Account checkingAccount = new Account(Account.CHECKING);
Account savingsAccount = new Account(Account.SAVINGS);

Customer henry = new Customer("Henry").openAccount(checkingAccount).openAccount(savingsAccount);
Customer henry = new Customer("Henry", customer_id++).openAccount(checkingAccount).openAccount(savingsAccount);

checkingAccount.deposit(100.0);
savingsAccount.deposit(4000.0);
Expand All @@ -35,23 +37,38 @@ public void testApp(){

@Test
public void testOneAccount(){
Customer oscar = new Customer("Oscar").openAccount(new Account(Account.SAVINGS));
Customer oscar = new Customer("Oscar", customer_id++).openAccount(new Account(Account.SAVINGS));
assertEquals(1, oscar.getNumberOfAccounts());
}

@Test
public void testTwoAccount(){
Customer oscar = new Customer("Oscar")
Customer oscar = new Customer("Oscar", customer_id++)
.openAccount(new Account(Account.SAVINGS));
oscar.openAccount(new Account(Account.CHECKING));
assertEquals(2, oscar.getNumberOfAccounts());
}

@Ignore
@Test
public void testThreeAcounts() {
Customer oscar = new Customer("Oscar")
Customer oscar = new Customer("Oscar", customer_id++)
.openAccount(new Account(Account.SAVINGS));
oscar.openAccount(new Account(Account.CHECKING));
oscar.openAccount(new Account(Account.CHECKING));
assertEquals(3, oscar.getNumberOfAccounts());
}

@Test
public void transferBetweenAccounts() {
Account savings = new Account(Account.SAVINGS);
savings.deposit(10000);
Account checking = new Account(Account.CHECKING);
checking.deposit(200);
Customer oscar = new Customer("Oscar", customer_id++)
.openAccount(savings);
oscar.openAccount(checking);
oscar.transferBtwnAccounts(savings, checking, 3000);
assertEquals(7000.0, savings.sumTransactions(),DOUBLE_DELTA);
assertEquals(3200.0, checking.sumTransactions(),DOUBLE_DELTA);
}
}