Cash Register Exercise

Use Cash Register & Coin Classes

Page 1 of 1

5 Replies - 17567 Views - Last Post: 23 January 2010 - 11:03 AM Rate Topic: -----

#1 ldunham   User is offline

  • New D.I.C Head

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

Cash Register Exercise

Post icon  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;
		}

		//Enters the amount received.
		  //@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   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 ldunham   User is offline

  • New D.I.C Head

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

Re: Cash Register Exercise

Posted 21 January 2010 - 07:00 PM

View Postn8wxs, 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

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12390
  • View blog
  • Posts: 45,521
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 ldunham   User is offline

  • New D.I.C Head

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

Re: Cash Register Exercise

Posted 23 January 2010 - 10:57 AM

View Postmacosxnerd101, 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?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12390
  • View blog
  • Posts: 45,521
  • 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. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1