├── ATM.java ├── ATMCaseStudy.java ├── Account.java ├── AccountFactory.java ├── AccountIterator.java ├── BalanceInquiry.java ├── BankDatabase.java ├── CashDispenser.java ├── Deposit.java ├── Iterator.java ├── Keypad.java ├── README.md ├── Screen.java ├── Transaction.java └── Withdrawal.java /ATM.java: -------------------------------------------------------------------------------- 1 | import java.awt.BorderLayout; 2 | import java.awt.Button; 3 | import java.awt.event.ActionEvent; 4 | import java.awt.event.ActionListener; 5 | 6 | import javax.swing.JButton; 7 | import javax.swing.JFrame; 8 | 9 | 10 | 11 | // ATM.java 12 | // Represents an automated teller machine 13 | 14 | public class ATM 15 | { 16 | private boolean userAuthenticated; // whether user is authenticated 17 | private int currentAccountNumber; // current user's account number 18 | private Screen screen; // ATM's screen 19 | private Keypad keypad; // ATM's keypad 20 | private CashDispenser cashDispenser; // ATM's cash dispenser 21 | private DepositSlot depositSlot; // ATM's deposit slot 22 | private BankDatabase bankDatabase; // account information database 23 | private int AdminCheck; 24 | private String userinput = ""; 25 | private int position = 0; 26 | private static ATM uniqueinstance; 27 | Iterator Users = BankDatabase.createIterator(); 28 | 29 | // constants corresponding to main menu options 30 | private static final int BALANCE_INQUIRY = 1; 31 | private static final int WITHDRAWAL = 2; 32 | private static final int DEPOSIT = 3; 33 | private static final int EXIT = 4; 34 | 35 | // no-argument ATM constructor initializes instance variables 36 | public ATM() 37 | { 38 | 39 | userAuthenticated = false; // user is not authenticated to start 40 | currentAccountNumber = 0; // no current account number to start 41 | screen = new Screen(); // create screen 42 | 43 | keypad = new Keypad(); // create keypad 44 | 45 | cashDispenser = new CashDispenser(); // create cash dispenser 46 | depositSlot = new DepositSlot(); // create deposit slot 47 | bankDatabase = new BankDatabase(); // create acct info database 48 | } // end no-argument ATM constructor 49 | 50 | // start ATM 51 | public void run() 52 | { 53 | // welcome and authenticate user; perform transactions 54 | 55 | // loop while user is not yet authenticated 56 | 57 | startlogin(); // authenticate user 58 | } // end while 59 | //else 60 | //performTransactions(); // user is now authenticated 61 | //userAuthenticated = false; // reset before next ATM session 62 | //currentAccountNumber = 0; // reset before next ATM session 63 | //screen.displayMessageLine("\nThank you! Goodbye!"); 64 | // end while 65 | // end method run 66 | 67 | // attempts to authenticate user against database 68 | void startlogin() 69 | { 70 | 71 | 72 | position = 0; 73 | screen.createlogin(); 74 | userinput = ""; 75 | 76 | authenticate check = new authenticate(); 77 | screen.Mainframe.revalidate(); 78 | screen.Inputfield2.setText(""); 79 | keypad.setbuttons(); 80 | addkeypadlisteners(); 81 | 82 | screen.Mainframe.add( keypad.addkeypad(), BorderLayout.CENTER); 83 | 84 | screen.Mainframe.revalidate(); 85 | keypad.BEnter.addActionListener( check ); 86 | screen.Mainframe.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 87 | screen.Mainframe.setSize( 400, 280 ); // set frame size 88 | screen.Mainframe.setVisible( true ); 89 | screen.Mainframe.revalidate(); 90 | } 91 | 92 | // set userAuthenticated to boolean value returned by database 93 | public void authenticateuser(int pin){ 94 | userAuthenticated = 95 | bankDatabase.authenticateUser(pin); 96 | 97 | // check whether authentication succeeded 98 | if (userAuthenticated) 99 | { 100 | int accountNumber = bankDatabase.getaccpin(pin); 101 | AdminCheck = bankDatabase.getadmin(pin); 102 | if (AdminCheck == 0){ 103 | currentAccountNumber = accountNumber; 104 | screen.Mainframe.getContentPane().removeAll(); 105 | screen.Mainframe.revalidate(); 106 | createmenu(); 107 | screen.Mainframe.add( keypad.addkeypad(), BorderLayout.CENTER); 108 | screen.Mainframe.revalidate(); 109 | } 110 | 111 | else 112 | 113 | createAdminGUI(); 114 | Iterator UserIterator = BankDatabase.createIterator(); 115 | Addcheck check = new Addcheck(); 116 | Deletecheck check2 = new Deletecheck(); 117 | screen.button2.addActionListener(check); 118 | screen.button3.addActionListener(check2); 119 | 120 | //currentAccountNumber = accountNumber; // save user's account # 121 | 122 | } // end if 123 | else 124 | screen.messageJLabel3.setText( 125 | "Invalid account number or PIN. Please try again."); 126 | } // end method authenticateUser 127 | 128 | private class authenticate implements ActionListener 129 | { 130 | public void actionPerformed( ActionEvent e ) 131 | { 132 | 133 | //int accnum = Integer.parseInt( screen.Inputfield1.getText() ); 134 | int PIN = Integer.parseInt( screen.Inputfield2.getText() ); 135 | //Get the PIN from the GUI text field. 136 | authenticateuser(PIN); 137 | } 138 | } 139 | private class Addcheck implements ActionListener 140 | { 141 | public void actionPerformed( ActionEvent e ) 142 | { 143 | //Action Listener for adding user. 144 | BankDatabase.Adduser(); 145 | 146 | } 147 | } 148 | private class Deletecheck implements ActionListener 149 | { 150 | public void actionPerformed( ActionEvent e ) 151 | { 152 | //Action Listener for deleting a user. 153 | BankDatabase.Deleteuser(position); 154 | position = position - 1; 155 | 156 | } 157 | } 158 | //creating the main menu GUI 159 | public void createmenu(){ 160 | screen.setSize( 300, 150 ); 161 | balancecheck check1 = new balancecheck(); 162 | Depositcheck check2 = new Depositcheck(); 163 | Withdrawcheck check3 = new Withdrawcheck(); 164 | Exitcheck check4 = new Exitcheck(); 165 | screen.Mainframe.getContentPane().removeAll(); 166 | screen.Mainframe.revalidate(); 167 | //Add the keypad panel to the GUI 168 | screen.Mainframe.add( keypad.addkeypad(), BorderLayout.CENTER); 169 | screen.createmenu(); 170 | Account Account1 = bankDatabase.getAccount(currentAccountNumber); 171 | screen.messageJLabel.setText("Welcome " + Account1.getUsername() + " "); 172 | 173 | keypad.B1.addActionListener( check1 ); 174 | keypad.B2.addActionListener(check3); 175 | keypad.B3.addActionListener(check2); 176 | keypad.B4.addActionListener(check4); 177 | screen.Mainframe.revalidate(); 178 | } 179 | // display the main menu and perform transactions 180 | //the four below actionlisters are the main menu options. 181 | private class balancecheck implements ActionListener 182 | { 183 | public void actionPerformed( ActionEvent e ) 184 | { 185 | userinput = ""; 186 | performTransactions(1); 187 | } 188 | } 189 | 190 | private class Depositcheck implements ActionListener 191 | { 192 | public void actionPerformed( ActionEvent e ) 193 | { 194 | userinput = ""; 195 | performTransactions(3); 196 | } 197 | } 198 | 199 | private class Withdrawcheck implements ActionListener 200 | { 201 | public void actionPerformed( ActionEvent e ) 202 | { 203 | userinput = ""; 204 | performTransactions(2); 205 | } 206 | } 207 | 208 | private class Exitcheck implements ActionListener 209 | { 210 | public void actionPerformed( ActionEvent e ) 211 | { 212 | startlogin(); 213 | } 214 | } 215 | 216 | 217 | private void performTransactions(int a) 218 | { 219 | 220 | // local variable to store transaction currently being processed 221 | Transaction currentTransaction = null; 222 | 223 | 224 | currentTransaction = 225 | createTransaction(a); 226 | keypad.setbuttons(); 227 | addkeypadlisteners(); 228 | 229 | 230 | userinput = ""; 231 | screen.Inputfield2.setText(userinput); 232 | currentTransaction.execute(); 233 | 234 | 235 | 236 | 237 | Backcheck Back = new Backcheck(); 238 | screen.Exit.addActionListener(Back); 239 | screen.Mainframe.revalidate(); 240 | 241 | } 242 | public class Backcheck implements ActionListener 243 | { 244 | public void actionPerformed( ActionEvent e ) 245 | { 246 | //This takes the user back to the main menu. 247 | 248 | 249 | createmenu(); 250 | screen.Mainframe.add( keypad.addkeypad(), BorderLayout.CENTER); 251 | screen.Mainframe.revalidate(); 252 | userinput=""; 253 | screen.Inputfield2.setText(userinput); 254 | } 255 | } 256 | 257 | 258 | private Transaction createTransaction(int type) 259 | { 260 | Transaction temp = null; // temporary Transaction variable 261 | screen.getContentPane().removeAll(); 262 | screen.revalidate(); 263 | 264 | // determine which type of Transaction to create 265 | 266 | if(type == 1) // create new BalanceInquiry transaction 267 | temp = new BalanceInquiry( 268 | currentAccountNumber, screen, bankDatabase); 269 | else if(type == 2)// create new Withdrawal transaction 270 | temp = new Withdrawal(currentAccountNumber, screen, 271 | bankDatabase, keypad, cashDispenser); 272 | else if(type == 3){ // create new Deposit transaction 273 | screen.setSize( 400, 250 ); 274 | temp = new Deposit(currentAccountNumber, screen, 275 | bankDatabase, keypad, depositSlot);} 276 | // end switch 277 | 278 | return temp; // return the newly created object 279 | } 280 | // end method createTransaction 281 | 282 | //This creates the 'admin' screen if the Isadmin field is set to 1. 283 | public void createAdminGUI(){ 284 | 285 | screen.Mainframe.getContentPane().removeAll(); 286 | Nextcheck Ncheck = new Nextcheck(); 287 | Prevcheck Pcheck = new Prevcheck(); 288 | Exitcheck check4 = new Exitcheck(); 289 | screen.Mainframe.revalidate(); 290 | screen.createAdminpage(); 291 | screen.button1.addActionListener(Ncheck); 292 | screen.button4.addActionListener(Pcheck); 293 | screen.Exit.addActionListener(check4); 294 | screen.Mainframe.revalidate(); 295 | 296 | } 297 | 298 | 299 | //This code adds action listeners to the keypad. 300 | public void addkeypadlisteners(){ 301 | BCheck BC = new BCheck(); 302 | BClear BC1 = new BClear(); 303 | keypad.B1.addActionListener(BC); 304 | keypad.B2.addActionListener(BC); 305 | keypad.B3.addActionListener(BC); 306 | keypad.B4.addActionListener(BC); 307 | keypad.B5.addActionListener(BC); 308 | keypad.B6.addActionListener(BC); 309 | keypad.B7.addActionListener(BC); 310 | keypad.B8.addActionListener(BC); 311 | keypad.B9.addActionListener(BC); 312 | keypad.B0.addActionListener(BC); 313 | keypad.BClear.addActionListener(BC1); 314 | } 315 | 316 | //This code checks what button was pressed on the keypad. 317 | public class BCheck implements ActionListener 318 | { 319 | public void actionPerformed( ActionEvent e ) 320 | { 321 | JButton b = (JButton)e.getSource(); 322 | String label = b.getLabel(); 323 | userinput = userinput + label; 324 | //update the text field using the user's input. 325 | screen.Inputfield2.setText(userinput); 326 | 327 | } 328 | } 329 | public class BClear implements ActionListener 330 | { 331 | public void actionPerformed( ActionEvent e) 332 | { 333 | //Clear the input field. 334 | userinput = ""; 335 | screen.Inputfield2.setText(userinput); 336 | } 337 | } 338 | 339 | //Action listener used for the literator pattern 340 | public class Nextcheck implements ActionListener 341 | { 342 | public void actionPerformed( ActionEvent e ) 343 | { 344 | 345 | IterateUser(BankDatabase.createIterator()); 346 | } 347 | } 348 | //Action listener used for the literator pattern 349 | public class Prevcheck implements ActionListener 350 | { 351 | public void actionPerformed( ActionEvent e ) 352 | { 353 | 354 | prevIterateUser(BankDatabase.createIterator()); 355 | } 356 | } 357 | 358 | 359 | 360 | //Action listener used for the literator pattern 361 | public void IterateUser(Iterator Iterator){ 362 | if(Iterator.hasNext(position) == true) { 363 | position = position + 1; 364 | //Display the current user in the GUI message labels. 365 | Account AccountItem = (Account)Iterator.next(position); 366 | screen.messageJLabel2.setText("Username: " + AccountItem.getUsername()); 367 | screen.messageJLabel3.setText("Avaliable Balance: " + AccountItem.getAvailableBalance()); 368 | screen.messageJLabel4.setText("Avaliable Balance: " + AccountItem.getTotalBalance()); 369 | } 370 | 371 | 372 | } 373 | //Action listener used for the literator pattern 374 | public void prevIterateUser(Iterator Iterator){ 375 | if(Iterator.hasPrev(position) == true) { 376 | position = position - 1; 377 | Account AccountItem = (Account)Iterator.next(position); 378 | screen.messageJLabel2.setText("Username: " + AccountItem.getUsername()); 379 | screen.messageJLabel3.setText("Avaliable Balance: " + AccountItem.getAvailableBalance()); 380 | screen.messageJLabel4.setText("Avaliable Balance: " + AccountItem.getTotalBalance()); 381 | 382 | 383 | } 384 | 385 | 386 | } 387 | 388 | //Code used for the Singleton pattern. 389 | public static ATM getinstance() { 390 | if (uniqueinstance == null) { 391 | uniqueinstance = new ATM(); 392 | } 393 | return uniqueinstance; 394 | } 395 | 396 | 397 | } 398 | 399 | 400 | 401 | // end method actionPerformed 402 | 403 | 404 | // end i 405 | // end class ATM 406 | 407 | 408 | 409 | /************************************************************************** 410 | * (C) Copyright 1992-2014 by Deitel & Associates, Inc. and * 411 | * Pearson Education, Inc. All Rights Reserved. * 412 | * * 413 | * DISCLAIMER: The authors and publisher of this book have used their * 414 | * best efforts in preparing the book. These efforts include the * 415 | * development, research, and testing of the theories and programs * 416 | * to determine their effectiveness. The authors and publisher make * 417 | * no warranty of any kind, expressed or implied, with regard to these * 418 | * programs or to the documentation contained in these books. The authors * 419 | * and publisher shall not be liable in any event for incidental or * 420 | * consequential damages in connection with, or arising out of, the * 421 | * furnishing, performance, or use of these programs. * 422 | *************************************************************************/ -------------------------------------------------------------------------------- /ATMCaseStudy.java: -------------------------------------------------------------------------------- 1 | // ATMCaseStudy.java 2 | // Driver program for the ATM case study 3 | 4 | public class ATMCaseStudy 5 | { 6 | // main method creates and runs the ATM 7 | public static void main(String[] args) 8 | { 9 | 10 | ATM theATM = ATM.getinstance(); 11 | 12 | theATM.run(); 13 | } // end main 14 | } // end class ATMCaseStudy 15 | 16 | 17 | 18 | /************************************************************************** 19 | * (C) Copyright 1992-2014 by Deitel & Associates, Inc. and * 20 | * Pearson Education, Inc. All Rights Reserved. * 21 | * * 22 | * DISCLAIMER: The authors and publisher of this book have used their * 23 | * best efforts in preparing the book. These efforts include the * 24 | * development, research, and testing of the theories and programs * 25 | * to determine their effectiveness. The authors and publisher make * 26 | * no warranty of any kind, expressed or implied, with regard to these * 27 | * programs or to the documentation contained in these books. The authors * 28 | * and publisher shall not be liable in any event for incidental or * 29 | * consequential damages in connection with, or arising out of, the * 30 | * furnishing, performance, or use of these programs. * 31 | *************************************************************************/ -------------------------------------------------------------------------------- /Account.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | // Account.java 4 | // Represents a bank account 5 | 6 | public class Account 7 | { 8 | private int accountNumber; // account number 9 | private int pin; // PIN for authentication 10 | private double availableBalance; // funds available for withdrawal 11 | private double totalBalance; // funds available + pending deposits 12 | private int admin; 13 | private String username; 14 | 15 | // Account constructor initializes attributes 16 | public Account(String Username, int theAccountNumber, int thePIN, 17 | double theAvailableBalance, double theTotalBalance, int isadmin) 18 | { 19 | setUsername(Username); 20 | setAccountNumber(theAccountNumber); 21 | setPin(thePIN); 22 | setAvailableBalance(theAvailableBalance); 23 | setTotalBalance(theTotalBalance); 24 | setAdmin(isadmin); 25 | } // end Account constructor 26 | 27 | // determines whether a user-specified PIN matches PIN in Account 28 | public boolean validatePIN(int userPIN) 29 | { 30 | if (userPIN == getPin()) 31 | return true; 32 | else 33 | return false; 34 | } // end method validatePIN 35 | 36 | // returns available balance 37 | public double getAvailableBalance() 38 | { 39 | return availableBalance; 40 | } // end getAvailableBalance 41 | 42 | // returns the total balance 43 | public double getTotalBalance() 44 | { 45 | return totalBalance; 46 | } // end method getTotalBalance 47 | 48 | // credits an amount to the account 49 | public void credit(double amount) 50 | { 51 | setTotalBalance(getTotalBalance() + amount); // add to total balance 52 | } // end method credit 53 | 54 | // debits an amount from the account 55 | public void debit(double amount) 56 | { 57 | setAvailableBalance(getAvailableBalance() - amount); // subtract from available balance 58 | setTotalBalance(getTotalBalance() - amount); // subtract from total balance 59 | } // end method debit 60 | 61 | // returns account number 62 | public int getAccountNumber() 63 | { 64 | return accountNumber; 65 | } // end method getAccountNumber 66 | 67 | public int getISadmin() 68 | { 69 | return getAdmin(); 70 | } 71 | 72 | public int GetPin(){ 73 | return getPin(); 74 | } 75 | 76 | public String getUsername() { 77 | return username; 78 | } 79 | 80 | public void setUsername(String username) { 81 | this.username = username; 82 | } 83 | 84 | public void setAccountNumber(int accountNumber) { 85 | this.accountNumber = accountNumber; 86 | } 87 | 88 | public int getPin() { 89 | return pin; 90 | } 91 | 92 | public void setPin(int pin) { 93 | this.pin = pin; 94 | } 95 | 96 | public void setAvailableBalance(double availableBalance) { 97 | this.availableBalance = availableBalance; 98 | } 99 | 100 | public void setTotalBalance(double totalBalance) { 101 | this.totalBalance = totalBalance; 102 | } 103 | 104 | public int getAdmin() { 105 | return admin; 106 | } 107 | 108 | public void setAdmin(int admin) { 109 | this.admin = admin; 110 | } 111 | 112 | 113 | 114 | } // end class Account 115 | 116 | 117 | /************************************************************************** 118 | * (C) Copyright 1992-2014 by Deitel & Associates, Inc. and * 119 | * Pearson Education, Inc. All Rights Reserved. * 120 | * * 121 | * DISCLAIMER: The authors and publisher of this book have used their * 122 | * best efforts in preparing the book. These efforts include the * 123 | * development, research, and testing of the theories and programs * 124 | * to determine their effectiveness. The authors and publisher make * 125 | * no warranty of any kind, expressed or implied, with regard to these * 126 | * programs or to the documentation contained in these books. The authors * 127 | * and publisher shall not be liable in any event for incidental or * 128 | * consequential damages in connection with, or arising out of, the * 129 | * furnishing, performance, or use of these programs. * 130 | *************************************************************************/ -------------------------------------------------------------------------------- /AccountFactory.java: -------------------------------------------------------------------------------- 1 | 2 | public class AccountFactory extends Account { 3 | 4 | //This code is used for creating additional accounts to add to the database. 5 | public AccountFactory(String Username, int theAccountNumber, int thePIN, double theAvailableBalance, 6 | double theTotalBalance, int isadmin) { 7 | super(Username, theAccountNumber, thePIN, theAvailableBalance, theTotalBalance, isadmin); 8 | setUsername(Username); 9 | setAccountNumber(theAccountNumber); 10 | setPin(thePIN); 11 | setAvailableBalance(theAvailableBalance); 12 | setTotalBalance(theTotalBalance); 13 | setAdmin(isadmin); 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /AccountIterator.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | //This class is responsible the Iterator design pattern 4 | 5 | public class AccountIterator implements Iterator { 6 | ArrayList accounts; 7 | 8 | public AccountIterator(ArrayList accounts2) { 9 | this.accounts = accounts2; 10 | } //This obtains an already existing ArrayList for use within this class. 11 | 12 | 13 | //This function returns true if the ArrayList has a space next to the current one 14 | public boolean hasNext(int position) { 15 | if (position >= accounts.size()) { 16 | return false; 17 | } else { 18 | return true; 19 | } 20 | } 21 | 22 | 23 | @Override //This function iterates to the next position in the ArrayList. 24 | public Object next(int position) { 25 | Account AccountItem = accounts.get(position); 26 | return AccountItem; 27 | } 28 | 29 | 30 | @Override 31 | //This function checks when the position is at 0, and prevents the user from going back even further. 32 | public boolean hasPrev(int position) { 33 | if(position == 0) 34 | return false; 35 | else 36 | return true; 37 | } 38 | 39 | 40 | 41 | 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /BalanceInquiry.java: -------------------------------------------------------------------------------- 1 | import java.awt.event.ActionEvent; 2 | import java.awt.event.ActionListener; 3 | 4 | 5 | // BalanceInquiry.java 6 | // Represents a balance inquiry ATM transaction 7 | 8 | public class BalanceInquiry extends Transaction 9 | { 10 | // BalanceInquiry constructor 11 | public BalanceInquiry(int userAccountNumber, Screen atmScreen, 12 | BankDatabase atmBankDatabase) 13 | { 14 | super(userAccountNumber, atmScreen, atmBankDatabase); 15 | } // end BalanceInquiry constructor 16 | 17 | // performs the transaction 18 | @Override 19 | public void execute() 20 | { 21 | // get references to bank database and screen 22 | BankDatabase bankDatabase = getBankDatabase(); 23 | Screen screen = getScreen(); 24 | 25 | // get the available balance for the account involved 26 | double availableBalance = 27 | bankDatabase.getAvailableBalance(getAccountNumber()); 28 | 29 | // get the total balance for the account involved 30 | double totalBalance = 31 | bankDatabase.getTotalBalance(getAccountNumber()); 32 | 33 | // display the balance information on the screen 34 | 35 | 36 | screen.creatBalanceGUI(); 37 | screen.messageJLabel2.setText("Avaliable Balance: " + availableBalance); 38 | screen.messageJLabel3.setText("Total Balance: " + totalBalance); 39 | screen.Mainframe.revalidate(); 40 | 41 | } // end method execute 42 | 43 | 44 | } // end class BalanceInquiry 45 | 46 | 47 | 48 | /************************************************************************** 49 | * (C) Copyright 1992-2014 by Deitel & Associates, Inc. and * 50 | * Pearson Education, Inc. All Rights Reserved. * 51 | * * 52 | * DISCLAIMER: The authors and publisher of this book have used their * 53 | * best efforts in preparing the book. These efforts include the * 54 | * development, research, and testing of the theories and programs * 55 | * to determine their effectiveness. The authors and publisher make * 56 | * no warranty of any kind, expressed or implied, with regard to these * 57 | * programs or to the documentation contained in these books. The authors * 58 | * and publisher shall not be liable in any event for incidental or * 59 | * consequential damages in connection with, or arising out of, the * 60 | * furnishing, performance, or use of these programs. * 61 | *************************************************************************/ -------------------------------------------------------------------------------- /BankDatabase.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | // BankDatabase.java 4 | // Represents the bank account information database 5 | 6 | public class BankDatabase 7 | { 8 | static ArrayList accounts = new ArrayList() ; // array of Accounts 9 | 10 | 11 | 12 | // no-argument BankDatabase constructor initializes accounts 13 | public BankDatabase() 14 | { 15 | //The original array has been changed into an arraylist, this makes it easier to add/delete from the database 16 | Account accounts1 = new Account("Customer1", 12345, 11111, 1000.0, 1200.0, 0); 17 | Account accounts2 = new Account("Customer2", 98765, 22222, 200.0, 200.0, 0); 18 | Account accounts3 = new Account("Customer3", 19234, 33333, 200.0, 200.0, 0); 19 | Account accounts4 = new Account("Manager1", 99999, 00000, 0, 0, 1); 20 | accounts.add(accounts1); 21 | accounts.add(accounts2); 22 | accounts.add(accounts3); 23 | accounts.add(accounts4); 24 | } // end no-argument BankDatabase constructor 25 | 26 | // retrieve Account object containing specified account number 27 | public Account getAccount(int accountnumber) 28 | { 29 | // loop through accounts searching for matching account number 30 | for (Account currentAccount : accounts) 31 | { 32 | // return current account if match found 33 | if (currentAccount.getAccountNumber() == accountnumber) 34 | return currentAccount; 35 | } // end for 36 | 37 | return null; // if no matching account was found, return null 38 | } // end method getAccount 39 | 40 | private Account getAccountpin(int PIN) 41 | { 42 | // loop through accounts searching for matching account number 43 | for (Account currentAccount : accounts) 44 | { 45 | // return current account if match found 46 | if (currentAccount.GetPin() == PIN) 47 | return currentAccount; 48 | } // end for 49 | 50 | return null; // if no matching account was found, return null 51 | } // 52 | 53 | // determine whether user-specified account number and PIN match 54 | // those of an account in the database 55 | public boolean authenticateUser(int userPIN) 56 | { 57 | // attempt to retrieve the account with the account number 58 | Account userAccount = getAccountpin(userPIN); 59 | 60 | // if account exists, return result of Account method validatePIN 61 | if (userAccount != null) 62 | return userAccount.validatePIN(userPIN); 63 | else 64 | return false; // account number not found, so return false 65 | } // end method authenticateUser 66 | 67 | // return available balance of Account with specified account number 68 | public double getAvailableBalance(int userAccountNumber) 69 | { 70 | return getAccount(userAccountNumber).getAvailableBalance(); 71 | } // end method getAvailableBalance 72 | 73 | // return total balance of Account with specified account number 74 | public double getTotalBalance(int userAccountNumber) 75 | { 76 | return getAccount(userAccountNumber).getTotalBalance(); 77 | } // end method getTotalBalance 78 | 79 | // credit an amount to Account with specified account number 80 | public void credit(int userAccountNumber, double amount) 81 | { 82 | getAccount(userAccountNumber).credit(amount); 83 | } // end method credit 84 | 85 | // debit an amount from Account with specified account number 86 | public void debit(int userAccountNumber, double amount) 87 | { 88 | getAccount(userAccountNumber).debit(amount); 89 | } // end method debit 90 | public int getadmin(int userAccountNumber) 91 | { 92 | return getAccountpin(userAccountNumber).getISadmin(); 93 | } 94 | 95 | public static Iterator createIterator() { 96 | return new AccountIterator(accounts); 97 | } 98 | 99 | public int getaccpin(int PIN){ 100 | for (Account currentAccount : accounts) 101 | { 102 | // return current account if match found 103 | if (currentAccount.GetPin() == PIN) 104 | return currentAccount.getAccountNumber(); 105 | else 106 | return 1; 107 | } // end for 108 | return PIN; 109 | } 110 | 111 | public static void Adduser(){ 112 | String name = Screen.Inputfield1.getText(); 113 | int accountnumber = Integer.parseInt( Screen.Inputfield2.getText() ); 114 | int pin = Integer.parseInt( Screen.Inputfield4.getText() ); 115 | int balance = Integer.parseInt( Screen.Inputfield3.getText() ); 116 | 117 | Account newaccount = new Account(name, accountnumber, pin, balance, balance, 0); 118 | accounts.add(newaccount); 119 | 120 | Screen.Inputfield1.setText(""); 121 | Screen.Inputfield2.setText(""); 122 | Screen.Inputfield3.setText(""); 123 | Screen.Inputfield4.setText(""); 124 | } 125 | 126 | public static void Deleteuser(int position) { 127 | accounts.remove(position); 128 | 129 | } 130 | 131 | 132 | 133 | 134 | } // end class BankDatabase 135 | 136 | 137 | 138 | /************************************************************************** 139 | * (C) Copyright 1992-2014 by Deitel & Associates, Inc. and * 140 | * Pearson Education, Inc. All Rights Reserved. * 141 | * * 142 | * DISCLAIMER: The authors and publisher of this book have used their * 143 | * best efforts in preparing the book. These efforts include the * 144 | * development, research, and testing of the theories and programs * 145 | * to determine their effectiveness. The authors and publisher make * 146 | * no warranty of any kind, expressed or implied, with regard to these * 147 | * programs or to the documentation contained in these books. The authors * 148 | * and publisher shall not be liable in any event for incidental or * 149 | * consequential damages in connection with, or arising out of, the * 150 | * furnishing, performance, or use of these programs. * 151 | *************************************************************************/ -------------------------------------------------------------------------------- /CashDispenser.java: -------------------------------------------------------------------------------- 1 | // CashDispenser.java 2 | // Represents the cash dispenser of the ATM 3 | 4 | public class CashDispenser 5 | { 6 | // the default initial number of bills in the cash dispenser 7 | private final static int INITIAL_COUNT = 500; 8 | private int count; // number of $20 bills remaining 9 | 10 | // no-argument CashDispenser constructor initializes count to default 11 | public CashDispenser() 12 | { 13 | count = INITIAL_COUNT; // set count attribute to default 14 | } // end CashDispenser constructor 15 | 16 | // simulates dispensing of specified amount of cash 17 | public void dispenseCash(int amount) 18 | { 19 | int billsRequired = amount / 20; // number of $20 bills required 20 | count -= billsRequired; // update the count of bills 21 | } // end method dispenseCash 22 | 23 | // indicates whether cash dispenser can dispense desired amount 24 | public boolean isSufficientCashAvailable(int amount) 25 | { 26 | int billsRequired = amount / 20; // number of $20 bills required 27 | 28 | if (count >= billsRequired ) 29 | return true; // enough bills available 30 | else 31 | return false; // not enough bills available 32 | } // end method isSufficientCashAvailable 33 | } // end class CashDispenser 34 | 35 | 36 | 37 | /************************************************************************** 38 | * (C) Copyright 1992-2014 by Deitel & Associates, Inc. and * 39 | * Pearson Education, Inc. All Rights Reserved. * 40 | * * 41 | * DISCLAIMER: The authors and publisher of this book have used their * 42 | * best efforts in preparing the book. These efforts include the * 43 | * development, research, and testing of the theories and programs * 44 | * to determine their effectiveness. The authors and publisher make * 45 | * no warranty of any kind, expressed or implied, with regard to these * 46 | * programs or to the documentation contained in these books. The authors * 47 | * and publisher shall not be liable in any event for incidental or * 48 | * consequential damages in connection with, or arising out of, the * 49 | * furnishing, performance, or use of these programs. * 50 | *************************************************************************/ -------------------------------------------------------------------------------- /Deposit.java: -------------------------------------------------------------------------------- 1 | import java.awt.BorderLayout; 2 | import java.awt.event.ActionEvent; 3 | import java.awt.event.ActionListener; 4 | 5 | 6 | // Deposit.java 7 | // Represents a deposit ATM transaction 8 | 9 | public class Deposit extends Transaction 10 | { 11 | private double amount; // amount to deposit 12 | private Keypad keypad; // reference to keypad 13 | private DepositSlot depositSlot; // reference to deposit slot 14 | private final static int CANCELED = 0; // constant for cancel option 15 | 16 | // Deposit constructor 17 | public Deposit(int userAccountNumber, Screen atmScreen, 18 | BankDatabase atmBankDatabase, Keypad atmKeypad, 19 | DepositSlot atmDepositSlot) 20 | { 21 | // initialize superclass variables 22 | super(userAccountNumber, atmScreen, atmBankDatabase); 23 | 24 | // initialize references to keypad and deposit slot 25 | keypad = atmKeypad; 26 | depositSlot = atmDepositSlot; 27 | } // end Deposit constructor 28 | 29 | // perform transaction 30 | @Override 31 | public void execute() 32 | { 33 | promptForDepositAmount(); 34 | } 35 | public void makedeposit(double amount){ 36 | BankDatabase bankDatabase = getBankDatabase(); // get reference 37 | Screen screen = getScreen(); // get reference 38 | // get deposit amount from user 39 | 40 | // check whether user entered a deposit amount or canceled 41 | if (amount != CANCELED) 42 | { 43 | // request deposit envelope containing specified amount 44 | screen.messageJLabel2.setText( "\nPlease insert a deposit envelope containing " + amount); 45 | 46 | // receive deposit envelope 47 | boolean envelopeReceived = depositSlot.isEnvelopeReceived(); 48 | 49 | // check whether deposit envelope was received 50 | if (envelopeReceived) 51 | { 52 | screen.messageJLabel2.setText("\nYour envelope has been " + 53 | "received.\nNOTE: The money just deposited will not "); 54 | screen.messageJLabel3.setText("be available until we verify the amount of any " + 55 | "enclosed cash and your checks clear."); 56 | 57 | // credit account to reflect the deposit 58 | bankDatabase.credit(getAccountNumber(), amount); 59 | } // end if 60 | else // deposit envelope not received 61 | { 62 | screen.messageJLabel2.setText("\nYou did not insert an " + 63 | "envelope, so the ATM has canceled your transaction."); 64 | } // end else 65 | } // end if 66 | else // user canceled instead of entering amount 67 | { 68 | screen.messageJLabel2.setText("\nCanceling transaction..."); 69 | } // end else 70 | } // end method execute 71 | 72 | // prompt user to enter a deposit amount in cents 73 | private void promptForDepositAmount() 74 | { 75 | Screen screen = getScreen(); // get reference to screen 76 | 77 | // display the prompt 78 | screen.CreateDepositGUI(); // receive input of deposit amount 79 | screen.Mainframe.add( keypad.addkeypad(), BorderLayout.CENTER); 80 | Depositcheck check1 = new Depositcheck(); 81 | keypad.BEnter.addActionListener( check1 ); 82 | screen.Mainframe.revalidate(); 83 | // check whether the user cancelled or entered a valid amount 84 | 85 | // return dollar amount 86 | } // end else 87 | // end method promptForDepositAmount 88 | 89 | private class Depositcheck implements ActionListener 90 | { 91 | public void actionPerformed( ActionEvent e ) 92 | { 93 | 94 | double input = Integer.parseInt( screen.Inputfield2.getText() ); 95 | double amount = input / 100; 96 | 97 | makedeposit(amount); 98 | 99 | } 100 | } 101 | } 102 | // end class Deposit 103 | 104 | 105 | 106 | /************************************************************************** 107 | * (C) Copyright 1992-2014 by Deitel & Associates, Inc. and * 108 | * Pearson Education, Inc. All Rights Reserved. * 109 | * * 110 | * DISCLAIMER: The authors and publisher of this book have used their * 111 | * best efforts in preparing the book. These efforts include the * 112 | * development, research, and testing of the theories and programs * 113 | * to determine their effectiveness. The authors and publisher make * 114 | * no warranty of any kind, expressed or implied, with regard to these * 115 | * programs or to the documentation contained in these books. The authors * 116 | * and publisher shall not be liable in any event for incidental or * 117 | * consequential damages in connection with, or arising out of, the * 118 | * furnishing, performance, or use of these programs. * 119 | *************************************************************************/ -------------------------------------------------------------------------------- /Iterator.java: -------------------------------------------------------------------------------- 1 | 2 | public interface Iterator { 3 | //This is the interface for the Iterator pattern. 4 | boolean hasNext(int position); 5 | Object next(int position); 6 | boolean hasPrev(int position); 7 | 8 | } 9 | -------------------------------------------------------------------------------- /Keypad.java: -------------------------------------------------------------------------------- 1 | // Keypad.java 2 | // Represents the keypad of the ATM 3 | import java.util.Scanner; 4 | import java.awt.Button; 5 | import java.awt.Color; 6 | import java.awt.Dimension; 7 | import java.awt.FlowLayout; 8 | import java.awt.Graphics; 9 | import java.awt.event.ActionListener; 10 | import java.awt.event.ActionEvent; 11 | import java.util.Random; 12 | import javax.swing.JFrame; 13 | import javax.swing.JTextField; 14 | 15 | import javax.swing.JLabel; 16 | import javax.swing.JPanel; 17 | import javax.swing.JButton;// program uses Scanner to obtain user input 18 | 19 | public class Keypad 20 | { 21 | private Scanner input; // reads data from the command line 22 | private String userinput; 23 | public static JButton B1; 24 | public static JButton B2; 25 | public static JButton B3; 26 | public static JButton B4; 27 | public static JButton B5; 28 | public JButton B6; 29 | public JButton B7; 30 | public JButton B8; 31 | public JButton B9; 32 | public JButton B0; 33 | public JButton BClear; 34 | public JButton BEnter; 35 | 36 | 37 | 38 | 39 | // no-argument constructor initializes the Scanner 40 | public Keypad() 41 | { 42 | input = new Scanner(System.in); 43 | } // end no-argument Keypad constructor 44 | 45 | // return an integer value entered by user 46 | public int getInput() 47 | { 48 | return input.nextInt(); // we assume that user enters an integer 49 | } // end method getInput 50 | 51 | public void setbuttons(){ 52 | 53 | B1 = new JButton("1"); 54 | B1.setText("1"); 55 | B2 = new JButton("2"); 56 | B3 = new JButton("3"); 57 | B4 = new JButton("4"); 58 | B5 = new JButton("5"); 59 | B6 = new JButton("6"); 60 | B7 = new JButton("7"); 61 | B8 = new JButton("8"); 62 | B9 = new JButton("9"); 63 | B0 = new JButton("0"); 64 | BClear = new JButton("Clear"); 65 | BEnter = new JButton("Enter"); 66 | 67 | } 68 | 69 | public JPanel addkeypad(){ 70 | JPanel panel = new JPanel(); 71 | panel.setPreferredSize(new Dimension(180, 160)); 72 | panel.setBackground(Color.gray); 73 | panel.setLayout(new FlowLayout()); 74 | panel.add(B1); 75 | panel.add(B2); 76 | panel.add(B3); 77 | panel.add(B4); 78 | panel.add(B5); 79 | panel.add(B6); 80 | panel.add(B7); 81 | panel.add(B8); 82 | panel.add(B9); 83 | panel.add(BClear); 84 | panel.add(B0); 85 | panel.add(BEnter); 86 | 87 | return panel; 88 | } 89 | 90 | 91 | 92 | 93 | 94 | public String userinput(){ 95 | return userinput(); 96 | } 97 | 98 | public void resetuserinput(){ 99 | userinput = ""; 100 | } 101 | 102 | } // end class Keypad 103 | 104 | 105 | 106 | 107 | 108 | /************************************************************************** 109 | * (C) Copyright 1992-2014 by Deitel & Associates, Inc. and * 110 | * Pearson Education, Inc. All Rights Reserved. * 111 | * * 112 | * DISCLAIMER: The authors and publisher of this book have used their * 113 | * best efforts in preparing the book. These efforts include the * 114 | * development, research, and testing of the theories and programs * 115 | * to determine their effectiveness. The authors and publisher make * 116 | * no warranty of any kind, expressed or implied, with regard to these * 117 | * programs or to the documentation contained in these books. The authors * 118 | * and publisher shall not be liable in any event for incidental or * 119 | * consequential damages in connection with, or arising out of, the * 120 | * furnishing, performance, or use of these programs. * 121 | *************************************************************************/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ATM-Machine 2 | ATM machine written in Java, with interface. 3 | -------------------------------------------------------------------------------- /Screen.java: -------------------------------------------------------------------------------- 1 | // Screen.java 2 | // Represents the screen of the ATM 3 | import java.awt.Button; 4 | import java.awt.Color; 5 | import java.awt.FlowLayout; 6 | import java.awt.Graphics; 7 | import java.awt.event.ActionListener; 8 | import java.awt.event.ActionEvent; 9 | import java.util.Random; 10 | import javax.swing.JFrame; 11 | import javax.swing.JTextField; 12 | 13 | import javax.swing.JLabel; 14 | import javax.swing.JButton; 15 | 16 | public class Screen extends JFrame 17 | { 18 | public JFrame Mainframe; 19 | public static JTextField Inputfield1; 20 | public static JTextField Inputfield2; 21 | public static JTextField Inputfield3; 22 | public static JTextField Inputfield4; 23 | public JLabel messageJLabel; 24 | public JLabel messageJLabel2; // displays message of game status 25 | public JLabel messageJLabel3; 26 | public JLabel messageJLabel4; 27 | public JLabel messageJLabel5; 28 | public JLabel messageJLabel8; 29 | public JLabel messageJLabel9; 30 | public JLabel messageJLabel10; 31 | public JButton loginbutton; // creates new game 32 | public JButton button1; 33 | public JButton button2; 34 | public JButton button3; 35 | public JButton button4; 36 | public JButton button5; 37 | public JButton Exit; 38 | public int accnum = 0; 39 | public int PIN = 0; 40 | public JLabel messageJLabel6; 41 | public JLabel messageJLabel7; 42 | 43 | 44 | // displays a message without a carriage return 45 | public void displayMessage(String message) 46 | { 47 | System.out.print(message); 48 | } // end method displayMessage 49 | 50 | // display a message with a carriage return 51 | public void displayMessageLine(String message) 52 | { 53 | System.out.println(message); 54 | } // end method displayMessageLine 55 | 56 | // display a dollar amount 57 | public void displayDollarAmount(double amount) 58 | { 59 | System.out.printf("$%,.2f", amount); 60 | } // end method displayDollarAmount 61 | 62 | //create the login GUI 63 | public void createlogin() { 64 | 65 | Mainframe = new JFrame("ATM"); 66 | messageJLabel4 = new JLabel("Insert your credit/debit card then "); 67 | messageJLabel = new JLabel(" Enter your PIN number: "); 68 | 69 | Inputfield1 = new JTextField( 10 ); 70 | 71 | messageJLabel2 = new JLabel(" "); 72 | Inputfield2 = new JTextField( 10 ); 73 | loginbutton = new JButton("Login"); 74 | messageJLabel3 = new JLabel(""); 75 | Mainframe.setLayout( new FlowLayout() ); // set layout 76 | Mainframe.add(messageJLabel4); 77 | Mainframe.add( messageJLabel ); // add first prompt 78 | 79 | Mainframe.add( Inputfield2 ); 80 | Mainframe.add( messageJLabel2 ); 81 | //Mainframe.add(loginbutton); 82 | // add message label 83 | Mainframe.add(messageJLabel3); 84 | Inputfield2.setEditable(false); 85 | Mainframe.repaint(); 86 | 87 | 88 | } 89 | //create the main menu GUI 90 | public void createmenu(){ 91 | Mainframe.getContentPane().removeAll(); 92 | messageJLabel = new JLabel("Welcome"); 93 | messageJLabel2 = new JLabel("1 - Balance"); 94 | messageJLabel3 = new JLabel("2 - Withdrawal"); 95 | messageJLabel4 = new JLabel("3 - Deposit"); 96 | messageJLabel5 = new JLabel("4 - Exit"); 97 | Mainframe.setLayout( new FlowLayout() ); // set layout 98 | Mainframe.add(messageJLabel); 99 | Mainframe.add( messageJLabel2 ); // add first prompt 100 | Mainframe.add( messageJLabel3 ); // add second prompt 101 | Mainframe.add( messageJLabel4 ); // add message label 102 | Mainframe.add( messageJLabel5 ); 103 | Mainframe.repaint(); 104 | } 105 | 106 | //create the Balance GUI 107 | public void creatBalanceGUI(){ 108 | Mainframe.getContentPane().removeAll(); 109 | messageJLabel = new JLabel("Balance Information: "); 110 | messageJLabel2 = new JLabel("Avaliable Balance:"); 111 | messageJLabel3 = new JLabel("Total Balance:"); 112 | Exit = new JButton("Back"); 113 | Mainframe.setLayout( new FlowLayout() ); 114 | Mainframe.add(messageJLabel); 115 | Mainframe.add(messageJLabel2); 116 | Mainframe.add(messageJLabel3); 117 | Mainframe.add(Exit); 118 | Mainframe.repaint(); 119 | } 120 | 121 | //Create the withdraw GUI 122 | public void createWithdrawGUI(){ 123 | Mainframe.getContentPane().removeAll(); 124 | Mainframe.revalidate(); 125 | messageJLabel = new JLabel("Withdraw Menu: "); 126 | messageJLabel2 = new JLabel("1 - $20 "); 127 | messageJLabel3 = new JLabel("2 - $40 "); 128 | messageJLabel4 = new JLabel("3 - $60 "); 129 | messageJLabel5 = new JLabel("4 - $100 "); 130 | messageJLabel6 = new JLabel("5 - $200 "); 131 | messageJLabel7 = new JLabel(" Choose an amount to withdraw"); 132 | Exit = new JButton("Cancel"); 133 | Mainframe.setLayout( new FlowLayout() ); 134 | Mainframe.add(messageJLabel); 135 | Mainframe.add(messageJLabel2); 136 | Mainframe.add(messageJLabel3); 137 | Mainframe.add(messageJLabel4); 138 | Mainframe.add(messageJLabel5); 139 | Mainframe.add(messageJLabel6); 140 | Mainframe.add(Exit); 141 | Mainframe.add(messageJLabel7); 142 | Mainframe.repaint(); 143 | 144 | } 145 | 146 | //Create the Deposit GUI 147 | public void CreateDepositGUI(){ 148 | Mainframe.getContentPane().removeAll(); 149 | messageJLabel2 = new JLabel("Please enter a deposit amount in CENTS"); 150 | messageJLabel3 = new JLabel(""); 151 | Inputfield2 = new JTextField(10); 152 | Inputfield2.setEditable(false); 153 | button1 = new JButton("Deposit"); 154 | Exit = new JButton("Cancel"); 155 | Mainframe.add(messageJLabel2); 156 | Mainframe.add(messageJLabel3); 157 | Mainframe.add(Inputfield2); 158 | Mainframe.add(Exit); 159 | Mainframe.repaint(); 160 | } 161 | 162 | 163 | public void setGUI(){ 164 | repaint(); 165 | } 166 | 167 | //Create the admin page GUI 168 | public void createAdminpage(){ 169 | messageJLabel = new JLabel("View Users:"); 170 | messageJLabel2 = new JLabel("Account number:"); 171 | messageJLabel3 = new JLabel("Avaliable Balance:"); 172 | messageJLabel4 = new JLabel("Total Balance:"); 173 | messageJLabel5 = new JLabel("________________________________________________"); 174 | button1 = new JButton("Next"); 175 | button4 = new JButton("Previous"); 176 | Exit = new JButton("Back"); 177 | Inputfield1 = new JTextField(10); 178 | Inputfield2 = new JTextField(10); 179 | Inputfield3 = new JTextField(10); 180 | Inputfield4 = new JTextField(10); 181 | Mainframe.setLayout( new FlowLayout() ); 182 | messageJLabel6 = new JLabel("Add Account: "); 183 | messageJLabel7 = new JLabel("User name: "); 184 | Mainframe.add(messageJLabel); 185 | messageJLabel8 = new JLabel(" Account number: "); 186 | Mainframe.add(messageJLabel2); 187 | messageJLabel10 = new JLabel(" PIN: "); 188 | 189 | messageJLabel9 = new JLabel(" Balance number: "); 190 | button2 = new JButton("Add"); 191 | button3 = new JButton("Delete"); 192 | 193 | 194 | Mainframe.add(messageJLabel3); 195 | Mainframe.add(messageJLabel4); 196 | Mainframe.add(button4); 197 | Mainframe.add(button1); 198 | Mainframe.add(button3); 199 | Mainframe.add(messageJLabel5); 200 | Mainframe.add(messageJLabel6); 201 | Mainframe.add(messageJLabel7); 202 | Mainframe.add(Inputfield1); 203 | Mainframe.add(messageJLabel8); 204 | Mainframe.add(Inputfield2); 205 | Mainframe.add(messageJLabel10); 206 | Mainframe.add(Inputfield4); 207 | Mainframe.add(messageJLabel9); 208 | Mainframe.add(Inputfield3); 209 | 210 | Mainframe.add(button2); 211 | 212 | Mainframe.add(Exit); 213 | Mainframe.repaint(); 214 | } 215 | 216 | 217 | } // end class Screen 218 | 219 | 220 | 221 | /************************************************************************** 222 | * (C) Copyright 1992-2014 by Deitel & Associates, Inc. and * 223 | * Pearson Education, Inc. All Rights Reserved. * 224 | * * 225 | * DISCLAIMER: The authors and publisher of this book have used their * 226 | * best efforts in preparing the book. These efforts include the * 227 | * development, research, and testing of the theories and programs * 228 | * to determine their effectiveness. The authors and publisher make * 229 | * no warranty of any kind, expressed or implied, with regard to these * 230 | * programs or to the documentation contained in these books. The authors * 231 | * and publisher shall not be liable in any event for incidental or * 232 | * consequential damages in connection with, or arising out of, the * 233 | * furnishing, performance, or use of these programs. * 234 | *************************************************************************/ -------------------------------------------------------------------------------- /Transaction.java: -------------------------------------------------------------------------------- 1 | // Transaction.java 2 | // Abstract superclass Transaction represents an ATM transaction 3 | 4 | public abstract class Transaction 5 | { 6 | private int accountNumber; // indicates account involved 7 | protected Screen screen; // ATM's screen 8 | private BankDatabase bankDatabase; // account info database 9 | 10 | // Transaction constructor invoked by subclasses using super() 11 | public Transaction(int userAccountNumber, Screen atmScreen, 12 | BankDatabase atmBankDatabase) 13 | { 14 | accountNumber = userAccountNumber; 15 | screen = atmScreen; 16 | bankDatabase = atmBankDatabase; 17 | } // end Transaction constructor 18 | 19 | // return account number 20 | public int getAccountNumber() 21 | { 22 | return accountNumber; 23 | } // end method getAccountNumber 24 | 25 | // return reference to screen 26 | public Screen getScreen() 27 | { 28 | return screen; 29 | } // end method getScreen 30 | 31 | // return reference to bank database 32 | public BankDatabase getBankDatabase() 33 | { 34 | return bankDatabase; 35 | } // end method getBankDatabase 36 | 37 | // perform the transaction (overridden by each subclass) 38 | abstract public void execute(); 39 | } // end class Transaction 40 | 41 | 42 | 43 | /************************************************************************** 44 | * (C) Copyright 1992-2014 by Deitel & Associates, Inc. and * 45 | * Pearson Education, Inc. All Rights Reserved. * 46 | * * 47 | * DISCLAIMER: The authors and publisher of this book have used their * 48 | * best efforts in preparing the book. These efforts include the * 49 | * development, research, and testing of the theories and programs * 50 | * to determine their effectiveness. The authors and publisher make * 51 | * no warranty of any kind, expressed or implied, with regard to these * 52 | * programs or to the documentation contained in these books. The authors * 53 | * and publisher shall not be liable in any event for incidental or * 54 | * consequential damages in connection with, or arising out of, the * 55 | * furnishing, performance, or use of these programs. * 56 | *************************************************************************/ -------------------------------------------------------------------------------- /Withdrawal.java: -------------------------------------------------------------------------------- 1 | import java.awt.BorderLayout; 2 | import java.awt.event.ActionEvent; 3 | import java.awt.event.ActionListener; 4 | 5 | // Withdrawal.java 6 | // Represents a withdrawal ATM transaction 7 | 8 | public class Withdrawal extends Transaction 9 | { 10 | private int amount; // amount to withdraw 11 | private Keypad keypad; // reference to keypad 12 | private CashDispenser cashDispenser; // reference to cash dispenser 13 | 14 | // constant corresponding to menu option to cancel 15 | private final static int CANCELED = 6; 16 | 17 | // Withdrawal constructor 18 | public Withdrawal(int userAccountNumber, Screen atmScreen, 19 | BankDatabase atmBankDatabase, Keypad atmKeypad, 20 | CashDispenser atmCashDispenser) 21 | { 22 | // initialize superclass variables 23 | super(userAccountNumber, atmScreen, atmBankDatabase); 24 | 25 | // initialize references to keypad and cash dispenser 26 | keypad = atmKeypad; 27 | cashDispenser = atmCashDispenser; 28 | } // end Withdrawal constructor 29 | 30 | // perform transaction 31 | @Override 32 | public void execute() 33 | { 34 | // amount available for withdrawal 35 | 36 | // get references to bank database and screen 37 | 38 | 39 | // loop until cash is dispensed or the user cancels 40 | displayMenuOfAmounts(); 41 | } 42 | public void transaction(int amount){ 43 | BankDatabase bankDatabase = getBankDatabase(); 44 | Screen screen = getScreen(); 45 | boolean cashDispensed = false; // cash was not dispensed yet 46 | double availableBalance; 47 | // check whether user chose a withdrawal amount or canceled 48 | 49 | // get available balance of account involved 50 | availableBalance = 51 | bankDatabase.getAvailableBalance(getAccountNumber()); 52 | 53 | // check whether the user has enough money in the account 54 | if (amount <= availableBalance) 55 | { 56 | // check whether the cash dispenser has enough money 57 | if (cashDispenser.isSufficientCashAvailable(amount)) 58 | { 59 | // update the account involved to reflect the withdrawal 60 | bankDatabase.debit(getAccountNumber(), amount); 61 | 62 | cashDispenser.dispenseCash(amount); // dispense cash 63 | cashDispensed = true; // cash was dispensed 64 | 65 | // instruct user to take cash 66 | screen.messageJLabel7.setText("\nYour cash has been" + 67 | " dispensed. Please take your cash now."); 68 | } // end if 69 | else // cash dispenser does not have enough cash 70 | screen.messageJLabel7.setText( 71 | "\nInsufficient cash available in the ATM." + 72 | "\n\nPlease choose a smaller amount."); 73 | } // end if 74 | else // not enough money available in user's account 75 | { 76 | screen.messageJLabel7.setText( 77 | "\nInsufficient funds in your account." + 78 | "\n\nPlease choose a smaller amount."); 79 | } // end else 80 | } // end if 81 | // end else 82 | 83 | 84 | // end method execute 85 | 86 | // display a menu of withdrawal amounts and the option to cancel; 87 | // return the chosen amount or 0 if the user chooses to cancel 88 | private void displayMenuOfAmounts() 89 | { 90 | 91 | int userChoice = 0; // local variable to store return value 92 | 93 | Screen screen = getScreen(); // get screen reference 94 | screen.createWithdrawGUI(); 95 | screen.Mainframe.add( keypad.addkeypad(), BorderLayout.CENTER); 96 | withdraw1 check1 = new withdraw1(); 97 | withdraw2 check2 = new withdraw2(); 98 | withdraw3 check3 = new withdraw3(); 99 | withdraw4 check4 = new withdraw4(); 100 | withdraw5 check5 = new withdraw5(); 101 | Keypad.B1.addActionListener(check1); 102 | Keypad.B2.addActionListener(check2); 103 | Keypad.B3.addActionListener(check3); 104 | Keypad.B4.addActionListener(check4); 105 | Keypad.B5.addActionListener(check5); 106 | 107 | 108 | 109 | screen.Mainframe.revalidate(); 110 | } 111 | 112 | public class withdraw1 implements ActionListener 113 | { 114 | public void actionPerformed( ActionEvent e ) 115 | { 116 | transaction(20); 117 | } 118 | } 119 | public class withdraw2 implements ActionListener 120 | { 121 | public void actionPerformed( ActionEvent e ) 122 | { 123 | transaction(40); 124 | } 125 | } 126 | public class withdraw3 implements ActionListener 127 | { 128 | public void actionPerformed( ActionEvent e ) 129 | { 130 | transaction(60); 131 | } 132 | } 133 | public class withdraw4 implements ActionListener 134 | { 135 | public void actionPerformed( ActionEvent e ) 136 | { 137 | transaction(100); 138 | } 139 | } 140 | public class withdraw5 implements ActionListener 141 | { 142 | public void actionPerformed( ActionEvent e ) 143 | { 144 | transaction(200); 145 | } 146 | } 147 | } 148 | 149 | 150 | 151 | 152 | /************************************************************************** 153 | * (C) Copyright 1992-2014 by Deitel & Associates, Inc. and * 154 | * Pearson Education, Inc. All Rights Reserved. * 155 | * * 156 | * DISCLAIMER: The authors and publisher of this book have used their * 157 | * best efforts in preparing the book. These efforts include the * 158 | * development, research, and testing of the theories and programs * 159 | * to determine their effectiveness. The authors and publisher make * 160 | * no warranty of any kind, expressed or implied, with regard to these * 161 | * programs or to the documentation contained in these books. The authors * 162 | * and publisher shall not be liable in any event for incidental or * 163 | * consequential damages in connection with, or arising out of, the * 164 | * furnishing, performance, or use of these programs. * 165 | *************************************************************************/ --------------------------------------------------------------------------------