modify the CheckingAccount class again, to become a simple derived class of the
ID: 3822604 • Letter: M
Question
modify the CheckingAccount class again, to become a simple derived class of the parent class Account. The Account class contains the instance variables String name (the name of the person who owns the account) and double balance (the current balance of the account). There should be appropriate get methods in Account class to get the name and balance fields. The account name should appear in all reports and dialogue. The CheckingAccount class will still have the instance variables double totalServiceCharges, and Transaction [ ] transList (or ArrayListtransList). Also, create a Check class, a simple derived class of the Transaction class. The Check class will include the check number for each check transaction. The check number will be prompted for as the check is entered, and displayed in the checks listing. Also, create a Deposit class, a simple derived class of the Transaction class. The Deposit class will include the cash amount and check amount that total to the transaction amount. The cash and check amount for each deposit will be input in a new Deposit input screen. (See sample run below.) In this application’s design, you should have the following classes: 1. The Main class: This creates the frame, adds the panel, and sets it visible. This program is event-driven. You cannot put all the processing steps/logic in main method. However, you can put them in helper methods in the Main class. 2. The CheckOptionsPanel class: This creates the GUI components, adds listeners, and adds to the Panel. This is the class where most of the action (driven by events) takes place. You might want to put the processing methods (to be called by the listeners) in this class, along with the declaration of the CheckingAccount instance that you are processing. 3. The CheckingAccount class: This defines the CheckingAccount object. (Please keep this class methods simple. This is the file cabinet object which will be written to a file in a later assignment.) 4. The Account class: This serves as a Parent to the CheckingAccount class. 5. The Transaction class: This defines the transactions stored in the array inside the CheckingAccount objects. It is the parent of the Check class. 6. The Check class: This defines the check number and get method for processing the check number for each check transaction. 7. The Deposit class: This defines the cash and check amounts that comprise the total deposit transaction amount.
Here is what I have so far
------------
Main.java
------------
package accounting;
/**
*
* @author Ares
*/
import javax.swing.JFrame;
public class Main
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Checking Account Actions");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CheckingAccountActions panel = new CheckingAccountActions();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
-------------------
CheckingAccountActions.Java
--------------------
package accounting;
/**
*
* @author Ares
*/
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import javax.swing.*;
public class CheckingAccountActions extends JPanel
{
public static double initialBalance, transactionAmount;
public static int firstTime = 0;
static CheckingAccount ca;
static DecimalFormat dollar;
static Transaction t;
public static int check = 1, deposit = 2, serviceCharge = 3;
private JLabel message;
private JRadioButton transaction, listTrans, checks, deposits;
ArrayList transList;
public CheckingAccountActions()
{
initialBalance = initialBalance();
ca = new CheckingAccount(initialBalance);
dollar = new DecimalFormat("#,###.00");
message = new JLabel ("Choose an action: ");
message.setFont (new Font ("Helvetica", Font.BOLD, 24));
transaction = new JRadioButton("Entering a Transaction");
transaction.setBackground (Color.yellow);
listTrans = new JRadioButton("Listing All Transactions");
listTrans.setBackground(Color.yellow);
checks = new JRadioButton("Listing All Checks");
checks.setBackground(Color.yellow);
deposits = new JRadioButton("Listing All Deposits");
deposits.setBackground(Color.yellow);
ButtonGroup group = new ButtonGroup();
group.add(transaction);
group.add(listTrans);
group.add(checks);
group.add(deposits);
CheckingAccountActionsListener listener = new CheckingAccountActionsListener();
transaction.addActionListener(listener);
listTrans.addActionListener(listener);
checks.addActionListener(listener);
deposits.addActionListener(listener);
add(message);
add(transaction);
add(listTrans);
add(checks);
add(deposits);
setBackground(Color.YELLOW);
setPreferredSize (new Dimension(250, 180));
}
private class CheckingAccountActionsListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
int transactionCode;
double transAmt = 0;
int i = 0;
if(source==transaction)
{
transactionCode = getTransCode();
t = new Transaction(ca.getTransCount(),transactionCode,transAmt);
ca.addTrans(t);
switch(transactionCode)
{
case 1:
transAmt = getTransAmt();
ca.setBalance(transAmt, transactionCode);
processCheck(transAmt);
JOptionPane.showMessageDialog(null, transList.get(i));
break;
case 2:
transAmt = getTransAmt();
ca.setBalance(transAmt, transactionCode);
processDeposit(transAmt);
JOptionPane.showMessageDialog(null, transList.get(i));
break;
case 0:
JOptionPane.showMessageDialog(null, "Transaction: End "
+ "Current Balance: $" + ca.getBalance() + " "
+ "Total Service Charge: $" + dollar.format(ca.getServiceCharge()) + " "
+ "Final Balance: $"
+ dollar.format(ca.getBalance() - ca.getServiceCharge()));
break;
default:
JOptionPane.showMessageDialog(null, "Invalid Choice. Enter Again.");
}
}
else if(source==listTrans)
{
int types = 0;
int nums = 0;
double amount = 0;
String line;
line = String.format("List All Transactions" + " "
+ "ID Type Amount" + " ");
for(int index = 0; index < ca.getSize(); index++)
{
types = ca.getTrans(index).getTransId();
nums = ca.getTrans(index).getTransNumber();
amount = ca.getTrans(index).getTransAmount();
line += String.format("%-10s %3d %10s", nums,types,amount) + " ";
}
JTextArea text = new JTextArea(line);
text.setBorder(null);
text.setOpaque(false);
text.setFont(new Font("Monospaced", Font.PLAIN, 14));
JOptionPane.showMessageDialog(null, text);
}
else if(source==checks)
{
String line = String.format("Checks made: ");
JTextArea text = new JTextArea(line);
text.setBorder(null);
text.setOpaque(false);
text.setFont(new Font("Monospaced", Font.PLAIN, 14) );
JOptionPane.showMessageDialog(null, text);
}
else if(source==deposits)
{
String line = String.format("Deposits made: ");
JTextArea text = new JTextArea(line);
text.setBorder(null);
text.setOpaque(false);
text.setFont(new Font("Monospaced", Font.PLAIN, 14) );
JOptionPane.showMessageDialog(null, text);
}
}
}
public static int getTransCode()
{
int code;
String userInput;
userInput=JOptionPane.showInputDialog("Enter the transaction code: "+
"1) Check 2) Deposit 0) Exit the program");
code=Integer.parseInt(userInput);
return code;
}
public static double initialBalance()
{
double initialBalance;
String userInput;
userInput = JOptionPane.showInputDialog("Enter initial account balance :");
initialBalance = Double.parseDouble(userInput);
return initialBalance;
}
public static double getTransAmt()
{
double amount;
String userInput;
userInput=JOptionPane.showInputDialog("Enter the transaction amount: ");
amount=Double.parseDouble(userInput);
return amount;
}
public static double processCheck(double transAmt)
{
ca.setBalance(transAmt, 1);
ca.setServiceCharge(0.15);
if(ca.getBalance()<500)
{
if(firstTime==0)
{
ca.setServiceCharge(5.00);
JOptionPane.showMessageDialog(null,"Transaction: Check in Amount of $"
+transactionAmount+ " " +"Current Balance: $" + dollar.format(ca.getBalance())
+ " "+"Service Charge: Check --- charge $0.15 "
+ "Service Charge: Below $500 --- charge $5.00 "
+ "Total Service Charge: $" + dollar.format(ca.getServiceCharge()) );
firstTime++;
}
JOptionPane.showMessageDialog(null,"Transaction: Check in Amount of $"
+ transactionAmount + " " +"Current Balance: $" + dollar.format(ca.getBalance())
+ " "+"Service Charge: Check --- charge $0.15 "
+ "Total Service Charge: $" + dollar.format(ca.getServiceCharge()) );
}
else
{
JOptionPane.showMessageDialog(null,"Transaction: Check in Amount of $"
+ transactionAmount + " " + "Current Balance: $"
+ dollar.format(ca.getBalance())
+ " "+"Service Charge: Check --- charge $0.15 "
+ "Service Charge: None " + "Total Service Charge: $"
+ dollar.format(ca.getServiceCharge()) );
}
if(ca.getBalance()<0)
{
ca.setServiceCharge(10.00);
JOptionPane.showMessageDialog(null, "Your balance is under $0.");
}
if(ca.getBalance()<50)
{
JOptionPane.showMessageDialog(null, "Your balance is under $50.");
}
return transAmt;
}
public static double processDeposit(double transAmt)
{
ca.setBalance(transAmt, 2);
ca.setServiceCharge(0.10);
if(ca.getBalance()<500)
{
if(firstTime==0)
{
ca.setServiceCharge(5.00);
JOptionPane.showMessageDialog(null,"Transaction Amount: Deposit in Amount of "
+ transactionAmount + " " +"Current Balance: "
+ dollar.format(ca.getBalance())
+ " "+"Service Charge: Deposit --- charge $0.10 "
+ "Service Charge: Below $500 --- charge $5.00 "
+ "Total Service Charge: " + dollar.format(ca.getServiceCharge()));
}
JOptionPane.showMessageDialog(null,"Transaction Amount: Deposit in Amount of "
+ transactionAmount + " " +"Current Balance: "
+ dollar.format(ca.getBalance())
+ " "+"Service Charge: Deposit --- charge $0.10 "
+ "Total Service Charge: " + dollar.format(ca.getServiceCharge()));
}
else
{
JOptionPane.showMessageDialog(null,"Transaction Amount: Deposit in Amount of "
+ transactionAmount + " " +"Current Balance: "
+ dollar.format(ca.getBalance())
+ " "+"Service Charge: Deposit --- charge $0.10 "
+ "Service Charge: None " + "Total Service Charge: "
+ dollar.format(ca.getServiceCharge()));
}
if(ca.getBalance()<0)
{
ca.setServiceCharge(10.00);
JOptionPane.showMessageDialog(null, "Your balance is under $0.");
}
if(ca.getBalance()<50)
{
JOptionPane.showMessageDialog(null, "Your balance is under $50.");
}
return transAmt;
}
}
--------------------
CheckingAccount.java
-------------------
package accounting;
/**
*
* @author Natnael Cherenet
*/
import java.util.ArrayList;
public class CheckingAccount
{
private double balance;
private double totalServiceCharge;
public ArrayList<Transaction> transList = new ArrayList<Transaction>();
private int transCount = 0;
private int transSize = 0;
public CheckingAccount(double initialBalance)
{
balance = initialBalance;
totalServiceCharge = 0;
}
public double getBalance()
{
return balance;
}
public void setBalance(double transAmt, int tCode)
{
if(tCode==1)
balance-=0.5*transAmt;
else if(tCode==2)
balance+=0.5*transAmt;
}
public double getServiceCharge()
{
return totalServiceCharge;
}
public void setServiceCharge(double currentServiceCharge)
{
totalServiceCharge += currentServiceCharge;
}
public void addTrans(Transaction newTrans)
{
transList.add(newTrans);
}
public int getTransCount()
{
transCount++;
return transCount;
}
---------------
Transaction.Java
----------
package accounting;
/**
*
* @author Ares
*/
public class Transaction
{
private int transNumber;
private int transId;
private double transAmt;
public Transaction(int number, int id, double amount)
{
transNumber = number;
transId = id;
transAmt = amount;
}
public int getTransNumber()
{
return transNumber;
}
public int getTransId()
{
return transId;
}
public double getTransAmount()
{
return transAmt;
}
}
Explanation / Answer
Added Account, Check and Deposit classes and added mentioned fields, corrected compilation errors and added code to list all checks and deposits correctly with out error. I have commented code which was not working correctly.Added comment as //Added New for newly added code.
Changed name of CheckingAccountActions.java to CheckOptionsPanel.java as mentioned in description
Account.java
public class Account {
private String name;
private double balance;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
CheckingAccount.java
/**
*
* @author Natnael Cherenet
*/
/**
*
* @author Natnael Cherenet
*/
import java.util.ArrayList;
public class CheckingAccount extends Account
{
private double balance;
private double totalServiceCharge;
public ArrayList<Transaction> transList = new ArrayList<Transaction>();
private int transCount = 0;
public CheckingAccount(double initialBalance) {
balance = initialBalance;
totalServiceCharge = 0;
}
public double getBalance() {
return balance;
}
public void setBalance(double transAmt, int tCode) {
if (tCode == 1)
balance -= 0.5 * transAmt;
else if (tCode == 2)
balance += 0.5 * transAmt;
}
public double getServiceCharge() {
return totalServiceCharge;
}
public void setServiceCharge(double currentServiceCharge) {
totalServiceCharge += currentServiceCharge;
}
public void addTrans(Transaction newTrans) {
transList.add(newTrans);
}
public int getTransCount() {
transCount++;
return transCount;
}
//Added New
public ArrayList<Transaction> getTransList() {
return transList;
}
}
Check.java
public class Check extends Transaction {
private int checkNumber;
public Check(int number, int id, double amount) {
super(number, id, amount);
// TODO Auto-generated constructor stub
}
public int getCheckNumber() {
return checkNumber;
}
public void setCheckNumber(int checkNumber) {
this.checkNumber = checkNumber;
}
}
Deposit.java
public class Deposit extends Transaction {
private double cashAmount;
private double checkAmount;
public Deposit(int number, int id, double amount) {
super(number, id, amount);
// TODO Auto-generated constructor stub
}
public double getCashAmount() {
return cashAmount;
}
public void setCashAmount(double cashAmount) {
this.cashAmount = cashAmount;
}
public double getCheckAmount() {
return checkAmount;
}
public void setCheckAmount(double checkAmount) {
this.checkAmount = checkAmount;
}
}
Transaction.java
/**
*
* @author Ares
*/
public class Transaction
{
private int transNumber;
private int transId;
private double transAmt;
public Transaction(int number, int id, double amount)
{
transNumber = number;
transId = id;
transAmt = amount;
}
public int getTransNumber()
{
return transNumber;
}
public int getTransId()
{
return transId;
}
public double getTransAmount()
{
return transAmt;
}
}
CheckOptionsPanel.java
/**
*
* @author Ares
*/
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import javax.swing.*;
public class CheckOptionsPanel extends JPanel
{
public static double initialBalance, transactionAmount;
public static int firstTime = 0;
static CheckingAccount ca;
static DecimalFormat dollar;
static Transaction trans;
public static int check = 1, deposit = 2, serviceCharge = 3;
private JLabel message;
private JRadioButton transaction, listTrans, checks, deposits;
ArrayList<Transaction> transList;
public CheckOptionsPanel()
{
initialBalance = initialBalance();
ca = new CheckingAccount(initialBalance);
dollar = new DecimalFormat("#,###.00");
message = new JLabel ("Choose an action: ");
message.setFont (new Font ("Helvetica", Font.BOLD, 24));
transaction = new JRadioButton("Entering a Transaction");
transaction.setBackground (Color.yellow);
listTrans = new JRadioButton("Listing All Transactions");
listTrans.setBackground(Color.yellow);
checks = new JRadioButton("Listing All Checks");
checks.setBackground(Color.yellow);
deposits = new JRadioButton("Listing All Deposits");
deposits.setBackground(Color.yellow);
ButtonGroup group = new ButtonGroup();
group.add(transaction);
group.add(listTrans);
group.add(checks);
group.add(deposits);
CheckingAccountActionsListener listener = new CheckingAccountActionsListener();
transaction.addActionListener(listener);
listTrans.addActionListener(listener);
checks.addActionListener(listener);
deposits.addActionListener(listener);
add(message);
add(transaction);
add(listTrans);
add(checks);
add(deposits);
setBackground(Color.YELLOW);
setPreferredSize (new Dimension(250, 180));
}
private class CheckingAccountActionsListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
int transactionCode;
double transAmt = 0;
if(source==transaction)
{
transactionCode = getTransCode();
// t = new Transaction(ca.getTransCount(),transactionCode,transAmt);
switch(transactionCode)
{
case 1:
transAmt = getTransAmt();
ca.setBalance(transAmt, transactionCode);
processCheck(transAmt);
trans=new Check(ca.getTransCount(), transactionCode, transAmt);//Added new
ca.addTrans(trans);//moved to down to set transaction type
// JOptionPane.showMessageDialog(null, transList.get(i));
break;
case 2:
transAmt = getTransAmt();
ca.setBalance(transAmt, transactionCode);
processDeposit(transAmt);
trans=new Deposit(ca.getTransCount(), transactionCode, transAmt);//Added
ca.addTrans(trans);//moved to down to set transaction type
// JOptionPane.showMessageDialog(null, transList.get(i));
break;
case 0:
JOptionPane.showMessageDialog(null, "Transaction: End "
+ "Current Balance: $" + ca.getBalance() + " "
+ "Total Service Charge: $" + dollar.format(ca.getServiceCharge()) + " "
+ "Final Balance: $"
+ dollar.format(ca.getBalance() - ca.getServiceCharge()));
break;
default:
JOptionPane.showMessageDialog(null, "Invalid Choice. Enter Again.");
}
}
else if(source==listTrans)
{
int types = 0;
int nums = 0;
double amount = 0;
String line;
line = String.format("List All Transactions" + " "
+ "ID Type Amount" + " ");
//Newly Added
for (Transaction transaction : ca.getTransList()) {
types=transaction.getTransId();
nums=transaction.getTransNumber();
amount=transaction.getTransAmount();
line += String.format("%-10s %3d %10s", nums,types,amount) + " ";
}
/* for(int index = 0; index < ca.getTransList().size(); index++)
{
types = ca.getTransList(index).getTransId();
nums = ca.getTransList(index).getTransNumber();
amount = ca.getTransList(index).getTransAmount();
line += String.format("%-10s %3d %10s", nums,types,amount) + " ";
}*/
JTextArea text = new JTextArea(line);
text.setBorder(null);
text.setOpaque(false);
text.setFont(new Font("Monospaced", Font.PLAIN, 14));
JOptionPane.showMessageDialog(null, text);
}
else if(source==checks)
{
String line = String.format("Checks Made" + " "
+ "ID Type Amount" + " ");
JTextArea text = new JTextArea(line);
text.setBorder(null);
text.setOpaque(false);
text.setFont(new Font("Monospaced", Font.PLAIN, 14) );
line = String.format("List All Transactions" + " "
+ "ID Type Amount" + " ");
int types = 0;
int nums = 0;
double amount = 0;
//Newly Added
for (Transaction transaction : ca.getTransList()) {
if(transaction instanceof Check){
types=transaction.getTransId();
nums=transaction.getTransNumber();
amount=transaction.getTransAmount();
line += String.format("%-10s %3d %10s", nums,types,amount) + " ";
}
}
JOptionPane.showMessageDialog(null, line);
}
else if(source==deposits)
{
String line = String.format("Deposits made: "
+ "ID Type Amount" + " ");
JTextArea text = new JTextArea(line);
text.setBorder(null);
text.setOpaque(false);
text.setFont(new Font("Monospaced", Font.PLAIN, 14) );
int types = 0;
int nums = 0;
double amount = 0;
//Newly Added
for (Transaction transaction : ca.getTransList()) {
if(transaction instanceof Deposit){
types=transaction.getTransId();
nums=transaction.getTransNumber();
amount=transaction.getTransAmount();
line += String.format("%-10s %3d %10s", nums,types,amount) + " ";
}
}
JOptionPane.showMessageDialog(null, line);
}
}
}
public static int getTransCode()
{
int code;
String userInput;
userInput=JOptionPane.showInputDialog("Enter the transaction code: "+
"1) Check 2) Deposit 0) Exit the program");
code=Integer.parseInt(userInput);
return code;
}
public static double initialBalance()
{
double initialBalance;
String userInput;
userInput = JOptionPane.showInputDialog("Enter initial account balance :");
initialBalance = Double.parseDouble(userInput);
return initialBalance;
}
public static double getTransAmt()
{
double amount;
String userInput;
userInput=JOptionPane.showInputDialog("Enter the transaction amount: ");
amount=Double.parseDouble(userInput);
transactionAmount=amount;//Newly added
return amount;
}
public static double processCheck(double transAmt)
{
ca.setBalance(transAmt, 1);
ca.setServiceCharge(0.15);
if(ca.getBalance()<500)
{
if(firstTime==0)
{
ca.setServiceCharge(5.00);
JOptionPane.showMessageDialog(null,"Transaction: Check in Amount of $"
+transactionAmount+ " " +"Current Balance: $" + dollar.format(ca.getBalance())
+ " "+"Service Charge: Check --- charge $0.15 "
+ "Service Charge: Below $500 --- charge $5.00 "
+ "Total Service Charge: $" + dollar.format(ca.getServiceCharge()) );
firstTime++;
}
JOptionPane.showMessageDialog(null,"Transaction: Check in Amount of $"
+ transactionAmount + " " +"Current Balance: $" + dollar.format(ca.getBalance())
+ " "+"Service Charge: Check --- charge $0.15 "
+ "Total Service Charge: $" + dollar.format(ca.getServiceCharge()) );
}
else
{
JOptionPane.showMessageDialog(null,"Transaction: Check in Amount of $"
+ transactionAmount + " " + "Current Balance: $"
+ dollar.format(ca.getBalance())
+ " "+"Service Charge: Check --- charge $0.15 "
+ "Service Charge: None " + "Total Service Charge: $"
+ dollar.format(ca.getServiceCharge()) );
}
if(ca.getBalance()<50 && ca.getBalance()>0)//Updated
{
JOptionPane.showMessageDialog(null, "Your balance is under $50.: "+ca.getBalance());//updated to display balance
}
if(ca.getBalance()<0)
{
ca.setServiceCharge(10.00);
JOptionPane.showMessageDialog(null, "Your balance is under $0. "+ca.getBalance());//updated to display balance
}
return transAmt;
}
public static double processDeposit(double transAmt)
{
ca.setBalance(transAmt, 2);
ca.setServiceCharge(0.10);
if(ca.getBalance()<500)
{
if(firstTime==0)
{
ca.setServiceCharge(5.00);
JOptionPane.showMessageDialog(null,"Transaction Amount: Deposit in Amount of "
+ transactionAmount + " " +"Current Balance: "
+ dollar.format(ca.getBalance())
+ " "+"Service Charge: Deposit --- charge $0.10 "
+ "Service Charge: Below $500 --- charge $5.00 "
+ "Total Service Charge: " + dollar.format(ca.getServiceCharge()));
}
JOptionPane.showMessageDialog(null,"Transaction Amount: Deposit in Amount of "
+ transactionAmount + " " +"Current Balance: "
+ dollar.format(ca.getBalance())
+ " "+"Service Charge: Deposit --- charge $0.10 "
+ "Total Service Charge: " + dollar.format(ca.getServiceCharge()));
}
else
{
JOptionPane.showMessageDialog(null,"Transaction Amount: Deposit in Amount of "
+ transactionAmount + " " +"Current Balance: "
+ dollar.format(ca.getBalance())
+ " "+"Service Charge: Deposit --- charge $0.10 "
+ "Service Charge: None " + "Total Service Charge: "
+ dollar.format(ca.getServiceCharge()));
}
if(ca.getBalance()<0)
{
ca.setServiceCharge(10.00);
JOptionPane.showMessageDialog(null, "Your balance is under $0.");
}
if(ca.getBalance()<50)
{
JOptionPane.showMessageDialog(null, "Your balance is under $50.");
}
return transAmt;
}
}
Main.java
/**
*
* @author Ares
*/
import javax.swing.JFrame;
public class Main
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Checking Account Actions");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CheckOptionsPanel panel = new CheckOptionsPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.