Write a class with methods to help you balance your checking account (an object
ID: 3838815 • Letter: W
Question
Write a class with methods to help you balance your checking account (an object class-main method is not in this class). The CheckingAccount Class should have at least two instance variables: the balance and the total service charges, along with methods to get and set each instance variable. You may add other variables and methods if you like. The program should read the initial balance for the month, followed by a series of transactions. For each transaction entered, the program should display the transaction data, the current balance for the account, and the total service charges. Service charges are $0.10 for a deposit and $0.15 for a check. If a check forces the balance to drop below $500.00 at any point during the month, a service charge of $5.00 is assessed but only for the first time this happens. Anytime the balance drops below $50.00, the program should print a warning message. If a check results in a negative balance, an additional service charge of $10.00 should be assessed. A transaction takes the form of an int number (the transaction code), followed by a double number (the transaction amount). If the int number is a 1, then the double number is the amount of a check. If the int number is 2, then the double number is the amount of a deposit. The last transaction is 0 with no number to follow it. Use the JOptionPane to produce the dialog. A sample Input/Output dialogue is provided via the Sample Run Link below. Use proper style and indentation, efficient programming techniques and meaningful variable and method names according to Java conventions.
Code Template The following code template will help with this assignment:
----------------- Main.java ------------------------
public class Main { // global variables // define a CheckingAccount object to keep trach of the // account information public static void main (String[] args) { // defines local variables CS 3 – Spring 2017 E. Ambrosio 2 // get initial balance from the user // perform in a loop until the transaction code = 0 // get the transaction code from the user // and process it with appropriate helper method // When loop ends show final balance to user. } public static __________ getTransCode() { } public static _________ getTransAmt() { } public static __________ processCheck(___________) { } public static __________ processDeposit(___________) { } } --------------------CheckingAccount.java ------------------------- public class CheckingAccount { private double balance; private double totalServiceCharge; public CheckingAccount(double initialBalance) { balance = ______________________; totalServiceCharge = ______________; } public ____________ getBalance() { return _______________; } CS 3 – Spring 2017 E. Ambrosio 3 public void setBalance(double transAmt, int tCode) { if(tCode == 1) balance = ___________________; else //if(tCode == 2) balance = ______________________; } public ____________ getServiceCharge() { return totalServiceCharge; } public void setServiceCharge(double currentServiceCharge) { totalServiceCharge = ___________________________; } }
Explanation / Answer
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class Main
{
public static double initialBalance, transactionAmount;
public static int transactionCode;
public static CheckingAccount ca;
public static int firstTime = 0;
public static void main(String[] args)
{
String initialBal;
DecimalFormat dollar = new DecimalFormat("#,###.00");
initialBal = JOptionPane.showInputDialog("Enter your initial balance: ");
initialBalance = Double.parseDouble(initialBal);
ca = new CheckingAccount(initialBalance);
do
{
transactionCode = getTransCode();
switch(transactionCode)
{
case 1:
transactionAmount = getTransAmt();
processCheck(transactionAmount);
break;
case 2:
transactionAmount = getTransAmt();
processDeposit(transactionAmount);
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.");
}
}while(transactionCode!=0);
}
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 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 Amount: Check in Amount of $"
+ transactionAmount + " " +"Current Balance: $" + ca.getBalance()
+ " "+"Service Charge: Check --- charge $0.15 "
+ "Service Charge: Below $500 --- charge $5.00 "
+ "Total Service Charge: $" + ca.getServiceCharge() );
firstTime++;
}
JOptionPane.showMessageDialog(null,"Transaction Amount: Check in Amount of $"
+ transactionAmount + " " +"Current Balance: $" + ca.getBalance()
+ " "+"Service Charge: Check --- charge $0.15 "
+ "Total Service Charge: $" + ca.getServiceCharge() );
}
else
{
JOptionPane.showMessageDialog(null,"Transaction Amount: Check in Amount of $"
+ transactionAmount + " " + "Current Balance: $" + ca.getBalance()
+ " "+"Service Charge: Check --- charge $0.15 "
+ "Service Charge: None " + "Total Service Charge: $" + (ca.getServiceCharge()) );
}
if(ca.getBalance()<50)
{
JOptionPane.showMessageDialog(null, "Your balance is under $50.");
}
if(ca.getBalance()<0)
{
ca.setServiceCharge(10.00);
JOptionPane.showMessageDialog(null, "Your balance is under $0.");
}
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: " + ca.getBalance()
+ " "+"Service Charge: Deposit --- charge $0.10 "
+ "Service Charge: Below $500 --- charge $5.00 "
+ "Total Service Charge: " + ca.getServiceCharge());
}
JOptionPane.showMessageDialog(null,"Transaction Amount: Deposit in Amount of "
+ transactionAmount + " " +"Current Balance: " + ca.getBalance()
+ " "+"Service Charge: Deposit --- charge $0.10 "
+ "Total Service Charge: " + ca.getServiceCharge());
}
else
{
JOptionPane.showMessageDialog(null,"Transaction Amount: Deposit in Amount of "
+ transactionAmount + " " +"Current Balance: " + ca.getBalance()
+ " "+"Service Charge: Deposit --- charge $0.10 "
+ "Service Charge: None " + "Total Service Charge: "
+ ca.getServiceCharge());
}
if(ca.getBalance()<50)
{
JOptionPane.showMessageDialog(null, "Your balance is under $50.");
}
if(ca.getBalance()<0)
{
ca.setServiceCharge(10.00);
JOptionPane.showMessageDialog(null, "Your balance is under $0.");
}
return transAmt;
}
}
public class CheckingAccount
{
private double balance;
private double totalServiceCharge;
public CheckingAccount(double initialBalance)
{
balance = initialBalance;
totalServiceCharge = 0;
}
public double getBalance()
{
return balance;
}
public void setBalance(double transAmt, int tCode)
{
if(tCode==1)
balance-=transAmt;
else if(tCode==2)
balance+=transAmt;
}
public double getServiceCharge()
{
return totalServiceCharge;
}
public void setServiceCharge(double currentServiceCharge)
{
totalServiceCharge += currentServiceCharge;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.