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

Thomas Wiik #106

Open
wants to merge 35 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
abaa69e
Created tentative domain model and class diagram.
Aug 26, 2024
2c6bfec
Implemented test for creating current account.
Aug 26, 2024
a6e6733
Updated domain model with branch class and createAccount in customer …
Aug 26, 2024
00f6fac
Implemented test for creating savings account.
Aug 26, 2024
80a46da
Implemented functionality to pass the create savings account test.
Aug 26, 2024
5c816db
Added checking of instance in tests to verify that the classes are in…
Aug 26, 2024
db8eb2e
Changes to access modifiers in account and customer class. Made chang…
Aug 26, 2024
c45daa1
Implemented tests for deposit and withdrawal.
Aug 26, 2024
9a0c73f
Added transaction to domain model and class diagram.
Aug 26, 2024
2c84111
Changed date in Transaction class to string in class diagram and doma…
Aug 26, 2024
a5372a3
Changed to using getters when getting amount in Account test.
Aug 26, 2024
de176bd
Implemented test for calculating current balance based on transaction…
Aug 26, 2024
4c1a79d
Implemented functionality to deposit, withdraw and calculateCurrentBa…
Aug 26, 2024
38a5b12
Minor adjustments to class diagram and domain model.
Aug 26, 2024
97c933a
Changed return value of generatebankstatement to void.
Aug 26, 2024
5ba3a98
Implemented test for generating bank statement.
Aug 26, 2024
c365505
Implemented functionality for generating and printing bank statement.…
Aug 26, 2024
275d817
Added test for requesting overdraft.
Aug 26, 2024
2d3a8e1
Added line in request overdraft test.
Aug 26, 2024
0b33077
Added line in request overdraft test.
Aug 26, 2024
d94d7a4
Added tests for managing overdraft requests.
Aug 26, 2024
f2f4335
Implemented functionality for handling overdraftRequests.
Aug 26, 2024
a9daac9
Implemented test for demonstrating relationship between account and b…
Aug 26, 2024
c50be1b
Implemented getter for branch name, facilitating testing.
Aug 26, 2024
83cf57d
Added user stories to tests to make it clear what user stories have b…
Aug 26, 2024
47584a9
Implemented test for overdraft through withdraw function.
Aug 26, 2024
596d9d7
Implemented added overdraft functionality in withdraw function. Minor…
Aug 26, 2024
1020daf
Fixed minor typo in customertest.
Aug 26, 2024
9a4d3a8
Added additional tests for depositing or withdrawing less than 1.
Aug 26, 2024
40e10c8
Implemented checks to cover the previous tests.
Aug 26, 2024
ff64471
Updated domain model and class diagram.
Aug 26, 2024
053e07e
Minor refactoring.
Aug 26, 2024
50247e9
Implemented additional tests for testing overdrafting an account more…
Aug 27, 2024
d725762
Introduced member variable maxOverdraft to class diagram and domain m…
Aug 27, 2024
0190978
Implemented functionality to limit the amount a user can overdraft a …
Aug 27, 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
43 changes: 43 additions & 0 deletions classDiagram.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
@startuml

Class Customer {
- accounts : ArrayList<Account>
+ accountType: enum
+ createAccount(type: AccountType, branch: Branch): void
}

Class Account {
- transactionHistory: ArrayList<Transaction>
- canOverdraft: boolean
- overdraftRequested: boolean
- ownerBranch: Branch
- maxOverdraft: int

+ deposit(amount: int): void
+ withdraw(amount: int): boolean

+ calculateBalanceFromTransactionHistory(): int
+ generateBankStatement(): void
+ printBankStatement(list: ArrayList<String>): void

+ requestOverdraft(): void
+ rejectOverdraftRequest(): void
+ acceptOverdraftRequest(): void
}

Class Savings extends Account {
}

Class Current extends Account {
}

Class Transaction {
- date: String
- amount: int
- currentBalance: int
}

Class Branch {
- name: String
}
@enduml
118 changes: 118 additions & 0 deletions domainModel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Domain model

