This was the assignment:
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 variables. 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 the balance drops below $500.00 at any point during the month, a service charge of $5.00 is assessed once for the month. Anytime the balance drops below $50.00, the program should print a warning message. If the balance becomes negative, an additional service charge of $10.00 should be assessed for each check until the balance becomes positive again. A transaction takes the form of an int number, followed by a double number. 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.
and here are my codes:
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\n"
+ "Current Balance: $" + ca.getBalance() + "\n"
+ "Total Service Charge: $" + dollar.format(ca.getServiceCharge()) + "\n"
+ "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:\n"+
"1) Check \n2) Deposit \n0) 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 + "\n" +"Current Balance: $" + ca.getBalance()
+ "\n"+"Service Charge: Check --- charge $0.15 \n"
+ "Service Charge: Below $500 --- charge $5.00\n"
+ "Total Service Charge: $" + ca.getServiceCharge() );
firstTime++;
}
JOptionPane.showMessageDialog(null,"Transaction Amount: Check in Amount of $"
+ transactionAmount + "\n" +"Current Balance: $" + ca.getBalance()
+ "\n"+"Service Charge: Check --- charge $0.15 \n"
+ "Total Service Charge: $" + ca.getServiceCharge() );
}
else
{
JOptionPane.showMessageDialog(null,"Transaction Amount: Check in Amount of $"
+ transactionAmount + "\n" + "Current Balance: $" + ca.getBalance()
+ "\n"+"Service Charge: Check --- charge $0.15 \n"
+ "Service Charge: None\n" + "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 + "\n" +"Current Balance: " + ca.getBalance()
+ "\n"+"Service Charge: Deposit --- charge $0.10 \n"
+ "Service Charge: Below $500 --- charge $5.00\n"
+ "Total Service Charge: " + ca.getServiceCharge());
}
JOptionPane.showMessageDialog(null,"Transaction Amount: Deposit in Amount of "
+ transactionAmount + "\n" +"Current Balance: " + ca.getBalance()
+ "\n"+"Service Charge: Deposit --- charge $0.10 \n"
+ "Total Service Charge: " + ca.getServiceCharge());
}
else
{
JOptionPane.showMessageDialog(null,"Transaction Amount: Deposit in Amount of "
+ transactionAmount + "\n" +"Current Balance: " + ca.getBalance()
+ "\n"+"Service Charge: Deposit --- charge $0.10 \n"
+ "Service Charge: None\n" + "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;
}
}
and I got a reply from my professor:
"...The below 0 charge of 10.00 is not being applied."
I figured out that if statement would not work. Is there any way to apply that charge? Thanks so much!