Skip to content

Commit

Permalink
Merge branch 'master' into exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
DanySK committed Nov 15, 2023
2 parents 3b3b7dd + 792bb8d commit 051e6ce
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 50 deletions.
14 changes: 1 addition & 13 deletions java/testing/junit/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
plugins {
java
application
}

tasks.javadoc {
isFailOnError = false
}

repositories {
Expand All @@ -19,14 +14,7 @@ dependencies {
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.10.1")
}

val mainClass: String by project

application {
// The following allows to run with: ./gradlew -PmainClass=it.unibo.oop.MyMainClass run
mainClass.set(project.properties["mainClass"].toString())
}

val test by tasks.getting(Test::class) {
tasks.withType<Test> {
// Use junit platform for unit tests
useJUnitPlatform()
testLogging {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
/**
* A BankAccount holder.
*/
public class AccountHolder {
public final class AccountHolder {
private final String name;
private final String surname;
private final int id;

/**
* Builds a new {@link AccountHolder}.
* @param name the name of the bank account holder.
* @param surname the surname of the bank account holder.
* @param id the user id of the bank account holder.
*/
public AccountHolder(final String name, final String surname, final int id) {
this.name = name;
this.surname = surname;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package it.unibo.bank.api;

/**
* Representation of a Banking account
* Representation of a Banking account.
*/
public interface BankAccount {
/**
Expand All @@ -10,6 +10,7 @@ public interface BankAccount {
* computed, it directly collects the amount from the balance.
* It also resets the transaction number of the bank account.
* The operation is performed only if the user ID is corresponding.
*
* @param id the account holder identifier.
* @throws IllegalArgumentException if the user ID does not correspond.
*/
Expand All @@ -18,6 +19,7 @@ public interface BankAccount {
/**
* Increments the number of transactions and adds the amount to the bank account's balance.
* The deposit is performed only if the user ID is corresponding.
*
* @param id the account holder identifier.
* @param amount the amount of money to deposit into the bank account.
* @throws IllegalArgumentException if the user ID does not correspond.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@ public class SimpleBankAccount implements BankAccount {
private double balance;
private int transactions;

/**
* Builds a new {@link SimpleBankAccount}.
* @param accountHolder the account holder
* @param balance the initial balance
*/
public SimpleBankAccount(final AccountHolder accountHolder, final double balance) {
this.holder = accountHolder;
this.balance = balance;
this.transactions = 0;
}

/**
* {@inheritDoc}
*/
@Override
public void chargeManagementFees(final int id) {
if (checkUser(id)) {
Expand All @@ -38,40 +46,66 @@ public void chargeManagementFees(final int id) {
}
}

/**
* {@inheritDoc}
*/
@Override
public void deposit(final int id, final double amount) {
this.transactionOp(id, amount);
}

/**
* {@inheritDoc}
*/
@Override
public void depositFromATM(final int id, final double amount) {
this.deposit(id, amount - SimpleBankAccount.ATM_TRANSACTION_FEE);
}

/**
* {@inheritDoc}
*/
@Override
public AccountHolder getAccountHolder() {
return holder;
}

/**
* {@inheritDoc}
*/
@Override
public double getBalance() {
return this.balance;
}

/**
* Sets the balance of the bank account.
*
* @param balance the new balance
*/
protected void setBalance(final double balance) {
this.balance = balance;
}

/**
* {@inheritDoc}
*/
@Override
public int getTransactionsCount() {
return this.transactions;
}

/**
* {@inheritDoc}
*/
@Override
public void withdraw(final int id, final double amount) {
this.transactionOp(id, -amount);
}

/**
* {@inheritDoc}
*/
@Override
public void withdrawFromATM(final int id, final double amount) {
this.withdraw(id, amount + SimpleBankAccount.ATM_TRANSACTION_FEE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@
*/
public class StrictBankAccount extends SimpleBankAccount {

/**
* Transaction fee charged to the bank account.
*/
public static final double TRANSACTION_FEE = 0.1;

/**
* Builds a new {@link StrictBankAccount}.
*
* @param accountHolder the account holder
* @param balance the initial balance
*/
public StrictBankAccount(final AccountHolder accountHolder, final double balance) {
super(accountHolder, balance);
}
Expand All @@ -20,9 +29,10 @@ public StrictBankAccount(final AccountHolder accountHolder, final double balance
* @param usrID the account holder identifier.
* @throws IllegalArgumentException if the id does not correspond.
*/
@Override
public void chargeManagementFees(final int usrID) {
final double feeAmount = MANAGEMENT_FEE + getTransactionsCount() * TRANSACTION_FEE;
if (checkUser(usrID) ) {
if (checkUser(usrID)) {
if (isWithdrawAllowed(feeAmount)) {
setBalance(getBalance() - feeAmount);
resetTransactions();
Expand All @@ -35,11 +45,13 @@ public void chargeManagementFees(final int usrID) {

/**
* Takes an amount of money from the bank account.
*
* @param usrID the account holder identifier.
* @param amount the amount of money to withdraw into the bank account.
* @throws IllegalArgumentException if the amount to withdraw is a negative value.
* @throws IllegalArgumentException if the balance is lower than the amount to take.
*/
@Override
public void withdraw(final int usrID, final double amount) {
if (amount < 0) {
throw new IllegalArgumentException("Cannot withdraw a negative amount");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,25 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestSimpleBankAccount {
/**
* Test class for the {@link SimpleBankAccount} class.
*/
class TestSimpleBankAccount {
private AccountHolder mRossi;
private AccountHolder aBianchi;
private BankAccount bankAccount;

private static final int AMOUNT = 100;
private static final int ACCEPTABLE_MESSAGE_LENGTH = 10;

/**
* Configuration step: this is performed BEFORE each test.
*/
@BeforeEach
public void setUp() {
void setUp() {
this.mRossi = new AccountHolder("Mario", "Rossi", 1);
this.aBianchi = new AccountHolder("Andrea", "Bianchi", 2);
this.bankAccount = new SimpleBankAccount(mRossi, 0.0);
Expand All @@ -29,7 +36,7 @@ public void setUp() {
* Check that the initialization of the BankAccount is created with the correct values.
*/
@Test
public void testBankAccountInitialization() {
void testBankAccountInitialization() {
assertEquals(0.0, bankAccount.getBalance());
assertEquals(0, bankAccount.getTransactionsCount());
assertEquals(mRossi, bankAccount.getAccountHolder());
Expand All @@ -39,29 +46,38 @@ public void testBankAccountInitialization() {
* Check that the deposit is performed correctly on the Bank Account.
*/
@Test
public void testBankAccountDeposit() {
int expectedValue = 0;
assertFalse(bankAccount.getTransactionsCount() > 0);
for(int i = 0; i < 10; i++) {
expectedValue += i * 100;
bankAccount.deposit(mRossi.getUserID(), i * 100);
void testBankAccountDeposit() {
for (int i = 0; i < 10;) {
assertEquals(i, bankAccount.getTransactionsCount());
assertEquals(i * AMOUNT, bankAccount.getBalance());
bankAccount.deposit(mRossi.getUserID(), AMOUNT);
i++;
assertEquals(i * AMOUNT, bankAccount.getBalance());
assertEquals(i, bankAccount.getTransactionsCount());
}
assertEquals(expectedValue, bankAccount.getBalance());
assertTrue(bankAccount.getTransactionsCount() > 0);
}

/**
* Checks that if the wrong AccountHolder id is given, the deposit will return an IllegalArgumentException.
*/
@Test
public void testWrongBankAccountDeposit() {
void testWrongBankAccountDeposit() {
try {
bankAccount.deposit(aBianchi.getUserID(), 10000);
Assertions.fail();
bankAccount.deposit(aBianchi.getUserID(), AMOUNT);
Assertions.fail("Depositing from a wrong account was possible, but should have thrown an exception");
} catch (IllegalArgumentException e) {
assertEquals("ID not corresponding: cannot perform transaction", e.getMessage());
assertEquals(0, bankAccount.getBalance()); // No money was deposited, balance is consistent
assertNotNull(e.getMessage()); // Non-null message
assertFalse(e.getMessage().isBlank()); // Not a blank or empty message
assertTrue(e.getMessage().length() >= ACCEPTABLE_MESSAGE_LENGTH); // A message with a decent length
}
// Alternative (with reflection): Assertions.assertThrows
/*
* Conciser alternative
* (once you learn reflection, and preferably after you have learnt lambda expressions):
* Assertions.assertThrows
*
* Use only if you **already** know reflection and lambda expressions.
*/
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import static org.junit.jupiter.api.Assertions.fail;

public class TestStrictBankAccount {

private final static int INITIAL_AMOUNT = 100;
/**
* Test class for the {@link StrictBankAccount} class.
*/
class TestStrictBankAccount {

// Create a new AccountHolder and a StrictBankAccount for it each time tests are executed.
private AccountHolder mRossi;
Expand Down
14 changes: 1 addition & 13 deletions java/testing/tdd-deathnote/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
plugins {
java
application
}

tasks.javadoc {
isFailOnError = false
}

repositories {
Expand All @@ -19,14 +14,7 @@ dependencies {
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.10.1")
}

val mainClass: String by project

application {
// The following allows to run with: ./gradlew -PmainClass=it.unibo.oop.MyMainClass run
mainClass.set(project.properties["mainClass"].toString())
}

val test by tasks.getting(Test::class) {
tasks.withType<Test> {
// Use junit platform for unit tests
useJUnitPlatform()
testLogging {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface DeathNote {
/**
* Returns the list of rules for this DeathNote.
*/
static List<String> RULES = List.of(
List<String> RULES = List.of(
"""
The human whose name is written in this note shall die.
""",
Expand Down
2 changes: 1 addition & 1 deletion java/testing/tdd/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ application {
mainClass.set(project.properties["mainClass"].toString())
}

val test by tasks.getting(Test::class) {
tasks.withType<Test> {
// Use junit platform for unit tests
useJUnitPlatform()
testLogging {
Expand Down

0 comments on commit 051e6ce

Please sign in to comment.