diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6091879 --- /dev/null +++ b/.gitignore @@ -0,0 +1,24 @@ +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +replay_pid* \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index e112a70..c40394c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,6 +2,8 @@ "java.project.sourcePaths": ["src"], "java.project.outputPath": "bin", "java.project.referencedLibraries": [ - "lib/**/*.jar" + "lib/**/*.jar", + "c:\\Users\\cacci\\.m2\\repository\\junit\\junit\\4.13.2\\junit-4.13.2.jar", + "c:\\Users\\cacci\\.m2\\repository\\org\\hamcrest\\hamcrest-core\\1.3\\hamcrest-core-1.3.jar" ] } diff --git a/bin/ATM.class b/bin/ATM.class deleted file mode 100644 index 76543bf..0000000 Binary files a/bin/ATM.class and /dev/null differ diff --git a/bin/ATMCaseStudy.class b/bin/ATMCaseStudy.class deleted file mode 100644 index 607c10d..0000000 Binary files a/bin/ATMCaseStudy.class and /dev/null differ diff --git a/bin/Account.class b/bin/Account.class deleted file mode 100644 index 5d2d636..0000000 Binary files a/bin/Account.class and /dev/null differ diff --git a/bin/BalanceInquiry.class b/bin/BalanceInquiry.class deleted file mode 100644 index 48f8e6a..0000000 Binary files a/bin/BalanceInquiry.class and /dev/null differ diff --git a/bin/BankDatabase.class b/bin/BankDatabase.class deleted file mode 100644 index 429aec1..0000000 Binary files a/bin/BankDatabase.class and /dev/null differ diff --git a/bin/CashDispenser.class b/bin/CashDispenser.class deleted file mode 100644 index b8cedba..0000000 Binary files a/bin/CashDispenser.class and /dev/null differ diff --git a/bin/Deposit.class b/bin/Deposit.class deleted file mode 100644 index 57b0c3f..0000000 Binary files a/bin/Deposit.class and /dev/null differ diff --git a/bin/DepositSlot.class b/bin/DepositSlot.class deleted file mode 100644 index a2664ad..0000000 Binary files a/bin/DepositSlot.class and /dev/null differ diff --git a/bin/Keypad.class b/bin/Keypad.class deleted file mode 100644 index 9b066e2..0000000 Binary files a/bin/Keypad.class and /dev/null differ diff --git a/bin/Screen.class b/bin/Screen.class deleted file mode 100644 index a036758..0000000 Binary files a/bin/Screen.class and /dev/null differ diff --git a/bin/Transaction.class b/bin/Transaction.class deleted file mode 100644 index 1330b55..0000000 Binary files a/bin/Transaction.class and /dev/null differ diff --git a/bin/Withdrawal.class b/bin/Withdrawal.class deleted file mode 100644 index 291d645..0000000 Binary files a/bin/Withdrawal.class and /dev/null differ diff --git a/componenti.txt b/componenti.txt new file mode 100644 index 0000000..ad63ffd --- /dev/null +++ b/componenti.txt @@ -0,0 +1,2 @@ +Charnelle Sonia Kengue Kentsop S4654827 +Mattia Cacciattore S4850100 \ No newline at end of file diff --git a/src/DepositSlot.java b/src/code/Business_logic/DepositSlot.java similarity index 98% rename from src/DepositSlot.java rename to src/code/Business_logic/DepositSlot.java index 64e02c2..e310a6b 100644 --- a/src/DepositSlot.java +++ b/src/code/Business_logic/DepositSlot.java @@ -1,3 +1,4 @@ +package code.Business_logic; // DepositSlot.java // Represents the deposit slot of the ATM diff --git a/src/code/Business_logic/Euro.java b/src/code/Business_logic/Euro.java new file mode 100644 index 0000000..921f1e8 --- /dev/null +++ b/src/code/Business_logic/Euro.java @@ -0,0 +1,49 @@ +package code.Business_logic; +//ricordare di aggiungere il .class al file gitignore. + +public class Euro { + + private long valore; + + public Euro(long euro, long cent) { + if (euro >= 0) { + valore = euro*100 + cent; + } else { + valore = euro*100 - cent; + } + } + + public Euro(double d) { + valore = (long)(d*100); + } + + public long getValore() { + return valore; + } + + public Euro somma(Euro e) { + this.valore = this.valore + e.getValore(); + return this; + } + + public Euro sottrai(Euro e) { + this.valore = this.valore - e.getValore(); + return this; + } + + public boolean ugualeA(Euro e){ + if (valore == e.getValore()) + return true; + else return false; + } + + public boolean minoreOUgualeDi(Euro e){ + if (valore <= e.getValore()) + return true; + else return false; + } + + public String stampa(){ + return (double)valore/100 +" euro"; + } +} diff --git a/src/Account.java b/src/code/Database/Account.java similarity index 79% rename from src/Account.java rename to src/code/Database/Account.java index c308eca..d4134cf 100644 --- a/src/Account.java +++ b/src/code/Database/Account.java @@ -1,3 +1,5 @@ +package code.Database; +import code.Business_logic.Euro; // Account.java // Represents a bank account @@ -5,12 +7,12 @@ public class Account { private int accountNumber; // account number private int pin; // PIN for authentication - private double availableBalance; // funds available for withdrawal - private double totalBalance; // funds available + pending deposits + private Euro availableBalance; // funds available for withdrawal + private Euro totalBalance; // funds available + pending deposits // Account constructor initializes attributes public Account( int theAccountNumber, int thePIN, - double theAvailableBalance, double theTotalBalance ) + Euro theAvailableBalance, Euro theTotalBalance ) { accountNumber = theAccountNumber; pin = thePIN; @@ -28,28 +30,28 @@ public boolean validatePIN( int userPIN ) } // end method validatePIN // returns available balance - public double getAvailableBalance() + public Euro getAvailableBalance() { return availableBalance; } // end getAvailableBalance // returns the total balance - public double getTotalBalance() + public Euro getTotalBalance() { return totalBalance; } // end method getTotalBalance // credits an amount to the account - public void credit( double amount ) + public void credit( Euro amount ) { - totalBalance += amount; // add to total balance + totalBalance.somma(amount); // add to total balance } // end method credit // debits an amount from the account - public void debit( double amount ) + public void debit( Euro amount ) { - availableBalance -= amount; // subtract from available balance - totalBalance -= amount; // subtract from total balance + availableBalance.sottrai(amount); // subtract from available balance + totalBalance.sottrai(amount); // subtract from total balance } // end method debit // returns account number diff --git a/src/BankDatabase.java b/src/code/Database/BankDatabase.java similarity index 82% rename from src/BankDatabase.java rename to src/code/Database/BankDatabase.java index 3978497..1addd84 100644 --- a/src/BankDatabase.java +++ b/src/code/Database/BankDatabase.java @@ -1,3 +1,5 @@ +package code.Database; +import code.Business_logic.Euro; // BankDatabase.java // Represents the bank account information database @@ -9,8 +11,8 @@ public class BankDatabase public BankDatabase() { accounts = new Account[ 2 ]; // just 2 accounts for testing - accounts[ 0 ] = new Account( 12345, 54321, 1000.0, 1200.0 ); - accounts[ 1 ] = new Account( 98765, 56789, 200.0, 200.0 ); + accounts[0] = new Account(12345, 54321, new Euro(1000.0), new Euro(1200.0)); + accounts[1] = new Account(98765, 56789, new Euro(200.0), new Euro(200.0)); } // end no-argument BankDatabase constructor // retrieve Account object containing specified account number @@ -42,27 +44,30 @@ public boolean authenticateUser( int userAccountNumber, int userPIN ) } // end method authenticateUser // return available balance of Account with specified account number - public double getAvailableBalance( int userAccountNumber ) + public Euro getAvailableBalance( int userAccountNumber ) { return getAccount( userAccountNumber ).getAvailableBalance(); } // end method getAvailableBalance // return total balance of Account with specified account number - public double getTotalBalance( int userAccountNumber ) + public Euro getTotalBalance( int userAccountNumber ) { return getAccount( userAccountNumber ).getTotalBalance(); } // end method getTotalBalance // credit an amount to Account with specified account number - public void credit( int userAccountNumber, double amount ) + public void credit( int userAccountNumber, Euro amount ) { getAccount( userAccountNumber ).credit( amount ); } // end method credit // debit an amount from of Account with specified account number - public void debit( int userAccountNumber, double amount ) + public void debit( int userAccountNumber, Euro amount ) { - getAccount( userAccountNumber ).debit( amount ); + // Check if the account has enough money to withdraw + if(getAccount( userAccountNumber ).getAvailableBalance().getValore() >= amount.getValore()){ + getAccount( userAccountNumber ).debit( amount ); + } } // end method debit } // end class BankDatabase diff --git a/src/CashDispenser.java b/src/code/Database/CashDispenser.java similarity index 85% rename from src/CashDispenser.java rename to src/code/Database/CashDispenser.java index b249faf..fe39aa6 100644 --- a/src/CashDispenser.java +++ b/src/code/Database/CashDispenser.java @@ -1,3 +1,5 @@ +package code.Database; +import code.Business_logic.Euro; // CashDispenser.java // Represents the cash dispenser of the ATM @@ -14,16 +16,16 @@ public CashDispenser() } // end CashDispenser constructor // simulates dispensing of specified amount of cash - public void dispenseCash( int amount ) + public void dispenseCash( Euro amount ) { - int billsRequired = amount / 20; // number of $20 bills required + long billsRequired = (amount.getValore()/100) / 20; // number of $20 bills required count -= billsRequired; // update the count of bills } // end method dispenseCash // indicates whether cash dispenser can dispense desired amount - public boolean isSufficientCashAvailable( int amount ) + public boolean isSufficientCashAvailable( Euro amount ) { - int billsRequired = amount / 20; // number of $20 bills required + long billsRequired = (amount.getValore()/100) / 20; // number of $20 bills required if ( count >= billsRequired ) return true; // enough bills available diff --git a/src/Deposit.java b/src/code/Database/Deposit.java similarity index 85% rename from src/Deposit.java rename to src/code/Database/Deposit.java index 916ef70..10a43cd 100644 --- a/src/Deposit.java +++ b/src/code/Database/Deposit.java @@ -1,12 +1,19 @@ +package code.Database; // Deposit.java // Represents a deposit ATM transaction +import code.Business_logic.DepositSlot; +import code.Business_logic.Euro; +import code.GUI.Keypad; +import code.GUI.Screen; +import code.GUI.Transaction; + public class Deposit extends Transaction { - private double amount; // amount to deposit + private Euro amount; // amount to deposit private Keypad keypad; // reference to keypad private DepositSlot depositSlot; // reference to deposit slot - private final static int CANCELED = 0; // constant for cancel option + private final static Euro CANCELED = new Euro(0.0); // constant for cancel option // Deposit constructor public Deposit( int userAccountNumber, Screen atmScreen, @@ -30,12 +37,12 @@ public void execute() amount = promptForDepositAmount(); // get deposit amount from user // check whether user entered a deposit amount or canceled - if ( amount != CANCELED ) + if (!amount.ugualeA(CANCELED)) { // request deposit envelope containing specified amount screen.displayMessage( "\nPlease insert a deposit envelope containing " ); - screen.displayDollarAmount( amount ); + screen.displayEuroAmount( amount ); screen.displayMessageLine( "." ); // receive deposit envelope @@ -64,22 +71,22 @@ public void execute() } // end else } // end method execute - // prompt user to enter a deposit amount in cents - private double promptForDepositAmount() + // prompt user to enter a deposit amount in euro + private Euro promptForDepositAmount() { Screen screen = getScreen(); // get reference to screen // display the prompt screen.displayMessage( "\nPlease enter a deposit amount in " + - "CENTS (or 0 to cancel): " ); + "EURO (or 0 to cancel): " ); int input = keypad.getInput(); // receive input of deposit amount // check whether the user canceled or entered a valid amount - if ( input == CANCELED ) + if ( input == CANCELED.getValore() ) return CANCELED; else { - return ( double ) input / 100; // return dollar amount + return new Euro(input / 100.0); // return dollar amount } // end else } // end method promptForDepositAmount } // end class Deposit diff --git a/src/ATM.java b/src/code/GUI/ATM.java similarity index 97% rename from src/ATM.java rename to src/code/GUI/ATM.java index aa3d187..ec7b09b 100644 --- a/src/ATM.java +++ b/src/code/GUI/ATM.java @@ -1,6 +1,12 @@ +package code.GUI; // ATM.java // Represents an automated teller machine +import code.Business_logic.DepositSlot; +import code.Database.BankDatabase; +import code.Database.CashDispenser; +import code.Database.Deposit; + public class ATM { private boolean userAuthenticated; // whether user is authenticated diff --git a/src/ATMCaseStudy.java b/src/code/GUI/ATMCaseStudy.java similarity index 98% rename from src/ATMCaseStudy.java rename to src/code/GUI/ATMCaseStudy.java index b9033bd..5beedde 100644 --- a/src/ATMCaseStudy.java +++ b/src/code/GUI/ATMCaseStudy.java @@ -1,3 +1,4 @@ +package code.GUI; // ATMCaseStudy.java // Driver program for the ATM case study diff --git a/src/BalanceInquiry.java b/src/code/GUI/BalanceInquiry.java similarity index 85% rename from src/BalanceInquiry.java rename to src/code/GUI/BalanceInquiry.java index d45fa6a..df3c2a0 100644 --- a/src/BalanceInquiry.java +++ b/src/code/GUI/BalanceInquiry.java @@ -1,5 +1,8 @@ +package code.GUI; // BalanceInquiry.java // Represents a balance inquiry ATM transaction +import code.Business_logic.Euro; +import code.Database.BankDatabase; public class BalanceInquiry extends Transaction { @@ -18,19 +21,17 @@ public void execute() Screen screen = getScreen(); // get the available balance for the account involved - double availableBalance = - bankDatabase.getAvailableBalance( getAccountNumber() ); + Euro availableBalance = bankDatabase.getAvailableBalance( getAccountNumber() ); // get the total balance for the account involved - double totalBalance = - bankDatabase.getTotalBalance( getAccountNumber() ); + Euro totalBalance = bankDatabase.getTotalBalance( getAccountNumber() ); // display the balance information on the screen screen.displayMessageLine( "\nBalance Information:" ); screen.displayMessage( " - Available balance: " ); - screen.displayDollarAmount( availableBalance ); + screen.displayEuroAmount( availableBalance ); screen.displayMessage( "\n - Total balance: " ); - screen.displayDollarAmount( totalBalance ); + screen.displayEuroAmount( totalBalance ); screen.displayMessageLine( "" ); } // end method execute } // end class BalanceInquiry diff --git a/src/Keypad.java b/src/code/GUI/Keypad.java similarity index 98% rename from src/Keypad.java rename to src/code/GUI/Keypad.java index cd035c7..f0f716c 100644 --- a/src/Keypad.java +++ b/src/code/GUI/Keypad.java @@ -1,3 +1,4 @@ +package code.GUI; // Keypad.java // Represents the keypad of the ATM import java.util.Scanner; // program uses Scanner to obtain user input diff --git a/src/Screen.java b/src/code/GUI/Screen.java similarity index 73% rename from src/Screen.java rename to src/code/GUI/Screen.java index 44d3f30..d0327cf 100644 --- a/src/Screen.java +++ b/src/code/GUI/Screen.java @@ -1,3 +1,5 @@ +package code.GUI; +import code.Business_logic.Euro; // Screen.java // Represents the screen of the ATM @@ -16,12 +18,20 @@ public void displayMessageLine( String message ) } // end method displayMessageLine // display a dollar amount - public void displayDollarAmount( double amount ) + public void displayEuroAmount( Euro amount ) { - System.out.printf( "$%,.2f", amount ); - } // end method displayDollarAmount + // Note: \u20ac is the Unicode character for the Euro symbol. + System.out.print("Euro "); + System.out.printf( "%,.2f", amount.getValore()/100.0 ); + } // end method displayEuroAmount } // end class Screen +/*In questo modo, il metodo displayEuroAmount accetta un oggetto di tipo Euro e lo formatta +correttamente per essere visualizzato sulla schermata dell'ATM. L'importo viene diviso per 100.0 + perché la rappresentazione interna di Euro è in centesimi.*/ + + + /************************************************************************** diff --git a/src/Transaction.java b/src/code/GUI/Transaction.java similarity index 97% rename from src/Transaction.java rename to src/code/GUI/Transaction.java index 508fea8..756cc05 100644 --- a/src/Transaction.java +++ b/src/code/GUI/Transaction.java @@ -1,6 +1,9 @@ +package code.GUI; // Transaction.java // Abstract superclass Transaction represents an ATM transaction +import code.Database.BankDatabase; + public abstract class Transaction { private int accountNumber; // indicates account involved diff --git a/src/Withdrawal.java b/src/code/GUI/Withdrawal.java similarity index 87% rename from src/Withdrawal.java rename to src/code/GUI/Withdrawal.java index 6e0af62..e4645df 100644 --- a/src/Withdrawal.java +++ b/src/code/GUI/Withdrawal.java @@ -1,9 +1,14 @@ +package code.GUI; // Withdrawal.java // Represents a withdrawal ATM transaction +import code.Database.CashDispenser; +import code.Business_logic.Euro; +import code.Database.BankDatabase; + public class Withdrawal extends Transaction { - private int amount; // amount to withdraw + private Euro amount; // amount to withdraw private Keypad keypad; // reference to keypad private CashDispenser cashDispenser; // reference to cash dispenser @@ -27,7 +32,7 @@ public Withdrawal( int userAccountNumber, Screen atmScreen, public void execute() { boolean cashDispensed = false; // cash was not dispensed yet - double availableBalance; // amount available for withdrawal + Euro availableBalance; // amount available for withdrawal // get references to bank database and screen BankDatabase bankDatabase = getBankDatabase(); @@ -37,25 +42,24 @@ public void execute() do { // obtain a chosen withdrawal amount from the user - amount = displayMenuOfAmounts(); - + amount = new Euro(displayMenuOfAmounts()); // check whether user chose a withdrawal amount or canceled - if ( amount != CANCELED ) - { + if (amount.getValore() / 100 != CANCELED) + { // get available balance of account involved availableBalance = - bankDatabase.getAvailableBalance( getAccountNumber() ); + bankDatabase.getAvailableBalance( getAccountNumber()); // check whether the user has enough money in the account - if ( amount <= availableBalance ) + if ( amount.minoreOUgualeDi(availableBalance) ) { // check whether the cash dispenser has enough money - if ( cashDispenser.isSufficientCashAvailable( amount ) ) + if ( cashDispenser.isSufficientCashAvailable( amount) ) { // update the account involved to reflect withdrawal bankDatabase.debit( getAccountNumber(), amount ); - cashDispenser.dispenseCash( amount ); // dispense cash + cashDispenser.dispenseCash( amount); // dispense cash cashDispensed = true; // cash was dispensed // instruct user to take cash @@ -93,21 +97,20 @@ private int displayMenuOfAmounts() // array of amounts to correspond to menu numbers int amounts[] = { 0, 20, 40, 60, 100, 200 }; - // loop while no valid choice has been made - while ( userChoice == 0 ) - { + while ( userChoice == 0 ) { // display the menu screen.displayMessageLine( "\nWithdrawal Menu:" ); - screen.displayMessageLine( "1 - $20" ); - screen.displayMessageLine( "2 - $40" ); - screen.displayMessageLine( "3 - $60" ); - screen.displayMessageLine( "4 - $100" ); - screen.displayMessageLine( "5 - $200" ); + screen.displayMessageLine( "1 - 20 Euro" ); + screen.displayMessageLine( "2 - 40 Euro" ); + screen.displayMessageLine( "3 - 60 Euro" ); + screen.displayMessageLine( "4 - 100 Euro" ); + screen.displayMessageLine( "5 - 200 Euro" ); screen.displayMessageLine( "6 - Cancel transaction" ); screen.displayMessage( "\nChoose a withdrawal amount: " ); int input = keypad.getInput(); // get user input through keypad + // determine how to proceed based on the input value switch ( input ) @@ -117,6 +120,7 @@ private int displayMenuOfAmounts() case 3: // corresponding amount from amounts array case 4: case 5: + userChoice = amounts[ input ]; // save user's choice break; case CANCELED: // the user chose to cancel diff --git a/src/test/TestAccount.java b/src/test/TestAccount.java new file mode 100644 index 0000000..ba8ec4f --- /dev/null +++ b/src/test/TestAccount.java @@ -0,0 +1,81 @@ +package test; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; + +import code.Business_logic.Euro; +import code.Database.Account; + +public class TestAccount{ + + private Account account; + + @Before + public void setUp(){ + // Inizializza l'oggetto Euro prima di ogni test + account = new Account(5678, 8765, new Euro(5000, 0), new Euro(5000, 0)); + } + + @Test + public void testAccount(){ + // Verifica che l'oggetto Euro sia stato inizializzato correttamente + assertEquals(5678, account.getAccountNumber()); + assertTrue(account.validatePIN(8765)); + assertEquals(500000, account.getAvailableBalance().getValore()); + assertEquals(500000, account.getTotalBalance().getValore()); + } + + @Test + public void testValidatePIN(){ + // Verifica che il metodo validatePIN() restituisca true se il PIN è + // corretto + assertTrue(account.validatePIN(8765)); + // Verifica che il metodo validatePIN() restituisca false se il PIN è + // errato + assertFalse(account.validatePIN(9876)); + assertFalse(account.validatePIN(98765)); + assertFalse(account.validatePIN(1)); + } + + @Test + public void testGetAvailableBalance(){ + // Verifica che il metodo getAvailableBalance() restituisca il saldo disponibile + assertEquals(500000, account.getAvailableBalance().getValore()); + } + + @Test + public void testGetTotalBalance(){ + // Verifica che il metodo getTotalBalance() restituisca il saldo totale + assertEquals(500000, account.getTotalBalance().getValore()); + } + + @Test + public void testCredit(){ + // Verifica che il metodo credit() aggiunga il saldo al totale + account.credit(new Euro(100, 0)); + assertEquals(510000, account.getTotalBalance().getValore()); + // Ci si aspetta fallisca. + assertEquals(500000, account.getAvailableBalance().getValore()); + } + + @Test + public void testDebit(){ + account.debit(new Euro(100, 0)); + assertEquals(490000, account.getTotalBalance().getValore()); + // Ci si aspetta fallisca. + assertEquals(490000, account.getAvailableBalance().getValore()); + } + + @Test + public void testGetAccountNumber(){ + // Verifica che il metodo getAccountNumber() restituisca true + // se il numero dell'account è corretto + assertEquals(5678, account.getAccountNumber()); + // Verifica che il metodo getAccountNumber() restituisca false + // se il numero dell'account è errato + assertNotEquals(568, account.getAccountNumber()); + assertNotEquals(5688, account.getAccountNumber()); + assertNotEquals(8, account.getAccountNumber()); + } +} \ No newline at end of file diff --git a/src/test/TestBankDatabase.java b/src/test/TestBankDatabase.java new file mode 100644 index 0000000..aecdfe2 --- /dev/null +++ b/src/test/TestBankDatabase.java @@ -0,0 +1,72 @@ +package test; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import code.Database.BankDatabase; +import code.Business_logic.Euro; + +public class TestBankDatabase { + + private BankDatabase bankDatabase; + + @Before + public void setUp() { + bankDatabase = new BankDatabase(); + } + + @Test + public void testAuthenticateUserValidCredentials() { + assertTrue(bankDatabase.authenticateUser(12345, 54321)); + } + + @Test + public void testAuthenticateUserInvalidCredentials() { + assertFalse(bankDatabase.authenticateUser(12345, 99999)); + } + + @Test + public void testGetAvailableBalance() { + Euro expectedBalance = new Euro(1000.0); + assertEquals(expectedBalance.getValore(), bankDatabase.getAvailableBalance(12345).getValore()); + } + + @Test + public void testgetAvailableBalance() { + Euro expectedBalance = new Euro(1000.0); + assertEquals(expectedBalance.getValore(), bankDatabase.getAvailableBalance(12345).getValore()); + } + + @Test + public void testGetTotalBalance() { + Euro expectedBalance = new Euro(1200.0); + assertEquals(expectedBalance.getValore(), bankDatabase.getTotalBalance(12345).getValore()); + } + + + @Test + public void testCredit() { + Euro amountToCredit = new Euro(300.0); + bankDatabase.credit(12345, amountToCredit); + Euro expectedBalance = new Euro(1500.0); + assertEquals(expectedBalance.getValore(), bankDatabase.getTotalBalance(12345).getValore()); + } + + @Test + public void testDebitSufficientFunds() { + Euro amountToDebit = new Euro(200.0); + bankDatabase.debit(12345, amountToDebit); + Euro expectedBalance = new Euro(800.0); + assertEquals(expectedBalance.getValore(), bankDatabase.getAvailableBalance(12345).getValore()); + } + + @Test + public void testDebitInsufficientFunds() { + Euro amountToDebit = new Euro(2000.0); + // Ensure that the balance doesn't change if there are insufficient funds + bankDatabase.debit(12345, amountToDebit); + // Ultimo commento. + Euro expectedBalance = new Euro(1000.0); + assertEquals(expectedBalance.getValore(), bankDatabase.getAvailableBalance(12345).getValore()); + } +} diff --git a/src/test/TestEuro.java b/src/test/TestEuro.java new file mode 100644 index 0000000..61c9211 --- /dev/null +++ b/src/test/TestEuro.java @@ -0,0 +1,90 @@ +package test; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; + +import code.Business_logic.Euro; + +public class TestEuro { + + private Euro euro; + + @Before + public void setup() { + // Inizializza l'oggetto Euro prima di ogni test + euro = new Euro(50, 75); + } + + @Test + public void testGetValore() { + assertEquals(5075, euro.getValore()); + assertNotEquals(5076, euro.getValore()); + assertNotEquals(5074, euro.getValore()); + } + + @Test + public void testSomma() { + Euro altroEuro = new Euro(25, 50); + Euro risultato = euro.somma(altroEuro); + + assertEquals(76.25, risultato.getValore() / 100.0, 0.01); + assertNotEquals(77.26, risultato.getValore() / 100.0, 0.01); + assertNotEquals(75.25, risultato.getValore() / 100.0, 0.01); + } + + @Test + public void testSottrai() { + Euro altroEuro = new Euro(25, 50); + Euro risultato = euro.sottrai(altroEuro); + + assertEquals(25.25, risultato.getValore() / 100.0, 0.01); + assertNotEquals(25.26, risultato.getValore() / 100.0, 0.01); + assertNotEquals(24.25, risultato.getValore() / 100.0, 0.01); + } + + @Test + public void testUgualeA() { + Euro stessoEuro = new Euro(50, 75); + Euro euroMaggiore = new Euro(50, 76); + Euro euroMinore = new Euro(50, 74); + + assertTrue(euro.ugualeA(stessoEuro)); + assertFalse(euro.ugualeA(euroMaggiore)); + assertFalse(euro.ugualeA(euroMinore)); + } + + @Test + public void testminoreOUgualeDi() { + Euro euroMaggiore = new Euro(75, 0); + Euro stessoEuro = new Euro(50, 75); + Euro euroMinore = new Euro(50, 74); + + assertTrue(euro.minoreOUgualeDi(euroMaggiore)); + assertFalse(euro.minoreOUgualeDi(stessoEuro)); // Ci si aspetta fallisca. + + assertTrue(euro.minoreOUgualeDi(stessoEuro)); + assertFalse(euro.minoreOUgualeDi(euroMinore)); + } + + @Test + public void testCostruttoreDouble() { + double valoreDouble = 20.75; + long valoreCent = 2075; + long valoreCentErrato = 2076; + long valoreCentErrato2 = 2074; + + Euro euroCostruito = new Euro(valoreDouble); + + assertEquals(valoreCent, euroCostruito.getValore()); + assertNotEquals(valoreCentErrato, euroCostruito.getValore()); + assertNotEquals(valoreCentErrato2, euroCostruito.getValore()); + } + + @Test + public void testStampa() { + assertEquals("50.75 euro", euro.stampa()); + assertNotEquals("50.76 euro", euro.stampa()); + assertNotEquals("50.74 euro", euro.stampa()); + } +}