Home > Net >  Why does my program only provide change in dollar amounts?
Why does my program only provide change in dollar amounts?

Time:06-24

This is my current code:

import java.util.Scanner;

public class CustomerChange {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        double cost = 0.00;
        double cash = 0.00;
        double dollars = 0.00;

        System.out.println("Price: ");
        cost = scanner.nextDouble();

        System.out.println("Cash: ");
        cash = scanner.nextDouble();

        if (cash < cost) {
            System.out.println("Not enough cash. Goodbye.");
        }

        else if (cash > cost) {
            dollars = cash - cost;
            System.out.println("\nChange: \n");
            if ((int) dollars / 20 > 0) {
                System.out.println("Twenties: "   (int) dollars / 20);
                dollars %= 20;
            }

            if ((int) dollars / 10 > 0) {
                System.out.println("Tens: "   (int) dollars / 10);
                dollars %= 10;
            }
            if ((int) dollars / 5 > 0) {
                System.out.println("Fives: "   (int) dollars / 5);
                dollars %= 5;
            }
            if ((int) dollars / 1 > 0) {
                System.out.println("Ones: "   (int) dollars / 1);
                dollars %= 1;
                dollars *= 100;
            }
            if ((int) dollars / 25 > 0) {
                System.out.println("Quarters: "   (int) dollars / 25);
                dollars %= 25;
            }
            if ((int) dollars / 10 > 0) {
                System.out.println("Dimes: "   (int) dollars / 10);
                dollars %= 10;
            }
            if ((int) dollars / 5 > 0) {
                System.out.println("Nickels: "   (int) dollars / 5);
            }
            if ((int) dollars / 1 > 0) {
                System.out.println("Pennies: "   (int) dollars / 1);
            }

        } else if (cash == cost) {
            System.out.println("Thank you. Goodbye.");
        }
        scanner.close();
    }

}

Right now, when I enter in a price of 3.25 and a cash of 4, I don't get a return of my change, but when I enter in a price of 168.75 and a cash of 200, I get:

Price: 
168.75
Cash: 
200

Change: 

Twenties: 1
Tens: 1
Ones: 1
Quarters: 1

Where am I going wrong? It took forever to figure out the math and I thought that was the crux but I guess not?

CodePudding user response:

When you are inputting: Price: 3.25 and Cash: 4

(int)(0.75) will be 0.

So it will not get inside any of the if block.

Put a condition like

if ((int) dollars / 1 == 0) {
  System.out.println("Pennies: "   (int)(dollars*100));
}

Then it will definitely work.

CodePudding user response:

Your code is overly complicated due to attempting to work with double instead of just converting everything to an integer to begin with. This way everything is in the value of pennies, and you do not need to worry about any casting or multiplying a decimal later on by 100 in order to work with the coins, and the code will be less prone to bugs.

Here is what it would look like using similar logic you were using, but instead if in the form of pennies (you don't even need the if statements):

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    int pricePennies = 0;
    int cashPennies = 0;

    System.out.println("Price: ");
    pricePennies = (int) (scanner.nextDouble() * 100);

    System.out.println("Cash: ");
    cashPennies = (int) (scanner.nextDouble() * 100);

    if (cashPennies < pricePennies) {
        System.out.println("Not enough cash. Goodbye.");
    } 
    else if (cashPennies > pricePennies) {
        int change = cashPennies - pricePennies;
        System.out.println("Twenties: "   change / 2000);
        change %= 2000;
        
        System.out.println("Tens: "   change / 1000);
        change %= 1000;
        
        System.out.println("Fives: "   change / 500);
        change %= 500;
        
        System.out.println("Ones: "   change / 100);
        change %= 100;
        
        System.out.println("Quarters: "   change / 25);
        change %= 25;
        
        System.out.println("Dimes: "   change / 10);
        change %= 10;
        
        System.out.println("Nickels: "   change / 5);
        change %= 5;
        
        System.out.println("Pennies: "   change / 1);
        change %= 1;
    } 
    else if (cashPennies == pricePennies) {
        System.out.println("Thank you. Goodbye.");
    }
    scanner.close();
}

Test Runs:

Price: 
168.75
Cash: 
200
Twenties: 1
Tens: 1
Fives: 0
Ones: 1
Quarters: 1
Dimes: 0
Nickels: 0
Pennies: 0

Price: 
3.25
Cash: 
4
Twenties: 0
Tens: 0
Fives: 0
Ones: 0
Quarters: 3
Dimes: 0
Nickels: 0
Pennies: 0

Price: 
54748
Cash: 
63939.46
Twenties: 459
Tens: 1
Fives: 0
Ones: 1
Quarters: 1
Dimes: 2
Nickels: 0
Pennies: 1
  • Related