Skip to content
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
160 changes: 160 additions & 0 deletions src/main/java/com/booleanuk/core/Account.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package com.booleanuk.core;

import com.booleanuk.core.Users.BankManager;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class Account {
private List<Transaction> transactionList = new ArrayList<>();
private BankManager bm;
private String accountId;
private String accountType;
private String branch;
private int balance = 0;

// Constructor with BankManager (mostly for test and lack of service layer); ! or account has a manager
public Account(String accountType, int balance, String branch, BankManager bm) {
this.accountId = UUID.randomUUID().toString();
this.branch = branch;
this.bm = bm;
setAccountType(accountType);
checkBalance(balance);
}

// Constructor with balance
public Account(String accountType, int balance, String branch) {
this.accountId = UUID.randomUUID().toString();
this.branch = branch;
setAccountType(accountType);
checkBalance(balance);
}

// Constructor with no initial balance
public Account(String accountId, String accountType) {
this.accountId = UUID.randomUUID().toString();
setAccountType(accountType);
checkBalance(balance);
}

public void checkBalance(int balance) {
if (balance < 0)
throw new IllegalArgumentException("Initial balance cant be less than 0");
else
setBalance(balance);
}

public int deposit(int amount) {
if (amount <= 0){
System.out.println("Deposit must be larger than 0..");
return 0;
}
addTransaction(amount, "debit");
return this.balance += amount;
}

public int withdraw(int amount) {
if (amount < 0) {
System.out.println("Cant withdraw a negative value..");
return 0;
}

if (this.balance >= amount) {
balance -= amount;
addTransaction(amount, "credit");
return amount;
}

System.out.println("Cant withdraw amount, balance too low..");
return 0;
}

public void addTransaction(int amount, String type) {

if (!type.toLowerCase().trim().equals("debit") && !type.toLowerCase().trim().equals("credit")) {
type = "unknown";
}

LocalDate today = LocalDate.now();
Transaction transaction = new Transaction(amount, this.balance, today, type); // amount, date, type
transactionList.add(transaction);
}

public void generateBankStatement() {
System.out.printf("\n%-20s %-10s %-10s %-10s\n", "Date", "Credit", "Debit", "Balance");
for (Transaction t : transactionList) {
if (t.getType().equals("debit")) {
System.out.printf("%-20s %-10s %-10s %-10s\n", t.getDate(), " ", t.getAmount(), t.getBalance());
} else if (t.getType().equals("credit")) {
System.out.printf("%-20s %-10s %-10s %-10s\n", t.getDate(), t.getAmount(), " ", t.getBalance());
}
}
System.out.println("\n");
}

public void generateStatementForSMS() {
System.out.printf("%-10s %-10s %-10s %-10s\n", "Date", "Credit", "Debit", "Balance");
for (Transaction t : transactionList) {
if (t.getType().equals("debit")) {
System.out.printf("%-10s %-10s %-10s %-10s\n", t.getDate().getMonth(), " ", t.getAmount(), t.getBalance());
} else if (t.getType().equals("credit")) {
System.out.printf("%-10s %-10s %-10s %-10s\n", t.getDate().getMonth(), t.getAmount(), " ", t.getBalance());
}
}
}

public boolean requestOverdraft(int amount) {
return bm.verifyOverdraftRequest(this, amount);
}

// GETTERS & SETTERS

public String getBranch() {
return branch;
}

public void setBranch(String branch) {
this.branch = branch;
}

public String getAccountId() {
return accountId;
}

public void setAccountId(String accountId) {
this.accountId = accountId;
}

public int getBalance() {
return balance;
}

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

public String getAccountType() {
return accountType;
}

public void setAccountType(String accountType) {
switch (accountType) {
case "savings", "current":
this.accountType = accountType;
break;
default:
throw new IllegalArgumentException("Unexpected value: " + accountType);
}
}

public List<Transaction> getTransactionList() {
return transactionList;
}

public void setTransactionList(List<Transaction> transactionList) {
this.transactionList = transactionList;
}

}
45 changes: 45 additions & 0 deletions src/main/java/com/booleanuk/core/Branch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.booleanuk.core;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.DoubleAccumulator;

public class Branch {
private String id;
private String location;
private List<Account> accounts = new ArrayList<>();

public Branch(String location) {
this.id = UUID.randomUUID().toString();
this.location = location;
}

public void addAccount(Account acc) {
accounts.add(acc);
}

public List<Account> getAccounts() {
return accounts;
}

public void setAccounts(List<Account> accounts) {
this.accounts = accounts;
}

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}
}
57 changes: 57 additions & 0 deletions src/main/java/com/booleanuk/core/Transaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.booleanuk.core;

import java.time.LocalDate;
import java.util.Date;
import java.util.UUID;

public class Transaction {
private String id;
private int amount;
private LocalDate date;
private String type;
private int balance;

public Transaction(int amount, int balance, LocalDate date, String type) {
this.id = UUID.randomUUID().toString();
this.amount = amount;
this.balance = balance;
this.date = date;
this.type = type;
}

public int getBalance() {
return this.balance;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public LocalDate getDate() {
return this.date;
}

public void setDate(LocalDate date) {
this.date = date;
}

public int getAmount() {
return amount;
}

public void setAmount(int amount) {
this.amount = amount;
}
}
45 changes: 45 additions & 0 deletions src/main/java/com/booleanuk/core/Users/BankManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.booleanuk.core.Users;

import com.booleanuk.core.Account;
import com.booleanuk.core.Branch;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class BankManager {
private String id;
List<Branch> branches = new ArrayList<>();

public BankManager() {
this.id = UUID.randomUUID().toString();
}

// True if within balance range, else deny
public Boolean verifyOverdraftRequest(Account account, int amount) {
if (amount < 0)
return false;

return amount <= account.getBalance();
}

public Branch addBranch(String branchLocation) {
Branch branch = new Branch(branchLocation);
branches.add(branch);

return branch;
}

public void addAccountToBranch(Account account) {
for (Branch branch : branches) {
if (account.getBranch().equalsIgnoreCase(branch.getLocation())) {
branch.addAccount(account);
return;
}
}
}

public List<Branch> getBranches() {
return this.branches;
}
}
50 changes: 50 additions & 0 deletions src/main/java/com/booleanuk/core/Users/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.booleanuk.core.Users;

import com.booleanuk.core.Account;

import java.util.UUID;

public class Customer {
private String id;
private Account account;

public Customer() {
this.id = UUID.randomUUID().toString();
}

public void createAccount(String type, int balance, String branch) {
this.account = new Account(type, balance, branch);
}

// Mainly for testing interaction , Customer -> Account -> BankManager
public void createAccount(String type, int balance, String branch, BankManager bm) {
this.account = new Account(type, balance, branch, bm);
}

public void viewBankStatements() {
this.account.generateBankStatement();
}

public void depositToAccount(int amount) {
this.account.deposit(amount);
}

public void withdrawFromAccount(int amount) {
this.account.withdraw(amount);
}

public boolean requestOverdraft(int amount) {
return account.requestOverdraft(amount);
}

public void sendStatementAsSMS(String num) {
System.out.println("\n\nSend to number: " + num);
System.out.println("Sending SMS...");
System.out.println("...");
System.out.println("...");
System.out.println("\nIncoming...");
account.generateStatementForSMS();
System.out.println("\n");
}

}
Loading
Loading