1. 4. Here is the Output 5. Please modify my code to fit the above output. impor
ID: 3551437 • Letter: 1
Question
1.
4. Here is the Output
5. Please modify my code to fit the above output.
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class BankGUI extends JFrame
{
public BankGUI()
{
super("King's Bank");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(390, 375);
setLayout(new BorderLayout());
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("George 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);
JTextField 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);
JTextField 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);
JRadioButton saveCheckBox = new JRadioButton("Savings");
topConstraints.insets = new Insets(0, 10, 0, 60);
topConstraints.gridx = 0;
topConstraints.gridy = 4;
mainPanel.add(saveCheckBox, topConstraints);
JRadioButton checkingCheckBox = new JRadioButton("Checking");
topConstraints.insets = new Insets(0, 10, 0, 60);
topConstraints.gridx = 0;
topConstraints.gridy = 5;
mainPanel.add(checkingCheckBox, topConstraints);
JRadioButton depositCheckBox = new JRadioButton("Deposit");
topConstraints.insets = new Insets(0, -40, 0, 0);
topConstraints.gridx = 1;
topConstraints.gridy = 4;
mainPanel.add(depositCheckBox, topConstraints);
JRadioButton withdrawCheckBox = new JRadioButton("Withdraw");
topConstraints.insets = new Insets(0, -40, 0, 0);
topConstraints.gridx = 1;
topConstraints.gridy = 5;
mainPanel.add(withdrawCheckBox, topConstraints);
JRadioButton balanceCheckBox = new JRadioButton("Balance");
topConstraints.insets = new Insets(0, -40, 0, 0);
topConstraints.gridx = 1;
topConstraints.gridy = 6;
mainPanel.add(balanceCheckBox, topConstraints);
JButton createButton = new JButton("Create Account");
topConstraints.insets = new Insets(10, 10, 10, 55);
topConstraints.gridx = 0;
topConstraints.gridy = 7;
mainPanel.add(createButton, topConstraints);
JButton executeButton = new JButton("Execute");
topConstraints.insets = new Insets(5, -40, 5, 10);
topConstraints.gridx = 1;
topConstraints.gridy = 7;
mainPanel.add(executeButton, topConstraints);
JButton eomButton = new JButton("End of Month");
topConstraints.insets = new Insets(5, 10, 3, 55);
topConstraints.gridx = 0;
topConstraints.gridy = 8;
mainPanel.add(eomButton, topConstraints);
JButton reportButton = new JButton("Report");
topConstraints.insets = new Insets(5, -40, 3, 10);
topConstraints.gridx = 1;
topConstraints.gridy = 8;
mainPanel.add(reportButton, topConstraints);
add(mainPanel, BorderLayout.PAGE_START);
setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args)
{
BankGUI KingBank = new BankGUI();
}
}
Explanation / Answer
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
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 BankGUI 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 BankGUI()
{
super("King's Bank");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(390, 375);
setLayout(new BorderLayout());
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("George 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);
group.add(saveCheckBox);
group.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, 0);
topConstraints.gridx = 0;
topConstraints.gridy = 20;
statusArea.setEditable(false);
statusArea.setForeground(Color.black);
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) {
JOptionPane.showMessageDialog(this, "Please enter accountNo and/or amount in correct format", "Error!!", JOptionPane.ERROR_MESSAGE);
}
}
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){
JOptionPane.showMessageDialog(this, "Maximum account limit exceeded!!");
}
else{
accounts.put(accountNo, initialDeposit);
statusArea.setText("New Account #"+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)){
JOptionPane.showMessageDialog(this, "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)){
JOptionPane.showMessageDialog(this, "Account doesn't exists");
}
else{
double oldBal = accounts.get(accountNo);
accounts.put(accountNo, oldBal+deposit);
statusArea.setText(getCurrencyFormat(deposit)+" deposited to Account #"+accountNo);
}
}
private void withdraw(int accountNo,double withdrawl){
if(!accounts.containsKey(accountNo)){
JOptionPane.showMessageDialog(this, "Account doesn't exists");
}
else{
double oldBal = accounts.get(accountNo);
accounts.put(accountNo, oldBal-withdrawl);
statusArea.setText(getCurrencyFormat(withdrawl)+" withdrawn from Account #"+accountNo);
}
}
private void checkBalance(int accountNo){
if(!accounts.containsKey(accountNo)){
JOptionPane.showMessageDialog(this, "Account doesn't exists");
}
else{
double bal = accounts.get(accountNo);
statusArea.setText("Account #"+accountNo+" has "+getCurrencyFormat(bal));
rightTextField.setText(String.valueOf(bal));
}
}
private String getCurrencyFormat(double number){
DecimalFormat format = new DecimalFormat("#.00");
return "$"+format.format(number);
}
/**
* @param args
*/
public static void main(String[] args)
{
BankGUI KingBank = new BankGUI();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.