javawhile-loopswitch-statementcalculatorcredits

Control Statements: Why is my 'New Balance' change when I 'charge to credit' then 'pay credit' twice?


I tried reading from the start of this Java book and do some of its exercises for summer vacation. The I came up with this problem: "(Credit Limit Calculator) Develop a Java application that determines whether any of several department-store customers has exceeded the credit limit on a charge account.....For those customers whose credit limit is exceeded, the program should display the message "Credit limit exceeded"."

My code works only if I use Charge to Credit ONCE. If I use Charge to Credit again, my new balance value will change. Example:

* Acc # = 123
* Beginning Balance = 20000
* Credit Limit = 25000
* Select Transaction = 1 (Charge to Credit)
* Amount to charge = 4000
* Select Transaction = 2 (Pay credit)
* Amount to pay = 2000 [My balance should now be 22000)
* Select Transaction = 1 (Charge to Credit)
* Amount to charge = 10000 [This shud exceed credit limit]
* Select Transaction = 3 (Balance Inquiry)
* My new balance is now 18000

This is my problem now. My new balance should still be 22000 since the credit limit has exceeded. But now its 18000. I don't know where it went wrong so I need help, please.

So here's my code:

Customer.java

import java.util.Scanner;

public class Customer {

Scanner input = new Scanner (System.in);

int accNum,
    beginBal,
    creditLimit;

int itemsCharged,
    creditsPaid,
    newBalance;

int transaction;
String action;

public void start (){
    System.out.print("\nAccount Number: ");
    accNum = input.nextInt();

    System.out.print("Beginning Balance: ");
    beginBal = input.nextInt();

    System.out.print("Credit Limit: ");
    creditLimit = input.nextInt();

    transaction();
}

public void transaction (){

    boolean loop = true;
    while (loop){
        System.out.println("\n[1] Charge to Credit \n[2] Pay Credit \n[3] Balance Inquiry/Exit");
        System.out.print("Select Transaction #: ");

        transaction = input.nextInt();

        switch (transaction){
        case 1:
            System.out.print("\n--CHARGE TO CREDIT-- \nEnter amount: ");
            itemsCharged = input.nextInt();

            newBalance = beginBal + itemsCharged - creditsPaid;
            if (newBalance > creditLimit){
                System.err.println("Credit Limit Exceeded!");
                newBalance -= itemsCharged;
            } else {
                System.out.println("Amount charged.");
            }
            break;
        case 2:
            System.out.print("\n--PAY CREDIT-- \nEnter amount: ");
            creditsPaid = input.nextInt();

            newBalance = beginBal + itemsCharged - creditsPaid;
            if (creditsPaid > newBalance){
                System.err.println("Invalid Amount!");
                newBalance -= creditsPaid;
            } else {
                System.out.println("Payment posted!");
                newBalance = beginBal + itemsCharged - creditsPaid;
            }
            break;
        case 3:
            System.out.println("\n--BALANCE INQUIRY-- \nNew Balance: " + newBalance);
            restart();
            break;
        default:
            System.err.println("Invalid number!");
            transaction();
            break;
        }
    }
}

public void restart (){

    System.out.println("\nDo you have another transaction? [Y/N]");
    System.out.print("Select Action: ");

    boolean loop = true;
    while (loop){

        action = input.nextLine();

        switch (action){
        case "Y":
            start();
            break;
        case "N":
            System.err.println("Terminated.");
            System.exit(0);
            break;
        }
    }
}

} // end class

CreditLimitCal.java

public class CreditLimitCal {
public static void main (String[] args){

    Customer cus = new Customer();

    cus.start();

}

}

Solution

  • this line:

    newBalance = beginBal + itemsCharged - creditsPaid;
    

    Doesn't need to subtract creditsPayed, you've already done that previously when the user paid off part of the balance.

    newBalance should only be modified by one thing each time, so for case 1:

    newBalance = newBalance + itemsCharged;
    

    case 2:

    newBalance = newBalance - creditsPaid;