diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..919ce1f --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/libraries/junit_platform_console_standalone_1_10_1.xml b/.idea/libraries/junit_platform_console_standalone_1_10_1.xml new file mode 100644 index 0000000..108f342 --- /dev/null +++ b/.idea/libraries/junit_platform_console_standalone_1_10_1.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..639900d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..e47c47a --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ATM.iml b/ATM.iml new file mode 100644 index 0000000..13f0e95 --- /dev/null +++ b/ATM.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file 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/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/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/bin/code/Business_logic/Transaction.class b/bin/code/Business_logic/Transaction.class new file mode 100644 index 0000000..ac22b8e Binary files /dev/null and b/bin/code/Business_logic/Transaction.class differ diff --git a/bin/code/Database/BankDatabase.class b/bin/code/Database/BankDatabase.class new file mode 100644 index 0000000..b726534 Binary files /dev/null and b/bin/code/Database/BankDatabase.class differ diff --git a/bin/code/GUI/ATM.class b/bin/code/GUI/ATM.class new file mode 100644 index 0000000..8b1509f Binary files /dev/null and b/bin/code/GUI/ATM.class differ diff --git a/bin/code/GUI/ATMCaseStudy.class b/bin/code/GUI/ATMCaseStudy.class new file mode 100644 index 0000000..aac1d31 Binary files /dev/null and b/bin/code/GUI/ATMCaseStudy.class differ diff --git a/bin/code/GUI/Account.class b/bin/code/GUI/Account.class new file mode 100644 index 0000000..ad68278 Binary files /dev/null and b/bin/code/GUI/Account.class differ diff --git a/bin/code/GUI/BalanceInquiry.class b/bin/code/GUI/BalanceInquiry.class new file mode 100644 index 0000000..bf96a38 Binary files /dev/null and b/bin/code/GUI/BalanceInquiry.class differ diff --git a/bin/CashDispenser.class b/bin/code/GUI/CashDispenser.class similarity index 54% rename from bin/CashDispenser.class rename to bin/code/GUI/CashDispenser.class index b8cedba..94fd591 100644 Binary files a/bin/CashDispenser.class and b/bin/code/GUI/CashDispenser.class differ diff --git a/bin/code/GUI/Deposit.class b/bin/code/GUI/Deposit.class new file mode 100644 index 0000000..fff84ac Binary files /dev/null and b/bin/code/GUI/Deposit.class differ diff --git a/bin/code/GUI/DepositSlot.class b/bin/code/GUI/DepositSlot.class new file mode 100644 index 0000000..b66e9cd Binary files /dev/null and b/bin/code/GUI/DepositSlot.class differ diff --git a/bin/code/GUI/Euro.class b/bin/code/GUI/Euro.class new file mode 100644 index 0000000..b67578a Binary files /dev/null and b/bin/code/GUI/Euro.class differ diff --git a/bin/Keypad.class b/bin/code/GUI/Keypad.class similarity index 68% rename from bin/Keypad.class rename to bin/code/GUI/Keypad.class index 9b066e2..75bf83a 100644 Binary files a/bin/Keypad.class and b/bin/code/GUI/Keypad.class differ diff --git a/bin/code/GUI/Screen.class b/bin/code/GUI/Screen.class new file mode 100644 index 0000000..1482793 Binary files /dev/null and b/bin/code/GUI/Screen.class differ diff --git a/bin/code/GUI/Withdrawal.class b/bin/code/GUI/Withdrawal.class new file mode 100644 index 0000000..a46e0f6 Binary files /dev/null and b/bin/code/GUI/Withdrawal.class differ diff --git a/bin/test/AccountTest.class b/bin/test/AccountTest.class new file mode 100644 index 0000000..65f508f Binary files /dev/null and b/bin/test/AccountTest.class differ diff --git a/bin/test/BankDatabaseTest.class b/bin/test/BankDatabaseTest.class new file mode 100644 index 0000000..01708f2 Binary files /dev/null and b/bin/test/BankDatabaseTest.class differ diff --git a/bin/test/TestEuro.class b/bin/test/TestEuro.class new file mode 100644 index 0000000..3d3b226 Binary files /dev/null and b/bin/test/TestEuro.class differ diff --git a/componenti.txt b/componenti.txt new file mode 100644 index 0000000..eb87eca --- /dev/null +++ b/componenti.txt @@ -0,0 +1,2 @@ +Eleas, Bouras 5141292 +Iosif, Puha 5286128 \ 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/ATM.java b/src/ATM.java deleted file mode 100644 index aa3d187..0000000 --- a/src/ATM.java +++ /dev/null @@ -1,167 +0,0 @@ -// ATM.java -// Represents an automated teller machine - -public class ATM -{ - private boolean userAuthenticated; // whether user is authenticated - private int currentAccountNumber; // current user's account number - private Screen screen; // ATM's screen - private Keypad keypad; // ATM's keypad - private CashDispenser cashDispenser; // ATM's cash dispenser - private DepositSlot depositSlot; // ATM's deposit slot - private BankDatabase bankDatabase; // account information database - - // constants corresponding to main menu options - private static final int BALANCE_INQUIRY = 1; - private static final int WITHDRAWAL = 2; - private static final int DEPOSIT = 3; - private static final int EXIT = 4; - - // no-argument ATM constructor initializes instance variables - public ATM() - { - userAuthenticated = false; // user is not authenticated to start - currentAccountNumber = 0; // no current account number to start - screen = new Screen(); // create screen - keypad = new Keypad(); // create keypad - cashDispenser = new CashDispenser(); // create cash dispenser - depositSlot = new DepositSlot(); // create deposit slot - bankDatabase = new BankDatabase(); // create acct info database - } // end no-argument ATM constructor - - // start ATM - public void run() - { - // welcome and authenticate user; perform transactions - while ( true ) - { - // loop while user is not yet authenticated - while ( !userAuthenticated ) - { - screen.displayMessageLine( "\nWelcome!" ); - authenticateUser(); // authenticate user - } // end while - - performTransactions(); // user is now authenticated - userAuthenticated = false; // reset before next ATM session - currentAccountNumber = 0; // reset before next ATM session - screen.displayMessageLine( "\nThank you! Goodbye!" ); - } // end while - } // end method run - - // attempts to authenticate user against database - private void authenticateUser() - { - screen.displayMessage( "\nPlease enter your account number: " ); - int accountNumber = keypad.getInput(); // input account number - screen.displayMessage( "\nEnter your PIN: " ); // prompt for PIN - int pin = keypad.getInput(); // input PIN - - // set userAuthenticated to boolean value returned by database - userAuthenticated = - bankDatabase.authenticateUser( accountNumber, pin ); - - // check whether authentication succeeded - if ( userAuthenticated ) - { - currentAccountNumber = accountNumber; // save user's account # - } // end if - else - screen.displayMessageLine( - "Invalid account number or PIN. Please try again." ); - } // end method authenticateUser - - // display the main menu and perform transactions - private void performTransactions() - { - // local variable to store transaction currently being processed - Transaction currentTransaction = null; - - boolean userExited = false; // user has not chosen to exit - - // loop while user has not chosen option to exit system - while ( !userExited ) - { - // show main menu and get user selection - int mainMenuSelection = displayMainMenu(); - - // decide how to proceed based on user's menu selection - switch ( mainMenuSelection ) - { - // user chose to perform one of three transaction types - case BALANCE_INQUIRY: - case WITHDRAWAL: - case DEPOSIT: - - // initialize as new object of chosen type - currentTransaction = - createTransaction( mainMenuSelection ); - - currentTransaction.execute(); // execute transaction - break; - case EXIT: // user chose to terminate session - screen.displayMessageLine( "\nExiting the system..." ); - userExited = true; // this ATM session should end - break; - default: // user did not enter an integer from 1-4 - screen.displayMessageLine( - "\nYou did not enter a valid selection. Try again." ); - break; - } // end switch - } // end while - } // end method performTransactions - - // display the main menu and return an input selection - private int displayMainMenu() - { - screen.displayMessageLine( "\nMain menu:" ); - screen.displayMessageLine( "1 - View my balance" ); - screen.displayMessageLine( "2 - Withdraw cash" ); - screen.displayMessageLine( "3 - Deposit funds" ); - screen.displayMessageLine( "4 - Exit\n" ); - screen.displayMessage( "Enter a choice: " ); - return keypad.getInput(); // return user's selection - } // end method displayMainMenu - - // return object of specified Transaction subclass - private Transaction createTransaction( int type ) - { - Transaction temp = null; // temporary Transaction variable - - // determine which type of Transaction to create - switch ( type ) - { - case BALANCE_INQUIRY: // create new BalanceInquiry transaction - temp = new BalanceInquiry( - currentAccountNumber, screen, bankDatabase ); - break; - case WITHDRAWAL: // create new Withdrawal transaction - temp = new Withdrawal( currentAccountNumber, screen, - bankDatabase, keypad, cashDispenser ); - break; - case DEPOSIT: // create new Deposit transaction - temp = new Deposit( currentAccountNumber, screen, - bankDatabase, keypad, depositSlot ); - break; - } // end switch - - return temp; // return the newly created object - } // end method createTransaction -} // end class ATM - - - -/************************************************************************** - * (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/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/BalanceInquiry.java b/src/BalanceInquiry.java deleted file mode 100644 index d45fa6a..0000000 --- a/src/BalanceInquiry.java +++ /dev/null @@ -1,53 +0,0 @@ -// BalanceInquiry.java -// Represents a balance inquiry ATM transaction - -public class BalanceInquiry extends Transaction -{ - // BalanceInquiry constructor - public BalanceInquiry( int userAccountNumber, Screen atmScreen, - BankDatabase atmBankDatabase ) - { - super( userAccountNumber, atmScreen, atmBankDatabase ); - } // end BalanceInquiry constructor - - // performs the transaction - public void execute() - { - // get references to bank database and screen - BankDatabase bankDatabase = getBankDatabase(); - Screen screen = getScreen(); - - // get the available balance for the account involved - double availableBalance = - bankDatabase.getAvailableBalance( getAccountNumber() ); - - // get the total balance for the account involved - double totalBalance = - bankDatabase.getTotalBalance( getAccountNumber() ); - - // display the balance information on the screen - screen.displayMessageLine( "\nBalance Information:" ); - screen.displayMessage( " - Available balance: " ); - screen.displayDollarAmount( availableBalance ); - screen.displayMessage( "\n - Total balance: " ); - screen.displayDollarAmount( totalBalance ); - screen.displayMessageLine( "" ); - } // end method execute -} // end class BalanceInquiry - - - -/************************************************************************** - * (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/Transaction.java b/src/code/Business_logic/Transaction.java similarity index 78% rename from src/Transaction.java rename to src/code/Business_logic/Transaction.java index 508fea8..7b758b3 100644 --- a/src/Transaction.java +++ b/src/code/Business_logic/Transaction.java @@ -1,20 +1,23 @@ -// Transaction.java -// Abstract superclass Transaction represents an ATM transaction +package code.Business_logic;// code.Business_logic.Transaction.java +// Abstract superclass code.Business_logic.Transaction represents an code.GUI.ATM transaction + +import code.Database.BankDatabase; +import code.GUI.Screen; public abstract class Transaction { private int accountNumber; // indicates account involved - private Screen screen; // ATM's screen + private Screen screen; // code.GUI.ATM's screen private BankDatabase bankDatabase; // account info database - // Transaction constructor invoked by subclasses using super() - public Transaction( int userAccountNumber, Screen atmScreen, + // code.Business_logic.Transaction constructor invoked by subclasses using super() + public Transaction( int userAccountNumber, Screen atmScreen, BankDatabase atmBankDatabase ) { accountNumber = userAccountNumber; screen = atmScreen; bankDatabase = atmBankDatabase; - } // end Transaction constructor + } // end code.Business_logic.Transaction constructor // return account number public int getAccountNumber() @@ -36,7 +39,7 @@ public BankDatabase getBankDatabase() // perform the transaction (overridden by each subclass) abstract public void execute(); -} // end class Transaction +} // end class code.Business_logic.Transaction diff --git a/src/code/Database/BankDatabase.java b/src/code/Database/BankDatabase.java new file mode 100644 index 0000000..08cf926 --- /dev/null +++ b/src/code/Database/BankDatabase.java @@ -0,0 +1,77 @@ +package code.Database;// code.Database.BankDatabase.java + +// Represents the bank account information database + +import code.GUI.Account; +import code.GUI.Euro; + +public class BankDatabase { + private Account accounts[]; // array of Accounts + + // no-argument code.Database.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 code.Database.BankDatabase constructor + + // retrieve code.GUI.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 code.GUI.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 code.GUI.Account with specified account number + public Euro getAvailableBalance(int userAccountNumber) { + return getAccount(userAccountNumber).getAvailableBalance(); + } // end method getAvailableBalance + + // return total balance of code.GUI.Account with specified account number + public Euro getTotalBalance(int userAccountNumber) { + return getAccount(userAccountNumber).getTotalBalance(); + } // end method getTotalBalance + + // credit an amount to code.GUI.Account with specified account number + public void credit(int userAccountNumber, Euro amount) { + getAccount(userAccountNumber).credit(amount); + } // end method credit + + // debit an amount from of code.GUI.Account with specified account number + public void debit(int userAccountNumber, Euro amount) { + getAccount(userAccountNumber).debit(amount); + } // end method debit +} // end class code.Database.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/code/GUI/ATM.java b/src/code/GUI/ATM.java new file mode 100644 index 0000000..6ff6ffb --- /dev/null +++ b/src/code/GUI/ATM.java @@ -0,0 +1,154 @@ +package code.GUI;// code.GUI.ATM.java + +// Represents an automated teller machine + +import code.Business_logic.Transaction; +import code.Database.BankDatabase; + +public class ATM { + private boolean userAuthenticated; // whether user is authenticated + private int currentAccountNumber; // current user's account number + private Screen screen; // code.GUI.ATM's screen + private Keypad keypad; // code.GUI.ATM's keypad + private CashDispenser cashDispenser; // code.GUI.ATM's cash dispenser + private DepositSlot depositSlot; // code.GUI.ATM's deposit slot + private BankDatabase bankDatabase; // account information database + + // constants corresponding to main menu options + private static final int BALANCE_INQUIRY = 1; + private static final int WITHDRAWAL = 2; + private static final int DEPOSIT = 3; + private static final int EXIT = 4; + + // no-argument code.GUI.ATM constructor initializes instance variables + public ATM() { + userAuthenticated = false; // user is not authenticated to start + currentAccountNumber = 0; // no current account number to start + screen = new Screen(); // create screen + keypad = new Keypad(); // create keypad + cashDispenser = new CashDispenser(); // create cash dispenser + depositSlot = new DepositSlot(); // create deposit slot + bankDatabase = new BankDatabase(); // create acct info database + } // end no-argument code.GUI.ATM constructor + + // start code.GUI.ATM + public void run() { + // welcome and authenticate user; perform transactions + while (true) { + // loop while user is not yet authenticated + while (!userAuthenticated) { + screen.displayMessageLine("\nWelcome!"); + authenticateUser(); // authenticate user + } // end while + + performTransactions(); // user is now authenticated + userAuthenticated = false; // reset before next code.GUI.ATM session + currentAccountNumber = 0; // reset before next code.GUI.ATM session + screen.displayMessageLine("\nThank you! Goodbye!"); + } // end while + } // end method run + + // attempts to authenticate user against database + private void authenticateUser() { + screen.displayMessage("\nPlease enter your account number: "); + int accountNumber = keypad.getInput(); // input account number + screen.displayMessage("\nEnter your PIN: "); // prompt for PIN + int pin = keypad.getInput(); // input PIN + + // set userAuthenticated to boolean value returned by database + userAuthenticated = bankDatabase.authenticateUser(accountNumber, pin); + + // check whether authentication succeeded + if (userAuthenticated) { + currentAccountNumber = accountNumber; // save user's account # + } // end if + else + screen.displayMessageLine( + "Invalid account number or PIN. Please try again."); + } // end method authenticateUser + + // display the main menu and perform transactions + private void performTransactions() { + // local variable to store transaction currently being processed + Transaction currentTransaction = null; + + boolean userExited = false; // user has not chosen to exit + + // loop while user has not chosen option to exit system + while (!userExited) { + // show main menu and get user selection + int mainMenuSelection = displayMainMenu(); + + // decide how to proceed based on user's menu selection + switch (mainMenuSelection) { + // user chose to perform one of three transaction types + case BALANCE_INQUIRY: + case WITHDRAWAL: + case DEPOSIT: + + // initialize as new object of chosen type + currentTransaction = createTransaction(mainMenuSelection); + + currentTransaction.execute(); // execute transaction + break; + case EXIT: // user chose to terminate session + screen.displayMessageLine("\nExiting the system..."); + userExited = true; // this code.GUI.ATM session should end + break; + default: // user did not enter an integer from 1-4 + screen.displayMessageLine( + "\nYou did not enter a valid selection. Try again."); + break; + } // end switch + } // end while + } // end method performTransactions + + // display the main menu and return an input selection + private int displayMainMenu() { + screen.displayMessageLine("\nMain menu:"); + screen.displayMessageLine("1 - View my balance"); + screen.displayMessageLine("2 - Withdraw cash"); + screen.displayMessageLine("3 - Deposit funds"); + screen.displayMessageLine("4 - Exit\n"); + screen.displayMessage("Enter a choice: "); + return keypad.getInput(); // return user's selection + } // end method displayMainMenu + + // return object of specified code.Business_logic.Transaction subclass + private Transaction createTransaction(int type) { + Transaction temp = null; // temporary code.Business_logic.Transaction variable + + // determine which type of code.Business_logic.Transaction to create + switch (type) { + case BALANCE_INQUIRY: // create new code.GUI.BalanceInquiry transaction + temp = new BalanceInquiry( + currentAccountNumber, screen, bankDatabase); + break; + case WITHDRAWAL: // create new code.GUI.Withdrawal transaction + temp = new Withdrawal(currentAccountNumber, screen, + bankDatabase, keypad, cashDispenser); + break; + case DEPOSIT: // create new code.GUI.Deposit transaction + temp = new Deposit(currentAccountNumber, screen, + bankDatabase, keypad, depositSlot); + break; + } // end switch + + return temp; // return the newly created object + } // end method createTransaction +} // end class code.GUI.ATM + +/************************************************************************** + * (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/ATMCaseStudy.java b/src/code/GUI/ATMCaseStudy.java similarity index 83% rename from src/ATMCaseStudy.java rename to src/code/GUI/ATMCaseStudy.java index b9033bd..a23f2f3 100644 --- a/src/ATMCaseStudy.java +++ b/src/code/GUI/ATMCaseStudy.java @@ -1,15 +1,17 @@ -// ATMCaseStudy.java -// Driver program for the ATM case study +package code.GUI;// code.GUI.ATMCaseStudy.java +// Driver program for the code.GUI.ATM case study + +import code.GUI.ATM; public class ATMCaseStudy { - // main method creates and runs the ATM + // main method creates and runs the code.GUI.ATM public static void main( String[] args ) { - ATM theATM = new ATM(); + ATM theATM = new ATM(); theATM.run(); } // end main -} // end class ATMCaseStudy +} // end class code.GUI.ATMCaseStudy diff --git a/src/code/GUI/Account.java b/src/code/GUI/Account.java new file mode 100644 index 0000000..9e4816f --- /dev/null +++ b/src/code/GUI/Account.java @@ -0,0 +1,71 @@ +package code.GUI;// code.GUI.Account.java + +// Represents a bank account + +public class Account { // tutte le volte in cui double è usato come valuta, va cambiato con il datatype + // appena creato Euro + 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 + + // code.GUI.Account constructor initializes attributes + public Account(int theAccountNumber, int thePIN, + Euro theAvailableBalance, Euro theTotalBalance) { + accountNumber = theAccountNumber; + pin = thePIN; + availableBalance = theAvailableBalance; + totalBalance = theTotalBalance; + } // end code.GUI.Account constructor + + // determines whether a user-specified PIN matches PIN in code.GUI.Account + public boolean validatePIN(int userPIN) { + if (userPIN == pin) + return true; + else + return false; + } // end method validatePIN + + // returns available balance in Euro + public Euro getAvailableBalance() { + return availableBalance; + } // end getAvailableBalance + + // returns the total balance in Euro + public Euro getTotalBalance() { + return totalBalance; + } // end method getTotalBalance + + // credits an amount to the account + public void credit(Euro amount) { + // add to total balance + totalBalance.somma(amount); + } // 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 code.GUI.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/GUI/BalanceInquiry.java b/src/code/GUI/BalanceInquiry.java new file mode 100644 index 0000000..5c7ce27 --- /dev/null +++ b/src/code/GUI/BalanceInquiry.java @@ -0,0 +1,51 @@ +package code.GUI;// code.GUI.BalanceInquiry.java + +// Represents a balance inquiry code.GUI.ATM transaction + +import code.Business_logic.Transaction; +import code.Database.BankDatabase; +import code.GUI.Screen; + +public class BalanceInquiry extends Transaction { + // code.GUI.BalanceInquiry constructor + public BalanceInquiry(int userAccountNumber, Screen atmScreen, + BankDatabase atmBankDatabase) { + super(userAccountNumber, atmScreen, atmBankDatabase); + } // end code.GUI.BalanceInquiry constructor + + // performs the transaction + public void execute() { + // get references to bank database and screen + BankDatabase bankDatabase = getBankDatabase(); + Screen screen = getScreen(); + + // get the available balance for the account involved + Euro availableBalance = bankDatabase.getAvailableBalance(getAccountNumber()); + + // get the total balance for the account involved + Euro totalBalance = bankDatabase.getTotalBalance(getAccountNumber()); + + // display the balance information on the screen + screen.displayMessageLine("\nBalance Information:"); + screen.displayMessage(" - Available balance: "); + screen.displayEuroAmount(availableBalance); + screen.displayMessage("\n - Total balance: "); + screen.displayEuroAmount(totalBalance); + screen.displayMessageLine(""); + } // end method execute +} // end class code.GUI.BalanceInquiry + +/************************************************************************** + * (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/code/GUI/CashDispenser.java similarity index 88% rename from src/CashDispenser.java rename to src/code/GUI/CashDispenser.java index b249faf..7638997 100644 --- a/src/CashDispenser.java +++ b/src/code/GUI/CashDispenser.java @@ -1,5 +1,5 @@ -// CashDispenser.java -// Represents the cash dispenser of the ATM +package code.GUI;// code.GUI.CashDispenser.java +// Represents the cash dispenser of the code.GUI.ATM public class CashDispenser { @@ -7,11 +7,11 @@ public class CashDispenser private final static int INITIAL_COUNT = 500; private int count; // number of $20 bills remaining - // no-argument CashDispenser constructor initializes count to default + // no-argument code.GUI.CashDispenser constructor initializes count to default public CashDispenser() { count = INITIAL_COUNT; // set count attribute to default - } // end CashDispenser constructor + } // end code.GUI.CashDispenser constructor // simulates dispensing of specified amount of cash public void dispenseCash( int amount ) @@ -30,7 +30,7 @@ public boolean isSufficientCashAvailable( int amount ) else return false; // not enough bills available } // end method isSufficientCashAvailable -} // end class CashDispenser +} // end class code.GUI.CashDispenser diff --git a/src/Deposit.java b/src/code/GUI/Deposit.java similarity index 50% rename from src/Deposit.java rename to src/code/GUI/Deposit.java index 916ef70..1c03afc 100644 --- a/src/Deposit.java +++ b/src/code/GUI/Deposit.java @@ -1,102 +1,98 @@ -// Deposit.java -// Represents a deposit ATM transaction +package code.GUI;// code.GUI.Deposit.java -public class Deposit extends Transaction -{ - private double amount; // amount to deposit +// Represents a deposit code.GUI.ATM transaction + +import code.Database.BankDatabase; +import code.Business_logic.Transaction; + +public class Deposit extends Transaction { + private Euro amount; // amount to deposit in Euro private Keypad keypad; // reference to keypad private DepositSlot depositSlot; // reference to deposit slot private final static int CANCELED = 0; // constant for cancel option - // Deposit constructor - public Deposit( int userAccountNumber, Screen atmScreen, - BankDatabase atmBankDatabase, Keypad atmKeypad, - DepositSlot atmDepositSlot ) - { + // code.GUI.Deposit constructor + public Deposit(int userAccountNumber, Screen atmScreen, + BankDatabase atmBankDatabase, Keypad atmKeypad, + DepositSlot atmDepositSlot) { // initialize superclass variables - super( userAccountNumber, atmScreen, atmBankDatabase ); + super(userAccountNumber, atmScreen, atmBankDatabase); // initialize references to keypad and deposit slot keypad = atmKeypad; depositSlot = atmDepositSlot; - } // end Deposit constructor + } // end code.GUI.Deposit constructor // perform transaction - public void execute() - { + public void execute() { BankDatabase bankDatabase = getBankDatabase(); // get reference Screen screen = getScreen(); // get reference amount = promptForDepositAmount(); // get deposit amount from user // check whether user entered a deposit amount or canceled - if ( amount != CANCELED ) - { + if (amount.getValore() != CANCELED) { // request deposit envelope containing specified amount - screen.displayMessage( - "\nPlease insert a deposit envelope containing " ); - screen.displayDollarAmount( amount ); - screen.displayMessageLine( "." ); + screen.displayMessage( + "\nPlease insert a deposit envelope containing "); + screen.displayEuroAmount(amount); + screen.displayMessageLine("."); // receive deposit envelope boolean envelopeReceived = depositSlot.isEnvelopeReceived(); // check whether deposit envelope was received - if ( envelopeReceived ) - { - screen.displayMessageLine( "\nYour envelope has been " + - "received.\nNOTE: The money just deposited will not " + - "be available until we verify the amount of any " + - "enclosed cash and your checks clear." ); - + if (envelopeReceived) { + screen.displayMessageLine("\nYour envelope has been " + + "received.\nNOTE: The money just deposited will not " + + "be available until we verify the amount of any " + + "enclosed cash and your checks clear."); + // credit account to reflect the deposit - bankDatabase.credit( getAccountNumber(), amount ); + bankDatabase.credit(getAccountNumber(), amount); } // end if else // deposit envelope not received { - screen.displayMessageLine( "\nYou did not insert an " + - "envelope, so the ATM has canceled your transaction." ); + screen.displayMessageLine("\nYou did not insert an " + + "envelope, so the code.GUI.ATM has canceled your transaction."); } // end else - } // end if + } // end if else // user canceled instead of entering amount { - screen.displayMessageLine( "\nCanceling transaction..." ); + screen.displayMessageLine("\nCanceling transaction..."); } // 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 cents + 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): " ); + screen.displayMessage("\nPlease enter a deposit amount in " + + "CENTS (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 ) - return CANCELED; - else - { - return ( double ) input / 100; // return dollar amount + if (input == CANCELED) + return new Euro(0); + else { + return new Euro(input / 100); // return dollar amount + } // end else } // end method promptForDepositAmount -} // end class Deposit - - +} // end class code.GUI.Deposit /************************************************************************** - * (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 * + * (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. * + * 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 91% rename from src/DepositSlot.java rename to src/code/GUI/DepositSlot.java index 64e02c2..0f5f20d 100644 --- a/src/DepositSlot.java +++ b/src/code/GUI/DepositSlot.java @@ -1,5 +1,5 @@ -// DepositSlot.java -// Represents the deposit slot of the ATM +package code.GUI;// code.GUI.DepositSlot.java +// Represents the deposit slot of the code.GUI.ATM public class DepositSlot { @@ -9,7 +9,7 @@ public boolean isEnvelopeReceived() { return true; // deposit envelope was received } // end method isEnvelopeReceived -} // end class DepositSlot +} // end class code.GUI.DepositSlot diff --git a/src/code/GUI/Euro.java b/src/code/GUI/Euro.java new file mode 100644 index 0000000..a811b9a --- /dev/null +++ b/src/code/GUI/Euro.java @@ -0,0 +1,49 @@ +package code.GUI; + +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/Keypad.java b/src/code/GUI/Keypad.java similarity index 90% rename from src/Keypad.java rename to src/code/GUI/Keypad.java index cd035c7..6fe34a2 100644 --- a/src/Keypad.java +++ b/src/code/GUI/Keypad.java @@ -1,5 +1,5 @@ -// Keypad.java -// Represents the keypad of the ATM +package code.GUI;// code.GUI.Keypad.java +// Represents the keypad of the code.GUI.ATM import java.util.Scanner; // program uses Scanner to obtain user input public class Keypad @@ -10,14 +10,14 @@ public class Keypad public Keypad() { input = new Scanner( System.in ); - } // end no-argument Keypad constructor + } // end no-argument code.GUI.Keypad constructor // return an integer value entered by user public int getInput() { return input.nextInt(); // we assume that user enters an integer } // end method getInput -} // end class Keypad +} // end class code.GUI.Keypad diff --git a/src/Screen.java b/src/code/GUI/Screen.java similarity index 51% rename from src/Screen.java rename to src/code/GUI/Screen.java index 44d3f30..7908c21 100644 --- a/src/Screen.java +++ b/src/code/GUI/Screen.java @@ -1,40 +1,35 @@ -// Screen.java -// Represents the screen of the ATM +package code.GUI;// code.GUI.Screen.java -public class Screen -{ +// Represents the screen of the code.GUI.ATM + +public class Screen { // displays a message without a carriage return - public void displayMessage( String message ) - { - System.out.print( message ); + public void displayMessage(String message) { + System.out.print(message); } // end method displayMessage // display a message with a carriage return - public void displayMessageLine( String message ) - { - System.out.println( message ); + public void displayMessageLine(String message) { + System.out.println(message); } // end method displayMessageLine // display a dollar amount - public void displayDollarAmount( double amount ) - { - System.out.printf( "$%,.2f", amount ); - } // end method displayDollarAmount -} // end class Screen - - + public void displayEuroAmount(Euro amount) { + System.out.println(amount.stampa()); + } // end method displayDollarAmount +} // end class code.GUI.Screen /************************************************************************** - * (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 * + * (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. * + * 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/code/GUI/Withdrawal.java similarity index 52% rename from src/Withdrawal.java rename to src/code/GUI/Withdrawal.java index 6e0af62..e8f6442 100644 --- a/src/Withdrawal.java +++ b/src/code/GUI/Withdrawal.java @@ -1,8 +1,11 @@ -// Withdrawal.java -// Represents a withdrawal ATM transaction +package code.GUI;// code.GUI.Withdrawal.java -public class Withdrawal extends Transaction -{ +// Represents a withdrawal code.GUI.ATM transaction + +import code.Database.BankDatabase; +import code.Business_logic.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 @@ -10,141 +13,129 @@ public class Withdrawal extends Transaction // 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 ) - { + // code.GUI.Withdrawal constructor + public Withdrawal(int userAccountNumber, Screen atmScreen, + BankDatabase atmBankDatabase, Keypad atmKeypad, + CashDispenser atmCashDispenser) { // initialize superclass variables - super( userAccountNumber, atmScreen, atmBankDatabase ); - + super(userAccountNumber, atmScreen, atmBankDatabase); + // initialize references to keypad and cash dispenser keypad = atmKeypad; cashDispenser = atmCashDispenser; - } // end Withdrawal constructor + } // end code.GUI.Withdrawal constructor // perform transaction - public void execute() - { + 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(); + BankDatabase bankDatabase = getBankDatabase(); Screen screen = getScreen(); // loop until cash is dispensed or the user cancels - do - { - // obtain a chosen withdrawal amount from the user + do { + // obtain a chosen withdrawal amount from the user amount = displayMenuOfAmounts(); - + // check whether user chose a withdrawal amount or canceled - if ( amount != 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 ) - { + availableBalance = bankDatabase.getAvailableBalance(getAccountNumber()); + + // check whether the user has enough money in the account + if (!availableBalance.minoreDi(new Euro(amount))) { // 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 + bankDatabase.debit(getAccountNumber(), new Euro(amount)); + + cashDispenser.dispenseCash(amount); // dispense cash cashDispensed = true; // cash was dispensed // instruct user to take cash - screen.displayMessageLine( - "\nPlease take your cash now." ); + 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." ); + screen.displayMessageLine( + "\nInsufficient cash available in the code.GUI.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." ); + screen.displayMessageLine( + "\nInsufficient funds in your account." + + "\n\nPlease choose a smaller amount."); } // end else } // end if - else // user chose cancel menu option + else // user chose cancel menu option { - screen.displayMessageLine( "\nCanceling transaction..." ); + screen.displayMessageLine("\nCanceling transaction..."); return; // return to main menu because user canceled } // end else - } while ( !cashDispensed ); + } 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() - { + 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 ) - { + 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: " ); + screen.displayMessageLine("\ncode.GUI.Withdrawal 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 + 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; + 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." ); + screen.displayMessageLine( + "\nIvalid selection. Try again."); } // end switch } // end while return userChoice; // return withdrawal amount or CANCELED } // end method displayMenuOfAmounts -} // end class Withdrawal - - +} // end class code.GUI.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 * + * (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. * + * 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/test/AccountTest.java b/src/test/AccountTest.java new file mode 100644 index 0000000..1817dba --- /dev/null +++ b/src/test/AccountTest.java @@ -0,0 +1,73 @@ +package test; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import code.GUI.Account; +import code.GUI.Euro; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class AccountTest { + private Account account; + + @BeforeEach + public void setUp() { + // Set up an account with initial balances + Euro initialAvailableBalance = new Euro(100, 0); + Euro initialTotalBalance = new Euro(150, 0); + account = new Account(123456, 1234, initialAvailableBalance, initialTotalBalance); + } + + @Test + public void validatePIN_CorrectPIN_ShouldReturnTrue() { + assertTrue(account.validatePIN(1234)); + } // pin corretto + + @Test + public void validatePIN_IncorrectPIN_ShouldReturnFalse() { + assertFalse(account.validatePIN(5678)); + } // pin errato + + @Test + public void getAvailableBalance_ShouldReturnCorrectBalance() { + Euro balance = account.getAvailableBalance(); + assertEquals(new Euro(100, 0).getValore(), balance.getValore()); + } // saldo totale + + @Test + public void getTotalBalance_ShouldReturnCorrectBalance() { + Euro balance = account.getTotalBalance(); + assertEquals(new Euro(150, 0).getValore(), balance.getValore()); + } // saldo disponibile + + @Test + public void credit_AddAmountToTotalBalance() { + Euro amountToCredit = new Euro(50, 0); + account.credit(amountToCredit); + + Euro expectedTotalBalance = new Euro(200, 0); + assertEquals(expectedTotalBalance.getValore(), account.getTotalBalance().getValore()); + } // aggiunguiamo soldi al conto, dovrebbe essere 200 + + @ParameterizedTest + @CsvSource({ "30, 70, 120" }) + public void debit_SubtractAmountFromBalances(int amountToDebit, int expectedAvailable, int expectedTotal) { + account.debit(new Euro(amountToDebit, 0)); + + Euro expectedAvailableBalance = new Euro(expectedAvailable, 0); + Euro expectedTotalBalance = new Euro(expectedTotal, 0); + + assertEquals(expectedAvailableBalance.getValore(), account.getAvailableBalance().getValore()); + assertEquals(expectedTotalBalance.getValore(), account.getTotalBalance().getValore()); + } + + @Test + public void getAccountNumber_ShouldReturnCorrectAccountNumber() { + assertEquals(123456, account.getAccountNumber()); + } +} diff --git a/src/test/BankDatabaseTest.java b/src/test/BankDatabaseTest.java new file mode 100644 index 0000000..bb1bed8 --- /dev/null +++ b/src/test/BankDatabaseTest.java @@ -0,0 +1,71 @@ +package test; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +import code.Database.BankDatabase; +import code.GUI.Euro; + +public class BankDatabaseTest { + + private BankDatabase bankDatabase; + + @BeforeEach + public void setUp() { + bankDatabase = new BankDatabase(); + } + + @Test + public void authenticateUser_CorrectCredentials_ShouldReturnTrue() { + assertTrue(bankDatabase.authenticateUser(12345, 54321)); + } + + @Test + public void authenticateUser_IncorrectCredentials_ShouldReturnFalse() { + assertFalse(bankDatabase.authenticateUser(12345, 9999)); + } + + @Test + public void getAvailableBalance_ValidAccountNumber_ShouldReturnBalance() { + assertEquals(new Euro(1000.0).getValore(), bankDatabase.getAvailableBalance(12345).getValore()); + } + + @Test + public void getTotalBalance_ValidAccountNumber_ShouldReturnBalance() { + assertEquals(new Euro(1200.0).getValore(), bankDatabase.getTotalBalance(12345).getValore()); + } + + @Test + public void credit_ValidAccountNumber_ShouldUpdateBalance() { + Euro initialBalance = bankDatabase.getTotalBalance(12345); + Euro amountToCredit = new Euro(100.0); + + bankDatabase.credit(12345, amountToCredit); + + Euro newBalance = bankDatabase.getTotalBalance(12345); + assertEquals(initialBalance.somma(amountToCredit), newBalance); + } + + @Test + public void debit_ValidAccountNumberWithSufficientFunds_ShouldUpdateBalance() { + Euro initialBalance = bankDatabase.getTotalBalance(12345); + Euro amountToDebit = new Euro(100.0); + + bankDatabase.debit(12345, amountToDebit); + + Euro newBalance = bankDatabase.getTotalBalance(12345); + assertEquals(initialBalance.sottrai(amountToDebit), newBalance); + } + + @Test + public void debit_ValidAccountNumberWithInsufficientFunds_ShouldNotUpdateBalance() { + Euro initialBalance = bankDatabase.getTotalBalance(12345); + Euro amountToDebit = new Euro(1500.0); // Assuming the initial balance is 1200.0 + + bankDatabase.debit(12345, amountToDebit); + + Euro newBalance = bankDatabase.getTotalBalance(12345); + assertEquals(initialBalance, newBalance); + } +} diff --git a/src/test/TestEuro.java b/src/test/TestEuro.java new file mode 100644 index 0000000..dd3eac3 --- /dev/null +++ b/src/test/TestEuro.java @@ -0,0 +1,87 @@ +package test; + +import code.GUI.Euro; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.junit.jupiter.api.Assertions.*; + +public class TestEuro { + + private Euro euro; + + @BeforeEach + public void setup() { + // Inizializzazione prima di ogni test + euro = new Euro(5, 50); + } + + @Test + public void testCostruttoreConValoriPositivi() { + assertEquals(550, euro.getValore()); + } + + @Test + public void testCostruttoreConValoriNegativi() { + Euro euroNegativo = new Euro(-5, 50); + assertEquals(-450, euroNegativo.getValore()); + } // deve fallire (controlliamo -450 ma il valore è -550) + + @Test + public void testCostruttoreConDouble() { + Euro euroDouble = new Euro(5.50); + assertEquals(550, euroDouble.getValore()); + } + + @Test + public void testSomma() { + Euro euro2 = new Euro(3, 25); + Euro risultato = euro.somma(euro2); + assertEquals(875, risultato.getValore()); + } + + @Test + public void testSottrai() { + Euro euro2 = new Euro(3, 25); + Euro risultato = euro.sottrai(euro2); + assertEquals(225, risultato.getValore()); + } + + @Test + public void testUgualeA() { + Euro euroUguale = new Euro(5, 50); + assertTrue(euro.ugualeA(euroUguale)); + } + + @Test + public void testNonUgualeA() { + Euro euroNonUguale = new Euro(3, 25); + assertFalse(euro.ugualeA(euroNonUguale)); + } + + @Test + public void testMinoreDi() { + Euro euroMinore = new Euro(3, 50); + assertTrue(euroMinore.minoreDi(euro)); + } + + @Test + public void testNonMinoreDi() { + Euro euroNonMinore = new Euro(8, 25); + assertFalse(euroNonMinore.minoreDi(euro)); + } + + @ParameterizedTest + @CsvSource({ "5.50, 550", "3.75, 375", "7.80, 780" }) + public void testCostruttoreConDoubleParametrizzato(double valoreDouble, long valoreCentesimi) { + Euro euroParametrizzato = new Euro(valoreDouble); + assertEquals(valoreCentesimi, euroParametrizzato.getValore()); + } + + @Test + public void testStampa() { + assertEquals("5.5 euro", euro.stampa()); + } +}