fork download
  1. package com.mybank.gui;
  2.  
  3. import com.mybank.domain.*;
  4. import com.mybank.data.DataSource;
  5. import java.awt.*;
  6. import java.awt.event.*;
  7. import javax.swing.*;
  8. import java.io.IOException;
  9.  
  10. public class ATMClient {
  11.  
  12. private static final String USAGE
  13. = "USAGE: java com.mybank.gui.ATMClient <dataFilePath>";
  14.  
  15. public static void main(String[] args) {
  16.  
  17. // Retrieve the dataFilePath command-line argument
  18. if ( args.length != 1 ) {
  19. System.out.println(USAGE);
  20. } else {
  21. String dataFilePath = args[0];
  22.  
  23. try {
  24. System.out.println("Reading data file: " + dataFilePath);
  25. // Create the data source and load the Bank data
  26. DataSource dataSource = new DataSource(dataFilePath);
  27. dataSource.loadData();
  28.  
  29. // Run the ATM GUI
  30. ATMClient client = new ATMClient();
  31. client.launchFrame();
  32.  
  33. } catch (IOException ioe) {
  34. System.out.println("Could not load the data file.");
  35. System.out.println(ioe.getMessage());
  36. ioe.printStackTrace(System.err);
  37. }
  38. }
  39. }
  40.  
  41. // PLACE YOUR GUI CODE HERE
  42.  
  43. // GUI domain object instance variables
  44. Customer customer;
  45. Account account;
  46.  
  47. // GUI lifecycle instance variables
  48. private enum ATMCycle { ENTER_CUST_ID, PERFORM_OPERATION }
  49. private ATMCycle cycle = ATMCycle.ENTER_CUST_ID;
  50. private enum ATMOperation { UNKNOWN, GET_BALANCE, DEPOSIT, WITHDRAW }
  51. private ATMOperation operation = ATMOperation.UNKNOWN;
  52.  
  53. // GUI component instance variables
  54. private JFrame frame;
  55. private JPanel pLeftHalf;
  56. private JPanel pRightHalf;
  57. private JButton[] actionButtons;
  58. private JTextField entryField;
  59. private JTextField messageField;
  60. private JTextArea outputArea;
  61.  
  62. public ATMClient() {
  63. frame = new JFrame("The First Java Bank ATM");
  64. frame.addWindowListener(new CloseHandler());
  65. initializeFrameComponents();
  66. }
  67.  
  68. public void launchFrame() {
  69. frame.pack();
  70. frame.setResizable(false);
  71. frame.setVisible(true);
  72. performCycle(ATMCycle.ENTER_CUST_ID);
  73. }
  74.  
  75. private void enableActionButtons(boolean enableFlag) {
  76. for ( JButton b : actionButtons ) {
  77. b.setEnabled(enableFlag);
  78. }
  79. }
  80.  
  81. private void performCycle(ATMCycle cycle) {
  82. switch ( cycle ) {
  83. case ENTER_CUST_ID: {
  84. outputArea.setText(ENTER_CUSTOMER_TEXT);
  85. enableActionButtons(false);
  86. break;
  87. }
  88. case PERFORM_OPERATION: {
  89. performOperation();
  90. break;
  91. }
  92. }
  93. }
  94. private static final String ENTER_CUSTOMER_TEXT
  95. = "Enter your customer ID into the key pad and press the ENTER button.\n";
  96. private static final String PERFORM_OPERATION_TEXT
  97. = "";
  98.  
  99. private void performOperation() {
  100. double amount = -1.0;
  101. switch ( operation ) {
  102. case GET_BALANCE: {
  103. outputArea.append("Your account balance is: "
  104. + account.getBalance() + "\n");
  105. break;
  106. }
  107. case DEPOSIT: {
  108. try {
  109. amount = Double.parseDouble(entryField.getText());
  110. account.deposit(amount);
  111. outputArea.append("Your deposit of " + amount + " was successful.\n");
  112. this.operation = ATMOperation.GET_BALANCE;
  113. performOperation();
  114. } catch (NumberFormatException nfe) {
  115. outputArea.append("Deposit amount is not a number: "
  116. + entryField.getText());
  117. }
  118. break;
  119. }
  120. case WITHDRAW: {
  121. try {
  122. amount = Double.parseDouble(entryField.getText());
  123. account.withdraw(amount);
  124. outputArea.append("Your withdrawal of " + amount + " was successful.\n");
  125. this.operation = ATMOperation.GET_BALANCE;
  126. performOperation();
  127. } catch (OverdraftException nfe) {
  128. outputArea.append("Your withdrawal of "
  129. + amount + " was unsuccessful.\n");
  130. } catch (NumberFormatException nfe) {
  131. outputArea.append("Deposit amount is not a number: "
  132. + entryField.getText());
  133. }
  134. break;
  135. }
  136. case UNKNOWN: {
  137. assert false;
  138. break;
  139. }
  140. }
  141. }
  142.  
  143. private class CloseHandler extends WindowAdapter {
  144. public void windowClosing(WindowEvent e) {
  145. System.exit(0);
  146. }
  147. }
  148.  
  149. private class KeyPadHandler implements ActionListener {
  150. public void actionPerformed(ActionEvent event) {
  151. JButton b = (JButton) event.getSource();
  152. entryField.setText(entryField.getText() + b.getText());
  153. }
  154. }
  155.  
  156. private class EntryActionHandler implements ActionListener {
  157. public void actionPerformed(ActionEvent event) {
  158. switch ( cycle ) {
  159. case ENTER_CUST_ID: {
  160. int customerID = -1;
  161. try {
  162. customerID = Integer.parseInt(entryField.getText());
  163. customer = Bank.getCustomer(customerID);
  164. if ( customer == null ) {
  165. outputArea.append("Customer ID is not valid for this Bank: "
  166. + entryField.getText());
  167. } else {
  168. account = customer.getAccount(0);
  169. outputArea.append("Welcome " + customer.getFirstName()
  170. + " " + customer.getLastName() + "\n\n");
  171. cycle = ATMCycle.PERFORM_OPERATION;
  172. enableActionButtons(true);
  173. }
  174. } catch (NumberFormatException nfe) {
  175. outputArea.append("Customer ID is not a number: "
  176. + entryField.getText());
  177. }
  178. entryField.setText("");
  179. break;
  180. }
  181. case PERFORM_OPERATION: {
  182. performOperation();
  183. break;
  184. }
  185. }
  186. }
  187. }
  188.  
  189. private void initializeFrameComponents() {
  190. initLeftHalf();
  191. initRightHalf();
  192. }
  193.  
  194. private void initLeftHalf() {
  195. pLeftHalf = new JPanel();
  196. pLeftHalf.setLayout(new GridLayout(2, 1));
  197. initTopLeft();
  198. initBottomLeft();
  199. frame.add(pLeftHalf, BorderLayout.WEST);
  200. }
  201.  
  202. private void initTopLeft() {
  203. JPanel topLeftPanel = new JPanel();
  204. topLeftPanel.setLayout(new GridLayout(3, 1));
  205. actionButtons = new JButton[3];
  206. b = new JButton("Display account balance");
  207. actionButtons[0] = b;
  208. b.addActionListener(new ActionListener() {
  209. public void actionPerformed(ActionEvent event) {
  210. operation = ATMOperation.GET_BALANCE;
  211. entryField.setText("");
  212. performOperation();
  213. }
  214. });
  215. topLeftPanel.add(b);
  216. b = new JButton("Make a deposit");
  217. actionButtons[1] = b;
  218. b.addActionListener(new ActionListener() {
  219. public void actionPerformed(ActionEvent event) {
  220. operation = ATMOperation.DEPOSIT;
  221. outputArea.append("Enter the amount to deposit into the"
  222. + " key pad and press the ENTER button.\n");
  223. entryField.setText("");
  224. }
  225. });
  226. topLeftPanel.add(b);
  227. b = new JButton("Make a withdrawal");
  228. actionButtons[2] = b;
  229. b.addActionListener(new ActionListener() {
  230. public void actionPerformed(ActionEvent event) {
  231. operation = ATMOperation.WITHDRAW;
  232. outputArea.append("Enter the amount to withdraw into the"
  233. + " key pad and press the ENTER button.\n");
  234. entryField.setText("");
  235. }
  236. });
  237. topLeftPanel.add(b);
  238. pLeftHalf.add(topLeftPanel);
  239. }
  240.  
  241. private void initBottomLeft() {
  242. // Initialize entry text field and keypad grid panel
  243. JPanel entryKeyPadPanel = new JPanel();
  244. entryKeyPadPanel.setLayout(new BorderLayout());
  245. // Create and add entry text field
  246. entryField = new JTextField(10);
  247. entryKeyPadPanel.add(entryField, BorderLayout.NORTH);
  248. // Create keypad grid and buttons
  249. JPanel keyPadGrid = new JPanel();
  250. keyPadGrid.setLayout(new GridLayout(4, 3));
  251. KeyPadHandler keyPadHandler = new KeyPadHandler();
  252. JButton[] keyPadButtons = new JButton[]
  253. {new JButton("1"),
  254. new JButton("2"),
  255. new JButton("3"),
  256. new JButton("4"),
  257. new JButton("5"),
  258. new JButton("6"),
  259. new JButton("7"),
  260. new JButton("8"),
  261. new JButton("9"),
  262. new JButton("0"),
  263. new JButton(".")};
  264. for ( JButton b : keyPadButtons ) {
  265. b.addActionListener(keyPadHandler);
  266. keyPadGrid.add(b);
  267. }
  268. JButton enterButton = new JButton("ENTER");
  269. enterButton.addActionListener(new EntryActionHandler());
  270. keyPadGrid.add(enterButton);
  271. entryKeyPadPanel.add(keyPadGrid, BorderLayout.SOUTH);
  272. // Add entry/keypad panel to left-half panel
  273. pLeftHalf.add(entryKeyPadPanel);
  274. }
  275.  
  276. private void initRightHalf() {
  277. pRightHalf = new JPanel();
  278. pRightHalf.setLayout(new BorderLayout());
  279. outputArea = new JTextArea(10, 75);
  280. pRightHalf.add(outputArea, BorderLayout.CENTER);
  281. messageField = new JTextField(75);
  282. pRightHalf.add(messageField, BorderLayout.SOUTH);
  283. //pRightHalf.setEnabled(false);
  284. frame.add(pRightHalf, BorderLayout.EAST);
  285. }
  286. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:10: error: class ATMClient is public, should be declared in a file named ATMClient.java
public class ATMClient {
       ^
Main.java:3: error: package com.mybank.domain does not exist
import com.mybank.domain.*;
^
Main.java:4: error: package com.mybank.data does not exist
import com.mybank.data.DataSource;
                      ^
Main.java:44: error: cannot find symbol
    Customer customer;
    ^
  symbol:   class Customer
  location: class ATMClient
Main.java:45: error: cannot find symbol
    Account account;
    ^
  symbol:   class Account
  location: class ATMClient
Main.java:26: error: cannot find symbol
                DataSource dataSource = new DataSource(dataFilePath);
                ^
  symbol:   class DataSource
  location: class ATMClient
Main.java:26: error: cannot find symbol
                DataSource dataSource = new DataSource(dataFilePath);
                                            ^
  symbol:   class DataSource
  location: class ATMClient
Main.java:127: error: cannot find symbol
                } catch (OverdraftException nfe) {
                         ^
  symbol:   class OverdraftException
  location: class ATMClient
Main.java:163: error: cannot find symbol
                        customer = Bank.getCustomer(customerID);
                                   ^
  symbol:   variable Bank
  location: class ATMClient.EntryActionHandler
9 errors
stdout
Standard output is empty