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

Kamatchirani-Bank App #327

Open
wants to merge 10 commits 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
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,12 @@
<version>4.11</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
130 changes: 75 additions & 55 deletions src/main/java/com/abc/Account.java
Original file line number Diff line number Diff line change
@@ -1,73 +1,93 @@
package com.abc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Account {
public abstract class Account {

public static final int CHECKING = 0;
public static final int SAVINGS = 1;
public static final int MAXI_SAVINGS = 2;
private final Customer customer;
private final AccountType accountType;
public List<Transaction> transactions;
private double balance;

private final int accountType;
public List<Transaction> transactions;
public enum AccountType {
CHECKING("Checking Account"),
SAVINGS("Savings Account"),
MAXI_SAVINGS("Maxi Savings Account");

public Account(int accountType) {
this.accountType = accountType;
this.transactions = new ArrayList<Transaction>();
}
private final String name;

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

public void withdraw(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("amount must be greater than zero");
} else {
transactions.add(new Transaction(-amount));
}
}
public String getName() {
return name;
}
}

public Account(Customer customer, AccountType accountType) {
this.customer = customer;
this.accountType = accountType;
this.transactions = new ArrayList<Transaction>();
}

public void deposit(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("Amount must be greater than zero");
} else {
transactions.add(new Transaction(Transaction.TransactionType.DEPOSIT, amount));
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;
public void withdraw(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("Amount must be greater than zero");
}
if (balance < amount) {
throw new IllegalArgumentException("Insufficient Balance.");
}
transactions.add(new Transaction(Transaction.TransactionType.WITHDRAW, amount));
balance -= amount;
}

public abstract double interestEarned();

public double sumTransactions() {
double amount = 0.0;
for (Transaction t : transactions)
amount += t.getAmount();
return amount;
}

public void transfer(double amount, Account targetAccount) {
if (targetAccount == this) {
throw new IllegalArgumentException("Target account must be a different account.");
}
if (targetAccount == null) {
throw new IllegalArgumentException("Target account cannot be null.");
}
withdraw(amount);
targetAccount.deposit(amount);
}


public double sumTransactions() {
return checkIfTransactionsExist(true);
}
public List<Transaction> getTransactions() {
return Collections.unmodifiableList(transactions);
}

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

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

public double getBalance() {
return balance;
}

}
19 changes: 19 additions & 0 deletions src/main/java/com/abc/AccountCreation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.abc;

import com.abc.Account.AccountType;

public class AccountCreation {

public static Account create(Customer customer, AccountType accountType) {
switch (accountType) {
case CHECKING:
return new CheckingAccount(customer, accountType);
case SAVINGS:
return new SavingsAccount(customer, accountType);
case MAXI_SAVINGS:
return new MaxiSavingsAccount(customer, accountType);
}
throw new IllegalArgumentException("Invalid account type.");
}

}
91 changes: 52 additions & 39 deletions src/main/java/com/abc/Bank.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,59 @@
package com.abc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.abc.Account.AccountType;

public class Bank {
private List<Customer> customers;

public Bank() {
customers = new ArrayList<Customer>();
}

public void addCustomer(Customer customer) {
customers.add(customer);
}

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

//Make sure correct plural of word is created based on the number passed in:
//If number passed in is 1 just return the word otherwise add an 's' at the end
private String format(int number, String word) {
return number + " " + (number == 1 ? word : word + "s");
}

public double totalInterestPaid() {
double total = 0;
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 List<Customer> customers;

public Bank() {
customers = new ArrayList<Customer>();
}

public void addCustomer(Customer customer) {
if (customer != null && !customers.contains(customer)) {
customers.add(customer);
}
}

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

// Make sure correct plural of word is created based on the number passed in:
// If number passed in is 1 just return the word otherwise add an 's' at the end
private String format(int number, String word) {
return number + " " + (number == 1 ? word : word + "s");
}

public double totalInterestPaid() {
double total = 0;
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";
}
}

public Account createAccount(Customer customer, AccountType accountType) {
return AccountCreation.create(customer, accountType);
}

public List<Customer> getCustomers() {
return Collections.unmodifiableList(customers);
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/abc/CheckingAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.abc;

public class CheckingAccount extends Account {

public CheckingAccount(Customer customer, AccountType accountType) {
super(customer, accountType);
}

@Override
public double interestEarned() {
double amount = sumTransactions();
return amount * 0.001;
}

}
Loading