📄 atm.java
字号:
package server;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.*;
/**
A frame displaying the components of an ATM
*/
class ATM extends JApplet
{
/**
Constructs the user interface of the ATM application.
*/
public void init()
{
// initialize bank and customers
theBank = new Bank();
try
{
theBank.readCustomers("customers.txt");
}
catch(IOException e)
{
JOptionPane.showMessageDialog(null,
"Error opening accounts file.");
}
// construct components
pad = new KeyPad();
display = new JTextArea(4, 20);
aButton = new JButton(" A ");
aButton.addActionListener(new AButtonListener());
bButton = new JButton(" B ");
bButton.addActionListener(new BButtonListener());
cButton = new JButton(" C ");
cButton.addActionListener(new CButtonListener());
// add components to content pane
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(3, 1));
buttonPanel.add(aButton);
buttonPanel.add(bButton);
buttonPanel.add(cButton);
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(pad);
contentPane.add(display);
contentPane.add(buttonPanel);
setState(START_STATE);
}
/**
Sets the current customer number to the keypad value
and sets state to PIN.
*/
public void setCustomerNumber()
{
customerNumber = (int)pad.getValue();
setState(PIN_STATE);
}
/**
Gets PIN from keypad, finds customer in bank.
If found sets state to ACCOUNT, else to START.
*/
public void selectCustomer()
{
int pin = (int)pad.getValue();
currentCustomer
= theBank.findCustomer(customerNumber, pin);
if (currentCustomer == null)
setState(START_STATE);
else
setState(ACCOUNT_STATE);
}
/**
Sets current account to checking or savings. Sets
state to TRANSACT
@param account one of CHECKING_ACCOUNT or SAVINGS_ACCOUNT
*/
public void selectAccount(int account)
{
if (account == CHECKING_ACCOUNT)
currentAccount = currentCustomer.getCheckingAccount();
else
currentAccount = currentCustomer.getSavingsAccount();
setState(TRANSACT_STATE);
}
/**
Withdraws amount typed in keypad from current account.
Sets state to ACCOUNT.
*/
public void withdraw()
{
currentAccount.withdraw(pad.getValue(),currentCustomer);
setState(ACCOUNT_STATE);
}
/**
Deposits amount typed in keypad to current account.
Sets state to ACCOUNT.
*/
public void deposit()
{
currentAccount.deposit(pad.getValue(),currentCustomer);
setState(ACCOUNT_STATE);
}
/**
Sets state and updates display message.
@param state the next state
*/
public void setState(int newState)
{
state = newState;
pad.clear();
if (state == START_STATE)
display.setText("Enter customer number\nA = OK");
else if (state == PIN_STATE)
display.setText("Enter PIN\nA = OK");
else if (state == ACCOUNT_STATE)
display.setText("Select Account\n"
+ "A = Checking\nB = Savings\nC = Exit");
else if (state == TRANSACT_STATE)
display.setText("Balance = "
+ currentAccount.getBalance()
+ "\nEnter amount and select transaction\n"
+ "A = Withdraw\nB = Deposit\nC = Cancel");
}
private class AButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if (state == START_STATE)
setCustomerNumber();
else if (state == PIN_STATE)
selectCustomer();
else if (state == ACCOUNT_STATE)
selectAccount(CHECKING_ACCOUNT);
else if (state == TRANSACT_STATE)
withdraw();
}
}
private class BButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if (state == ACCOUNT_STATE)
selectAccount(SAVINGS_ACCOUNT);
else if (state == TRANSACT_STATE)
deposit();
}
}
private class CButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if (state == ACCOUNT_STATE)
setState(START_STATE);
else if (state == TRANSACT_STATE)
setState(ACCOUNT_STATE);
}
}
private int state;
private int customerNumber;
private Customer currentCustomer;
private BankAccount currentAccount;
private Bank theBank;
private JButton aButton;
private JButton bButton;
private JButton cButton;
private KeyPad pad;
private JTextArea display;
private static final int START_STATE = 1;
private static final int PIN_STATE = 2;
private static final int ACCOUNT_STATE = 3;
private static final int TRANSACT_STATE = 4;
private static final int CHECKING_ACCOUNT = 1;
private static final int SAVINGS_ACCOUNT = 2;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -