diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1241418 --- /dev/null +++ b/.gitignore @@ -0,0 +1,44 @@ +# Compiled class files +*.class + +# Log files +*.log + +# Package files +*.war +*.ear + +# IntelliJ IDEA files +.idea/ + +# Eclipse files +.classpath +.project +.settings/ + +# Maven files +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties + +# Gradle files +.gradle/ +build/ + +# NetBeans files +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ + +# VS Code files +<<<<<<< HEAD +.vscode/ +======= +.vscode/ +>>>>>>> origin/feature diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index e112a70..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "java.project.sourcePaths": ["src"], - "java.project.outputPath": "bin", - "java.project.referencedLibraries": [ - "lib/**/*.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..4062077 --- /dev/null +++ b/componenti.txt @@ -0,0 +1,3 @@ +Malcuit Martina 5260784 +Fazzeri Matteo 5156526 +Scarsi Marta 5236574 \ No newline at end of file diff --git a/lib/junit-platform-console-standalone-1.10.1.jar b/lib/junit-platform-console-standalone-1.10.1.jar new file mode 100644 index 0000000..75ee9b7 Binary files /dev/null and b/lib/junit-platform-console-standalone-1.10.1.jar differ diff --git a/src/Account.java b/src/Account.java deleted file mode 100644 index c308eca..0000000 --- a/src/Account.java +++ /dev/null @@ -1,76 +0,0 @@ -// Account.java -// Represents a bank account - -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 - - // Account constructor initializes attributes - public Account( int theAccountNumber, int thePIN, - double theAvailableBalance, double theTotalBalance ) - { - accountNumber = theAccountNumber; - pin = thePIN; - availableBalance = theAvailableBalance; - totalBalance = theTotalBalance; - } // end Account constructor - - // determines whether a user-specified PIN matches PIN in Account - public boolean validatePIN( int userPIN ) - { - if ( userPIN == pin ) - return true; - else - return false; - } // end method validatePIN - - // returns available balance - public double getAvailableBalance() - { - return availableBalance; - } // end getAvailableBalance - - // returns the total balance - public double getTotalBalance() - { - return totalBalance; - } // end method getTotalBalance - - // credits an amount to the account - public void credit( double amount ) - { - totalBalance += amount; // add to total balance - } // end method credit - - // debits an amount from the account - public void debit( double amount ) - { - availableBalance -= amount; // subtract from available balance - totalBalance -= amount; // subtract from total balance - } // end method debit - - // returns account number - public int getAccountNumber() - { - return accountNumber; - } // end method getAccountNumber -} // end class Account - - -/************************************************************************** - * (C) Copyright 1992-2007 by Deitel & Associates, Inc. and * - * Pearson Education, Inc. All Rights Reserved. * - * * - * DISCLAIMER: The authors and publisher of this book have used their * - * best efforts in preparing the book. These efforts include the * - * development, research, and testing of the theories and programs * - * to determine their effectiveness. The authors and publisher make * - * no warranty of any kind, expressed or implied, with regard to these * - * programs or to the documentation contained in these books. The authors * - * and publisher shall not be liable in any event for incidental or * - * consequential damages in connection with, or arising out of, the * - * furnishing, performance, or use of these programs. * - *************************************************************************/ \ No newline at end of file diff --git a/src/BankDatabase.java b/src/BankDatabase.java deleted file mode 100644 index 3978497..0000000 --- a/src/BankDatabase.java +++ /dev/null @@ -1,84 +0,0 @@ -// BankDatabase.java -// Represents the bank account information database - -public class BankDatabase -{ - private Account accounts[]; // array of Accounts - - // no-argument BankDatabase constructor initializes accounts - 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 ); - } // end no-argument BankDatabase constructor - - // retrieve Account object containing specified account number - private Account getAccount( int accountNumber ) - { - // loop through accounts searching for matching account number - for ( Account currentAccount : accounts ) - { - // return current account if match found - if ( currentAccount.getAccountNumber() == accountNumber ) - return currentAccount; - } // end for - - return null; // if no matching account was found, return null - } // end method getAccount - - // determine whether user-specified account number and PIN match - // those of an account in the database - public boolean authenticateUser( int userAccountNumber, int userPIN ) - { - // attempt to retrieve the account with the account number - Account userAccount = getAccount( userAccountNumber ); - - // if account exists, return result of Account method validatePIN - if ( userAccount != null ) - return userAccount.validatePIN( userPIN ); - else - return false; // account number not found, so return false - } // end method authenticateUser - - // return available balance of Account with specified account number - public double getAvailableBalance( int userAccountNumber ) - { - return getAccount( userAccountNumber ).getAvailableBalance(); - } // end method getAvailableBalance - - // return total balance of Account with specified account number - public double 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 ) - { - getAccount( userAccountNumber ).credit( amount ); - } // end method credit - - // debit an amount from of Account with specified account number - public void debit( int userAccountNumber, double amount ) - { - getAccount( userAccountNumber ).debit( amount ); - } // end method debit -} // end class BankDatabase - - - -/************************************************************************** - * (C) Copyright 1992-2007 by Deitel & Associates, Inc. and * - * Pearson Education, Inc. All Rights Reserved. * - * * - * DISCLAIMER: The authors and publisher of this book have used their * - * best efforts in preparing the book. These efforts include the * - * development, research, and testing of the theories and programs * - * to determine their effectiveness. The authors and publisher make * - * no warranty of any kind, expressed or implied, with regard to these * - * programs or to the documentation contained in these books. The authors * - * and publisher shall not be liable in any event for incidental or * - * consequential damages in connection with, or arising out of, the * - * furnishing, performance, or use of these programs. * - *************************************************************************/ \ No newline at end of file diff --git a/src/CashDispenser.java b/src/CashDispenser.java deleted file mode 100644 index b249faf..0000000 --- a/src/CashDispenser.java +++ /dev/null @@ -1,50 +0,0 @@ -// CashDispenser.java -// Represents the cash dispenser of the ATM - -public class CashDispenser -{ - // the default initial number of bills in the cash dispenser - private final static int INITIAL_COUNT = 500; - private int count; // number of $20 bills remaining - - // no-argument CashDispenser constructor initializes count to default - public CashDispenser() - { - count = INITIAL_COUNT; // set count attribute to default - } // end CashDispenser constructor - - // simulates dispensing of specified amount of cash - public void dispenseCash( int amount ) - { - int billsRequired = amount / 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 ) - { - int billsRequired = amount / 20; // number of $20 bills required - - if ( count >= billsRequired ) - return true; // enough bills available - else - return false; // not enough bills available - } // end method isSufficientCashAvailable -} // end class CashDispenser - - - -/************************************************************************** - * (C) Copyright 1992-2007 by Deitel & Associates, Inc. and * - * Pearson Education, Inc. All Rights Reserved. * - * * - * DISCLAIMER: The authors and publisher of this book have used their * - * best efforts in preparing the book. These efforts include the * - * development, research, and testing of the theories and programs * - * to determine their effectiveness. The authors and publisher make * - * no warranty of any kind, expressed or implied, with regard to these * - * programs or to the documentation contained in these books. The authors * - * and publisher shall not be liable in any event for incidental or * - * consequential damages in connection with, or arising out of, the * - * furnishing, performance, or use of these programs. * - *************************************************************************/ \ No newline at end of file diff --git a/src/Withdrawal.java b/src/Withdrawal.java deleted file mode 100644 index 6e0af62..0000000 --- a/src/Withdrawal.java +++ /dev/null @@ -1,150 +0,0 @@ -// Withdrawal.java -// Represents a withdrawal ATM transaction - -public class Withdrawal extends Transaction -{ - private int amount; // amount to withdraw - private Keypad keypad; // reference to keypad - private CashDispenser cashDispenser; // reference to cash dispenser - - // constant corresponding to menu option to cancel - private final static int CANCELED = 6; - - // Withdrawal constructor - public Withdrawal( int userAccountNumber, Screen atmScreen, - BankDatabase atmBankDatabase, Keypad atmKeypad, - CashDispenser atmCashDispenser ) - { - // initialize superclass variables - super( userAccountNumber, atmScreen, atmBankDatabase ); - - // initialize references to keypad and cash dispenser - keypad = atmKeypad; - cashDispenser = atmCashDispenser; - } // end Withdrawal constructor - - // perform transaction - public void execute() - { - boolean cashDispensed = false; // cash was not dispensed yet - double availableBalance; // amount available for withdrawal - - // get references to bank database and screen - BankDatabase bankDatabase = getBankDatabase(); - Screen screen = getScreen(); - - // loop until cash is dispensed or the user cancels - do - { - // obtain a chosen withdrawal amount from the user - amount = displayMenuOfAmounts(); - - // check whether user chose a withdrawal amount or canceled - if ( amount != CANCELED ) - { - // get available balance of account involved - availableBalance = - bankDatabase.getAvailableBalance( getAccountNumber() ); - - // check whether the user has enough money in the account - if ( amount <= availableBalance ) - { - // check whether the cash dispenser has enough money - if ( cashDispenser.isSufficientCashAvailable( amount ) ) - { - // update the account involved to reflect withdrawal - bankDatabase.debit( getAccountNumber(), amount ); - - cashDispenser.dispenseCash( amount ); // dispense cash - cashDispensed = true; // cash was dispensed - - // instruct user to take cash - screen.displayMessageLine( - "\nPlease take your cash now." ); - } // end if - else // cash dispenser does not have enough cash - screen.displayMessageLine( - "\nInsufficient cash available in the ATM." + - "\n\nPlease choose a smaller amount." ); - } // end if - else // not enough money available in user's account - { - screen.displayMessageLine( - "\nInsufficient funds in your account." + - "\n\nPlease choose a smaller amount." ); - } // end else - } // end if - else // user chose cancel menu option - { - screen.displayMessageLine( "\nCanceling transaction..." ); - return; // return to main menu because user canceled - } // end else - } while ( !cashDispensed ); - - } // end method 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() - { - int 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 }; - - // loop while no valid choice has been made - 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( "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 ) - { - case 1: // if the user chose a withdrawal amount - case 2: // (i.e., chose option 1, 2, 3, 4 or 5), return the - 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 - userChoice = CANCELED; // save user's choice - break; - default: // the user did not enter a value from 1-6 - screen.displayMessageLine( - "\nIvalid selection. Try again." ); - } // end switch - } // end while - - return userChoice; // return withdrawal amount or CANCELED - } // end method displayMenuOfAmounts -} // end class Withdrawal - - - -/************************************************************************** - * (C) Copyright 1992-2007 by Deitel & Associates, Inc. and * - * Pearson Education, Inc. All Rights Reserved. * - * * - * DISCLAIMER: The authors and publisher of this book have used their * - * best efforts in preparing the book. These efforts include the * - * development, research, and testing of the theories and programs * - * to determine their effectiveness. The authors and publisher make * - * no warranty of any kind, expressed or implied, with regard to these * - * programs or to the documentation contained in these books. The authors * - * and publisher shall not be liable in any event for incidental or * - * consequential damages in connection with, or arising out of, the * - * furnishing, performance, or use of these programs. * - *************************************************************************/ \ No newline at end of file diff --git a/src/ATM.java b/src/code/Business_logic/ATM.java similarity index 97% rename from src/ATM.java rename to src/code/Business_logic/ATM.java index aa3d187..850e5ed 100644 --- a/src/ATM.java +++ b/src/code/Business_logic/ATM.java @@ -1,6 +1,14 @@ +package code.Business_logic; // ATM.java // Represents an automated teller machine +import code.Database.BankDatabase; +import code.GUI.BalanceInquiry; +import code.GUI.CashDispenser; +import code.GUI.DepositSlot; +import code.GUI.Keypad; +import code.GUI.Screen; + public class ATM { private boolean userAuthenticated; // whether user is authenticated diff --git a/src/ATMCaseStudy.java b/src/code/Business_logic/ATMCaseStudy.java similarity index 97% rename from src/ATMCaseStudy.java rename to src/code/Business_logic/ATMCaseStudy.java index b9033bd..7a4dc31 100644 --- a/src/ATMCaseStudy.java +++ b/src/code/Business_logic/ATMCaseStudy.java @@ -1,3 +1,4 @@ +package code.Business_logic; // ATMCaseStudy.java // Driver program for the ATM case study diff --git a/src/Deposit.java b/src/code/Business_logic/Deposit.java similarity index 88% rename from src/Deposit.java rename to src/code/Business_logic/Deposit.java index 916ef70..8e55eb9 100644 --- a/src/Deposit.java +++ b/src/code/Business_logic/Deposit.java @@ -1,12 +1,18 @@ +package code.Business_logic; // Deposit.java // Represents a deposit ATM transaction +import code.Database.BankDatabase; +import code.GUI.DepositSlot; +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 + private final static Euro CANCELED = new Euro(0); // constant for cancel option // Deposit constructor public Deposit( int userAccountNumber, Screen atmScreen, @@ -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.getValore() != CANCELED.getValore() ) { // 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 @@ -75,11 +81,11 @@ private double promptForDepositAmount() 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(( double ) input / 100); // return dollar amount } // end else } // end method promptForDepositAmount } // end class Deposit diff --git a/src/code/Business_logic/Euro.java b/src/code/Business_logic/Euro.java new file mode 100644 index 0000000..127364e --- /dev/null +++ b/src/code/Business_logic/Euro.java @@ -0,0 +1,48 @@ +package code.Business_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/Business_logic/Transaction.java similarity index 96% rename from src/Transaction.java rename to src/code/Business_logic/Transaction.java index 508fea8..5256dca 100644 --- a/src/Transaction.java +++ b/src/code/Business_logic/Transaction.java @@ -1,6 +1,10 @@ +package code.Business_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/code/Business_logic/Withdrawal.java b/src/code/Business_logic/Withdrawal.java new file mode 100644 index 0000000..1ed8dce --- /dev/null +++ b/src/code/Business_logic/Withdrawal.java @@ -0,0 +1,145 @@ +package code.Business_logic; +// Withdrawal.java + +// Represents a withdrawal ATM transaction + +import code.Database.BankDatabase; +import code.GUI.CashDispenser; +import code.GUI.Keypad; +import code.GUI.Screen; + +public class Withdrawal extends Transaction { + private Euro amount; // amount to withdraw + private Keypad keypad; // reference to keypad + private CashDispenser cashDispenser; // reference to cash dispenser + + // constant corresponding to menu option to cancel + private final static int CANCELED = 6; // constant for cancel option + + // Withdrawal constructor + public Withdrawal(int userAccountNumber, Screen atmScreen, + BankDatabase atmBankDatabase, Keypad atmKeypad, + CashDispenser atmCashDispenser) { + // initialize superclass variables + super(userAccountNumber, atmScreen, atmBankDatabase); + + // initialize references to keypad and cash dispenser + keypad = atmKeypad; + cashDispenser = atmCashDispenser; + } // end Withdrawal constructor + + // perform transaction + public void execute() { + boolean cashDispensed = false; // cash was not dispensed yet + Euro availableBalance; // amount available for withdrawal + + // get references to bank database and screen + BankDatabase bankDatabase = getBankDatabase(); + Screen screen = getScreen(); + + // loop until cash is dispensed or the user cancels + do { + // obtain a chosen withdrawal amount from the user + amount = displayMenuOfAmounts(); + + // check whether user chose a withdrawal amount or canceled + // System.out.println("amount.getValore() = " + amount.stampa()); + if (amount.getValore() / 100 != CANCELED) { + // get available balance of account involved + availableBalance = bankDatabase.getAvailableBalance(getAccountNumber()); + + // check whether the user has enough money in the account + if (amount.getValore() <= availableBalance.getValore()) { + // check whether the cash dispenser has enough money + if (cashDispenser.isSufficientCashAvailable(amount)) { + // update the account involved to reflect withdrawal + bankDatabase.debit(getAccountNumber(), amount); + + cashDispenser.dispenseCash(amount); // dispense cash + cashDispensed = true; // cash was dispensed + + // instruct user to take cash + screen.displayMessageLine( + "\nPlease take your cash now."); + } // end if + else // cash dispenser does not have enough cash + screen.displayMessageLine( + "\nInsufficient cash available in the ATM." + + "\n\nPlease choose a smaller amount."); + } // end if + else // not enough money available in user's account + { + screen.displayMessageLine( + "\nInsufficient funds in your account." + + "\n\nPlease choose a smaller amount."); + } // end else + } // end if + else // user chose cancel menu option + { + screen.displayMessageLine("\nCanceling transaction..."); + return; // return to main menu because user canceled + } // end else + } while (!cashDispensed); + + } // end method 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 Euro displayMenuOfAmounts() { + int 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 }; + + // loop while no valid choice has been made + 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("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) { + case 1: // if the user chose a withdrawal amount + case 2: // (i.e., chose option 1, 2, 3, 4 or 5), return the + 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 + userChoice = CANCELED; // save user's choice + break; + default: // the user did not enter a value from 1-6 + screen.displayMessageLine( + "\nIvalid selection. Try again."); + } // end switch + } // end while + + return new Euro((double) userChoice); // return withdrawal amount or CANCELED + } // end method displayMenuOfAmounts +} // end class Withdrawal + +/************************************************************************** + * (C) Copyright 1992-2007 by Deitel & Associates, Inc. and * + * Pearson Education, Inc. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/src/code/Database/Account.java b/src/code/Database/Account.java new file mode 100644 index 0000000..4deddbc --- /dev/null +++ b/src/code/Database/Account.java @@ -0,0 +1,71 @@ +package code.Database; +// Account.java + +// Represents a bank account + +import code.Business_logic.Euro; + +public class Account { + private int accountNumber; // account number + private int pin; // PIN for authentication + private Euro availableBalance; // funds available for withdrawal + private Euro totalBalance; // funds available + pending deposits + + // Account constructor initializes attributes + public Account(int theAccountNumber, int thePIN, + Euro theAvailableBalance, Euro theTotalBalance) { + accountNumber = theAccountNumber; + pin = thePIN; + availableBalance = theAvailableBalance; + totalBalance = theTotalBalance; + } // end Account constructor + + // determines whether a user-specified PIN matches PIN in Account + public boolean validatePIN(int userPIN) { + if (userPIN == pin) + return true; + else + return false; + } // end method validatePIN + + // returns available balance + public Euro getAvailableBalance() { + return availableBalance; + } // end getAvailableBalance + + // returns the total balance + public Euro getTotalBalance() { + return totalBalance; + } // end method getTotalBalance + + // credits an amount to the account + public void credit(Euro amount) { + totalBalance.somma(amount); // add to total balance + } // end method credit + + // debits an amount from the account + public void debit(Euro amount) { + availableBalance.sottrai(amount); // subtract from available balance + totalBalance.sottrai(amount); // subtract from total balance + } // end method debit + + // returns account number + public int getAccountNumber() { + return accountNumber; + } // end method getAccountNumber +} // end class Account + +/************************************************************************** + * (C) Copyright 1992-2007 by Deitel & Associates, Inc. and * + * Pearson Education, Inc. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/src/code/Database/BankDatabase.java b/src/code/Database/BankDatabase.java new file mode 100644 index 0000000..fc4f9e4 --- /dev/null +++ b/src/code/Database/BankDatabase.java @@ -0,0 +1,77 @@ +package code.Database; +// BankDatabase.java + +// Represents the bank account information database + +import code.Business_logic.Euro; + +public class BankDatabase { + private Account accounts[]; // array of Accounts + + // no-argument BankDatabase constructor initializes accounts + public BankDatabase() { + accounts = new Account[2]; // just 2 accounts for testing + 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 + private Account getAccount(int accountNumber) { + // loop through accounts searching for matching account number + for (Account currentAccount : accounts) { + // return current account if match found + if (currentAccount.getAccountNumber() == accountNumber) + return currentAccount; + } // end for + + return null; // if no matching account was found, return null + } // end method getAccount + + // determine whether user-specified account number and PIN match + // those of an account in the database + public boolean authenticateUser(int userAccountNumber, int userPIN) { + // attempt to retrieve the account with the account number + Account userAccount = getAccount(userAccountNumber); + + // if account exists, return result of Account method validatePIN + if (userAccount != null) + return userAccount.validatePIN(userPIN); + else + return false; // account number not found, so return false + } // end method authenticateUser + + // return available balance of Account with specified account number + public Euro getAvailableBalance(int userAccountNumber) { + return getAccount(userAccountNumber).getAvailableBalance(); + } // end method getAvailableBalance + + // return total balance of Account with specified account number + 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, Euro amount) { + getAccount(userAccountNumber).credit(amount); + } // end method credit + + // debit an amount from of Account with specified account number + public void debit(int userAccountNumber, Euro amount) { + getAccount(userAccountNumber).debit(amount); + } // end method debit +} // end class BankDatabase + +/************************************************************************** + * (C) Copyright 1992-2007 by Deitel & Associates, Inc. and * + * Pearson Education, Inc. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file 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..549ac6d 100644 --- a/src/BalanceInquiry.java +++ b/src/code/GUI/BalanceInquiry.java @@ -1,6 +1,11 @@ +package code.GUI; // BalanceInquiry.java // Represents a balance inquiry ATM transaction +import code.Business_logic.Euro; +import code.Business_logic.Transaction; +import code.Database.BankDatabase; + public class BalanceInquiry extends Transaction { // BalanceInquiry constructor @@ -18,11 +23,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/code/GUI/CashDispenser.java b/src/code/GUI/CashDispenser.java new file mode 100644 index 0000000..095138b --- /dev/null +++ b/src/code/GUI/CashDispenser.java @@ -0,0 +1,48 @@ +package code.GUI; +// CashDispenser.java + +// Represents the cash dispenser of the ATM + +import code.Business_logic.Euro; + +public class CashDispenser { + // the default initial number of bills in the cash dispenser + private final static int INITIAL_COUNT = 500; + private int count; // number of $20 bills remaining + + // no-argument CashDispenser constructor initializes count to default + public CashDispenser() { + count = INITIAL_COUNT; // set count attribute to default + } // end CashDispenser constructor + + // simulates dispensing of specified amount of cash + public void dispenseCash(Euro amount) { + int billsRequired = (int) amount.getValore() / 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(Euro amount) { + int billsRequired = (int) amount.getValore() / 20; // number of $20 bills required + + if (count >= billsRequired) + return true; // enough bills available + else + return false; // not enough bills available + } // end method isSufficientCashAvailable +} // end class CashDispenser + +/************************************************************************** + * (C) Copyright 1992-2007 by Deitel & Associates, Inc. and * + * Pearson Education, Inc. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/src/DepositSlot.java b/src/code/GUI/DepositSlot.java similarity index 98% rename from src/DepositSlot.java rename to src/code/GUI/DepositSlot.java index 64e02c2..bde0339 100644 --- a/src/DepositSlot.java +++ b/src/code/GUI/DepositSlot.java @@ -1,3 +1,4 @@ +package code.GUI; // DepositSlot.java // Represents the deposit slot of the ATM 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 88% rename from src/Screen.java rename to src/code/GUI/Screen.java index 44d3f30..7ca2139 100644 --- a/src/Screen.java +++ b/src/code/GUI/Screen.java @@ -1,6 +1,9 @@ +package code.GUI; // Screen.java // Represents the screen of the ATM +import code.Business_logic.Euro; + public class Screen { // displays a message without a carriage return @@ -16,9 +19,10 @@ 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.println(amount.stampa()); + //System.out.printf( "$%,.2f", amount.stampa() ); } // end method displayDollarAmount } // end class Screen diff --git a/src/test/AccountTest.java b/src/test/AccountTest.java new file mode 100644 index 0000000..fdaa1c0 --- /dev/null +++ b/src/test/AccountTest.java @@ -0,0 +1,76 @@ +package test; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import code.Business_logic.Euro; +import code.Database.Account; + +public class AccountTest { + private Account p1; + private Account p2; + private Account p3; + + @BeforeEach + public void setUp() { + p1 = new Account(12345, 54321, new Euro(1000.0), new Euro(1200.0)); + p2 = new Account(98765, 56789, new Euro(200.0), new Euro(200.0)); + p3 = new Account(11111, 00000, new Euro(10000.0), new Euro(10000.0)); + } + + @Test + void testCredit() { + //Test case 1: p1 + 200 = 1400 + Euro expectedcreditp1 = new Euro(1400); + p1.credit(new Euro(200)); + assertEquals(expectedcreditp1.getValore(), p1.getTotalBalance().getValore()); + //Test case 2: p3 + 0 = 10000 + Euro expectedcreditp3 = new Euro(10000); + p3.credit(new Euro(0)); + assertEquals(expectedcreditp3.getValore(), p3.getTotalBalance().getValore()); + } + + @Test + void testDebit() { + //Test case 1: p2 - 200 = 0 + Euro expectedcreditp2 = new Euro(0); + p2.debit(new Euro(200)); + assertEquals(expectedcreditp2.getValore(), p2.getAvailableBalance().getValore()); + assertEquals(expectedcreditp2.getValore(), p2.getTotalBalance().getValore()); + //Test case 2: p3 - 1000 = 9000 + Euro expectedcreditp3 = new Euro(9000); + p3.debit(new Euro(1000)); + assertEquals(expectedcreditp3.getValore(), p3.getAvailableBalance().getValore()); + assertEquals(expectedcreditp3.getValore(), p3.getTotalBalance().getValore()); + } + + @Test + void testGetAccountNumber() { + assertEquals(12345, p1.getAccountNumber()); + assertEquals(98765, p2.getAccountNumber()); + assertEquals(11111, p3.getAccountNumber()); + } + + @Test + void testGetAvailableBalance() { + assertEquals(new Euro(1000.0).getValore(), p1.getAvailableBalance().getValore()); + assertEquals(new Euro(200.0).getValore(), p2.getAvailableBalance().getValore()); + assertEquals(new Euro(10000.0).getValore(), p3.getAvailableBalance().getValore()); + } + + @Test + void testGetTotalBalance() { + assertEquals(new Euro(1200.0).getValore(), p1.getTotalBalance().getValore()); + assertEquals(new Euro(200.0).getValore(), p2.getTotalBalance().getValore()); + assertEquals(new Euro(10000.0).getValore(), p3.getTotalBalance().getValore()); + } + + @Test + void testValidatePIN() { + assertTrue(p1.validatePIN(54321)); + assertTrue(p2.validatePIN(56789)); + assertTrue(p3.validatePIN(00000)); + } +} diff --git a/src/test/BankDatabaseTest.java b/src/test/BankDatabaseTest.java new file mode 100644 index 0000000..34885f9 --- /dev/null +++ b/src/test/BankDatabaseTest.java @@ -0,0 +1,70 @@ +package test; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.*; + +import code.Business_logic.Euro; +import code.Database.BankDatabase; + +public class BankDatabaseTest { + private static BankDatabase db1; + + @BeforeAll + public static void setUp() { + db1 = new BankDatabase(); + } + + @AfterEach + public void tearDown() { + db1 = new BankDatabase(); + } + + @Test + void testAuthenticateUser() { + assertTrue(db1.authenticateUser(12345, 54321)); + assertFalse(db1.authenticateUser(12345, 54322)); + assertTrue(db1.authenticateUser(98765, 56789)); + assertFalse(db1.authenticateUser(98765, 56788)); + } + + @Test + void testCredit() { + //Test case 1: 1200 + 200 = 1400 + Euro expectedcredit = new Euro(1400); + db1.credit(12345, new Euro(200)); + assertEquals(expectedcredit.getValore(), db1.getTotalBalance(12345).getValore()); + //Test case 2: 200 + 0 = 10000 + expectedcredit = new Euro(200); + db1.credit(98765, new Euro(0)); + assertEquals(expectedcredit.getValore(), db1.getTotalBalance(98765).getValore()); + } + + @Test + void testDebit() { + //Test case 1: 200 - 200 = 0 + Euro expectedcredit = new Euro(0); + db1.debit(98765, new Euro(200)); + assertEquals(expectedcredit.getValore(), db1.getAvailableBalance(98765).getValore()); + assertEquals(expectedcredit.getValore(), db1.getTotalBalance(98765).getValore()); + //Test case 2: p3 - 1000 = 9000 + expectedcredit = new Euro(0); + Euro expectedcredittotal = new Euro(200); + db1.debit(12345, new Euro(1000)); + assertEquals(expectedcredit.getValore(), db1.getAvailableBalance(12345).getValore()); + assertEquals(expectedcredittotal.getValore(), db1.getTotalBalance(12345).getValore()); + } + + @Test + void testGetAvailableBalance() { + assertEquals(new Euro(1000.0).getValore(), db1.getAvailableBalance(12345).getValore()); + assertEquals(new Euro(200.0).getValore(), db1.getAvailableBalance(98765).getValore()); + } + + @Test + void testGetTotalBalance() { + assertEquals(new Euro(1200.0).getValore(), db1.getTotalBalance(12345).getValore()); + assertEquals(new Euro(200.0).getValore(), db1.getTotalBalance(98765).getValore()); + + } +} diff --git a/src/test/TestEuro.java b/src/test/TestEuro.java new file mode 100644 index 0000000..b265cb1 --- /dev/null +++ b/src/test/TestEuro.java @@ -0,0 +1,100 @@ +package test; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.*; + +import code.Business_logic.Euro; + + +public class TestEuro { + + private Euro euro1; + private Euro euro2; + private Euro euro3; + private Euro euro4; + + @BeforeEach + public void setUp() { + euro1 = new Euro(10, 50); + euro2 = new Euro(15, 25); + euro3 = new Euro(20, 00); + euro4 = new Euro(30); + + } + + @Test + public void testGetValore() { + long expectedValore = 1050; + long actualValore = euro1.getValore(); + + long expectedValore1 = 2000; + long actualValore1 = euro3.getValore(); + + assertEquals(expectedValore, actualValore); + assertEquals(expectedValore1, actualValore1); + + } + + @Test + public void testMinoreDi() { + + assertTrue(euro1.minoreDi(euro2)); + assertFalse(euro2.minoreDi(euro1)); + assertTrue(euro3.minoreDi(euro4)); + assertFalse(euro4.minoreDi(euro3)); + assertTrue(euro2.minoreDi(euro3)); + } + + @Test + public void testSomma() { + // Test case 1: euro1 + euro2 + Euro expectedSum1 = new Euro(25, 75); + Euro actualSum1 = euro1.somma(euro2); + assertEquals(expectedSum1.getValore(), actualSum1.getValore()); + //Test case 2: euro2 + euro3 + Euro expectedSum2 = new Euro(35, 25); + Euro actualSum2 = euro2.somma(euro3); + assertEquals(expectedSum2.getValore(), actualSum2.getValore()); + //Test case 3: euro3 + euro4 + Euro expectedSum3 = new Euro(50, 00); + Euro actualSum3 = euro3.somma(euro4); + assertEquals(expectedSum3.getValore(), actualSum3.getValore()); + } + + @Test + public void testSottrai() { + //Test case 1: euro2 - euro1 (euro2 > euro1) + Euro expectedDiff1 = new Euro(4, 75); + Euro actualDiff1 = euro2.sottrai(euro1); + assertEquals(expectedDiff1.getValore(), actualDiff1.getValore()); + + + } + + @Test + public void testStampa() { + + String expectedOutput = "10.5 euro"; + String expectedOutput1 = "15.25 euro"; + String expectedOutput2 = "20.0 euro"; + String expectedOutput3 = "30.0 euro"; + + String actualOutput = euro1.stampa(); + String actualOutput1 = euro2.stampa(); + String actualOutput2 = euro3.stampa(); + String actualOutput3 = euro4.stampa(); + + assertEquals(expectedOutput, actualOutput); + assertEquals(expectedOutput1, actualOutput1); + assertEquals(expectedOutput2, actualOutput2); + assertEquals(expectedOutput3, actualOutput3); + } + + @Test + public void testUgualeA() { + + assertFalse(euro1.ugualeA(euro2)); + assertFalse(euro1.ugualeA(new Euro(10, 25))); + } +}