Cash Register for Shopping Program?

Need help setting up a cash register for my program

Page 1 of 1

10 Replies - 1789 Views - Last Post: 19 December 2009 - 03:23 PM Rate Topic: -----

#1 00musdan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 16-November 09

Cash Register for Shopping Program?

Post icon  Posted 18 December 2009 - 09:41 PM

hey guys im back. I am almost done my program all i need is a cash register for my program. I am having trouble finishing it. Any ideas? Ill post my code for my main class, currency class, and cash register class and leave out the inventory classes for my store code... lots of it. If needed i can post all my code if it would help the community.
public class Main {

	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args) {
		 HatInventory myHatList = new HatInventory();
		myHatList.addHat(100, "Red Sox", 23.6678, 1, 10);
		System.out.println("The price of Hat #100 is " +myHatList.getPrice(100));
		System.out.println("Hat purchased is for the team " +myHatList.getTeam(100));
		System.out.println("There are this many hats in the inventory before purchase "+myHatList.getQty(100));

		JacketInventory myJacketList = new JacketInventory();
		myJacketList.addJacket(200, "Red Sox", 55.6678, 1, 10);
		System.out.println("The price of Jacket #200 is " +myJacketList.getPrice(200));
		System.out.println("Jacket purchased is for the team " +myJacketList.getTeam(200));
		System.out.println("There are this many jackets in the inventory before purchase "+myJacketList.getQty(200));

		JerseyInventory myJerseyList = new JerseyInventory();
		myJerseyList.addJersey(300, "Red Sox", 44.5678, 1, 10);
		System.out.println("The price of Jersey #300 is " +myJerseyList.getPrice(300));
		System.out.println("Jersey purchased is for the team " +myJerseyList.getTeam(300));
		System.out.println("There are this many jerseys in the inventory before purchase " +myJerseyList.getQty(300));

		double addtocart = myHatList.purchase(100,1);
		System.out.println("Purchase amount " + addtocart);
		System.out.println("There are this many hats in the inventory after purchase "+myHatList.getQty(100));

		double addtocart2 = myJacketList.purchase(200, 1);
		System.out.println("Purchase amount " + addtocart2);
		System.out.println("There are this many jackets in the inventory after purchase "+myJacketList.getQty(200));

		double addtocart3 = myJerseyList.purchase(300, 1);
		System.out.println("Purchase amount " + addtocart3);
		System.out.println("There are this many jerseys in the inventory after purchase "+myJerseyList.getQty(300));
		
	}

}


public class Currency {
	 public static final double twenty_dollars = 20.0;
	public static final double ten_dollars = 10.0;
	public static final double five_dollars = 5.0;
	public static final double one_dollar = 1.0;
	public static final double twenty_five_cents = .25;
	public static final double ten_cents = .10;
	public static final double five_cents = .05;
	public static final double one_cent = .01;

}



This code is for the Cash register class. For example if the price is 43.26 and the payment is 60, 43.26 is two twenty dollar bills.
public class CashRegister {
 
	public int change;
	 
	giveChange(double price, double pay);
	
	int numTwenties = change/ twenty_dollars;
	change = change%twenty_dollars;

}



Is This A Good Question/Topic? 0
  • +

Replies To: Cash Register for Shopping Program?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Cash Register for Shopping Program?

Posted 18 December 2009 - 10:04 PM

Don't want to brake your fun but this is kind of useless
	public static final double twenty_dollars = 20.0;
	public static final double ten_dollars = 10.0;
	public static final double five_dollars = 5.0;
	public static final double one_dollar = 1.0;
	public static final double twenty_five_cents = .25;
	public static final double ten_cents = .10;
	public static final double five_cents = .05;
	public static final double one_cent = .01;



OK.. you might think... Hey hey stuff is not hardcoded... I have good coding practices... :^:
But you will have to test for twenty_dollars, ten_dollars, five_dollars....
you might as well check for 20.0, 10.0 and 5.0

Yes but if a day they invent the $ 7.00 bill I will only have to add

