Hi. I'm new to java programming and I need help with this assignment.

Giving change. Implement a program that directs a cashier how to give change. The program has two inputs: the amount due and the amount received from the customer. Compute the difference, and compute the dollars, quarters, dimes, nickels, and pennies that the customer should receive in return.

So far my code look like this. But I have no clue what to do now.

import java.util.*;

public class Cashier
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        
        System.out.print("The amount due: ");
        double amountDue = in.nextDouble();
        
        System.out.print("The amount paid: ");
        double amountPaid = in.nextDouble();
        
        System.out.print("Your change is: ");
        
        int pennies = (int)(amountPaid - amountDue)*100)
                
        int dollars=pennies/100;
            pennies=pennies%100;
            
        int quarters = pennies/25;
            pennies = pennies%25;
            
        int dimes = pennies/10;
            pennies = pennies%10;
            
        int nickels = pennies/5;
            pennies = pennies%5;
            
        int pennies = pennies/1;
            pennies = pennies%1;
            
        System.out.println("The change due:");
        System.out.println(dollars + "dollars");
        System.out.println(quarters + "quarters");
        System.out.println(dimes + "dimes");
        System.out.println(nickels + "nickels");
        System.out.println(pennies + "pennies");

    }
}

Please help. Thank You.

Member Avatar for DaSogo

Hi, Please indicate what you are having trouble with in your code. I don't think anyone has the time to figure this out! I can tell you are very new. I myself am a newbie so I'll give you some tips.

1. Your code is too cluttered. Instead of inserting all this in the main method, try using classes and have the main method as the basis for instantiating those classes. I always call mine "DRIVER.CLASS".

2. Try creating a class called "CASHREGISTER.CLASS" since this is the object you want to model.

3. Within "CASHREGISTER.CLASS", create methods for each action that you would like to perform. This will make it easier for others to read and maintain your programs.

Question? Have you even tried to run this program?

When I tried to compile this code.

It said that there was something wrong with this line:

int pennies = (int)(amountPaid - amountDue)*100)

I'm having trouble with the part that calculate the change

Member Avatar for DaSogo

Count your parenthesis () - you have a syntax error!

Oh. No wonder.

Didn't know about the parenthesis.

Thank you so much for helping me.

import java.util.*;
public class Cashier
{
    public static void main(String[] args)
    {

        Scanner console = new Scanner(System.in);

                int amountDue,amountPaid;
                double pennies,dollars,nickels,dimes,quarters;

        System.out.print("The amount due: ");
        amountDue = console.nextInt();

        System.out.print("The amount paid: ");
        amountPaid = console.nextInt();

        System.out.print("Your change is: ");

        pennies = amountPaid-amountDue+50;

         dollars=pennies/100;
            pennies=pennies%100;

         quarters = pennies/25;
            pennies = pennies%25;

         dimes = pennies/10;
            pennies = pennies%10;

         nickels = pennies/5;
            pennies = pennies%5;

         pennies = pennies/1;
            pennies = pennies%1;

        System.out.println("The change due:");
        System.out.println(dollars + "dollars");
        System.out.println(quarters + "quarters");
        System.out.println(dimes + "dimes");
        System.out.println(nickels + "nickels");
        System.out.println(pennies + "pennies");

    }
}

try this

I want output of pennies is 2 .How can I get that

System.out.println("pennies is 2");

commented: 2 pennies? I wanted a peanut. -Homer Simpson. +16
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.