1. 2. 3. My Code: import java.awt.BorderLayout; import java.awt.Color; import ja
ID: 3551965 • Letter: 1
Question
1.
2.
3.
My Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class BankGUI2 extends JFrame implements ActionListener{
private Map<Integer,Double> accounts = new HashMap<Integer,Double>();
private JTextField leftTextField;
private JTextField rightTextField;
private JRadioButton saveCheckBox;
private JRadioButton checkingCheckBox;
private JRadioButton depositCheckBox;
private JRadioButton withdrawCheckBox;
private JRadioButton balanceCheckBox;
private JButton createButton;
private JButton executeButton;
private JButton eomButton;
private JButton reportButton;
private JTextArea statusArea;
public BankGUI2()
{
super("George's Bank");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(390, 375);
setLayout(new BorderLayout());
setVisible(true);
JPanel mainPanel = new JPanel(new GridBagLayout());
GridBagConstraints topConstraints = new GridBagConstraints();
topConstraints.insets = new Insets(5, 10, 3, 5);
topConstraints.gridx = 0;
topConstraints.gridy = 0;
topConstraints.weightx = 1;
topConstraints.gridwidth = 2;
JLabel topLabelLine1 = new JLabel("King Bank", JLabel.CENTER);
JLabel topLabelLine2 = new JLabel("Fort Worth, Texas", JLabel.CENTER);
mainPanel.add(topLabelLine1, topConstraints);
topConstraints.insets = new Insets(-2, 10, -5, 5);
topConstraints.gridy = 1;
mainPanel.add(topLabelLine2, topConstraints);
leftTextField = new JTextField();
topConstraints.insets = new Insets(20, 10, 3, 55);
topConstraints.gridwidth = 1;
topConstraints.fill = GridBagConstraints.HORIZONTAL;
topConstraints.gridx = 0;
topConstraints.gridy = 2;
mainPanel.add(leftTextField, topConstraints);
rightTextField = new JTextField();
topConstraints.insets = new Insets(20, -42, 3, 10);
topConstraints.gridx = 1;
topConstraints.gridy = 2;
mainPanel.add(rightTextField, topConstraints);
JLabel acctLabel = new JLabel("Account ID", JLabel.CENTER);
topConstraints.insets = new Insets(0, 0, 0, 45);
topConstraints.gridx = 0;
topConstraints.gridy = 3;
mainPanel.add(acctLabel, topConstraints);
JLabel amtLabel = new JLabel("Amount", JLabel.CENTER);
topConstraints.gridx = 1;
topConstraints.gridy = 3;
mainPanel.add(amtLabel, topConstraints);
saveCheckBox = new JRadioButton("Savings");
topConstraints.insets = new Insets(0, 10, 0, 60);
topConstraints.gridx = 0;
topConstraints.gridy = 4;
mainPanel.add(saveCheckBox, topConstraints);
checkingCheckBox = new JRadioButton("Checking");
topConstraints.insets = new Insets(0, 10, 0, 60);
topConstraints.gridx = 0;
topConstraints.gridy = 5;
mainPanel.add(checkingCheckBox, topConstraints);
depositCheckBox = new JRadioButton("Deposit");
topConstraints.insets = new Insets(0, -40, 0, 0);
topConstraints.gridx = 1;
topConstraints.gridy = 4;
mainPanel.add(depositCheckBox, topConstraints);
withdrawCheckBox = new JRadioButton("Withdraw");
topConstraints.insets = new Insets(0, -40, 0, 0);
topConstraints.gridx = 1;
topConstraints.gridy = 5;
mainPanel.add(withdrawCheckBox, topConstraints);
balanceCheckBox = new JRadioButton("Balance");
topConstraints.insets = new Insets(0, -40, 0, 0);
topConstraints.gridx = 1;
topConstraints.gridy = 6;
mainPanel.add(balanceCheckBox, topConstraints);
ButtonGroup group = new ButtonGroup();
group.add(depositCheckBox);
group.add(withdrawCheckBox);
group.add(balanceCheckBox);
ButtonGroup creategroup = new ButtonGroup();
creategroup.add(saveCheckBox);
creategroup.add(checkingCheckBox);
createButton = new JButton("Create Account");
topConstraints.insets = new Insets(10, 10, 10, 55);
topConstraints.gridx = 0;
topConstraints.gridy = 7;
mainPanel.add(createButton, topConstraints);
executeButton = new JButton("Execute");
topConstraints.insets = new Insets(5, -40, 5, 10);
topConstraints.gridx = 1;
topConstraints.gridy = 7;
mainPanel.add(executeButton, topConstraints);
eomButton = new JButton("End of Month");
topConstraints.insets = new Insets(5, 10, 3, 55);
topConstraints.gridx = 0;
topConstraints.gridy = 8;
mainPanel.add(eomButton, topConstraints);
reportButton = new JButton("Report");
topConstraints.insets = new Insets(5, -40, 3, 10);
topConstraints.gridx = 1;
topConstraints.gridy = 8;
mainPanel.add(reportButton, topConstraints);
statusArea = new JTextArea();
topConstraints.insets = new Insets(5, 5, 3, -500);
topConstraints.gridx = 0;
topConstraints.gridy = 30;
statusArea.setEditable(false);
statusArea.setForeground(Color.black);
statusArea.setBounds(30, 30, 30, 30);
statusArea.setSize(300, 300);
statusArea.setBackground(mainPanel.getBackground());
mainPanel.add(statusArea,topConstraints);
add(mainPanel, BorderLayout.PAGE_START);
//Adding listeners
leftTextField.addActionListener(this);
rightTextField.addActionListener(this);
saveCheckBox.addActionListener(this);
checkingCheckBox.addActionListener(this);
depositCheckBox.addActionListener(this);
withdrawCheckBox.addActionListener(this);
balanceCheckBox.addActionListener(this);
createButton.addActionListener(this);
executeButton.addActionListener(this);
eomButton.addActionListener(this);
reportButton.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
Object action = e.getSource();
String amount = rightTextField.getText();
String accountNo = leftTextField.getText();
int accNo;
double amountDouble;
try{
if(action.equals(createButton)){
createAccount(Double.parseDouble(amount));
}
if(action.equals(executeButton) && depositCheckBox.isSelected()){
accNo = Integer.parseInt(accountNo);
amountDouble = Double.parseDouble(amount);
deposit(accNo, amountDouble);
}
else if(action.equals(executeButton) && withdrawCheckBox.isSelected()){
accNo = Integer.parseInt(accountNo);
amountDouble = Double.parseDouble(amount);
withdraw(accNo, amountDouble);
}
else if(action.equals(executeButton) && balanceCheckBox.isSelected()){
accNo = Integer.parseInt(accountNo);
checkBalance(accNo);
}
else if(action.equals(executeButton)){
accNo = Integer.parseInt(accountNo);
amountDouble = Double.parseDouble(amount);
adjustBalance(accNo, amountDouble);
}
}
catch (Exception ex) {
statusArea.setText(accountNo + " is an invalid account number");
}
}
private void createAccount(double initialDeposit){
int accountNo;
int largest = 0;
for(int x: accounts.keySet()){
if(x>largest)
largest = x;
}
accountNo = largest+1;
if(accountNo>9){
statusArea.setText("Maximum account limit exceeded!!");
}
else{
accounts.put(accountNo, initialDeposit);
statusArea.setText("New Account ID #"+accountNo+" created With an initial balance of "+getCurrencyFormat(accounts.get(accountNo)));
leftTextField.setText(String.valueOf(accountNo));
}
}
private void adjustBalance(int accountNo,double newBalance){
if(!accounts.containsKey(accountNo)){
statusArea.setText("Account doesn't exists");
}
else{
accounts.put(accountNo, newBalance);
statusArea.setText("New Balance of Account #"+accountNo+" is "+getCurrencyFormat(newBalance));
}
}
private void deposit(int accountNo,double deposit){
if(!accounts.containsKey(accountNo)){
statusArea.setText("Account doesn't exists");
}
else{
double oldBal = accounts.get(accountNo);
accounts.put(accountNo, oldBal+deposit);
statusArea.setText("Deposited " + getCurrencyFormat(deposit) + " into account #" + accountNo +" Current Balance is "+getCurrencyFormat(accounts.get(accountNo)));
}
}
private void withdraw(int accountNo,double withdrawl){
if(!accounts.containsKey(accountNo)){
statusArea.setText("Account doesn't exists");
}
else{
double oldBal = accounts.get(accountNo);
if(oldBal-withdrawl<0){
statusArea.setText("Not much amount left in Account #"+accountNo+" Check Balance");
}
else{
accounts.put(accountNo, oldBal-withdrawl);
statusArea.setText("Withdrew " + getCurrencyFormat(withdrawl) + " from account #" + accountNo +" Current Balance is "+getCurrencyFormat(accounts.get(accountNo)));
}
}
}
private void checkBalance(int accountNo){
if(!accounts.containsKey(accountNo)){
statusArea.setText("Account doesn't exists");
}
else{
double bal = accounts.get(accountNo);
statusArea.setText("The Current balance in account #"+accountNo+" is "+getCurrencyFormat(bal));
}
}
private String getCurrencyFormat(double number){
DecimalFormat format = new DecimalFormat("#0.00");
return "$"+format.format(number);
}
public static void main(String[] args)
{
BankGUI2 KingBank = new BankGUI2();
Font f = new Font(KingBank.getFont().getName(),Font.BOLD, KingBank.getFont().getSize());
KingBank.statusArea.setFont(f);
}
}
Explanation / Answer
When is it due ?
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.