# CashRegister class- adding a method getItemCount

• January 20th, 2010, 03:10 PM
etidd
CashRegister class- adding a method getItemCount
hello everyone,

new person here. i'm a college student working on my programming homework. i have a class CashRegister that reads the payment in and calculates change due. i have to add a new method to it called getItemCount() that keeps track of the total number of items in a sale. it returns the number of items in the current purchase. remember to reset the count at the end of the purchase. how do i do this?

public class CashRegister
{
/**
* Constructs a cash register with no money in it
* */
public CashRegister()
{
purchase = 0;
payment = 0;
}

public int getItemCount()
{

/**
* Records the purchase price of an item.
* @param amount the price of a purchased item
* */
public void recordPurchase(double amount)
{
purchase = purchase + amount;
}

/**
* Enters the amount in bills received from the customer
* */
public void enterDollars(int dollars)
{
payment = dollars;
}

/*
* the following methods take the number of each coins given, and multiply them by
* their constant value and add that to the total payment received
* */
public void enterQuarters(int quarters)
{
payment = payment + quarters * QUARTER_VALUE;
}

public void enterDimes(int dimes)
{
payment = payment + dimes * DIME_VALUE;
}

public void enterNickels(int nickels)
{
payment = payment + nickels * NICKEL_VALUE;
}

public void enterPennies(int pennies)
{
payment = payment + pennies * PENNY_VALUE;
}

/*
* this method supplies the total payment received and subtracts the purchase price.
* change is then computed.
* */
public double giveChange()
{
double change = payment - purchase;
purchase = 0;
payment = 0;
return change;
}

public static final double QUARTER_VALUE = .25;
public static final double DIME_VALUE = .1;
public static final double NICKEL_VALUE = .05;
public static final double PENNY_VALUE = .01;

private double purchase;
private double payment;
}
• January 20th, 2010, 04:04 PM
Johannes
Re: CashRegister class- adding a method getItemCount
You could introduce a new field called count. Adding an item to the items purchased increments the variable, while bying sets it to 0.
• January 21st, 2010, 07:29 PM
mdv2000
Re: CashRegister class- adding a method getItemCount
Well, for me I would look at the process as a transaction. I would have a start() method that sets up everything... store each purchase amount in an array so you can easily get a count and also return a list of items like a receipt. I would also create getters for the amount paid in and the amount due (total). the giveChange() would probably be the last method you call - but I would let the start method put everything back to zero in case you want to handle the fact the amount paid in is less than the amount due and giveChange() would return less than 0. If you set everything to zero you can't add another dollar and make it "right" like a real cash register.