1. 2. 3. 4. Account class public abstract class Account { int id; double balance
ID: 3552757 • Letter: 1
Question
1.
2.
3.
4.
Account class
public abstract class Account {
int id;
double balance;
public Account(){
id=0;
balance=0;
}
public Account(int newID, double initialBalance){
id = newID;
balance = initialBalance;
}
public void setID(int newID){
id = newID;
}
public int getID(){
return id;
}
public void setbalance(double newBalance){
balance = newBalance;
}
public double getBalance(){
return balance;
}
public void withdraw(double amount){
balance -= amount;
}
public void deposit(double amount){
balance += amount;
}
public abstract void closeMonth();
}
5. Checking class
public class Checking
extends Account
{
private double fee;
private double minimum;
public Checking(int id, double balance, double monthly, double min)
{
super(id, balance);
fee = monthly;
minimum = min;
}
public double getFee(double monthcost)
{
monthcost = fee;
return monthcost;
}
public double getMinimum(double min1)
{
min1 = minimum;
return min1;
}
public void closeMonth()
{
if (getBalance() < minimum) {
balance -= fee;
}
}
}
6. Savings class
public class Savings
extends Account
{
private double interestRate;
private double minBalance;
public Savings(int id, double balance, double interest, double min)
{
super(id, balance);
this.interestRate = interest;
this.minBalance = min;
}
public double getInterestRate(double ir)
{
ir = this.interestRate;
return ir;
}
public double getMinBalance(double minBal)
{
minBal = this.minBalance;
return minBal;
}
public void closeMonth()
{
if (getBalance() > this.minBalance) {
this.balance *= (1.0D + this.interestRate / 12.0D);
}
}
}
7. BankGUI
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
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.ArrayList;
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.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
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;
private ArrayList<Account> accountList = new ArrayList();
private DecimalFormat dollar = new DecimalFormat("$#0.00");
public BankGUI()
{
super("George's Bank");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(390, 455);
setLayout(new BorderLayout());
setLocationRelativeTo(null);
setVisible(true);
JScrollPane scrollpane;
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);
JScrollPane pane = new JScrollPane(statusArea);
statusArea.setBackground(Color.white);
statusArea.setOpaque(true);
pane.setBackground(Color.white);
pane.setOpaque(true);
pane.setMaximumSize(new Dimension(30,300));
pane.setPreferredSize(new Dimension(300, 200));
add(pane);
pane.getViewport().setBackground(Color.WHITE);
//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(new BankGUI2.EndMonthButtonListener());
reportButton.addActionListener(new BankGUI2.ReportButtonListener());
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.append(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.append("Maximum account limit exceeded!!");
}
else{
double amount = Double.parseDouble(rightTextField.getText());
if (BankGUI2.this.checkingCheckBox.isSelected())
{
double minimum = Double.parseDouble(JOptionPane.showInputDialog("Please enter the minimum balance"));
double fee = Double.parseDouble(JOptionPane.showInputDialog("Please enter the monthly fee"));
accountList.add(new Checking(accountNo, amount, fee, minimum));
accounts.put(accountNo, initialDeposit);
statusArea.append(" New Checking account ID #"+accountNo+" created With an initial balance of "+getCurrencyFormat(accounts.get(accountNo)));
leftTextField.setText(String.valueOf(accountNo));
}
else if (BankGUI2.this.saveCheckBox.isSelected())
{
double minimum = Double.parseDouble(JOptionPane.showInputDialog("Please enter the minimum balance"));
double rate = Double.parseDouble(JOptionPane.showInputDialog("Please enter the annual interest rate"));
accountList.add(new Savings(accountNo, amount, rate, minimum));
accounts.put(accountNo, initialDeposit);
statusArea.append(" New Savings account ID #"+accountNo+" created With an initial balance of "+getCurrencyFormat(accounts.get(accountNo)));
leftTextField.setText(String.valueOf(accountNo));
}
else
{
statusArea.append(" Please Select Savings or Checking");
}
}
}
private void adjustBalance(int accountNo,double newBalance){
if(!accounts.containsKey(accountNo)){
statusArea.append(" Account doesn't exists");
}
else{
accounts.put(accountNo, newBalance);
statusArea.append(" New Balance of Account #"+accountNo+" is "+getCurrencyFormat(newBalance));
}
}
private void deposit(int accountNo,double deposit){
if(!accounts.containsKey(accountNo)){
statusArea.append(" 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.append(" Account doesn't exists");
}
else{
double oldBal = accounts.get(accountNo);
if(oldBal-withdrawl<0){
statusArea.append(" 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.append(" Account doesn't exists");
}
else{
double bal = accounts.get(accountNo);
statusArea.append(" The Current balance in account #"+accountNo+" is "+getCurrencyFormat(bal));
}
}
private String getCurrencyFormat(double number){
DecimalFormat format = new DecimalFormat("#0.00");
return "$"+format.format(number);
}
private class EndMonthButtonListener
implements ActionListener
{
private EndMonthButtonListener() {}
public void actionPerformed(ActionEvent e)
{
for (Account type : BankGUI.this.accountList) {
type.closeMonth();
}
statusArea.append(" End of month processing completed");
}
}
private class ReportButtonListener
implements ActionListener
{
private ReportButtonListener() {}
public void actionPerformed(ActionEvent e)
{
String Reportbutton = "";
for (Account type : accountList)
{
int accountNo = type.getID();
String Reporttypes;
if ((type instanceof Savings)) {
Reporttypes = "Savings ";
} else {
Reporttypes = "Checking ";
}
Reportbutton = Reportbutton + " " + Reporttypes + "account #"+accountNo+" has a balance of "+getCurrencyFormat(accounts.get(accountNo));
}
statusArea.append(Reportbutton);
}
}
public static void main(String[] args)
{
BankGUI KingBank = new BankGUI();
Font f = new Font(KingBank.getFont().getName(),Font.BOLD, KingBank.getFont().getSize());
KingBank.statusArea.setFont(f);
}
Explanation / Answer
I have completed your work. It's working perfectly now. Just mail me : vanway555@gmail.com
I will provide the solution files separately in a zip there. And you just need to rate me here :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.