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!

Recommended Answers

All 5 Replies

Can you reference the lines of code where you are having problems with the logic?
Where in the 195 lines of code it the problem?

line 106~114
line 151-159

if(ca.getBalance()<0)
{
ca.setServiceCharge(10.00);
JOptionPane.showMessageDialog(null, "Your balance is under $0.");
}
return transAmt;
}

The code looks like if the getBalance() method returns a value < 0
then the setServiceCharge() method will be called with a argument of 10.0
and a message will be displayed

What problems are you having with this logic?
Does getBalance() return the correct value?
Does setServiceCharge() do the right thing?

Yes, all values are correct and each method does its job.
However, if the balance goes under the zero, the warning sign does not show up at all.
I want the warning to show up during the process if the remaining balance goes under zero.
When i first tried it, it did work and somewhat made sense to me.
But my professor said there's another way to show that warning message, besides using if statement.
am i doing something wrong or ... i have no idea.

Look at the sequence of when you change values and when you display them. I think you are not doing it in the right order.

An if statement seems like a good way to test values. I don't know what your prof is referring to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.