954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Cash Register Code Help

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.

Kurosaki
Newbie Poster
5 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

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?

DaSogo
Light Poster
25 posts since May 2006
Reputation Points: 10
Solved Threads: 2
 

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

Kurosaki
Newbie Poster
5 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

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

DaSogo
Light Poster
25 posts since May 2006
Reputation Points: 10
Solved Threads: 2
 

Oh. No wonder.

Didn't know about the parenthesis.

Thank you so much for helping me.

Kurosaki
Newbie Poster
5 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

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

BrianDave
Newbie Poster
1 post since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You