public static final double seven_dollars = 7.0;

false ! You will have to change your code to check for seven_dollars

However this approach makes lot more sense

int[] currency = {2000, 1000, 500, 100, 25, 10, 5, 1};

now if you have to give 33.57 as change you can

double change = 33.57;
int inCent = (int) change * 100;
for(int i = 0; i < currency.length; i++) {
   int nb = inCent / currency[i];
   System.out.println(nb + " " + currency[i]);
   inCent %= currency[i];
}


now, if they introduce the $ 7.00 bill the only thing to do is to
int[] currency = {2000, 1000, 700, 500, 100, 25, 10, 5, 1};
without having to change your code

Happy coding

This post has been edited by pbl: 18 December 2009 - 10:05 PM

Was This Post Helpful? 0
  • +
  • -

#3 00musdan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 16-November 09

Re: Cash Register for Shopping Program?

Posted 18 December 2009 - 10:14 PM

OK.. you might think... Hey hey stuff is not hardcoded... I have good coding practices... :^:
But you will have to test for twenty_dollars, ten_dollars, five_dollars....
you might as well check for 20.0, 10.0 and 5.0

Yes but if a day they invent the $ 7.00 bill I will only have to add

public static final double seven_dollars = 7.0;

false ! You will have to change your code to check for seven_dollars

However this approach makes lot more sense

int[] currency = {2000, 1000, 500, 100, 25, 10, 5, 1};

now if you have to give 33.57 as change you can

double change = 33.57;
int inCent = (int) change * 100;
for(int i = 0; i < currency.length; i++) {
   int nb = inCent / currency[i];
   System.out.println(nb + " " + currency[i]);
   inCent %= currency[i];
}


now, if they introduce the $ 7.00 bill the only thing to do is to
int[] currency = {2000, 1000, 700, 500, 100, 25, 10, 5, 1};
without having to change your code

Happy coding
[/quote]

That looks like it works but the part where you said i'd have to check for $20 $10 $5 etc. is true! this is exactly what the teacher wants! It may not be the best way but for beginners its the easiest to understand. Sorry i didnt explain earlier. Do you think you could show me that way?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Cash Register for Shopping Program?

Posted 18 December 2009 - 10:20 PM

int nb20 = (int) (change / twenty_dollars);
change = change - (nb20 * twenty_dollars);
int nb10 = (int) (change / ten_dollars);
change = change - (nb10 * ten_dollars);
....
Was This Post Helpful? 0
  • +
  • -

#5 00musdan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 16-November 09

Re: Cash Register for Shopping Program?

Posted 18 December 2009 - 10:40 PM

View Postpbl, on 18 Dec, 2009 - 09:20 PM, said:

int nb20 = (int) (change / twenty_dollars);
change = change - (nb20 * twenty_dollars);
int nb10 = (int) (change / ten_dollars);
change = change - (nb10 * ten_dollars);
....


Hey pbl im getting errors that im missing the symbol twenty_dollars, ten_dollars, five_dollars, etc. in my cashregister class how do i call them because i already declared them in my currency class. and im also missing change even though i thought i initialized it right at the start of the code. :crazy:
public int change;

int nb20 = (int) (change / twenty_dollars);
change = change - (nb20 * twenty_dollars);
int nb10 = (int) (change / ten_dollars);
change = change - (nb10 * ten_dollars);
int nb5 = (int) (change / five_dollars);
change = change - (nb5 * five_dollars);
int nb1 = (int) (change / one_dollar);
change = change - (nb1 * one_dollar);
int nb25cents = (int) (change / twenty_five_cents);
change = change - (nb25cents * twenty_five_cents);
int nb10cents = (int) (change / ten_cents);
change = change - (nb10cents * ten_cents);
int nb5cents = (int) (change / five_cents);
change = change - (nb5cents * five_cents);
int nb1cent = (int) (change / one_cent);
change = change - (nb1cent * one_cent);


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Cash Register for Shopping Program?

