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

Test submit from Wenshuo Yang #328

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
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
Expand Down
60 changes: 11 additions & 49 deletions src/main/java/com/abc/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,32 @@
import java.util.ArrayList;
import java.util.List;

public class Account {
public class Account implements AccountInterface {

public static final int CHECKING = 0;
public static final int SAVINGS = 1;
public static final int MAXI_SAVINGS = 2;

private final int accountType;
public final AccountTypeEnum accountType;
public List<Transaction> transactions;
private double balance;

public Account(int accountType) {
public Account(AccountTypeEnum accountType) {
this.accountType = accountType;
this.transactions = new ArrayList<Transaction>();
this.balance = 0;
}

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

public void withdraw(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("amount must be greater than zero");
} else {
transactions.add(new Transaction(-amount));
public void transact(double amount) {
transactions.add(new Transaction(amount));
this.balance += amount;
}
}

public double interestEarned() {
double amount = sumTransactions();
switch(accountType){
case SAVINGS:
if (amount <= 1000)
return amount * 0.001;
else
return 1 + (amount-1000) * 0.002;
// case SUPER_SAVINGS:
// if (amount <= 4000)
// return 20;
case MAXI_SAVINGS:
if (amount <= 1000)
return amount * 0.02;
if (amount <= 2000)
return 20 + (amount-1000) * 0.05;
return 70 + (amount-2000) * 0.1;
default:
return amount * 0.001;
}
return 0;
}

public double sumTransactions() {
return checkIfTransactionsExist(true);
}

private double checkIfTransactionsExist(boolean checkAll) {
double amount = 0.0;
for (Transaction t: transactions)
amount += t.amount;
return amount;
return balance;
}

public int getAccountType() {
public AccountTypeEnum getAccountType() {
return accountType;
}

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/abc/AccountInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.abc;

public interface AccountInterface {

void transact(double amount);

double interestEarned();

double sumTransactions();

AccountTypeEnum getAccountType();
}
15 changes: 15 additions & 0 deletions src/main/java/com/abc/AccountTypeEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.abc;

public enum AccountTypeEnum {
CHECKING("Checking"),
SAVINGS("Savings"),
MAXI_SAVINGS("Maxi Savings");
private final String value;

AccountTypeEnum(final String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
21 changes: 10 additions & 11 deletions src/main/java/com/abc/Bank.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.ArrayList;
import java.util.List;

public class Bank {
public class Bank implements BankInterface {
private List<Customer> customers;

public Bank() {
Expand All @@ -16,8 +16,10 @@ public void addCustomer(Customer customer) {

public String customerSummary() {
String summary = "Customer Summary";
for (Customer c : customers)
summary += "\n - " + c.getName() + " (" + format(c.getNumberOfAccounts(), "account") + ")";
if (isCustomerExisting()) {
for (Customer c : customers)
summary += "\n - " + c.getName() + " (" + format(c.getNumberOfAccounts(), "account") + ")";
}
return summary;
}

Expand All @@ -29,18 +31,15 @@ private String format(int number, String word) {

public double totalInterestPaid() {
double total = 0;
for(Customer c: customers)
for (Customer c : customers)
total += c.totalInterestEarned();
return total;
}

public String getFirstCustomer() {
try {
customers = null;
return customers.get(0).getName();
} catch (Exception e){
e.printStackTrace();
return "Error";
private boolean isCustomerExisting() {
if (customers.size() > 0) {
return true;
}
throw new Error("there is no customer, get your salesman working");
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/abc/BankInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.abc;

public interface BankInterface {
void addCustomer(Customer customer);
String customerSummary();
double totalInterestPaid();
}
14 changes: 14 additions & 0 deletions src/main/java/com/abc/CheckingAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.abc;

public class CheckingAccount extends Account{
private static final double defaultInterestRate = 0.001;
public CheckingAccount(){
super(AccountTypeEnum.CHECKING);
}

@Override
public double interestEarned() {
double amount = sumTransactions();
return amount * defaultInterestRate;
}
}
22 changes: 4 additions & 18 deletions src/main/java/com/abc/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import static java.lang.Math.abs;

public class Customer {
public class Customer implements CustomerInterface {
private String name;
private List<Account> accounts;

Expand Down Expand Up @@ -35,8 +35,7 @@ public double totalInterestEarned() {
}

public String getStatement() {
String statement = null;
statement = "Statement for " + name + "\n";
String statement = "Statement for " + name + "\n";
double total = 0.0;
for (Account a : accounts) {
statement += "\n" + statementForAccount(a) + "\n";
Expand All @@ -47,20 +46,7 @@ public String getStatement() {
}

private String statementForAccount(Account a) {
String s = "";

//Translate to pretty account type
switch(a.getAccountType()){
case Account.CHECKING:
s += "Checking Account\n";
break;
case Account.SAVINGS:
s += "Savings Account\n";
break;
case Account.MAXI_SAVINGS:
s += "Maxi Savings Account\n";
break;
}
String s = a.getAccountType().getValue() + " Account\n";

//Now total up all the transactions
double total = 0.0;
Expand All @@ -72,7 +58,7 @@ private String statementForAccount(Account a) {
return s;
}

private String toDollars(double d){
private String toDollars(double d) {
return String.format("$%,.2f", abs(d));
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/abc/CustomerInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.abc;

public interface CustomerInterface {
String getName();

Customer openAccount(Account account);

int getNumberOfAccounts();
double totalInterestEarned();
String getStatement();
}
17 changes: 17 additions & 0 deletions src/main/java/com/abc/MaxiSavingsAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.abc;

public class MaxiSavingsAccount extends Account {
public MaxiSavingsAccount(){
super(AccountTypeEnum.MAXI_SAVINGS);
}

@Override
public double interestEarned() {
double amount = sumTransactions();
if (amount <= 1000)
return amount * 0.02;
if (amount <= 2000)
return 20 + (amount - 1000) * 0.05;
return 70 + (amount - 2000) * 0.1;
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/abc/SavingsAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.abc;

public class SavingsAccount extends Account{
private static final double defaultInterestRate = 0.001;
private static final double secondTierInterestRate = 0.002;
public SavingsAccount () {
super(AccountTypeEnum.SAVINGS);
}

@Override
public double interestEarned() {
double amount = sumTransactions();
if (amount <= 1000)
return amount * defaultInterestRate;
else
return 1 + (amount - 1000) * secondTierInterestRate;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/abc/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.Calendar;
import java.util.Date;

public class Transaction {
public class Transaction implements TransactionInterface{
public final double amount;

private Date transactionDate;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/abc/TransactionInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.abc;

public interface TransactionInterface {
}
23 changes: 23 additions & 0 deletions src/test/java/com/abc/AccountTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.abc;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class AccountTest {
private static final double DOUBLE_DELTA = 1e-15;
@Test
public void should_returnBalanceAs10_when_10WasDepositedAnd20WasWithdrew (){
Account account = new Account(AccountTypeEnum.CHECKING);
account.transact(10);
account.transact(-20);
assertEquals(account.sumTransactions(),-10,DOUBLE_DELTA);
}

@Test
public void should_returnAccountTypeAsSavings_when_savingsAccountWasCreated (){
Account account = new Account(AccountTypeEnum.SAVINGS);
assertEquals(account.getAccountType(),AccountTypeEnum.SAVINGS);
}

}
49 changes: 22 additions & 27 deletions src/test/java/com/abc/BankTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,42 @@ public class BankTest {
private static final double DOUBLE_DELTA = 1e-15;

@Test
public void customerSummary() {
public void should_returnSummaryWithThreeAccounts_when_customerWithThreeAccountsAreAdded() {
Bank bank = new Bank();
Customer john = new Customer("John");
john.openAccount(new Account(Account.CHECKING));
john.openAccount(new CheckingAccount());
john.openAccount(new SavingsAccount());
john.openAccount(new MaxiSavingsAccount());
bank.addCustomer(john);

assertEquals("Customer Summary\n - John (1 account)", bank.customerSummary());
assertEquals("Customer Summary\n - John (3 accounts)", bank.customerSummary());
}

@Test
public void checkingAccount() {
public void should_throwError_when_noCustomerIsAdded() {
Bank bank = new Bank();
Account checkingAccount = new Account(Account.CHECKING);
Customer bill = new Customer("Bill").openAccount(checkingAccount);
bank.addCustomer(bill);

checkingAccount.deposit(100.0);

assertEquals(0.1, bank.totalInterestPaid(), DOUBLE_DELTA);
try {
bank.customerSummary();
} catch (Error e) {
assertEquals(e.getMessage(), "there is no customer, get your salesman working");
}
}

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

checkingAccount.deposit(1500.0);
CheckingAccount checkingAccount = new CheckingAccount();
SavingsAccount savingsAccount = new SavingsAccount();
Customer bill = new Customer("Bill").openAccount(checkingAccount);
bill.openAccount(savingsAccount);
bank.addCustomer(bill);

assertEquals(2.0, bank.totalInterestPaid(), DOUBLE_DELTA);
checkingAccount.transact(100.0);
savingsAccount.transact(2000);
assertEquals(3.1, bank.totalInterestPaid(), DOUBLE_DELTA);
}

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

checkingAccount.deposit(3000.0);

assertEquals(170.0, bank.totalInterestPaid(), DOUBLE_DELTA);
assertEquals(0, bank.totalInterestPaid(), DOUBLE_DELTA);
}

}
Loading