📄 bankaccounttest.java
字号:
import java.awt.*;import java.awt.event.*;/*=========================================================================== PLEASE NOTE: This file is the test harness for this exercise. You do not need to modify this file. However, you may be interested in viewing it to understand what it is doing.===========================================================================*/public class BankAccountTest extends Panel implements ActionListener, ItemListener{ private java.util.Vector accounts = new java.util.Vector(); // GUI Components private List accountList = new List(); private Label nameLabel = new Label("Name:"); private Label balanceLabel = new Label("Opening Balance:"); private TextField nameField = new TextField(); private TextField balanceField = new TextField(); private Label limitLabel = new Label("Overdraft Limit:"); private TextField limitField = new TextField(); private Button addAccountButton = new Button("Add Account"); private Button addOverdraftButton = new Button("Add Overdraft Account"); private Label amountLabel = new Label("Amount:"); private TextField amountField = new TextField(); private Button withdrawButton = new Button("Withdraw"); private Button depositButton = new Button("Deposit"); public BankAccountTest() { setLayout(null); setupGUI(); } private void setupGUI() { setupEdit(); setupList(); }// Set up account editing gui components private void setupEdit() { add(nameLabel); add(nameField); add(balanceLabel); add(balanceField); add(limitLabel); add(limitField); add(addAccountButton); add(addOverdraftButton); nameLabel.setSize(50,24); nameLabel.setLocation(20,20); nameField.setSize(150,24); nameField.setLocation(130,20); balanceLabel.setSize(100,24); balanceLabel.setLocation(20,50); balanceField.setSize(150,24); balanceField.setLocation(130,50); limitLabel.setSize(100,24); limitLabel.setLocation(20,80); limitField.setSize(150,24); limitField.setLocation(130,80); addAccountButton.setSize(120,24); addAccountButton.setLocation(20,110); addAccountButton.addActionListener(this); addOverdraftButton.setSize(160,24); addOverdraftButton.setLocation(150,110); addOverdraftButton.addActionListener(this); }// Set up display list and withdraw/deposit buttons private void setupList() { add(accountList); add(amountField); add(amountLabel); add(withdrawButton); add(depositButton); accountList.setSize(200,100); accountList.setLocation(20,170); accountList.addItemListener(this); amountLabel.setSize(75, 24); amountLabel.setLocation(20, 280); amountField.setSize(100,24); amountField.setLocation(100,280); withdrawButton.setSize(100,24); withdrawButton.setLocation(20,310); withdrawButton.setEnabled(false); withdrawButton.addActionListener(this); depositButton.setSize(100,24); depositButton.setLocation(130,310); depositButton.setEnabled(false); depositButton.addActionListener(this); }// Action listener interface public void actionPerformed(ActionEvent e) { if (e.getSource()==addAccountButton) { BankAccount anAccount = createAccount(nameField.getText(), getDoubleFromTextField(balanceField) ); accounts.addElement(anAccount); accountList.add(anAccount.toString()); // POLYMORPHIC! clearFields(); } else if (e.getSource()==addOverdraftButton) { BankAccount anAccount = createOverdraftAccount(nameField.getText(), getDoubleFromTextField(balanceField), getDoubleFromTextField(limitField) ); accounts.addElement(anAccount); accountList.add(anAccount.toString()); // POLYMORPHIC! clearFields(); } else if (e.getSource()==withdrawButton) { int index = accountList.getSelectedIndex(); BankAccount anAccount = (BankAccount) accounts.elementAt(index); withdraw(anAccount, getDoubleFromTextField(amountField)); accountList.replaceItem(anAccount.toString(), index); accountList.select(index); } else if (e.getSource()==depositButton) { int index = accountList.getSelectedIndex(); BankAccount anAccount = (BankAccount) accounts.elementAt(index); deposit(anAccount, getDoubleFromTextField(amountField)); accountList.replaceItem(anAccount.toString(), index); accountList.select(index); } }// Item listener interface public void itemStateChanged(ItemEvent e) { int index = accountList.getSelectedIndex(); if (index >= 0) { withdrawButton.setEnabled(true); depositButton.setEnabled(true); } else { clearFields(); withdrawButton.setEnabled(false); depositButton.setEnabled(false); } }// Clear entry GUI components private void clearFields() { nameField.setText(""); balanceField.setText(""); limitField.setText(""); }// Read text from the specified textField and return the double equivalent. return 0.0// if the value is not a parsable number. private double getDoubleFromTextField(TextField aTextField) { try { return Double.parseDouble(aTextField.getText()); } catch (Exception x) { return 0.0; } }// Starting method public static void main(String[] args) { Frame aFrame = new Frame("EmployeeTest"); aFrame.setSize(400,350); aFrame.setLocation(100,100); BankAccountTest aTest = new BankAccountTest(); aFrame.add(aTest); aFrame.setVisible(true); }// This method is invoked when the user presses the "Add Account" button. This method// should create a BankAccount object and initialize it with the specified parameters.// The created BankAccount object should then be returned. public BankAccount createAccount(String aName, double balance) { BankAccount temp = new BankAccount(aName, balance); return temp; }// This method is invoked when the user presses the "Add Overdraft Account" Button. This// method should create an OverdraftAccount object and initialize it with the specified// parameters. The created object should then be returned. public BankAccount createOverdraftAccount(String aName, double balance, double overdraftLimit) { OverdraftAccount temp = new OverdraftAccount(aName, balance, overdraftLimit); return temp; }// This method is invoked when the withdraw button is pressed. It should invoke the// withdraw method on the bank account instance provided. public void withdraw(BankAccount anAccount, double anAmount) { anAccount.withdraw(anAmount); // POLYMORPHIC! }// This method is invoked when the deposit button is pressed. It should invoke the// deposit method on the bank account instance provided. public void deposit(BankAccount anAccount, double anAmount) { anAccount.deposit(anAmount); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -