import javax.swing.JOptionPane; import java.text.DecimalFormat; public class Assignment1 { public static CheckingAccount info; public static void main(String[] args) { String balance, message; double ibalance; boolean above = true; DecimalFormat formatter = new DecimalFormat("#0.00"); int tcode = 1; double tamount; balance = JOptionPane.showInputDialog ("Enter your initial balance: "); ibalance = Double.parseDouble(balance); info = new CheckingAccount(ibalance); if (info.getBalance() < 500) above = false; while(tcode != 0) { tcode = getTransCode(); if (tcode == 1 ) { tamount = getTransAmt(); info.setBalance(tamount, tcode); info.setServiceCharge(tcode); if(info.getBalance() <500 && above ==true) { info.setServiceCharge(1.0); processCheck (tcode, info.getBalance(), tamount, info.getServiceCharge(), above); above = false; } else if (info.getBalance() <0 && above == false) { info.setServiceCharge(2.0); processCheck (tcode, info.getBalance(), tamount, info.getServiceCharge(), above); } else if (info.getBalance() <0 && above == true) { info.setServiceCharge(2.0); processCheck (tcode, info.getBalance(), tamount, info.getServiceCharge(), above); above = false; } else { processCheck (tcode, info.getBalance(), tamount, info.getServiceCharge(), above); } } else if (tcode ==2) { tamount = getTransAmt(); info.setBalance (tamount, tcode); info.setServiceCharge(tcode); processDeposit (tcode, info.getBalance(), tamount, info.getServiceCharge()); } } if (tcode ==3) { message = "End of Transaction" + "\n" + "\nTotal balance: $" + formatter.format(info.getBalance()) + "\nTotal service charge: $" + formatter.format(info.getServiceCharge()) + "\nFinal Balance: $" + formatter.format(info.EndBalance()); JOptionPane.showMessageDialog (null, message); } } public static int getTransCode() { int tcode; String transcode; do { transcode = JOptionPane.showInputDialog ("Enter your transaction" + " code:" + "\n1 For Check" + "\n2 For Deposit" + "\n3 To Exit Program" ); tcode = Integer.parseInt(transcode); }while (tcode >2 || tcode < 0); return tcode; } public static double getTransAmt() { double tamount; String transamount; transamount = JOptionPane.showInputDialog ("Enter your trans amount:"); tamount = Double.parseDouble(transamount); return tamount; } public static void processCheck(int Transcode, double Balance, double TransAmt, double totalServiceCharge, boolean above) { DecimalFormat formatter = new DecimalFormat("#0.00"); String message; if (Balance >= 500) { message = "Transaction: Check in the amount of $" + TransAmt+ "\n" + "\nCurrent balance: $" + formatter.format(info.getBalance()) + "\nService Charge: Check --- charge $0.15" + "\nService Charge: Below $500 --- charge 5.00" + "\nTotal service charge:" + formatter.format(info.getServiceCharge()); JOptionPane.showMessageDialog (null,message); } else if (Balance < 500 && Balance > 0 && above == true) { message = "Transaction: Check in the amount of $" + TransAmt+ "\n" + "\nCurrent balance: $" + formatter.format(info.getBalance()) + "\nService Charge: Check --- charge $0.15" + "\nService Charge: Below $500 --- charge 5.00" + "\nTotal service charge:" + formatter.format(info.getServiceCharge()); JOptionPane.showMessageDialog (null, message); } else if (Balance < 500 && Balance > 0 && above == false) { message = "Transaction: Check in the amount of $" + TransAmt+ "\n" + "\nCurrent balance: $" + formatter.format(info.getBalance()) + "\nService Charge: Check --- charge $0.15" + "\nTotal service charge:" + formatter.format(info.getServiceCharge()); JOptionPane.showMessageDialog (null, message); } else { message = "Transaction: Check in the amount of $" + TransAmt+ "\n" + "\nCurrent balance: $" + formatter.format(info.getBalance()) + "\nService Charge: Check --- charge $0.15" + "\nService Charge: Below $0 --- charge 10.00" + "\nTotal service charge:" + formatter.format(info.getServiceCharge()); JOptionPane.showMessageDialog (null, message); } } public static void processDeposit(int Transcode, double Balance, double TransAmt, double totalServiceCharge) { String message; DecimalFormat formatter = new DecimalFormat("#0.00"); message = "Transaction: Deposit in the amount of $" + TransAmt+ "\n" + "\nCurrent balance: $" + formatter.format(info.getBalance()) + "\nService Charge: Deposit --- charge $0.10" + "\nTotal service charge:" + formatter.format(info.getServiceCharge()); JOptionPane.showMessageDialog (null, message); } }
public class CheckingAccount { private double balance; private double totalServiceCharge = 0; public CheckingAccount(double number) { balance = number; } 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 EndBalance() { balance = balance - totalServiceCharge; return balance; } public double CurrentBalance() { balance = balance - totalServiceCharge; return balance; } public double getServiceCharge() { return totalServiceCharge; } public void setServiceCharge(int tcode) { if (tcode ==1) { totalServiceCharge = totalServiceCharge + 0.15; } else if (tcode ==2) { totalServiceCharge = totalServiceCharge + 0.10; } } public void setServiceCharge(double sale) { if (sale == 1) { totalServiceCharge = totalServiceCharge + 5.00; } else if (sale ==2) { totalServiceCharge = totalServiceCharge + 10.00; } } }
So as you can see, if the user inputs 3 into tcode, the program is supposed to print the last transaction invoice and end the loop/program.
However, when the user inputs 3, the program does not end, and does not print the last transaction.