public class BankView extends EFrame { private BankAccount modelAccount = new BankAccount(); // 1 private ETextArea itsOutput = new ETextArea (10, 42); // 2 /** The View: Lay out the GUI components. */ public BankView() { add (new LastFiveButton().text // 3 ("show last five transactions")); // 4 add (new ELabel().text ("Enter deposit(+) or check(-)"));// 5 add (new UpdateField().width (5).text ("0")); // 6 add (itsOutput); // 7 setVisible (true); // 8 } //====================== /** Controllers: React to click of button or ENTER key. */ private class LastFiveButton extends EButton { public void onClick() { itsOutput.say (modelAccount.lastFiveTransactions()); // 9 } } //====================== private class UpdateField extends EField { public void onEnter() { String input = this.getText(); //10 if (input == null || input.length() == 0) //11 return; //12 modelAccount.makeChange (input); //13 itsOutput.say (modelAccount.currentBalance() + //14 " = balance."); //15 } } //====================== }//############################################################################# class BankViewApp { public static void main (String[ ] args) { new BankView(); } //====================== }//############################################################################# class BankAccount { private int itsBalance = 0; private String itsFive = ".. .. .. .. .."; /** If the given String is not a properly-formed integer, do nothing; otherwise add it to the balance (which decreases the balance if it is a negative number, representing a check) and update the last 5 transactions. */ public void makeChange (String given) { try { int input = Integer.parseInt (given.trim()); itsBalance += input; itsFive = itsFive.substring (itsFive.indexOf (" ") + 1) + " " + input; } catch (Exception e) { } // do nothing if given is not a properly-formed integer } /** Tell the balance currently in the bank account. */ public int currentBalance() { return itsBalance; } /** Display the last 5 transactions, or all of them if less than 5. */ public String lastFiveTransactions() { return itsFive; } /* EXERCISE: Change this so that there are 2 blanks between entries instead of just 1. EXERCISE: Change this so that, for instance, a display of 48 -20 becomes dep48 chk20. */ }//#############################################################################