Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
e8d9365
Add domain model
shyye Aug 27, 2024
6cdb69a
Add Test and code for IdGenerator, update domain model
shyye Aug 27, 2024
cf40de6
Add Test and code for Transaction, update domain model
shyye Aug 27, 2024
40a173d
Add Test and code for Account with CurrentAccount and SavingsAccount,…
shyye Aug 27, 2024
15ffc41
Add Test and code for withdraw() and deposit()
shyye Aug 28, 2024
1281c1f
Update domain model for Account
shyye Aug 28, 2024
7a0d6f3
Add interestRate variable to SavingsAccount
shyye Aug 28, 2024
8043aad
Add Test and Code for Request, overdrafts, update domain model
shyye Aug 28, 2024
ac498d9
Add Test and Code for Bank, makeRequest, update domain model
shyye Aug 28, 2024
5768989
Change IdGenerator Test and Code
shyye Aug 28, 2024
e9a37a8
Remove unused imports
shyye Aug 28, 2024
c871a14
Redo Test and Code for Transactions/Accounts
shyye Aug 28, 2024
69125d3
Redo Test and Code for Transactions/Accounts
shyye Aug 28, 2024
a1fd768
Redo Test and Code for Transactions/Accounts
shyye Aug 28, 2024
ff7a642
Redo Test and Code for Request/Bank
shyye Aug 28, 2024
bdadbda
Redo Test and Code for Request/Bank
shyye Aug 28, 2024
fb61790
Redo Test and Code for Request/Bank
shyye Aug 28, 2024
ec563fc
Redo Test and Code for Customer/User renamed
shyye Aug 28, 2024
0d9e315
Redo Test and Code for Customer/User renamed
shyye Aug 28, 2024
a6cca4a
Redo Test and Code for Accounts
shyye Aug 28, 2024
1e763ee
Update readme and remove comments
shyye Aug 28, 2024
d3b821c
Fix id issues
shyye Aug 28, 2024
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
116 changes: 116 additions & 0 deletions src/main/java/com/booleanuk/core/accounts/Account.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package com.booleanuk.core.accounts;

import com.booleanuk.core.idgenerator.IdGenerator;
import com.booleanuk.core.idgenerator.IdPrefix;
import com.booleanuk.core.transactons.Transaction;
import com.booleanuk.core.transactons.TransactionType;

import java.util.*;

