I have an assignment Enhance the CashRegister class so that it keeps track of the total number of items in a sale. Count all recorded purchases and supply a method
int getItemCount()
that returns the number of items of the current purchase. Remember to reset the count ast the end of the pruchase.

I have my code for both the CashRegister and CashRegisterTester below. I'm not really sure exactly what the question is asking so I don't even know where to begin to add it into my program.

Am I supposed to be asking the user for information on multiple purchases and then finding out how many items they purchased? If so I have no idea how to begin doing this.

public class CashRegister 
{
	public static final double QUARTER_VALUE = 0.25;
	public static final double DIME_VALUE = 0.1;
	public static final double NICKEL_VALUE = 0.05;
	public static final double PENNY_VALUE = 0.01;

		private double purchase;
		private double payment;	
				
	public CashRegister()
	{
		purchase = 0;
		payment = 0;
	}
	
	public void recordPurchase(double amount)
	{
		double newTotal = purchase + amount;
		purchase = newTotal;
	}
	
	public void enterPayment(int dollars, int quarters,
			int dimes, int nickles, int pennies)
	{
		payment = dollars + quarters * QUARTER_VALUE + dimes * DIME_VALUE 
				+ nickles * NICKEL_VALUE + pennies * PENNY_VALUE;
		
	}
	
	public double giveChange()
	{
		double change = payment - purchase;
		purchase = 0;
		payment = 0;
		return change;		
	}
	
	public int Counter(int purchase)
	{
		purchase = purchase + 1;
		return purchase;
		
		
	}
	public double getItemCount()
	{
				
		return purchase;
	}


}
import java.util.Scanner;

public class CashRegisterTester 
{

	public static void main(String[] args) 
	{
		Scanner in = new Scanner(System.in);
		
		CashRegister register = new CashRegister();
		
		System.out.print("Enter price: ");
		double price = in.nextDouble();
		register.recordPurchase(price);
		
		System.out.print("Enter dollars: ");
		int dollars = in.nextInt();
		System.out.print("Enter quarters: ");
		int quarters = in.nextInt();
		System.out.print("Enter dimes: ");
		int dimes = in.nextInt();
		System.out.print("Enter nickels: ");
		int nickels = in.nextInt();
		System.out.print("Enter pennies: ");
		int pennies= in.nextInt();
		register.enterPayment(dollars,  quarters, dimes, nickels, pennies);
		
		
		System.out.print("Your change: ");
		System.out.println(register.giveChange());
		
		System.out.println(register.getItemCount());
		
		System.exit(0);

	}

}

Recommended Answers

All 3 Replies

Think about it in real life first. How do you count number of item purchased? Each time an item is ringed (scanned), the number of item is increased by 1, correct? Now, which method in the CashRegister class is represented as ringing up an item? If you found it, what you need to do is to add a variable type integer which will be increased by 1 each time an item is counted as purchased to the class. Remember to declare it as the class's variable, not a method's variable. Then return the value of the variable via getItemCount() method.

I do understand that but how would I go about getting the program to ask me for multiple items? Right now it asks for how much the purchase price is and what the payment was to show the amount of change due. I know if I were using another language such as C++ or Visual Basic I would have to incorporate a loop for it to continue to ask me for purchase price of each item but I haven't learned anything about that yet for JAVA so what would I need to do for it to ask for multiple items in one purchase.

The for-loop or do-while loop in Java is exactly the same compared to C++ or VB. If you could do it in C++, you should have no problem in Java unless all you did in C++ were not a OO programming at all.

The different that may throw you off right now is syntax. Let's look at CashRegisterTester class. It is similar to a main class of C++. If you understand loop concept, you would know where to put a loop around the code portion in order to create a loop. Also, think of the CashRegister class as a Cashier or a person. Anything or method the class calls is already encapsulated in itself as a real cashier.

Now, look at your code...
You need to put a loop at the right place in CashRegisterTester class

class CashRegisterTester {
  ...
  ...
  boolean done = false;
  // use either do-while or while-do, I will use while-do here
  while (!done) {
    System.out.print("Enter price: ");
    double price = in.nextDouble();
    register.recordPurchase(price);
    ...
    // Need to do something to accept whether or not there are any more items.
    // Don't forget to update the "done" value as well if needed.
    // If you don't do it, you will go into an infinite loop
  }
  ...
  ...
}

The class CashierRegister line 19 of recordPurchase() method, you do not need the local variable here. You could simply add the "purchase" variable with the "amount" because the "purchase" variable is the total amount of what each item price is.

public void recordPurchase(double amount) {
  purchase += amount;  // this is exactly the same syntax as in C++
  // then you count that this item is added in the purchase
  // by increasing the item count right here
  ...
}

The method Counter() in line 39 is not necessary because the purpose of the "purchase" variable is to hold the sum of total amount that a customer needs to pay. It is not an item counter. Besides, you will get a compile error because of the ambiguous of variable name.

What you need is to add a new variable for an item counter, and initialize it as 0 (for clarification and reset). Then each time a purchase of an item is made, increment the counter by 1. When does an item is purchased? When its price is recorded in the total amount *hint hint*.

class CashierRegister {
  ...
  int itemCounter;

  public CashierRegister() {
    ...
    itemCounter = 0;  // reset the value
  }
  ...
  ...
  public double getItemCount() {
    return itemCounter;  // the real number of item purchased
  }
  ...
  ...
}

At the current implementation, your CashRegister instant may not be reused because it keeps all the previous values. Though, this could be later lesson for you but may not be now.

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.