Question

I've recently embarked into Java programming and consider myself a programming novice. It seem that I'm having an issue with my source code arithmetic. I have verified all the nested if-else statements and they all work up to the final else statement's arithmetic. It doesn't calculating correctly I have set the arithmetic up just as the above if-else statements.

the else statement is suppose to subtract 40 from the amount and then apply 1 percent charge. I have tried for the else statement fee = ((checkAmount - 40) * .01) and fee = ((checkAmount * .01) - 40)

This is just an exercise from the book

  import java.util.Scanner;
public class ServiceCharge {
public static void main(String[] args)
{
    double checkAmount;
    double fee;
    Scanner kb = new Scanner(System.in);
    System.out.println("I will calulate the service charge to cash your check");
    System.out.print("Enter amount of check: $");
    checkAmount = kb.nextDouble();

    if (checkAmount > 0)
    {
        if (checkAmount <= 10)
        {
            fee = -1;
            System.out.println("$1 service charge");
            checkAmount = checkAmount + fee;
            System.err.println("You have " + checkAmount + " left after service charge.");
        }
        else if ((checkAmount > 10) && (checkAmount <= 100))
        {
            System.out.println("There will be a 10 percent charge.");
            fee = (checkAmount * .10);
            checkAmount = checkAmount - fee;
            System.out.printf("Processing fee: $%.2f\n" , fee);
            System.out.printf("Check amount: $%.2f\n" , checkAmount);
        }
        else if ((checkAmount > 100) && (checkAmount <= 1000))
        {
            System.out.println("There will be a $5 charge plus 5 percent");
            fee = ((checkAmount - 5) * .05);
            checkAmount = (checkAmount - fee);
            System.out.printf("Processing fee: $%.2f\n" , fee);
            System.out.printf("Check amount: $%.2f\n", checkAmount);
        }
        else
        {
            System.out.println("$40 processing fee plus 1 percent");
            fee = ((checkAmount - 40) * .01);
            checkAmount = (checkAmount - fee);
            System.out.printf("Processing fee: $%.2f\n" , fee);
            System.out.printf("Check amount: $%.2f\n" , checkAmount);
        }
        System.out.println("Thanks for using Service Charger." + "\nGood bye");
    }
}
}
Was it helpful?

Solution 2

            System.out.println("$40 processing fee plus 1 percent");
            fee = ((checkAmount - 40) * .01);

That's not a 40 dollar fee + 1 percent. That's a fee of slightly less than 1 percent; it's as if you cash out the first 40 dollars for free, and then apply a 1 percent charge to the rest.

Assuming the 1% charge applies to the whole check, rather than what's left after subtracting 40 dollars, the correct expression for the fee is

            fee = 40 + 0.01*checkAmount;

OTHER TIPS

For the last else statement, it seems a bit off from the rest of your statements. You're using "hold" to store the original checkAmount value, then modifying checkAmount to be the fee for the first three statements. You should model the last one like the one before it. The checkAmount should be checkAmount = (checkAmount * .01) + 40, then hold - checkAmount should return the value you're looking for. By having checkAmount = checkAmount - 40, the last line is returning hold (checkAmount) - (checkAmount - 40), which will always return 40.

Actually, you wanted to only charge the 1% fee over the check amount less the $40 dollar fixed fee according to your original expression, so the expression should be:

fee = 40 + (checkAmount - 40) * .01;

There's a lot of duplication in your code, which makes it harder to see what's going on, and if you decide to change - for example - the message that you want to show to the user, you now need to change it in 4 locations, and there's a big change that you forget to do it somewhere, or that you make a typo.

One of the goals of good programming is to avoid duplication as much as possible.

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

        Scanner kb = new Scanner(System.in);
        System.out.println("I will calulate the service charge to cash your check");
        System.out.print("Enter amount of check: $");
        double checkAmount = kb.nextDouble();

        if (checkAmount > 0) {
            double fee;
            String feeMessage;
            if (checkAmount <= 10) {
                fee = 1;
                feeMessage = "$1 service charge";
            } else if ((checkAmount > 10) && (checkAmount <= 100)) {
                feeMessage = "10 percent charge.";
                fee = (checkAmount * .10);
            } else if ((checkAmount > 100) && (checkAmount <= 1000)) {
                feeMessage = "$5 charge plus 5 percent";
                fee = 5 + ((checkAmount - 5) * .05);
            } else {
                feeMessage = "$40 processing fee plus 1 percent";
                fee = 40 + ((checkAmount - 40) * .01);
            }
            checkAmount = checkAmount - fee;

            System.out.printf("Fee structure: " + feeMessage);
            System.out.printf("Processing fee: $%.2f\n", fee);
            System.out.printf("Check amount: $%.2f\n", checkAmount);
            System.out.println("Thanks for using Service Charger.\nGood bye");
        }
    }
}

The next step you may want to look into is to decompose your program functionally into functions. For example, the part where you ask for the amount, the part where you do the calculation, and the part where you show the result, are three very distinct parts. Each of those three you may want to change separately - you may want to get the input from a file or from a web request, and you may want to store use the result in another calculation rather than show it to the user.

So these could go into separate functions.

And then you could think about object decomposition - perhaps we're talking here about a CheckCashAction object that has properties for checkAmount, fee, feeStructure and payoutAmount.

Etc.

The nice thing then is that you can look at each of the steps separately and test them in isolation, which makes it easier to pinpoint bugs and to maintain the code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top