Java Concepts, 5th Edition 99 { 100 assert state == TRANSACT; 101 return currentAccount.getBalance(); 102 } 103 104 /** 105Moves back to the previous state. 106 */ 107 public void back() 108 { 109 if (state == TRANSACT) 110 state = ACCOUNT; 111 else if (state == ACCOUNT) 112 state = PIN; 113 else if (state == PIN) 114 state = START; 115 } 116 117 /** 118Gets the current state of this ATM. 119 @return the current state 120 */ 121 public int getState() 122 { 123 return state; 124 } 125 126 private int state; 127 private int customerNumber; 128 private Customer currentCustomer; 129 private BankAccount currentAccount; 130 private Bank theBank; 131 132 public static final int START = 1; 133 public static final int PIN = 2; 134 public static final int ACCOUNT = 3; 135 public static final int TRANSACT = 4; 136 137 public static final int CHECKING = 1; 138 public static final int SAVINGS = 2; 139 } 566 567 Chapter 12 Object-Oriented Design Page 51 of 77 Java Concepts, 5th Edition ch12/atm/Bank.java 1 import java.io.FileReader; 2 import java.io.IOException; 3 import java.util.ArrayList; 4 import java.util.Scanner; 5 6 /** 7A bank contains customers with bank accounts. 8 */ 9 public class Bank 10 { 11 /** 12Constructs a bank with no customers. 13 */ 14 public Bank() 15 { 16 customers = new ArrayList<Customer>(); 17 } 18 19 /** 20Reads the customer numbers and pins 21and initializes the bank accounts. 22 @param filename the name of the customer file 23 */ 24 public void readCustomers(String filename) 25 throws IOException 26 { 27 Scanner in = new Scanner(new FileReader(filename)); 28 while (in.hasNext()) 29 { 30 int number = in.nextInt(); 31 int pin = in.nextInt(); 32 Customer c = new Customer(number, pin); 33 addCustomer(c); 34 } 35 in.close(); 36 } 37 567 568 Chapter 12 Object-Oriented Design Page 52 of 77 Java Concepts, 5th Edition 38 /** 39Adds a customer to the bank. 40 @param c the customer to add 41 */ 42 public void addCustomer(Customer c) 43 { 44 customers.add(c); 45 } 46 47 /** 48Finds a customer in the bank. 49 @param aNumber a customer number 50 @param aPin a personal identification number 51 @return the matching customer, or null if no customer 52matches 53 */ 54 public Customer findCustomer(int aNumber, int aPin) 55 { 56 for (Customer c : customers) 57 { 58 if (c.match(aNumber, aPin)) 59 return c; 60 } 61 return null; 62 } 63 64 private ArrayList<Customer> customers; 65 } ch12/atm/Customer.java 1 /** 2A bank customer with a checking and a savings account. 3 */ 4 public class Customer 5 { 6 /** 7Constructs a customer with a given number and PIN. 8 @param aNumber the customer number Chapter 12 Object-Oriented Design Page 53 of 77 Java Concepts, 5th Edition 9 @param aPinthe personal identification number 10 */ 11 public Customer(int aNumber, int aPin) 12 { 13 customerNumber = aNumber; 14 pin = aPin; 15 checkingAccount = new BankAccount(); 16 savingsAccount = new BankAccount(); 17 } 18 19 /** 20Tests if this customer matches a customer number 21and PIN. 22 @param aNumber a customer number 23 @param aPin a personal identification number 24 @return true if the customer number and PIN match 25 */ 26 public boolean match(int aNumber, int aPin) 27 { 28 return customerNumber == aNumber && pin == aPin; 29 } 30 31 /** 32Gets the checking account of this customer. 33 @return the checking account 34 */ 35 public BankAccount getCheckingAccount() 36 { 37 return checkingAccount; 38 } 39 40 /** 41Gets the savings account of this customer. 42 @return the checking account 43 */ 44 public BankAccount getSavingsAccount() 45 { 46 return savingsAccount; 47 } 48 568 569 Chapter 12 Object-Oriented Design Page 54 of 77 Java Concepts, 5th Edition 49 private int customerNumber; 50 private int pin; 51 private BankAccount checkingAccount; 52 private BankAccount savingsAccount; 53 } The following class implements a console user interface for the ATM. ch12/atm/ATMSimulator.java 1 import java.io.IOException; 2 import java.util.Scanner; 3 4 /** 5A text-based simulation of an automatic teller machine. 6 */ 7 public class ATMSimulator 8 { 9 public static void main(String[] args) 10 { 11 ATM theATM; 12 try 13 { 14 Bank theBank = new Bank(); 15 theBank.readCustomers(“customers.txt”); 16 theATM = new ATM(theBank); 17 } 18 catch(IOException e) 19 { 20 System.out.println(“Error opening accounts file.”); 21 return; 22 } 23 24 Scanner in = new Scanner(System.in); 25 26 while (true) 27 { 28 int state = theATM.getState(); 29 if (state == ATM.START) 30 { 569 570 Chapter 12 Object-Oriented Design Page 55 of 77 Java Concepts, 5th Edition 31 System.out.print(“Enter customer number: ”); 32 int number = in.nextInt(); 33 theATM.setCustomerNumber(number); 34 } 35 else if (state == ATM.PIN) 36 { 37 System.out.print(“Enter PIN: ”); 38 int pin = in.nextInt(); 39 theATM.selectCustomer(pin); 40 } 41 else if (state == ATM.ACCOUNT) 42 { 43 System.out.print(“A=Checking, B=Savings, C=Quit: ”); 44 String command = in.next(); 45 if (command.equalsIgnoreCase(“A”)) 46 theATM.selectAccount(ATM.CHECKING); 47 else if (command.equalsIgnoreCase(“B”)) 48 theATM.selectAccount(ATM.SAVINGS); 49 else if (command.equalsIgnoreCase(“C”)) 50 theATM.reset(); 51 else 52 System.out.println(“Illegal input!”); 53 } 54 else if (state == ATM.TRANSACT) 55 { 56 System.out.println(“Balance=” + theATM.getBalance()); 57 System.out.print(“A=Deposit, B=Withdrawal, C=Cancel: ”); 58 String command = in.next(); 59 if (command.equalsIgnoreCase(“A”)) 60 { 61 System.out.print(“Amount: ”); 62 double amount = in.nextDouble(); 63 theATM.deposit(amount); 64 theATM.back(); 65 } Chapter 12 Object-Oriented Design Page 56 of 77 Java Concepts, 5th Edition 66 else if (command.equalsIgnoreCase(“B”)) 67 { 68 System.out.print(“Amount: ”); 69 double amount = in.nextDouble(); 70 theATM.withdraw(amount); 71 theATM.back(); 72 } 73 else if (command.equalsIgnoreCase(“C”)) 74 theATM.back(); 75 else 76 System.out.println(“Illegal input!”); 77 } 78 } 79 } 80 } Output Enter account number: 1 Enter PIN: 1234 A=Checking, B=Savings, C=Quit: A Balance=0.0 A=Deposit, B=Withdrawal, C=Cancel: A Amount: 1000 A=Checking, B=Savings, C=Quit: C . . . Here are the user interface classes for the GUI version of the user interface. ch12/atm/ATMViewer.java 1 import java.io.IOException; 2 import javax.swing.JFrame; 3 import javax.swing.JOptionPane; 4 5 /** 6A graphical simulation of an automatic teller machine. 7 */ 8 public class ATMViewer 570 571 Chapter 12 Object-Oriented Design Page 57 of 77 Java Concepts, 5th Edition 9 { 10 public static void main(String[] args) 11 { 12 ATM theATM; 13 14 try 15 { 16 Bank theBank = new Bank(); 17 theBank.readCustomers(“customers.txt”); 18 theATM = new ATM(theBank); 19 } 20 catch(IOException e) 21 { 22 JOptionPane.showMessageDialog(null, 23 “Error opening accounts file.”); 24 return; 25 } 26 27 JFrame frame = new ATMFrame(theATM); 28 frame.setTitle(“First National Bank of Java”); 29 frame.setDefaultCloseOperation(JFrame.EXIT_ ON 30 frame.setVisible(true); 31 } 32 } ch12/atm/ATMFrame.java 1 import java.awt.FlowLayout; 2 import java.awt.GridLayout; 3 import java.awt.event.ActionEvent; 4 import java.awt.event.ActionListener; 5 import javax.swing.JButton; 6 import javax.swing.JFrame; 7 import javax.swing.JPanel; 8 import javax.swing.JTextArea; 9 10 /** 11A frame displaying the components of an ATM. 12 */ 13 public class ATMFrame extends JFrame 571 572 Chapter 12 Object-Oriented Design Page 58 of 77 Java Concepts, 5th Edition 14 { 15 /** 16Constructs the user interface of the ATM frame. 17 */ 18 public ATMFrame(ATM anATM) 19 { 20 theATM = anATM; 21 22// Construct components 23 pad = new KeyPad(); 24 25 display = new JTextArea(4, 20); 26 27 aButton = new JButton(“ A ”); 28 aButton.addActionListener(new AButtonListener()); 29 30 bButton = new JButton(“ B ”); 31 bButton.addActionListener(new BButtonListener()); 32 33 cButton = new JButton(“ C ”); 34 cButton.addActionListener(new CButtonListener()); 35 36// Add components 37 38 JPanel buttonPanel = new JPanel(); 39 buttonPanel.add(aButton); 40 buttonPanel.add(bButton); 41 buttonPanel.add(cButton); 42 43 setLayout(new FlowLayout()); 44 add(pad); 45 add(display); 46 add(buttonPanel); 47 showState(); 48 49 setSize(FRAME_WIDTH, FRAME_HEIGHT); 50 } 51 52 /** 572 573 Chapter 12 Object-Oriented Design Page 59 of 77 Java Concepts, 5th Edition 53Updates display message. 54 */ 55 public void showState() 56 { 57 int state = theATM.getState(); 58 pad.clear(); 59 if (state == ATM.START) 60 display.setText(“Enter customer number\nA = OK”); 61 else if (state == ATM.PIN) 62 display.setText(“Enter PIN\nA = OK”); 63 else if (state == ATM.ACCOUNT) 64 display.setText(“Select Account\n” 65 + “A = Checking\nB = Savings\nC = Exit”); 66 else if (state == ATM.TRANSACT) 67 display.setText(“Balance = ” 68 + theATM.getBalance() 69 + “\nEnter amount and select transaction\n” 70 + “A = Withdraw\nB = Deposit\nC = Cancel”); 71 } 72 73 private class AButtonListener implements ActionListener 74 { 75 public void actionPerformed(ActionEvent event) 76 { 77 int state = theATM.getState(); 78 if (state == ATM.START) 79 theATM.setCustomerNumber((int) pad.getValue()); 80 else if (state == ATM.PIN) 81 theATM.selectCustomer((int) pad.getValue()); 82 else if (state == ATM.ACCOUNT) 83 theATM.selectAccount(ATM.CHECKING); 84 else if (state == ATM.TRANSACT) 85 { 86 theATM.withdraw(pad.getValue()); 87 theATM.back(); Chapter 12 Object-Oriented Design Page 60 of 77 [...]... actionPerformed(ActionEvent event) 75 { 76 77 // Don't add two decimal points 78 if (label.equals(“.”) 79 && display.getText().indexOf(“.”) != -1) return; 80 81 82 // Append label text to button 83 display.setText(display.getText() + label); 84 } 85 } 86 87 JButton button = new JButton (label); 88 buttonPanel.add(button); 89 ActionListener listener = new DigitButtonListener(); 90 button.addActionListener(listener);... 77 Java Concepts, 5th Edition 127 1 28 129 130 131 132 300; 133 private JTextArea display; private ATM theATM; private static final int FRAME_WIDTH = 300; private static final int FRAME_HEIGHT = } This class uses layout managers to arrange the text field and the keypad buttons See Chapter 18 for more information about layout managers ch12/atm/KeyPad .java 1 import java. awt.BorderLayout; 2 import java. awt.GridLayout;... 62 of 77 Java Concepts, 5th Edition 28 29 buttonPanel = new JPanel(); 30 buttonPanel.setLayout(new GridLayout(4, 3)); 31 32 //Add digit buttons 33 34 addButton(“7”); 35 addButton( 8 ); 36 addButton(“9”); 37 addButton(“4”); 38 addButton(“5”); 39 addButton(“6”); 40 addButton(“1”); 41 addButton(“2”); 42 addButton(“3”); 43 addButton(“0”); 44 addButton(“.”); 45 46 // Add clear entry button 47 48 clearButton.. .Java Concepts, 5th Edition 88 } 89 showState(); 90 } 91 } 92 93 private class BButtonListener implements ActionListener 94 { 95 public void actionPerformed(ActionEvent event) 96 { 97 int state = theATM.getState(); 98 if (state == ATM.ACCOUNT) 99 theATM.selectAccount(ATM.SAVINGS); 100 else if (state == ATM.TRANSACT)... combination is 583 584 Now it is the human's turn, who will of course choose Chapter 12 Object-Oriented Design Page 75 of 77 Java Concepts, 5th Edition The computer should then remember the preceding combination as a losing combination As a result, the computer will never again choose that combination from or Discover classes and supply a UML diagram before you begin to program 584 585 ANSWERS TO SELF-CHECK... it be a good idea? 579 580 ★ Exercise R12.9 Write CRC cards for the Coin and CashRegister classes described in Section 8. 2 ★ Exercise R12.10 Write CRC cards for the Bank and BankAccount classes in Section 7.2 Chapter 12 Object-Oriented Design Page 69 of 77 Java Concepts, 5th Edition ★★ Exercise R12.11 Draw a UML diagram for the Coin and CashRegister classes described in Section 8. 2 ★★★ Exercise R12.12... import java. awt.event.ActionEvent; 4 import java. awt.event.ActionListener; 5 import javax.swing.JButton; 6 import javax.swing.JPanel; 7 import javax.swing.JTextField; 8 9 /** 10 A component that lets the user enter a number, using 11 a keypad labeled with digits 12 */ 13 public class KeyPad extends JPanel 14 { 15 /** 16 Constructs the keypad panel 17 */ 18 public... return 1; 26 27 Triangle smallerTriangle = new Triangle(width - 1); Chapter 13 Recursion 590 591 Page 5 of 54 Java Concepts, 5th Edition 28 int small erArea = smallerTriangle.getArea(); 29 return smallerArea + width; 30 } 31 private int width; 32 33 } ch13/triangle/TriangleTester .java 1 2 3 4 5 6 7 8 9 10 public class TriangleTester { public static void main(String[] args) { Triangle t = new Triangle(10);... program that prints out all permutations of the string "eat": ch13/permute/PermutationGeneratorDemo .java 1 import java. util.ArrayList; 2 3 /** 4 This program demonstrates the permutation generator 5 */ Chapter 13 Recursion Page 7 of 54 Java Concepts, 5th Edition 6 public class PermutationGeneratorDemo 7 { 8 public static void main(String[] args) 9 { 10 PermutationGenerator generator 11 = new PermutationGenerator(“eat”);... a sample file: T Which Java keyword is used to define a subclass? extends S What is the original name of the Java language? - *7 - C-+ Oak - Gosling M Which of the following types are supertypes of Rectangle? - PrintStream + Shape + RectangularShape + Object - String N What is the square root of 2? 1.41421356 Chapter 12 Object-Oriented Design Page 73 of 77 Java Concepts, 5th Edition Your program should . 1 38 public static final int SAVINGS = 2; 139 } 566 567 Chapter 12 Object-Oriented Design Page 51 of 77 Java Concepts, 5th Edition ch12/atm/Bank .java 1 import java. io.FileReader; 2 import java. io.IOException; . 83 theATM.selectAccount(ATM.CHECKING); 84 else if (state == ATM.TRANSACT) 85 { 86 theATM.withdraw(pad.getValue()); 87 theATM.back(); Chapter 12 Object-Oriented Design Page 60 of 77 Java Concepts, . points 78 if (label.equals(“.”) 79 && display.getText().indexOf(“.”) != -1) 80 return; 81 82 // Append label text to button 83 display.setText(display.getText() + label); 84 } 85