## User stories
### 1
```
As a customer,
So I can safely store and use my money,
I want to create a current account.
```

### 2
```
As a customer,
So I can save for a rainy day,
I want to create a savings account.
```

### 3
```
As a customer,
So I can keep a record of my finances,
I want to generate bank statements with transaction dates, amounts, and balance at the time of transaction.
```

### 4
```
As a customer,
So I can use my account,
I want to deposit and withdraw funds.
```

### 5
```
As an engineer,
So I don't need to keep track of state,
I want account balances to be calculated based on transaction history instead of stored in memory.
```

### 6
```
As a bank manager,
So I can expand,
I want accounts to be associated with specific branches.
```

### 7
```
As a customer,
So I have an emergency fund,
I want to be able to request an overdraft on my account.
```

### 8
```
As a bank manager,
So I can safeguard our funds,
I want to approve or reject overdraft requests.
```

### 9
```
As a customer,
So I can stay up to date,
I want statements to be sent as messages to my phone.
```

## Classes
`Construcors, getters and setters have been omitted from the model.`

### Customer Class
| Variables | Description |
|-------------------------------|-------------------------------------------------------------|
| `ArrayList<Account> accounts` | List of all the accounts belonging to a specific customer. |

| Methods | Scenario | Outputs |
|--------------------------------------------------------------|--------------------------------------|---------|
| `void createAccount(AccountType accountType, Branch branch)` | Customer wants to create an account. | - |


### Account Abstract Class
| Variables | Description |
|---------------------------------------------|------------------------------------------------------------------------------------|
| `ArrayList<Transaction> transactionHistory` | Contains the history of transactions. |
| `boolean canOverdraft` | Decides if customer can overdraft account. |
| `boolean overdraftRequested` | Contains information whether overdrafted has been requested for the given account. |
| `Branch ownerBranch` | Branch associated with account. |
| `int maxOverdraft` | Max amount the can be overdrafted from the account. |

| Methods | Scenario | Outputs |
|------------------------------------------------|------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------|
| `void deposit(int amount)` | Customer wants to deposit money into the account. | - |
| `boolean withdraw(int amount)` | Customer wants to withdraw money from the account. | If customer has sufficient money in account, or overdraft has been accepted. |
| | | False if customer does not have sufficient money in account and overdraft has not been accepted. |
| `void generateBankStatement()` | Creates list of transactions, including dates, amounts and balance. | - |
| `void printBankStatement()` | Prints the bank statement. | - |
| `int calculateBalanceFromTransactionHistory()` | Customer wants account balance to be based on transaction history instead of stored in memory. | - |
| `void printBankStatement()` | Prints either of the aforementioned bank statements. | |
| `void requestOverdraft()` | Customer wants to be allowed to overdraft the account. | - |
| `void rejectOverdraftRequest()` | The manager wants to reject an overdraft request. | - |
| `void acceptOverdraftRequest()` | The manager wants to accept an overdraft request. ||

### SavingsAccount extends Account Class


### CurrentAccount extends Account Class

### Transaction
| Variables | Description |
|----------------------|--------------------------------------------------|
| `String date` | Contains the date of the transaction. |
| `int amount` | Contains the amount of the transaction. |
| `int currentBalance` | Contains the balance at the time of transaction. |

### Branch Class
| Variables | Description |
|---------------|----------------------------------|
| `String name` | Contains the name of the branch. |

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

import java.util.ArrayList;

