Practice 2 - 21
Practice 2 - 21
Practice 2_2
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
// constants
//public final static Maximum Accounts that can be created;
public final static int MAXACCOUNTS = 10;
// constructor
public JavaBank() {
for (int i=0; i <10; i++) {
accountNames[i] = "EMPTY";
//System.out.println(AccountNames[i]);
}//ENDFOR
createUserInterface();
}//end constructor
// set up inputDetailJPanel
inputDetailJPanel = new JPanel();
inputDetailJPanel.setBounds(16, 16, 208, 250);
inputDetailJPanel.setBorder(new TitledBorder("Input Details"));
inputDetailJPanel.setLayout(null);
contentPane.add(inputDetailJPanel);
inputDetailJPanel.setBackground(COMPANY_COLOR);
// set up NameJLabel
nameJLabel = new JLabel();
nameJLabel.setBounds(8, 32, 90, 23);
nameJLabel.setText("Name:");
nameJLabel.setBackground(COMPANY_COLOR);
inputDetailJPanel.add(nameJLabel);
// set up NameJTextField
nameJTextField = new JTextField();
nameJTextField.setBounds(112, 32, 80, 21);
nameJTextField.setHorizontalAlignment(JTextField.RIGHT);
nameJTextField.setBackground(COMPANY_COLOR);
inputDetailJPanel.add(nameJTextField);
// set up AccountnumJLabel
accountNumJLabel = new JLabel();
accountNumJLabel.setBounds(8, 56, 100, 23);
accountNumJLabel.setText("Account Number:");
accountNumJLabel.setBackground(COMPANY_COLOR);
inputDetailJPanel.add(accountNumJLabel);
// set up AccountnumTextField
accountNumJTextField = new JTextField();
accountNumJTextField.setBounds(112, 56, 80, 21);
accountNumJTextField.setHorizontalAlignment(JTextField.RIGHT);
accountNumJTextField.setBackground(COMPANY_COLOR);
inputDetailJPanel.add(accountNumJTextField);
// set up BalanceJLabel
balanceJLabel = new JLabel();
balanceJLabel.setBounds(8, 80, 60, 23);
balanceJLabel.setText("Balance:");
balanceJLabel.setBackground(COMPANY_COLOR);
inputDetailJPanel.add(balanceJLabel);
// set up BalanceTextField
balanceJTextField = new JTextField();
balanceJTextField.setBounds(112, 80, 80, 21);
balanceJTextField.setHorizontalAlignment(JTextField.RIGHT);
balanceJTextField.setBackground(COMPANY_COLOR);
inputDetailJPanel.add(balanceJTextField);
// set up DepositJLabel
depositJLabel = new JLabel();
depositJLabel.setBounds(8, 104, 80, 23);
depositJLabel.setText("Deposit:");
depositJLabel.setBackground(COMPANY_COLOR);
inputDetailJPanel.add(depositJLabel);
// set up DepositJTextField
depositJTextField = new JTextField();
depositJTextField.setBounds(112, 104, 80, 21);
depositJTextField.setHorizontalAlignment(JTextField.RIGHT);
depositJTextField.setBackground(COMPANY_COLOR);
inputDetailJPanel.add(depositJTextField);
// set up WithdrawJLabel
withdrawJLabel = new JLabel();
withdrawJLabel.setBounds(8, 128, 60, 23);
withdrawJLabel.setText("Withdraw:");
withdrawJLabel.setBackground(COMPANY_COLOR);
inputDetailJPanel.add(withdrawJLabel);
// set up WithdrawJTextField
withdrawJTextField = new JTextField();
withdrawJTextField.setBounds(112, 128, 80, 21);
withdrawJTextField.setHorizontalAlignment(JTextField.RIGHT);
withdrawJTextField.setBackground(COMPANY_COLOR);
inputDetailJPanel.add(withdrawJTextField);
// set up CreateAccountButton
createAccountJButton = new JButton();
createAccountJButton.setBounds(112, 152, 80, 24);
createAccountJButton.setText("Create");
createAccountJButton.setBackground(COMPANY_COLOR);
inputDetailJPanel.add(createAccountJButton);
createAccountJButton.addActionListener(
new ActionListener() {
// event handler called when CreateAccountJButton
// is clicked
public void actionPerformed(ActionEvent event) {
createAccountJButtonActionPerformed(event);
}//end method actionPerformed
}//end method ActionListener
); //end call to createAccountJButton ActionListener
// set up DeleteAccountButton
deleteAccountJButton = new JButton();
deleteAccountJButton.setBounds(16, 152, 80, 24);
deleteAccountJButton.setText("Delete");
deleteAccountJButton.setBackground(COMPANY_COLOR);
inputDetailJPanel.add(deleteAccountJButton);
deleteAccountJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when DeleteAccountJButton
// is clicked
public void actionPerformed(ActionEvent event) {
deleteAccountJButtonActionPerformed(event);
}//end method actionPerformed
}//end method ActionListener
); //end call to deleteAccountJButton ActionListener
// set up TransactionJButton
transactionJButton = new JButton();
transactionJButton.setBounds(16, 180, 176, 24);
transactionJButton.setText("Make Transaction");
inputDetailJPanel.add(transactionJButton);
transactionJButton.setBackground(COMPANY_COLOR);
transactionJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when TransactionJButton
// is clicked
public void actionPerformed(ActionEvent event) {
transactionJButtonActionPerformed(event);
}//end method actionPerformed
}//end method ActionListener
); //end call to transactionJButton ActionListener
// set up DisplayJButton
displayJButton = new JButton();
displayJButton.setBounds(16, 208, 176, 24);
displayJButton.setText("Display Accounts");
displayJButton.setBackground(COMPANY_COLOR);
inputDetailJPanel.add(displayJButton);
displayJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when TransactionJButton
// is clicked
public void actionPerformed(ActionEvent event) {
displayJButtonActionPerformed(event);
}//end method actionPerformed
}//end method ActionListener
); //end call to displayJButton ActionListener
// set up displayJLabel
displayJLabel = new JLabel();
displayJLabel.setBounds(240, 16, 150, 23);
displayJLabel.setText("Account Details:");
displayJLabel.setBackground(COMPANY_COLOR);
contentPane.add(displayJLabel);
// set up displayJTextArea
displayJTextArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(displayJTextArea);
scrollPane.setBounds(240,48,402,184);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
contentPane.add(scrollPane);
scrollPane.setBackground(COMPANY_COLOR);
displayJTextArea.setText("Welcome to Java Bank - There are currently no
Accounts created");
displayJTextArea.setText("");
name = "";
//Get accountNum from Text Field and convert to int unless blank then set to 0
if (accountNumJTextField.getText() == "0") {
accountNum = 0;
}
else {
accountNum = Integer.parseInt(accountNumJTextField.getText());
}//endif
//Get Balance from Text Field and convert to int unless blank then set to 0
if (balanceJTextField.getText() == "0") {
balance = 0;
}
else {
balance = Integer.parseInt(balanceJTextField.getText());
}//endif
if (noAccounts == 10) {
// Once account 10 is created. All accounts full.
displayJTextArea.setText("All Accounts Full!");
}
nameJTextField.setText(" ");
accountNumJTextField.setText("0");
balanceJTextField.setText("0");
depositJTextField.setText("0");
withdrawJTextField.setText("0");
displayJTextArea.setText("");
if (noAccounts == 0) {
displayJTextArea.setText("No Accounts currently created");
}else
{
// get user input
int accountNum = Integer.parseInt(accountNumJTextField.getText());
int deposit = Integer.parseInt(depositJTextField.getText());
int withdraw = Integer.parseInt(withdrawJTextField.getText());
name = nameJTextField.getText();
displayJTextArea.setText("");
if (noAccounts == 0) {
displayJTextArea.setText("No Accounts currently created");
}else {
for (int i=0; i<noAccounts; i++) {
displayJTextArea.append(myAccounts[i].getAccountName() + " " +
myAccounts[i].getAccountNum() + " " + myAccounts[i].getBalance() + "\n");
}//endfor
}//endif