Posted 18 December 2009 - 10:47 PM

Because, just to complicated your life, you made the Currency class apart :D

int nb20 = (int) (change / Currency.twenty_dollars);
change = change - (nb20 * Currency.twenty_dollars);
int nb10 = (int) (change / Currency.ten_dollars);
change = change - (nb10 * Currency.ten_dollars);
....



:^:
Was This Post Helpful? 0
  • +
  • -

#7 00musdan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 16-November 09

Re: Cash Register for Shopping Program?

Posted 18 December 2009 - 10:57 PM

[quote name='pbl' date='18 Dec, 2009 - 09:47 PM' post='871308']
Because, just to complicated your life, you made the Currency class apart :D

Weird what you said fixed half of them. Like the twenty_dollars in the first line is fixed but the twenty_dollars in the second line has the error <identifier> expected. and im still getting the error for missing change symbol.
public int change;

int nb20 = (int) (change / Currency.twenty_dollars);
change = change - (nb20 * Currency.twenty_dollars);
int nb10 = (int) (change / Currency.ten_dollars);
change = change - (nb10 * Currency.ten_dollars);
int nb5 = (int) (change / Currency.five_dollars);
change = change - (nb5 * Currency.five_dollars);
int nb1 = (int) (change / Currency.one_dollar);
change = change - (nb1 * Currency.one_dollar);
int nb25cents = (int) (change / Currency.twenty_five_cents);
change = change - (nb25cents * Currency.twenty_five_cents);
int nb10cents = (int) (change / Currency.ten_cents);
change = change - (nb10cents * Currency.ten_cents);
int nb5cents = (int) (change / Currency.five_cents);
change = change - (nb5cents * Currency.five_cents);
int nb1cent = (int) (change / Currency.one_cent);
change = change - (nb1cent * Currency.one_cent);


This post has been edited by 00musdan: 18 December 2009 - 10:58 PM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Cash Register for Shopping Program?

Posted 18 December 2009 - 11:05 PM

public int change;

your change should be double as all your Currency stuff are double... that is why I cast them to (int) before doing the subtraction

Yes your error is weird to me to
repost your code
Was This Post Helpful? 0
  • +
  • -

#9 00musdan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 16-November 09

Re: Cash Register for Shopping Program?

Posted 18 December 2009 - 11:11 PM

View Postpbl, on 18 Dec, 2009 - 10:05 PM, said:

public int change;

your change should be double as all your Currency stuff are double... that is why I cast them to (int) before doing the subtraction

Yes your error is weird to me to
repost your code


ok here is everything and i mean EVERYTHING for my shopping program. My teacher has confirmed that everything is correct so far except i needed the cashregister class which is what we are working on now

public class Hat {
private String team;
private double price;
private int sku,taxable,qty;
private double taxRate=1.06;

	public Hat(int skuNumber, String tm, double cost, int tax,int quantity)
	{
		sku = skuNumber;
		taxable = tax;
		team = tm;
		price = cost;
		qty=quantity;

	}

	public double getPrice(){return price;}
	public double getSku(){return sku;}
	public int getQty(){return qty;}
	public String getTeam(){return team;}
	public double purchase(int amt){
		double amount = price*qty;
		if(taxable==1)
		amount = amount*taxRate;
		int newQty=qty-amt;
		qty=newQty;

		return amount;
	}


}


import java.util.ArrayList; 
/**
 *
 * @author Dan
 */
public class HatInventory {
	  ArrayList<Hat>hatList = new ArrayList<Hat>();

