# Cash Register Exercise

Page 1 of 1

## 5 Replies - 17363 Views - Last Post: 23 January 2010 - 11:03 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=151236&amp;s=44ddeaf82500dd408aafc90eef5ebe4c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ldunham

Reputation: 0
• Posts: 6
• Joined: 10-January 10

# Cash Register Exercise

Posted 21 January 2010 - 05:50 PM

Implement a coin class in conjunction with the Cash Register class. Make a payment and receive change. I think I have a logic error. It runs but the expected outcome does not match the result.

```public class Coin
{
private double value = 0;
private String type;

public Coin ()
{

}
public Coin (double aValue, String aName)
{

}

public Coin (String coinType)
{
type = coinType;
//  setValue(type);
}

public String getType()

{
return type;
}

public void setType(String type)

{
this.type = type;
}

public double getValue()

{
return value;
}

public void setValue(double value)

{
this.value = value;
}

}

___________________________________________________________

public class CashRegister
{
//Create a cash register with money in it.

private int numberOfQuarters;
private int numberOfDimes;
private int numberOfNickels;
private double totalValue;
private double purchase;
private double payment;
private double value1;
private double value2;
private double value3;

public CashRegister()
{
purchase = 0;
payment = 0;
}
public CashRegister(int quarters, int dimes, int nickels)
{
numberOfQuarters = quarters;
numberOfDimes = dimes;
numberOfNickels = nickels;
}

//Records the sale of an item
//@param amount the price of the item

public void recordPurchase(double amount) //double newTotal = purchase + amount; //purchase = newTotal;
{
double newTotal = purchase + amount;
purchase = newTotal;						//purchase = purchase + amount;
}

//@param dollars the number of dollars
//@param quarters the number of quarters
//@param dimes the number of dimes
//@param nickels the number of nickels
//@param pennies the number of pennies

public void enterPayment(int coinCount, Coin coinType)
{

if(coinType.getType() == "quarter")
{
numberOfQuarters = numberOfQuarters * coinCount;
payment = payment + (coinCount * coinType.getValue());
value1 = payment;//
}
if(coinType.getType() == "dime")
{
numberOfDimes = numberOfDimes * coinCount;
payment = payment + (coinCount * coinType.getValue());
value2 = payment;//
}
if(coinType.getType() == "nickel")
{
numberOfNickels = numberOfNickels * coinCount;
payment = payment + (coinCount * coinType.getValue());
value3 = payment;//
}//

payment = value1 + value2 + value3;//

}//

//Computes the change due and resets the machine for the next customer.
//@return the change due to the customer

public double giveChange()
{
double change = payment - purchase;
purchase = 0;
payment = 0;
return change;
}

}

____________________________________________________________

public class CashRegisterTester
{
public static void main(String[] args)
{
final double NICKEL_VALUE = 0.05;
final double DIME_VALUE = 0.10;
final double QUARTER_VALUE = 0.25;

CashRegister myRegister = new CashRegister();
myRegister.recordPurchase(.82);
myRegister.enterPayment(4, new Coin(QUARTER_VALUE, "quarter"));
myRegister.enterPayment(2, new Coin(NICKEL_VALUE, "nickel"));

double change = myRegister.giveChange();
System.out.println("Change: " + change);
System.out.println("Expected: 0.28");
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Cash Register Exercise

### #2 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: Cash Register Exercise

Posted 21 January 2010 - 06:37 PM

Your constructor does not initialize the instance variables:
```	public Coin(double aValue, String aName) {
}

```

So when Coin.getType() is called it returns a NULL.

Java does not have string operators == or !=. Use equals() or compareTo().

See the String Class

### #3 ldunham

Reputation: 0
• Posts: 6
• Joined: 10-January 10

## Re: Cash Register Exercise

Posted 21 January 2010 - 07:00 PM

n8wxs, on 21 Jan, 2010 - 05:37 PM, said:

Your constructor does not initialize the instance variables:
```	public Coin(double aValue, String aName) {
}

```

So when Coin.getType() is called it returns a NULL.

Java does not have string operators == or !=. Use equals() or compareTo().

See the String Class

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12355
• Posts: 45,464
• Joined: 27-December 08

## Re: Cash Register Exercise

Posted 21 January 2010 - 08:56 PM

Is there a question or are you just quoting n8wxs' reply?

### #5 ldunham

Reputation: 0
• Posts: 6
• Joined: 10-January 10

## Re: Cash Register Exercise

Posted 23 January 2010 - 10:57 AM

macosxnerd101, on 21 Jan, 2010 - 07:56 PM, said:

Is there a question or are you just quoting n8wxs' reply?

There was no question. I'm trying to type in the proper way, but don't know what I'm doing. What is PMs?

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12355
• Posts: 45,464
• Joined: 27-December 08

## Re: Cash Register Exercise

Posted 23 January 2010 - 11:03 AM

A PM is a Personal Message. Also, in the future, you don't need to quote a member's post if you don't have a reply to it. Thanks for helping us help you.