Skip to content

Commit

Permalink
Merge pull request #8 from Bartolem/edit-user-interface
Browse files Browse the repository at this point in the history
implement calculate interest rate mechanism for savings account
  • Loading branch information
Bartolem authored Aug 3, 2024
2 parents 4d966a6 + a388f28 commit b4843c1
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 50 deletions.
62 changes: 31 additions & 31 deletions out/artifacts/BankAccountManagementSystem_jar/accounts/accounts.csv
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
Account type,Account number,Status,Owner ID,Owner personal name,Currency code,Balance,Creation date
STANDARD,67890123,BLOCKED,42e7a5c4-3df5-4376-9598-3e9b7a79e44e,Andrzej Nowicki,PLN,2800.00,2024-07-17
STANDARD,90277601,ACTIVE,5802ad34-49b9-4f32-9f9d-a0c9cfd1ad32,Wojtas Surowy,PLN,3950.00,2024-07-17
CURRENT,41060146,ACTIVE,8a86f2d1-27d1-496d-b82f-08d3971dca90,Florian Drazga,EUR,12000.00,2024-07-17
STANDARD,78901234,BLOCKED,85a4b7d5-90a1-4b6e-8c6a-5b27c75a700d,Monika Stępień,PLN,3200.00,2024-07-17
CURRENT,80415498,ACTIVE,d34a0031-3e7d-45c8-bb1e-4747792178c2,Arnold Glumpod,EUR,42200.00,2024-07-17
STANDARD,23780456,ACTIVE,2d63d206-b9cb-439d-b077-6d3b377e7ea0,Jan Nowak,PLN,2000.00,2024-07-17
STANDARD,74656881,ACTIVE,d37f336a-fdd9-4ed9-891e-37da0a169021,Marta Szulc,PLN,2700.00,2024-07-17
STANDARD,45667901,ACTIVE,ea171ec8-c692-4d46-9f6a-64da6d94f17a,Piotr Lewandowski,PLN,2500.00,2024-07-17
STANDARD,39567890,ACTIVE,90e9d272-c3ef-4ac2-b2a0-ee1e41d0dcd7,Marek Szczepański,PLN,3100.00,2024-07-17
STANDARD,29455501,ACTIVE,769d5a1a-9f47-4c8a-bb18-1b2b7c39f7ed,Łukasz Kołodziej,PLN,2300.00,2024-07-17
SAVINGS,89551641,ACTIVE,ce40a1ad-8437-4c45-92e0-c3c2453186a3,Damian Kowal,DKK,6000.00,2024-07-17
STANDARD,69014156,ACTIVE,227a1a9d-eef4-4b86-b8eb-e8d8962da19f,Ewa Grabowska,PLN,1900.00,2024-07-17
STANDARD,53754785,ACTIVE,2f6ea29a-bdbd-4c92-871a-6d68c2d724d4,Agata Sikora,PLN,2700.00,2024-07-17
STANDARD,48948735,ACTIVE,47781568-f414-4c31-9db1-e2adc2569533,Wojtas Surowy,PLN,1200.00,2024-07-17
STANDARD,59816073,ACTIVE,ce40a1ad-8437-4c45-92e0-c3c2453186a3,Damian Kowal,EUR,23110.00,2024-07-17
STANDARD,51168018,ACTIVE,865e2174-643b-4557-b246-f7f5a03eda28,Edward Mazur,PLN,12000.00,2024-07-17
CURRENT,64864386,ACTIVE,ce40a1ad-8437-4c45-92e0-c3c2453186a3,Damian Kowal,PLN,90.00,2024-07-17
STANDARD,34512678,ACTIVE,3133fcd3-9eea-4e97-b89b-cc3e37cfe253,Anna Kowalska,PLN,1500.00,2024-07-17
SAVINGS,88910118,ACTIVE,ce40a1ad-8437-4c45-92e0-c3c2453186a3,Damian Kowal,USD,194000.00,2024-07-17
STANDARD,45008901,ACTIVE,dcaae597-724a-4164-a679-c30d64ddbd14,Jolanta Piotrowska,PLN,2400.00,2024-07-17
SAVINGS,30007906,ACTIVE,a2025028-3f92-408f-99b0-11ac2fd9797a,Dariusz Bąk,PLN,25100.00,2024-07-17
STANDARD,47563833,ACTIVE,ce40a1ad-8437-4c45-92e0-c3c2453186a3,Damian Kowal,PLN,1800.00,2024-07-17
STANDARD,83425300,ACTIVE,6d7a05bf-4064-4e71-b397-bf97a38103d9,Dorota Kubiak,PLN,2600.00,2024-07-17
STANDARD,27545353,ACTIVE,ef79d2eb-7e91-4963-8d9c-f8a1172b6a16,Robert Nowacki,PLN,2000.00,2024-07-17
STANDARD,63639012,ACTIVE,7e9d1b0b-9d85-4a21-8df3-17d1ff5b44de,Magdalena Kowalczyk,PLN,1800.00,2024-07-17
STANDARD,56473839,ACTIVE,831d2e2a-6a98-4873-963c-b2c8a62daa61,Karolina Jakubowska,PLN,2200.00,2024-07-17
STANDARD,56789012,ACTIVE,f5905299-019b-431a-86a8-3221475ea645,Rafał Witkowski,PLN,2600.00,2024-07-17
STANDARD,89012345,ACTIVE,3466c720-c3a3-4a9f-b6c5-2f4f35e5d953,Grzegorz Kaczmarek,PLN,2100.00,2024-07-17
STANDARD,43553890,ACTIVE,b3269a3c-31a1-4c8b-8d03-4eb863d5b5ea,Katarzyna Wójcik,PLN,3000.00,2024-07-17
STANDARD,67890992,ACTIVE,54d4b9e1-75fc-4db2-aa2b-f6bb0a651d91,Iwona Majewska,PLN,2900.00,2024-07-17
Account type,Account number,Status,Owner ID,Owner personal name,Currency code,Balance,Creation date,Last interest date
STANDARD,67890123,BLOCKED,42e7a5c4-3df5-4376-9598-3e9b7a79e44e,Andrzej Nowicki,PLN,2800.00,2024-07-17,
STANDARD,90277601,ACTIVE,5802ad34-49b9-4f32-9f9d-a0c9cfd1ad32,Wojtas Surowy,PLN,3950.00,2024-07-17,
CURRENT,41060146,ACTIVE,8a86f2d1-27d1-496d-b82f-08d3971dca90,Florian Drazga,EUR,12000.00,2024-07-17,
STANDARD,78901234,BLOCKED,85a4b7d5-90a1-4b6e-8c6a-5b27c75a700d,Monika Stępień,PLN,3200.00,2024-07-17,
CURRENT,80415498,ACTIVE,d34a0031-3e7d-45c8-bb1e-4747792178c2,Arnold Glumpod,EUR,42200.00,2024-07-17,
STANDARD,23780456,ACTIVE,2d63d206-b9cb-439d-b077-6d3b377e7ea0,Jan Nowak,PLN,2000.00,2024-07-17,
STANDARD,74656881,ACTIVE,d37f336a-fdd9-4ed9-891e-37da0a169021,Marta Szulc,PLN,2700.00,2024-07-17,
STANDARD,45667901,ACTIVE,ea171ec8-c692-4d46-9f6a-64da6d94f17a,Piotr Lewandowski,PLN,2500.00,2024-07-17,
STANDARD,39567890,ACTIVE,90e9d272-c3ef-4ac2-b2a0-ee1e41d0dcd7,Marek Szczepański,PLN,3100.00,2024-07-17,
STANDARD,29455501,ACTIVE,769d5a1a-9f47-4c8a-bb18-1b2b7c39f7ed,Łukasz Kołodziej,PLN,2300.00,2024-07-17,
SAVINGS,89551641,ACTIVE,ce40a1ad-8437-4c45-92e0-c3c2453186a3,Damian Kowal,DKK,6000.00,2024-07-17,03-08-2024 13:42:11
STANDARD,69014156,ACTIVE,227a1a9d-eef4-4b86-b8eb-e8d8962da19f,Ewa Grabowska,PLN,1900.00,2024-07-17,
STANDARD,53754785,ACTIVE,2f6ea29a-bdbd-4c92-871a-6d68c2d724d4,Agata Sikora,PLN,2700.00,2024-07-17,
STANDARD,48948735,ACTIVE,47781568-f414-4c31-9db1-e2adc2569533,Wojtas Surowy,PLN,1200.00,2024-07-17,
STANDARD,59816073,ACTIVE,ce40a1ad-8437-4c45-92e0-c3c2453186a3,Damian Kowal,EUR,23110.00,2024-07-17,
STANDARD,51168018,ACTIVE,865e2174-643b-4557-b246-f7f5a03eda28,Edward Mazur,PLN,12000.00,2024-07-17,
CURRENT,64864386,ACTIVE,ce40a1ad-8437-4c45-92e0-c3c2453186a3,Damian Kowal,PLN,90.00,2024-07-17,
STANDARD,34512678,ACTIVE,3133fcd3-9eea-4e97-b89b-cc3e37cfe253,Anna Kowalska,PLN,1500.00,2024-07-17,
SAVINGS,88910118,ACTIVE,ce40a1ad-8437-4c45-92e0-c3c2453186a3,Damian Kowal,USD,194000.00,2024-07-17,03-08-2024 13:42:11
STANDARD,45008901,ACTIVE,dcaae597-724a-4164-a679-c30d64ddbd14,Jolanta Piotrowska,PLN,2400.00,2024-07-17,
SAVINGS,30007906,ACTIVE,a2025028-3f92-408f-99b0-11ac2fd9797a,Dariusz Bąk,PLN,25100.00,2024-07-17,03-08-2024 13:42:11
STANDARD,47563833,ACTIVE,ce40a1ad-8437-4c45-92e0-c3c2453186a3,Damian Kowal,PLN,1800.00,2024-07-17,
STANDARD,83425300,ACTIVE,6d7a05bf-4064-4e71-b397-bf97a38103d9,Dorota Kubiak,PLN,2600.00,2024-07-17,
STANDARD,27545353,ACTIVE,ef79d2eb-7e91-4963-8d9c-f8a1172b6a16,Robert Nowacki,PLN,2000.00,2024-07-17,
STANDARD,63639012,ACTIVE,7e9d1b0b-9d85-4a21-8df3-17d1ff5b44de,Magdalena Kowalczyk,PLN,1800.00,2024-07-17,
STANDARD,56473839,ACTIVE,831d2e2a-6a98-4873-963c-b2c8a62daa61,Karolina Jakubowska,PLN,2200.00,2024-07-17,
STANDARD,56789012,ACTIVE,f5905299-019b-431a-86a8-3221475ea645,Rafał Witkowski,PLN,2600.00,2024-07-17,
STANDARD,89012345,ACTIVE,3466c720-c3a3-4a9f-b6c5-2f4f35e5d953,Grzegorz Kaczmarek,PLN,2100.00,2024-07-17,
STANDARD,43553890,ACTIVE,b3269a3c-31a1-4c8b-8d03-4eb863d5b5ea,Katarzyna Wójcik,PLN,3000.00,2024-07-17,
STANDARD,67890992,ACTIVE,54d4b9e1-75fc-4db2-aa2b-f6bb0a651d91,Iwona Majewska,PLN,2900.00,2024-07-17,
49 changes: 41 additions & 8 deletions src/main/java/accounts/SavingsAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.YearMonth;