	public void addHat(int skuNum, String team, double price, int tax, int qty)
	{
		hatList.add(new Hat(skuNum, team, price, tax, qty));
	}

public Hat findSku(int searchKey)
{
	for(Hat a : hatList)
	{
		if(a.getSku()==searchKey)
			return a;
	}
   return null;

}
public double getPrice(int sku)
{
	return findSku(sku).getPrice();
}
public String getTeam(int sku)
{
	return findSku(sku).getTeam();
}

public int getQty(int sku)
{
	return findSku(sku).getQty();
}
public double getRoundedPrice(int sku)
{
	double notRoundedPrice = findSku(sku).getPrice()*100;
	int notRoundedPriceInt = (int)Math.round(notRoundedPrice);
	double roundedPrice = notRoundedPriceInt;
	roundedPrice = notRoundedPriceInt/100;
	return roundedPrice;

}

public double purchase(int sku, int qty)
{
	return findSku(sku).purchase(qty);
}

}


public class Jacket {
private String team;
private double price;
private int sku,taxable, qty;
private double taxRate=1.06;

	public Jacket(int skuNumber, String tm, double cost, int tax, int quantity)
	{
		sku = skuNumber;
		taxable = tax;
		team = tm;
		price = cost;
		qty=quantity;
	}

	public double Getprice(){return price;}
	public double Getsku(){return sku;}
	public int getQty(){return qty;}
	public String Getteam(){return team;}
	public double purchase(int amt){
		double amount = price*qty;
		if(taxable==1)
		amount = amount*taxRate;
		 int newQty=qty-amt;
		qty=newQty;
		return amount;
	}


}


public class Jersey {
private String team;
private double price;
private int sku,taxable, qty;
private double taxRate=1.06;

	public Jersey(int skuNumber, String tm, double cost, int tax, int quantity)
	{
		sku = skuNumber;
		taxable = tax;
		team = tm;
		price = cost;
		qty=quantity;
	}

	public double Getprice(){return price;}
	public double Getsku(){return sku;}
	public int getQty(){return qty;}
	public String Getteam(){return team;}
	public double purchase(int amt){
	double amount = price*qty;
	if(taxable==1)
	amount = amount*taxRate;
	int newQty=qty-amt;
	qty=newQty;
	return amount;
	}



}


import java.util.ArrayList;
/**
 *
 * @author Dan
 */
public class JerseyInventory {
  ArrayList<Jersey>jerseyList = new ArrayList<Jersey>();

	public void addJersey(int skuNum, String team, double price, int tax, int qty)
	{
		jerseyList.add(new Jersey(skuNum, team, price, tax, qty));
	}

public Jersey findSku(int searchKey)
{
	for(Jersey a : jerseyList)
	{
		if(a.Getsku()==searchKey)
			return a;
	}
   return null;

}
public double getPrice(int sku)
{
	return findSku(sku).Getprice();
}
public String getTeam(int sku)
{
   return findSku(sku).Getteam();
}
public int getQty(int sku)
{
	return findSku(sku).getQty();
}
public double getRoundedPrice(int sku)
{
	double notRoundedPrice = findSku(sku).Getprice()*100;
	int notRoundedPriceInt = (int)Math.round(notRoundedPrice);
	double roundedPrice = notRoundedPriceInt;
	roundedPrice = notRoundedPriceInt/100;
	return roundedPrice;

}

public double purchase(int sku, int qty)
{
	return findSku(sku).purchase(qty);
}
}


public class Main {

	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args) {
		 HatInventory myHatList = new HatInventory();
		myHatList.addHat(100, "Red Sox", 23.6678, 1, 10);
		System.out.println("The price of Hat #100 is " +myHatList.getPrice(100));
		System.out.println("Hat purchased is for the team " +myHatList.getTeam(100));
		System.out.println("There are this many hats in the inventory before purchase "+myHatList.getQty(100));

		JacketInventory myJacketList = new JacketInventory();
		myJacketList.addJacket(200, "Red Sox", 55.6678, 1, 10);
		System.out.println("The price of Jacket #200 is " +myJacketList.getPrice(200));
		System.out.println("Jacket purchased is for the team " +myJacketList.getTeam(200));
		System.out.println("There are this many jackets in the inventory before purchase "+myJacketList.getQty(200));

		JerseyInventory myJerseyList = new JerseyInventory();
		myJerseyList.addJersey(300, "Red Sox", 44.5678, 1, 10);
		System.out.println("The price of Jersey #300 is " +myJerseyList.getPrice(300));
		System.out.println("Jersey purchased is for the team " +myJerseyList.getTeam(300));
		System.out.println("There are this many jerseys in the inventory before purchase " +myJerseyList.getQty(300));

		double addtocart = myHatList.purchase(100,1);
		System.out.println("Purchase amount " + addtocart);
		System.out.println("There are this many hats in the inventory after purchase "+myHatList.getQty(100));

		double addtocart2 = myJacketList.purchase(200, 1);
		System.out.println("Purchase amount " + addtocart2);
		System.out.println("There are this many jackets in the inventory after purchase "+myJacketList.getQty(200));

		double addtocart3 = myJerseyList.purchase(300, 1);
		System.out.println("Purchase amount " + addtocart3);
		System.out.println("There are this many jerseys in the inventory after purchase "+myJerseyList.getQty(300));
		
	}

}


