/** * This class implements a bank account * * @author Gregory Kesden * @version 1.0.0 */ public class BankAccount { // Balance in dollar plus cents as decimal fraction private double sum; // The name of the BankAccount used, for example, to label it private String name; /** * Initializes the bank account'sbalance
account * to $0.00 and sets its name * * @param name The name of the account. */ public BankAccount(String name) { sum = 0.0; this.name = name; } /** * Return the currentbalance
of the account with * the amount of money specified as a decimal number such that * the integer represents the dollars and the fractional * component the cents. * * @return the currentbalance
of the account */ public double balance() { return sum; } /** * Increase thebalance
of the account by the specified * amount of money. The amount of money is specified as a decimal * number with the integer representing the dollars and the * fractional component representing the cents. * * @param amount amount of money to add to thebalance
*/ public void deposit(double amount) { sum += amount; } /** * Decrease thebalance
of the account by * the specified amount of money. The amount of money is * specified as a decimal number with the integer representing * the dollars and the fractional component representing the * cents. * * @param amount amount of money to subtract from the *balance
. */ public void withdraw(double amount) { sum -= amount; } /** * Returns the name of the account * * @return returns thename
of the account as a String. */ public String getName() { return name; } }
import java.awt.*; import java.applet.*; import java.awt.event.*; /** * This applet provides a GUI to operate an bank account * ** This code is documented to help you along the way. * * @author Greg Kesden * @author Alfonso Rodriguez * @version 1.0.0 */ public class BankAccountApplet extends Applet implements ActionListener { /* * This should allow the user to enter a dollar amount for * deposit or withdrawel, as display the balance, if requested by * the user. */ private TextField amountField; /* * A panel used to hold the three checkboxes in a vertical * configuration. */ private Panel opsPad; /* * A panel used to hold all of the other panels in position */ private Panel mainPanel; /* * These are used to allow the user to select the next operation. */ private Checkbox depositBox; private Checkbox withdrawBox; private Checkbox balanceBox; /* * A panel used to hold the three checkboxes in a vertical * configuration. */ private CheckboxGroup ops; /* * The button that the user presses to initiate the selected * operation. */ private Button executeButton; /* * The label that instructs the user about the proper input * format. */ private Label msgLabel; /* * The bank account that actually keeps track of the money */ private BankAccount acct; /** * Create and install the GUI components, including the text field, * the button, the check boxes, and the instruction label, as well * as create the new bank account */ public void init() { /* Create a new BankAccount to track the money */ acct = new BankAccount("Yuechao Gao"); /* * Create a new panel to hold the Checkboxes. * Configure it to use a GridLayout with three rows and one * column */ opsPad = new Panel(); opsPad.setLayout(new GridLayout(3,1)); /* * Create a new Checkbox group. When Checkboxes * are placed in a CheckboxGroup, they will act as radio * buttons. Pressing one will reset all of the rest. * * Notice the use of the "new" operator. It creates a new * Object, in this case a CheckboxGroup, and then calls the * new Object's constructor to initalize it. After the object * has been initalized, new returns a reference to the newly * created object. * * Notice that we save the reference in the reference variable, * "ops". This will allow us to make use of it later. */ CheckboxGroup ops = new CheckboxGroup(); /* * Create the three Checkboxes, one for each operation. * Notice that each is being placed into the CheckboxGroup * "ops" by the second parameter. Notice also that depositBox * will be initally selected. This is seen in the third * parameter. */ depositBox = new Checkbox ("deposit", ops, true); withdrawBox = new Checkbox ("withdraw", ops, false); balanceBox = new Checkbox ("balance", ops, false); /* * Add the three buttons to the panel, in the order we want them * to appear, from top to bottom. */ opsPad.add (depositBox); opsPad.add (withdrawBox); opsPad.add (balanceBox); /* Create the Button that will be used to initiate the transaction. */ executeButton = new Button ("Execute Transaction"); /* * Create the TextField that will be used to get the dollar amount * from the user */ amountField = new TextField(); mainPanel = new Panel(); mainPanel.setLayout(new GridLayout(1,3)); mainPanel.add (amountField); mainPanel.add (opsPad); mainPanel.add (executeButton); setLayout (new GridLayout (3, 1)); add (new Label ("Account Name: " + acct.getName(), Label.CENTER)); add (mainPanel); /* * Create the label for the bottom of the screen. It describes * The proper format for entering the amount */ add (new Label ("Please do not use leading $, e.g. 100.00, not $100.00", Label.CENTER)); /* * Install the listeners to tell us when the user changes the * amount or hits the Execute button. */ executeButton.addActionListener (this); } /** * Ask the BankAccount object to add the amount specified by the * user to the account. */ private void deposit() { /* * Use the Double wrapper class to convert the dollar amount in * the form of a String into a Double. This involves creating a * new Double, and then using doubleValue() to get the primitive * double value. */ Double amountDouble = new Double (amountField.getText();); /* Then, ask the account, acct, to deposit this amount */ acct.deposit (amountDouble.doubleValue()); /* Clear the TextField. */ amountField.setText(""); } /** * Ask the BankAccount object to subtract the amount specified by * the user from the account. */ private void withdraw() { /* * This works just like deposit(). It first uses the * wrapper class to get the amount as a double, then * it asks "acct" to withdraw this amount. */ Double amountDouble = new Double (amountField.getText();); acct.withdraw (amountDouble.doubleValue()); amountField.setText(""); } /** * Ask the BankAccount object to report the current balance of the * account, and then display this amount in the text field. */ private void balance() { double amount = 0.0; // The balance from acct.balance() /* First ask "acct" for its balance */ amount = acct.balance(); /* * Then, convert this to a Double, whcih can be converted to a * String. */ Double balanceDouble = new Double (amount); /* Then ask the "amountField" on the GUI to display it */ amountField.setText(balanceDouble.toString()); } /** * Takes one of the three actions described below, depending on * the state of the operation checkboxes * * deposit * withdraw * balance * * @param evt the event generated. */ public void actionPerformed(ActionEvent evt) { /* Just sanity check to make sure executeButton was pressed */ if (evt.getSource().equals(executeButton)) { /* If it was, see which Checkbox is checked and take * the corresponding action */ if (depositBox.getState()) { deposit(); // Add money to the account } else if (withdrawBox.getState()) { withdraw(); // Take money out of the account } else if (balanceBox.getState()) { balance(); // Query the balance of the account } } /* * After taking the appropriate action, of it the user did * not hit the executeButton, do nothing else, just return * and wait for the user to do something else. */ } }
Previou page | Next page |