public abstract class Account {
protected ArrayList<Transaction> transactionHistory;
protected boolean canOverdraft;
protected boolean overdraftRequested;
protected Branch ownerBranch;
protected int maxOverdraft;

public Account(Branch branch){
this.ownerBranch = branch;
this.transactionHistory = new ArrayList<>();
this.canOverdraft = false;
this.maxOverdraft = -500;
}

public ArrayList<Transaction> getTransactionHistory(){ return this.transactionHistory; }
public Branch getOwnerBranch(){ return this.ownerBranch; }

public void deposit(int amount){
if (amount < 1){
System.out.println("Invalid amount.");
return;
}

int currentBalance = calculateCurrentBalanceFromTransactionHistory();
Transaction t = new Transaction(amount, currentBalance);
this.transactionHistory.add(t);
}

public boolean withdraw(int amount){
if (amount < 1){
System.out.println("Invalid amount.");
return false;
}

int currentBalance = calculateCurrentBalanceFromTransactionHistory();
if (currentBalance < amount){
if (!canOverdraft){
System.out.println("Insufficient funds.");
return false;
}

else if (currentBalance - amount < maxOverdraft){
System.out.println("Exceeding available overdraft.");
return false;
}
}

Transaction t = new Transaction(-amount, currentBalance);
this.transactionHistory.add(t);
System.out.println("Withdrawal successful.");
return true;
}

public int calculateCurrentBalanceFromTransactionHistory(){
int currentBalance = 0;

for (Transaction t : this.transactionHistory){
currentBalance += t.getAmount();
}
return currentBalance;
}

public void generateBankStatement(){
ArrayList<String> statement = new ArrayList<>();
String s = String.format("%-10s %2s %-10s %2s %-10s %2s %-10s", "date", "||", "credit", "||", "debit", "||", "balance");
statement.add(s);

for (Transaction t : this.transactionHistory){
if (0 < t.getAmount()){
s = String.format("%-10s %2s %-10s %2s %-10s %2s %-10s", t.getDate(), "||", t.getAmount() + ".00", "||", " ", "||", t.getCurrentBalance() + ".00");
} else {
s = String.format("%-10s %2s %-10s %2s %-10s %2s %-10s", t.getDate(), "||", " ", "||", -t.getAmount() + ".00", "||", t.getCurrentBalance() + ".00");
}
statement.add(s);
}
printBankStatement(statement);
}

private void printBankStatement(ArrayList<String> list){
for (String s : list){
System.out.println(s);
}
}

public void requestOverdraft(){
if (!this.overdraftRequested) {
this.overdraftRequested = true;
System.out.println("Overdraft requested.");
} else {
System.out.println("Overdraft has already been requested.");
}
}

/*
The following functionality is meant for the manager only.
*/
public void rejectOverdraftRequest(){
if (overdraftRequested) { this.canOverdraft = false; }
}

public void acceptOverdraftRequest(){
if (overdraftRequested) { this.canOverdraft = true; }
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/booleanuk/core/Branch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.booleanuk.core;

public class Branch {
private final String name;

public Branch(String name){
this.name = name;
}

public String getName(){
return this.name;
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/booleanuk/core/CurrentAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.booleanuk.core;

public class CurrentAccount extends Account{
public CurrentAccount(Branch branch){
super(branch);
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/booleanuk/core/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.booleanuk.core;

import java.util.ArrayList;

public class Customer {
private final ArrayList<Account> accounts;

public enum AccountType {
SAVINGS,
CURRENT
}

public Customer(){
this.accounts = new ArrayList<>();
}

public ArrayList<Account> getAccounts(){
return this.accounts;
}

public void createAccount(AccountType type, Branch branch){
if (type == AccountType.CURRENT) {
accounts.add(new CurrentAccount(branch));
} else {
accounts.add(new SavingsAccount(branch));
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/booleanuk/core/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.booleanuk.core;

public class Main {
public static void main(String[] args){
Customer c = new Customer();
c.createAccount(Customer.AccountType.CURRENT, new Branch("Oslo"));

Account account = c.getAccounts().getFirst();

int deposit = 20;

int withdraw = 15;

account.deposit(deposit);
account.withdraw(withdraw);
account.generateBankStatement();
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/booleanuk/core/SavingsAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.booleanuk.core;

public class SavingsAccount extends Account{
public SavingsAccount(Branch branch){
super(branch);
}

@Override
public void requestOverdraft(){
System.out.println("Overdrafting a savings account is not allowed.");
}

/*
The following functionality is meant for the manager only.
*/

@Override
public void rejectOverdraftRequest(){
System.out.println("Overdrafting a savings account is not allowed.");
}

@Override
public void acceptOverdraftRequest(){
System.out.println("Overdrafting a savings account is not allowed.");
}
}
Loading