Skip to content

Commit

Permalink
implement the initial tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DanySK committed Nov 10, 2023
1 parent d9d8cb2 commit 4f1d047
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

public class SimpleBankAccount implements BankAccount {

protected static final double ATM_TRANSACTION_FEE = 1;
protected static final double MANAGEMENT_FEE = 5;
public static final double ATM_TRANSACTION_FEE = 1;
public static final double MANAGEMENT_FEE = 5;

private final AccountHolder holder;
private double balance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class StrictBankAccount extends SimpleBankAccount {

private static final double TRANSACTION_FEE = 0.1;
public static final double TRANSACTION_FEE = 0.1;

public StrictBankAccount(final AccountHolder accountHolder, final double balance) {
super(accountHolder, balance);
Expand All @@ -16,15 +16,18 @@ public void chargeManagementFees(final int usrID) {
setBalance(getBalance() - feeAmount);
resetTransactions();
} else {
throw new IllegalArgumentException("ID not corresponding: cannot charge management fees.");
throw new IllegalArgumentException("ID not corresponding: cannot charge management fees");
}
}

public void withdraw(final int usrID, final double amount) {
if (amount < 0) {
throw new IllegalArgumentException("Cannot withdraw a negative amount");
}
if (isWithdrawAllowed(amount)) {
super.withdraw(usrID, amount);
} else {
throw new IllegalStateException("Not enough balance: cannot withdraw.");
throw new IllegalArgumentException("Insufficient balance");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void testWrongBankAccountDeposit() {
} catch (IllegalArgumentException e) {
Assertions.assertEquals("ID not corresponding: cannot perform transaction.", e.getMessage());
}
// Alternative (with reflection): Assertions.assertThrows
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,69 @@
package it.unibo.bank.impl;

import it.unibo.bank.api.AccountHolder;
import it.unibo.bank.api.BankAccount;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static it.unibo.bank.impl.SimpleBankAccount.*;
import static it.unibo.bank.impl.SimpleBankAccount.ATM_TRANSACTION_FEE;
import static it.unibo.bank.impl.StrictBankAccount.TRANSACTION_FEE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestStrictBankAccount {

private final static int INITIAL_AMOUNT = 100;

// 1. Create a new AccountHolder and a StrictBankAccount for it each time tests are executed.
private AccountHolder mRossi;
private BankAccount bankAccount;

@BeforeEach
public void setUp() {
mRossi = new AccountHolder("Mario", "Rossi", 1);
bankAccount = new StrictBankAccount(mRossi, 0.0);
}

// 2. Test the initial state of the StrictBankAccount
@Test
public void testInitialization() {
assertEquals(0.0, bankAccount.getBalance());
assertEquals(0, bankAccount.getTransactionsCount());
assertEquals(mRossi, bankAccount.getAccountHolder());
}


// 3. Perform a deposit of 100€ and check that the correct amount of management fees is applied to this BankAccount.
// 3. Perform a deposit of 100€, compute the management fees, and check that the balance is correctly reduced.
@Test
public void testManagementFees() {
bankAccount.deposit(mRossi.getUserID(), INITIAL_AMOUNT);
assertEquals(INITIAL_AMOUNT, bankAccount.getBalance());
bankAccount.chargeManagementFees(mRossi.getUserID());
assertEquals(INITIAL_AMOUNT - TRANSACTION_FEE - MANAGEMENT_FEE, bankAccount.getBalance());
}

// 4. Test the withdraw of a negative value
// 4. Test the withdraw of a negative value
@Test
public void testNegativeWithdraw() {
try {
bankAccount.withdraw(mRossi.getUserID(), -INITIAL_AMOUNT);
} catch (IllegalArgumentException e) {
assertNotNull(e.getMessage());
assertTrue(e.getMessage().length() > 1);
}
}

// 4. Test withdrawing more money than it is in the account
@Test
public void testWithdrawingTooMuch() {
try {
bankAccount.withdraw(mRossi.getUserID(), INITIAL_AMOUNT);
} catch (IllegalArgumentException e) {
assertNotNull(e.getMessage());
assertTrue(e.getMessage().length() > 1);
}
}
}

0 comments on commit 4f1d047

Please sign in to comment.