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

Asfandyar-Bin-Tassadaq-Java-bank-programming-test #330

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
111 changes: 75 additions & 36 deletions src/main/java/com/abc/Account.java
Original file line number Diff line number Diff line change
@@ -1,72 +1,111 @@
package com.abc;

import java.util.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
// import java.util.concurrent.TimeUnit;

public class Account {
import com.abc.Enums.AccountType;
import com.abc.Enums.TransactionType;
import static com.abc.AccountUtility.isBalanceEnough;
import static com.abc.AccountUtility.isValidAmount;
import static com.abc.AccountUtility.daysSinceFirstTransaction;

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

private final int accountType;
private final AccountType accountType;
public List<Transaction> transactions;
private double balance = 0.0;

public Account(int accountType) {
public Account(AccountType accountType) {
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(amount));
}
public void deposit(double amount, TransactionType transactionType) {
isValidAmount(amount);
addBalance(amount);
transactions.add(new Transaction(amount, TransactionType.DEPOSIT));

}

public void withdraw(double amount, TransactionType transactionType) {
isValidAmount(amount);
isBalanceEnough(amount, this);
removeBalance(amount);
transactions.add(new Transaction(amount, transactionType));

}

public void withdraw(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("amount must be greater than zero");
} else {
transactions.add(new Transaction(-amount));
// Balance functions
public double getBalance() {
return balance;
}

public void addBalance(double amount) {
double tempBalance = this.getBalance();
tempBalance += amount;
setBalance(tempBalance);
}

public void removeBalance(double amount) {
double tempBalance = this.getBalance();
tempBalance -= amount;
setBalance(tempBalance);
}

public void setBalance(double balance) {
this.balance = balance;
}
}

public double interestEarned() {
double amount = sumTransactions();
switch(accountType){

;
switch (accountType) {
case SAVINGS:
if (amount <= 1000)
return amount * 0.001;
return amount * (Math.pow(1 + (0.001 / 365), daysSinceFirstTransaction(this)) - 1);
else
return 1 + (amount-1000) * 0.002;
// case SUPER_SAVINGS:
// if (amount <= 4000)
// return 20;
return (1000 * (Math.pow(1 + (0.001 / 365), daysSinceFirstTransaction(this)) - 1))
+ (amount - 1000) * (Math.pow(1 + (0.002 / 365), daysSinceFirstTransaction(this)) - 1);
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;
if (!checkWithdrawInPastTenDays()) {
return amount * (Math.pow(1 + (0.05 / 365), daysSinceFirstTransaction(this)) - 1);
} else {
// 0.1% interest rate
return amount * (Math.pow(1 + (0.001 / 365), daysSinceFirstTransaction(this)) - 1);
}
default:
return amount * 0.001;
return amount * (Math.pow(1 + (0.001 / 365), daysSinceFirstTransaction(this)) - 1);
}
}

public double sumTransactions() {
return checkIfTransactionsExist(true);
private boolean checkWithdrawInPastTenDays() {
Date curDate = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(curDate);
calendar.add(Calendar.DAY_OF_MONTH, -10);
Date tenDaysBackDate = calendar.getTime();
for (Transaction t : transactions) {
if (t.getTransactionDate().after(tenDaysBackDate)) { // did withdraw
if (t.getTransactionType() == TransactionType.WITHDRAW) {
return true;
}
}
}
return false;
}

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

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

Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/abc/AccountUtility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.abc;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class AccountUtility {
public static void isValidAmount(double amount) {
if (amount <= 0) {
throw new Error("Amount has to be greater than zero");
}
}

public static void isBalanceEnough(double amount, Account account) {
if(account.getBalance() < amount) {
throw new Error( "You don't have sufficient funds in your account.");
}
}

public static double daysSinceFirstTransaction(Account account) {
System.out.println("In days since first transaction");

Date firstTransactionDate = account.transactions.get(0).getTransactionDate();
System.out.println(firstTransactionDate);
Date lastTransactionDate = account.transactions.get(account.transactions.size()-1).getTransactionDate();
System.out.println(lastTransactionDate);

long diffInMilliseconds = Math.abs(lastTransactionDate.getTime() - firstTransactionDate.getTime());
// Convert milliseconds to days
double diffInDays = Math.floor(TimeUnit.DAYS.convert(diffInMilliseconds, TimeUnit.MILLISECONDS));
return diffInDays;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/abc/Bank.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ public double totalInterestPaid() {

public String getFirstCustomer() {
try {
customers = null;
return customers.get(0).getName();
} catch (Exception e){
e.printStackTrace();
return "Error";
}
}
}

24 changes: 17 additions & 7 deletions src/main/java/com/abc/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.ArrayList;
import java.util.List;

import com.abc.Enums.TransactionType;

import static java.lang.Math.abs;

public class Customer {
Expand Down Expand Up @@ -46,31 +48,39 @@ public String getStatement() {
return statement;
}

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

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

//Now total up all the transactions
double total = 0.0;
for (Transaction t : a.transactions) {
s += " " + (t.amount < 0 ? "withdrawal" : "deposit") + " " + toDollars(t.amount) + "\n";
for (Transaction t : account.transactions) {
s += " " + (t.getTransactionType() == TransactionType.WITHDRAW ? "withdrawal" : "deposit") + " " + toDollars(t.amount) + "\n";
total += t.amount;
}
s += "Total " + toDollars(total);
return s;
}
public void transferBetween(Account a, Account b, double amount ) {
a.withdraw(amount, TransactionType.WITHDRAW);
b.deposit(amount,TransactionType.DEPOSIT);
}
//transfer function (account a, account b, double amount) {}
//withdraws from account a and deposits to account b



private String toDollars(double d){
return String.format("$%,.2f", abs(d));
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/abc/DateProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ public static DateProvider getInstance() {
public Date now() {
return Calendar.getInstance().getTime();
}

public Date addDays(Date date, int noOfDays) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
//manipulate date
cal.add(Calendar.DATE, noOfDays);
return cal.getTime();
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/abc/Enums/AccountType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.abc.Enums;

public enum AccountType {
CHECKING,
SAVINGS,
MAXI_SAVINGS
}
7 changes: 7 additions & 0 deletions src/main/java/com/abc/Enums/TransactionType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.abc.Enums;

public enum TransactionType {
WITHDRAW,
DEPOSIT,
FUND
}
27 changes: 22 additions & 5 deletions src/main/java/com/abc/Transaction.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
package com.abc;

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

import com.abc.Enums.TransactionType;

public class Transaction {
public final double amount;

private Date transactionDate;

public Transaction(double amount) {
this.amount = amount;
private TransactionType transactionType;
public Transaction(double amount, TransactionType transactionType) {
if (transactionType==TransactionType.WITHDRAW) {
this.amount = -amount;
}
else this.amount = amount;
this.transactionDate = DateProvider.getInstance().now();
this.transactionType = transactionType;
}
public double getAmount() {
return amount;
}
public Date getTransactionDate() {
return transactionDate;
}
public void setTransactionDate() {
this.transactionDate = DateProvider.getInstance().addDays(transactionDate, -20);
}
public TransactionType getTransactionType(){
return transactionType;
}

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

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

public class test {
public static void main(String[] args) {
Date currentDate = new Date();
Calendar cal = Calendar.getInstance();

cal.setTime(currentDate);
System.out.println(cal.getTime());
//manipulate date
cal.add(Calendar.DATE, -10);

System.out.println(3000 * (Math.pow(1 + (0.05/365), 365) - 1));
}
}
Loading