0

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();

}

}
Jean-Bernard Pellerin
  • 12,556
  • 10
  • 57
  • 79
lmpgdn
  • 19
  • 1
  • 1
  • 7

1 Answers1

1

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;
Jean-Bernard Pellerin
  • 12,556
  • 10
  • 57
  • 79
  • But that's also in case 1. So if I remove that, then my newbalance will get bigger and bigger discarding the credits paid. :-/ – lmpgdn May 02 '13 at 16:25
  • It worked, thanks! But then when I continue to "Do you have another transaction?" and enter another account num, with 10000 beginning balance and 15000 credit limit. then I charge to credit for the first time on that account, it says "Credit limit exceeded". Then the balance will be the previous balance from acc num 123. – lmpgdn May 02 '13 at 16:39
  • @javanerd in start() you should set newBal to beginBal and reset the other values. – Jean-Bernard Pellerin May 02 '13 at 16:41
  • Now it's good! Thank you sooo much! I could now rest in peace. Lol – lmpgdn May 02 '13 at 16:48