diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c23723a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.class +*.jar +*.json \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index e112a70..b98464e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,7 +1,25 @@ { "java.project.sourcePaths": ["src"], "java.project.outputPath": "bin", - "java.project.referencedLibraries": [ - "lib/**/*.jar" - ] + "java.project.referencedLibraries": { + "include": [ + "lib/**/*.jar", + "test-lib/junit-4.13.2.jar", + "test-lib/hamcrest-core-1.3.jar", + "test-lib/mockito-core-4.8.1.jar", + "test-lib/byte-buddy-1.12.16.jar", + "test-lib/byte-buddy-agent-1.12.16.jar", + "test-lib/generics-resolver-3.0.3.jar", + "test-lib/mockito-scala_2.13-1.17.30.jar", + "test-lib/mockito-scala-scalatest_2.13-1.17.30.jar", + "test-lib/objenesis-3.2.jar", + "test-lib/scalactic_2.13-3.2.17.jar", + "test-lib/scala-library-2.13.8.jar", + "test-lib/scala-parallel-collections_2.13-1.0.4.jar", + "test-lib/scala-reflect-2.13.8.jar" + ], + "exclude": [ + "lib/junit-4.13.2.jar" + ] + } } diff --git a/bin/Account.class b/bin/Account.class index 5d2d636..ba7e104 100644 Binary files a/bin/Account.class and b/bin/Account.class differ diff --git a/bin/TestEuro.class b/bin/TestEuro.class new file mode 100644 index 0000000..5a31d40 Binary files /dev/null and b/bin/TestEuro.class differ diff --git a/componenti.txt b/componenti.txt new file mode 100644 index 0000000..d541bfd --- /dev/null +++ b/componenti.txt @@ -0,0 +1,2 @@ +Matteo Zanardi, S5002780 +Lazzaro Simone S5208579 diff --git a/src/Account.java b/src/code/Businness_logic/Account.java similarity index 77% rename from src/Account.java rename to src/code/Businness_logic/Account.java index c308eca..3a7e2c2 100644 --- a/src/Account.java +++ b/src/code/Businness_logic/Account.java @@ -1,12 +1,12 @@ -// Account.java -// Represents a bank account +package code.Businness_logic; +//import code.Businness_logic.Euro; 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, @@ -14,8 +14,8 @@ public Account( int theAccountNumber, int thePIN, { accountNumber = theAccountNumber; pin = thePIN; - availableBalance = theAvailableBalance; - totalBalance = theTotalBalance; + availableBalance = new Euro(theAvailableBalance); + totalBalance = new Euro(theTotalBalance); } // end Account constructor // determines whether a user-specified PIN matches PIN in Account @@ -28,28 +28,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/CashDispenser.java b/src/code/Businness_logic/CashDispenser.java similarity index 85% rename from src/CashDispenser.java rename to src/code/Businness_logic/CashDispenser.java index b249faf..dd5d841 100644 --- a/src/CashDispenser.java +++ b/src/code/Businness_logic/CashDispenser.java @@ -1,3 +1,4 @@ +package code.Businness_logic; // CashDispenser.java // Represents the cash dispenser of the ATM @@ -14,16 +15,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 + int billsRequired = ((int)(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 + int billsRequired = ((int)(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/Businness_logic/Deposit.java similarity index 90% rename from src/Deposit.java rename to src/code/Businness_logic/Deposit.java index 916ef70..cb6dd32 100644 --- a/src/Deposit.java +++ b/src/code/Businness_logic/Deposit.java @@ -1,9 +1,15 @@ +package code.Businness_logic; +// import code.Businness_logic.Euro; // Deposit.java // Represents a deposit ATM transaction +import code.Database.BankDatabase; +import code.GUI.Keypad; +import code.GUI.Screen; + 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 @@ -30,7 +36,7 @@ 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((new Euro(-0, CANCELED))) ) { // request deposit envelope containing specified amount screen.displayMessage( @@ -65,7 +71,7 @@ public void execute() } // end method execute // prompt user to enter a deposit amount in cents - private double promptForDepositAmount() + private Euro promptForDepositAmount() { Screen screen = getScreen(); // get reference to screen @@ -76,10 +82,10 @@ private double promptForDepositAmount() // check whether the user canceled or entered a valid amount if ( input == CANCELED ) - return CANCELED; + return new Euro(-0, CANCELED); else { - return ( double ) input / 100; // return dollar amount + return new Euro(0, input); // return dollar amount } // end else } // end method promptForDepositAmount } // end class Deposit diff --git a/src/DepositSlot.java b/src/code/Businness_logic/DepositSlot.java similarity index 97% rename from src/DepositSlot.java rename to src/code/Businness_logic/DepositSlot.java index 64e02c2..6baba91 100644 --- a/src/DepositSlot.java +++ b/src/code/Businness_logic/DepositSlot.java @@ -1,3 +1,4 @@ +package code.Businness_logic; // DepositSlot.java // Represents the deposit slot of the ATM diff --git a/src/code/Businness_logic/Euro.java b/src/code/Businness_logic/Euro.java new file mode 100644 index 0000000..b04585a --- /dev/null +++ b/src/code/Businness_logic/Euro.java @@ -0,0 +1,48 @@ +package code.Businness_logic; +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 minoreDi(Euro e){ + if (valore <= e.getValore()) + return true; + else return false; + } + + public String stampa(){ + return (double)valore/100 +" euro"; + } +} \ No newline at end of file diff --git a/src/Transaction.java b/src/code/Businness_logic/Transaction.java similarity index 96% rename from src/Transaction.java rename to src/code/Businness_logic/Transaction.java index 508fea8..0ff6a84 100644 --- a/src/Transaction.java +++ b/src/code/Businness_logic/Transaction.java @@ -1,6 +1,10 @@ +package code.Businness_logic; // Transaction.java // Abstract superclass Transaction represents an ATM transaction +import code.Database.BankDatabase; +import code.GUI.Screen; + public abstract class Transaction { private int accountNumber; // indicates account involved diff --git a/src/Withdrawal.java b/src/code/Businness_logic/Withdrawal.java similarity index 90% rename from src/Withdrawal.java rename to src/code/Businness_logic/Withdrawal.java index 6e0af62..d84dca9 100644 --- a/src/Withdrawal.java +++ b/src/code/Businness_logic/Withdrawal.java @@ -1,9 +1,14 @@ +package code.Businness_logic; // Withdrawal.java // Represents a withdrawal ATM transaction +import code.Database.BankDatabase; +import code.GUI.Keypad; +import code.GUI.Screen; + 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(); @@ -40,14 +45,14 @@ public void execute() amount = displayMenuOfAmounts(); // check whether user chose a withdrawal amount or canceled - if ( amount != CANCELED ) + if ( amount.getValore() != CANCELED ) { // get available balance of account involved availableBalance = bankDatabase.getAvailableBalance( getAccountNumber() ); // check whether the user has enough money in the account - if ( amount <= availableBalance ) + if ( amount.getValore() <= availableBalance.getValore() ) { // check whether the cash dispenser has enough money if ( cashDispenser.isSufficientCashAvailable( amount ) ) @@ -85,14 +90,14 @@ public void execute() // display a menu of withdrawal amounts and the option to cancel; // return the chosen amount or 0 if the user chooses to cancel - private int displayMenuOfAmounts() + private Euro displayMenuOfAmounts() { - int userChoice = 0; // local variable to store return value + double userChoice = 0; // local variable to store return value Screen screen = getScreen(); // get screen reference // array of amounts to correspond to menu numbers - int amounts[] = { 0, 20, 40, 60, 100, 200 }; + double amounts[] = { 0.0, 20.0, 40.0, 60.0, 100.0, 200.0 }; // loop while no valid choice has been made while ( userChoice == 0 ) @@ -128,7 +133,7 @@ private int displayMenuOfAmounts() } // end switch } // end while - return userChoice; // return withdrawal amount or CANCELED + return new Euro(userChoice); // return withdrawal amount or CANCELED } // end method displayMenuOfAmounts } // end class Withdrawal diff --git a/src/BankDatabase.java b/src/code/Database/BankDatabase.java similarity index 90% rename from src/BankDatabase.java rename to src/code/Database/BankDatabase.java index 3978497..350ad6f 100644 --- a/src/BankDatabase.java +++ b/src/code/Database/BankDatabase.java @@ -1,5 +1,6 @@ -// BankDatabase.java -// Represents the bank account information database +package code.Database; +import code.Businness_logic.Account; +import code.Businness_logic.Euro; public class BankDatabase { @@ -42,25 +43,25 @@ 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 ); } // end method debit diff --git a/src/ATM.java b/src/code/GUI/ATM.java similarity index 96% rename from src/ATM.java rename to src/code/GUI/ATM.java index aa3d187..695d97d 100644 --- a/src/ATM.java +++ b/src/code/GUI/ATM.java @@ -1,6 +1,14 @@ +package code.GUI; // ATM.java // Represents an automated teller machine +import code.Businness_logic.CashDispenser; +import code.Businness_logic.Deposit; +import code.Businness_logic.DepositSlot; +import code.Businness_logic.Transaction; +import code.Businness_logic.Withdrawal; +import code.Database.BankDatabase; + 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 92% rename from src/BalanceInquiry.java rename to src/code/GUI/BalanceInquiry.java index d45fa6a..d88c92e 100644 --- a/src/BalanceInquiry.java +++ b/src/code/GUI/BalanceInquiry.java @@ -1,5 +1,7 @@ -// BalanceInquiry.java -// Represents a balance inquiry ATM transaction +package code.GUI; +import code.Businness_logic.Euro; +import code.Businness_logic.Transaction; +import code.Database.BankDatabase; public class BalanceInquiry extends Transaction { @@ -18,11 +20,11 @@ public void execute() Screen screen = getScreen(); // get the available balance for the account involved - double availableBalance = + Euro availableBalance = bankDatabase.getAvailableBalance( getAccountNumber() ); // get the total balance for the account involved - double totalBalance = + Euro totalBalance = bankDatabase.getTotalBalance( getAccountNumber() ); // display the balance information on the screen 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 89% rename from src/Screen.java rename to src/code/GUI/Screen.java index 44d3f30..5753644 100644 --- a/src/Screen.java +++ b/src/code/GUI/Screen.java @@ -1,5 +1,5 @@ -// Screen.java -// Represents the screen of the ATM +package code.GUI; +import code.Businness_logic.Euro; public class Screen { @@ -16,9 +16,9 @@ public void displayMessageLine( String message ) } // end method displayMessageLine // display a dollar amount - public void displayDollarAmount( double amount ) + public void displayDollarAmount( Euro amount ) { - System.out.printf( "$%,.2f", amount ); + System.out.printf( amount.stampa() ); //TODO: "$%,.2f", controllare che vada } // end method displayDollarAmount } // end class Screen diff --git a/src/test/TestEuro.java b/src/test/TestEuro.java new file mode 100644 index 0000000..51ccf5e --- /dev/null +++ b/src/test/TestEuro.java @@ -0,0 +1,62 @@ +package test; +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; +import code.Businness_logic.*; + +public class TestEuro{ + + private Account account; + + @Before + public void setUp() { + account = new Account(123, 321, 100.00, 100.00); // Impostazioni iniziali dell'account + } + + @Test + public void testAccountNumber() { + assertEquals(123, account.getAccountNumber()); // Verifica che il numero dell'account sia corretto + } + + @Test + public void testInitialBalanceAfterAccountCreation() { + Euro expected = new Euro(100.0); + assertEquals("", expected.stampa(), account.getAvailableBalance().stampa()); + assertEquals("", expected.stampa(), account.getTotalBalance().stampa()); + } + + @Test + public void testDebitLessThanAvailableBalance() { + Euro expected = new Euro(50.0); + Euro operation = new Euro(50.0); + account.debit(operation); + assertEquals("", expected.stampa(), account.getAvailableBalance().stampa()); + } + + @Test + public void testDebitEqualToAvailableBalance() { + Euro operation = new Euro(100.0); + account.debit(operation); + assertEquals("", new Euro(0.0).stampa(), account.getAvailableBalance().stampa()); + } + + @Test + public void testCreditPositiveAmount() { + Euro expected = new Euro(150.0); + Euro operation = new Euro(50.0); + account.credit(operation); + assertEquals("", expected.stampa(), account.getTotalBalance().stampa()); + } + + @Test + public void testInvalidPIN() { + assertFalse(account.validatePIN(789)); // Supponiamo che il PIN corretto sia 321 + } + + @Test + public void testValidPIN() { + assertTrue(account.validatePIN(321)); + } + + +} \ No newline at end of file diff --git a/test-lib/hamcrest-core-1.3.jar b/test-lib/hamcrest-core-1.3.jar new file mode 100644 index 0000000..9d5fe16 Binary files /dev/null and b/test-lib/hamcrest-core-1.3.jar differ diff --git a/test-lib/junit-4.13.2.jar b/test-lib/junit-4.13.2.jar new file mode 100644 index 0000000..6da55d8 Binary files /dev/null and b/test-lib/junit-4.13.2.jar differ