public abstract class Account {

private IdGenerator idGenerator;
private final String accountNumber;
private Branch branch;
private boolean isPossibleToOverdraft;
private Map<String, Transaction> transactions;

public Account(String accountNumber) {
this.idGenerator = new IdGenerator();
this.accountNumber = accountNumber;
this.transactions = new HashMap<>();
}

public String getAccountNumber() {
return this.accountNumber;
}

public Branch getBranch() {
return branch;
}

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

public boolean isPossibleToOverdraft() {
return isPossibleToOverdraft;
}

public void setPossibleToOverdraft(boolean possibleToOverdraft) {
isPossibleToOverdraft = possibleToOverdraft;
}

protected void addTransaction(Transaction transaction) {
this.transactions.put(transaction.getId(), transaction);
}

public void deposit(Double amount) {
Transaction transaction = new Transaction(
this.idGenerator.createId(IdPrefix.TR),
amount,
TransactionType.DEBIT,
calculateBalance()
);
this.addTransaction(transaction);
}

protected void validateOverdraft(Double currentBalance, Double withdrawAmount) throws Exception {
boolean isNegativeBalance = (currentBalance - withdrawAmount) < 0;
if (!this.isPossibleToOverdraft && isNegativeBalance) {

// TODO: should make custom exception

throw new Exception("Not possible to overdraft");
}
}

public void withdraw(Double amount) {
Double currentBalance = this.calculateBalance();
try {
this.validateOverdraft(currentBalance, amount);
} catch (Exception e) {
System.out.println(e.getMessage());
}

Transaction transaction = new Transaction(
this.idGenerator.createId(IdPrefix.TR),
amount,
TransactionType.CREDIT,
currentBalance
);
this.addTransaction(transaction);
}

public ArrayList<Transaction> getAllTransactions() {
ArrayList<Transaction> list = new ArrayList<>(transactions.values());
Collections.reverse(list);

return list;
}

protected Double calculateBalance() {
Double sum = 0.0;
for (Transaction transaction : transactions.values()) {
if (transaction.getType() == TransactionType.DEBIT) {
sum += transaction.getAmount();
} else if (transaction.getType() == TransactionType.CREDIT) {
sum -= transaction.getAmount();
}
}
return sum;
}

public Double getBalance() {
return calculateBalance();
}

// TODO: printTransactionHistory
public void printTransactionHistory() {
System.out.println();
System.out.printf("%s", "date", "||");
for (Transaction transaction : transactions.values()) {
System.out.println();
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/booleanuk/core/accounts/AccountType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.booleanuk.core.accounts;

public enum AccountType {
CURRENT,
SAVINGS
}
7 changes: 7 additions & 0 deletions src/main/java/com/booleanuk/core/accounts/Branch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.booleanuk.core.accounts;

public enum Branch {
BRANCH_1,
BRANCH_2,
BRANCH_3
}
13 changes: 13 additions & 0 deletions src/main/java/com/booleanuk/core/accounts/CurrentAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.booleanuk.core.accounts;

public class CurrentAccount extends Account{

// TODO: Could this constructor be protected?
// should not be able to create without a Bank

public CurrentAccount(String accountNumber) {
super(accountNumber);
this.setBranch(Branch.BRANCH_1);
this.setPossibleToOverdraft(true);
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/booleanuk/core/accounts/SavingsAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.booleanuk.core.accounts;

public class SavingsAccount extends Account{

private Double interestRate;

// TODO: Could this constructor be protected?
// should not be able to create without a Bank

public SavingsAccount(String accountNumber) {
super(accountNumber);
this.setBranch(Branch.BRANCH_1);
this.setPossibleToOverdraft(false);
this.interestRate = 0.12;
}
}
68 changes: 68 additions & 0 deletions src/main/java/com/booleanuk/core/bank/Bank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.booleanuk.core.bank;

import com.booleanuk.core.accounts.Account;
import com.booleanuk.core.accounts.AccountType;
import com.booleanuk.core.accounts.CurrentAccount;
import com.booleanuk.core.accounts.SavingsAccount;
import com.booleanuk.core.idgenerator.IdGenerator;
import com.booleanuk.core.idgenerator.IdPrefix;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class Bank {

private IdGenerator idGenerator;
private Map<String, User> customers;
private Map<String, Request> requests;

public Bank() {
this.customers = new HashMap<>();
this.requests = new HashMap<>();
this.idGenerator = new IdGenerator();
}

public User createUser(String personalNumber) {
User customer = new User(personalNumber);
this.customers.put(personalNumber, customer);
return customer;
}

public void createUserAccount(User user, AccountType type) {

String id = idGenerator.createId(IdPrefix.AC);

if (type == AccountType.CURRENT) {
Account currentAccount = new CurrentAccount(id);
user.addAccount(currentAccount);

} else if (type == AccountType.SAVINGS) {
Account savingsAccount = new SavingsAccount(id);
user.addAccount(savingsAccount);
}
}

public ArrayList<Request> getAllRequests() {
ArrayList<Request> list = new ArrayList<>(requests.values());
Collections.reverse(list);

return list;
}

private void addRequest(Request request) {
this.requests.put(request.getId(), request);
}

public void makeRequest(Account account, Double overdraftSum) {
String id = this.idGenerator.createId(IdPrefix.RE);

Request request = new Request(id, account, overdraftSum);
this.addRequest(request);
}

private void removeRequest(String requestId) {
this.requests.remove(requestId);
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/booleanuk/core/bank/Request.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.booleanuk.core.bank;

import com.booleanuk.core.accounts.Account;

public class Request {

private final String id;
private final Account account;
private Double overdraftSum;

public Request(String id, Account account, Double overdraftSum) {
this.id = id;
this.account = account;
this.overdraftSum = overdraftSum;
}

public String getId() {
return id;
}

// TODO: If time, add some function so the user can
// validate if it's an reasonable amount of overdraft
// with the overdraft sum

public void approve() {
this.account.setPossibleToOverdraft(true);
}

public void reject() {
this.account.setPossibleToOverdraft(false);
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/booleanuk/core/bank/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.booleanuk.core.bank;

import com.booleanuk.core.accounts.Account;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class User {

// TODO: Not safe to store personal numbers ike this, should be a uniqueId instead?
// It's personal number for now for simplicity

private String personalNumber;
private Map<String, Account> accounts;

public User(String personalNumber) {
this.personalNumber = personalNumber;
this.accounts = new HashMap<>();
}

protected void addAccount(Account account) {
this.accounts.put(account.getAccountNumber(), account);
}

public ArrayList<Account> getAllAccounts() {
ArrayList<Account> list = new ArrayList<>(accounts.values());
// Collections.reverse(list);

return list;
}
}
Loading