public class SavingsAccount extends Account {
private static BigDecimal interestRate = new BigDecimal("4.0");
private static BigDecimal minBalance = new BigDecimal("1000");
public static final AccountTypes TYPE = AccountTypes.SAVINGS;
private LocalDateTime lastInterestDate;

public SavingsAccount(User user, CurrencyCodes currencyCode, String balance) {
super(user, currencyCode, balance);
this.lastInterestDate = LocalDateTime.now();
}

public SavingsAccount(int accountNumber, User user, CurrencyCodes currencyCode, String balance, String date, boolean blocked, String status) {
public SavingsAccount(int accountNumber, User user, CurrencyCodes currencyCode, String balance, String date, boolean blocked, String status, String lastInterestDate) {
super(accountNumber, user, currencyCode, balance, date, blocked, status);
this.lastInterestDate = LocalDateTime.parse(lastInterestDate, DateTimeFormatter.ISO_DATE_TIME);
}

@Override
Expand All @@ -36,13 +40,21 @@ public void calculateInterestRate() {
setBalance(getBalance().add(getBalance().multiply(interestRate).divide(BigDecimal.valueOf(100), RoundingMode.HALF_UP)).toString());
}

// Calculate and add interest to the balance
public void calculateAndAddInterest() {
LocalDateTime now = LocalDateTime.now();
if (lastInterestDate.isBefore(now.minusMonths(1))) {
calculateInterestRate();
lastInterestDate = now;
}
}

public static BigDecimal getInterestRate() {
return interestRate;
}

public static void setInterestRate(BigDecimal interestRate) {
if (interestRate.compareTo(BigDecimal.valueOf(0.3)) >= 0
&& interestRate.compareTo(BigDecimal.valueOf(8)) <= 0) {
if (interestRate.compareTo(BigDecimal.valueOf(0.3)) >= 0 && interestRate.compareTo(BigDecimal.valueOf(8)) <= 0) {
SavingsAccount.interestRate = interestRate;
}
}
Expand All @@ -52,15 +64,16 @@ public static BigDecimal getMinBalance() {
}

public static void setMinBalance(BigDecimal amount) {
if (amount.compareTo(BigDecimal.valueOf(0)) >= 0
&& amount.compareTo(BigDecimal.valueOf(25000)) <= 0) {
if (amount.compareTo(BigDecimal.valueOf(0)) >= 0 && amount.compareTo(BigDecimal.valueOf(25000)) <= 0) {
SavingsAccount.minBalance = amount;
}
}

@Override
public boolean withdraw(BigDecimal amount) {
if ((getBalance().subtract(amount).compareTo(SavingsAccount.minBalance) >= 0)) {
calculateAndAddInterest(); // Ensure interest is calculated before withdrawal

if ((getBalance().subtract(amount).compareTo(SavingsAccount.minBalance) > -1)) {
if (getLimitManager().checkDailyLimit(amount)) {
System.out.println("Daily limit exceeded");
return false;
Expand All @@ -76,7 +89,23 @@ public boolean withdraw(BigDecimal amount) {
getLimitManager().updateMonthlyUsage(YearMonth.from(today), amount);
setBalance(getBalance().subtract(amount).toString());
super.getTransactionHistory().add(new Transaction(getAccountNumber(), TransactionTypes.WITHDRAW, LocalDateTime.now(), amount, getCurrencyCode()));
// Checks if the account exist in bank. Accounts created by unit testing are not included, so we don't need to save their transaction history.
// Checks if the account exists in bank. Accounts created by unit testing are not included, so we don't need to save their transaction history.
if (Bank.getInstance().contains(getAccountNumber())) {
TransactionHistoryCSVHandler.write(getTransactionHistory(), new File("transactions/transaction_history_" + getAccountNumber() + ".csv").getAbsolutePath());
}
return true;
}
return false;
}

@Override
public boolean deposit(BigDecimal amount) {
calculateAndAddInterest(); // Ensure interest is calculated before deposit

if (isPositiveAmount(amount)) {
setBalance(getBalance().add(amount).toString());
super.getTransactionHistory().add(new Transaction(getAccountNumber(), TransactionTypes.DEPOSIT, LocalDateTime.now(), amount, getCurrencyCode()));
// Checks if the account exists in bank. Accounts created by unit testing are not included, so we don't need to save their transaction history.
if (Bank.getInstance().contains(getAccountNumber())) {
TransactionHistoryCSVHandler.write(getTransactionHistory(), new File("transactions/transaction_history_" + getAccountNumber() + ".csv").getAbsolutePath());
}
Expand All @@ -87,8 +116,12 @@ public boolean withdraw(BigDecimal amount) {

@Override
public String toString() {
return super.toString() + "\n(" + TYPE + ")" + " (" + getStatus() + ")" +
return super.toString() + "(" + TYPE + ")" + " (" + getStatus() + ")" +
"\nInterest rate: " + getInterestRate() + "%" +
"\nMinimal balance allowed: " + getMinBalance();
}

public String getLastInterestDate() {
return lastInterestDate.format(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"));
}
}
41 changes: 30 additions & 11 deletions src/main/java/file_manipulation/AccountsCSVHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import users.Admin;

import java.io.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.logging.Logger;

Expand All @@ -16,19 +19,12 @@ public class AccountsCSVHandler {
public static void write(Collection<Account> accounts, String fileName) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
// Write headers
writer.write("Account type,Account number,Status,Owner ID,Owner personal name,Currency code,Balance,Creation date\n");
writer.write("Account type,Account number,Status,Owner ID,Owner personal name,Currency code,Balance,Creation date,Last interest date\n");

// Write account details
for (Account account : accounts) {
String line = String.format("%s,%d,%s,%s,%s,%s,%s,%s\n",
account.getType(),
account.getAccountNumber(),
account.getStatus(),
account.getUser().getPerson().getID(),
account.getOwnerName(),
account.getCurrencyCode(),
account.getBalance(),
account.getCreationDate());
String line = getAccountData(account);

writer.write(line);
}
writer.close();
Expand All @@ -39,6 +35,25 @@ public static void write(Collection<Account> accounts, String fileName) {
}
}

private static String getAccountData(Account account) {
String lastInterestDate = "";

if (account instanceof SavingsAccount savingsAccount) {
lastInterestDate = savingsAccount.getLastInterestDate();
}

return String.format("%s,%d,%s,%s,%s,%s,%s,%s,%s\n",
account.getType(),
account.getAccountNumber(),
account.getStatus(),
account.getUser().getPerson().getID(),
account.getOwnerName(),
account.getCurrencyCode(),
account.getBalance(),
account.getCreationDate(),
lastInterestDate);
}

public static void read(Bank bank, String fileName) {
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line = "";
Expand All @@ -57,13 +72,17 @@ public static void read(Bank bank, String fileName) {
CurrencyCodes currencyCode = CurrencyCodes.valueOf(fileContent[5]);
String balance = fileContent[6];
String date = fileContent[7];
String lastInterestDate = "";
if (fileContent.length > 8) {
lastInterestDate = fileContent[8];
}
boolean blocked = status.equals(AccountStatus.BLOCKED.toString());

// Create accounts based on type
switch (accountType) {
case STANDARD -> bank.addAccount(accountNumber, new StandardAccount(accountNumber, bank.getUser(ownerID), currencyCode, balance, date, blocked, status), Admin.getInstance());
case CURRENT -> bank.addAccount(accountNumber, new CurrentAccount(accountNumber, bank.getUser(ownerID), currencyCode, balance, date, blocked, status), Admin.getInstance());
case SAVINGS -> bank.addAccount(accountNumber, new SavingsAccount(accountNumber, bank.getUser(ownerID), currencyCode, balance, date, blocked, status), Admin.getInstance());
case SAVINGS -> bank.addAccount(accountNumber, new SavingsAccount(accountNumber, bank.getUser(ownerID), currencyCode, balance, date, blocked, status, lastInterestDate), Admin.getInstance());
}
}
LOGGER.finest("Accounts successfully loaded from " + fileName);
Expand Down

0 comments on commit b4843c1

Please sign in to comment.