import java.util.ArrayList;
/**
 *
 * @author Dan
 */
public class JacketInventory {
 ArrayList<Jacket>jacketList = new ArrayList<Jacket>();

	public void addJacket(int skuNum, String team, double price, int tax, int qty)
	{
		jacketList.add(new Jacket(skuNum, team, price, tax, qty));
	}

public Jacket findSku(int searchKey)
{
	for(Jacket a : jacketList)
	{
		if(a.Getsku()==searchKey)
			return a;
	}
   return null;

}
public double getPrice(int sku)
{
	return findSku(sku).Getprice();
}
public String getTeam(int sku)
{
	return findSku(sku).Getteam();
}
public int getQty(int sku)
{
	return findSku(sku).getQty();
}
public double getRoundedPrice(int sku)
{
	double notRoundedPrice = findSku(sku).Getprice()*100;
	int notRoundedPriceInt = (int)Math.round(notRoundedPrice);
	double roundedPrice = notRoundedPriceInt;
	roundedPrice = notRoundedPriceInt/100;
	return roundedPrice;

}

public double purchase(int sku, int qty)
{
	return findSku(sku).purchase(qty);
}
}


public class Currency {
	 public static final double twenty_dollars = 20.0;
	public static final double ten_dollars = 10.0;
	public static final double five_dollars = 5.0;
	public static final double one_dollar = 1.0;
	public static final double twenty_five_cents = .25;
	public static final double ten_cents = .10;
	public static final double five_cents = .05;
	public static final double one_cent = .01;

}


public class CashRegister {
public double change;

int nb20 = (int) (change / Currency.twenty_dollars);
change = change - (nb20 * Currency.twenty_dollars);
int nb10 = (int) (change / Currency.ten_dollars);
change = change - (nb10 * Currency.ten_dollars);
int nb5 = (int) (change / Currency.five_dollars);
change = change - (nb5 * Currency.five_dollars);
int nb1 = (int) (change / Currency.one_dollar);
change = change - (nb1 * Currency.one_dollar);
int nb25cents = (int) (change / Currency.twenty_five_cents);
change = change - (nb25cents * Currency.twenty_five_cents);
int nb10cents = (int) (change / Currency.ten_cents);
change = change - (nb10cents * Currency.ten_cents);
int nb5cents = (int) (change / Currency.five_cents);
change = change - (nb5cents * Currency.five_cents);
int nb1cent = (int) (change / Currency.one_cent);
change = change - (nb1cent * Currency.one_cent);

}


Was This Post Helpful? 0
  • +
  • -

#10 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Cash Register for Shopping Program?

Posted 19 December 2009 - 10:56 AM

So if everything is correct... what do you need our help with?
Was This Post Helpful? 0
  • +
  • -

#11 00musdan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 16-November 09

Re: Cash Register for Shopping Program?

Posted 19 December 2009 - 03:23 PM

View Postmarkhazlett9, on 19 Dec, 2009 - 09:56 AM, said:

So if everything is correct... what do you need our help with?


Finishing the CashRegister class
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1