Trying to Give Coins For Change

I am working on a program where you have to give change in coins. The program already gives the change amount, I'm just trying to figure out how to give the change in coins. This is what i have so far:

Code java:

/**
A cash register totals up sales and computes change due.
*/
package classes;
public class CashRegister
{
// add two attributes (variables) purchase and payment
private double purchase;
private double payment;
/**
Constructor that constructs a cash register with no money in it.
*/
public CashRegister()
{
//initialize the purchase and payment to 0.0
double purchase = 0.0;
double payment = 0.0;
}
/**
Records the sale of an item.
@param amount the price of the item
*/
public void recordPurchase(double amount)
{
//add the amount of purchase to purchase
purchase = purchase + amount;
}
/**
Enters the payment received from the customer; should be called once
for each coin type.
@param coinCount the number of coins
@param coinType the type of the coins in the payment
*/
public void enterPayment(int coinCount, Coin coinType)
{
//calculate the payment
payment = payment+ coinCount * coinType.getValue();
}
/**
Computes the change due and resets the machine for the next customer.
@return the change due to the customer
*/
public double giveChange()
{
double change = purchase - payment;
purchase = 0;
payment = 0;
return change;
//calculate change,change is the difference between payment and purchase, return change
}
public double giveCoins()
{
double change = purchase - payment;
purchase = 0;
payment = 0;
double dollars;
double quarters;
double nickels;
double dimes;
double pennies;
if(change>=1)
{dollars=change/1;}
change=change-dollars;
if(change>=.25)
{quarters=change/.25;}
change=change-(quarters*.25);
if(change>=.1)
{dimes=change/.10; }
change=change-(dimes*.10);
if(change>=.05)
{nickels=change/.05; }
change=change-(nickels*.05);
if(change>=.01)
{pennies=change/.01; }
change=change-(pennies*.01);
return dollars + quarters + dimes + nickels + pennies;
}
}

As you can see I'm trying to make it so say you have 5.55 in change due. The program will look at 5.50, it is greater than 1 so it will do 5.50/1 to get the dollar amount. Of course I need to find a way to round the 5.5 that will result to just 5. The program will then subtract 5 from 5.50 and continue to quaerters ad so forth.

Re: Trying to Give Coins For Change

Hint: What happens when you convert a double value into an int?

Or if you don't like that hint, you can always go the hard way: Math (Java Platform SE 6)

Edit- Why are the values for the counts of each of your coins doubles instead of ints? For example, how can you have 1.5 quarters?

Re: Trying to Give Coins For Change

Okay So I went back and had the doubles converted to ints. Lol, however, I had to convert them back to double so the equations would work. I'm error free however, when I call the method it gives me 0.0. I have the method return 5 values so I should at least be getting 0.0, 0.0, 0.0, 0.0, 0.0...

Any help is appreciated I'm not sure what the problem could be now.

Code java:

public double giveCoins()
{
double change = purchase - payment;
purchase = 0;
payment = 0;
double dollars=0;
double quarters=0;
double nickels=0;
double dimes=0;
double pennies=0;
if(change>=1)
{dollars=change/1;}
int dollarsint = (int)dollars;
double dollarsafter = (double)dollarsint;
change=change-dollarsafter;
if(change>=.25)
{quarters=change/.25;}
int quartersint = (int)quarters;
double quartersafter = (double)quartersint;
change=change-(quartersafter*.25);
if(change>=.1)
{dimes=change/.10; }
int dimesint = (int)dimes;
double dimesafter = (double)dimesint;
change=change-(dimesafter*.10);
if(change>=.05)
{nickels=change/.05; }
int nickelsint = (int)nickels;
double nickelsafter = (double)nickelsint;
change=change-(nickelsafter*.05);
if(change>=.01)
{pennies=change/.01; }
int penniesint = (int)pennies;
double penniesafter = (double)penniesint;
change=change-(penniesafter*.01);
return dollars + quarters + dimes + nickels + pennies;
}

Re: Trying to Give Coins For Change

I'm not sure what your code is supposed to do. What exactly are you supposed to return?

But I see one major problem: you're setting both purchase and payment to 0 at the top of the method, so of course everything is going to be 0.0. You have a few other logic errors, but that's a good place to start.

Re: Trying to Give Coins For Change

So I put the code in the givechange() method instead and it gave me the result of 25.

I found out this result is the total amount of coins to give back.

return dollarsint + quartersint + dimesint + nickelsint + penniesint;

How can I make it so the return statement separates the value instead of adding them together.

I was trying to get a System.out.println() to work on the return but I couldn't get it :(

Re: Trying to Give Coins For Change

Okay so I'm making progress...

I set the purchase at .25 and Pay with 1 penny, 1 dime, 8 quarters, and 1 nickel ($2.16)

So I should get back 1.91

Here is the Output

The Change Due is: 1.9099999999999997 Dollars: 1 Quarters: 3 Dimes: 1 Nickels: 1 Pennies: 0

Lol it is so close. There is something wrong with how the change is calculating any ideas?

Re: Trying to Give Coins For Change

That's happening because floating point arithmetic is not exact when talking about very small numbers. There are good explanations (try googling "what every computer scientist should know about floating-point arithmetic", but does anybody have the original link?), but the gist of it is this: you can't represent 1.91 exactly in binary, so the actual double value will be something very close to it but not quite exact.

For this reason, anything dealing with things like currency should be handled as ints. Instead of 1.91 dollars, you would represent that as 191 cents, as an int. Displaying it how you want is up to you, but there are classes in the API specifically for that (look for DecimalFormat).