6 Replies - 3271 Views - Last Post: 29 October 2009 - 03:41 PM Rate Topic: -----

#1 xxshexx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-October 09

printing a bill

Post icon  Posted 29 October 2009 - 10:09 AM

my program compiles however it isnt giving me the right output. I am getting 0 for everything when I should be getting specific amounts. Please tell me what i am doing wrong!!

public class TotalReceipt
{
	private double milk=1.89;
	private double sugar=0.60;
	private double candy=0.89;
	private double gloves=5.99;
	private double apples=1.29;
	private double milkTotal;
	private double sugarTotal;
	private double candyTotal;
	private double glovesTotal;
	private double applesTotal;
	private double sum;
	private double taxRate;
	private double quantity;
	private double totalBeforeTax;
	private double taxRateSum;
	private double totalReceipt;
	private double amountTendered = 100.00;
	public double TotalReceipt(double milk, double sugar, double candy, double gloves, double apples)
	{
		milk=1.89;
		sugar=0.60;
		candy=0.89;
		gloves=5.99;
		apples=1.29;
		
		totalReceipt= milk+ sugar+candy+gloves+apples;
		return totalReceipt;
	}
   public double calculateTotalReceipt()
   {
	   totalReceipt=1.89+0.60+0.89+5.99+1.29;
	   return totalReceipt;
	}
	 public void setMilk()
	 {	
		quantity=5;
		taxRate=0.00;
	  }
	public void setSugar()
	{	
		quantity=10;
		taxRate=0.08;
	}
	public void setCandy()
	{
		quantity=20;
		taxRate=0.08;
	}
	public void setGloves()
	{
		quantity=4;
		taxRate=0.04;
	}
	public void setApples()
	{	
		quantity=6.5;
		taxRate=0.0;
	}
 
  public double getTaxRateSum()
	{
		taxRateSum= (0.60*0.08+0.89*0.08+5.99*0.04);
		return taxRateSum;
	}
  public double getMilkTotal()
		{
			milkTotal=(milk*taxRate + milk*quantity);
			return milkTotal;
		}
  public double getSugarTotal()
		{   sugarTotal=(sugar*taxRate + sugar*quantity);   
			return sugarTotal;
		}
  public double getCandyTotal()
		{
			candyTotal=(candy*taxRate + candy*quantity);
			return candyTotal;
		}
  public double getGlovesTotal()
		{ 
			glovesTotal=(gloves*taxRate + gloves*quantity);
			return glovesTotal;
		}
  public double getApplesTotal()
		{
			applesTotal = (apples*taxRate + apples*quantity);
			return applesTotal;
		}
  
			
  public double getSum()
			{
				  sum=(milkTotal+sugarTotal+candyTotal+glovesTotal+applesTotal);
			
			   return sum;
			}
		   
		
{	  
System.out.println("Total Before Tax = $" +totalReceipt);
System.out.println("+ tax = $" +taxRateSum);
System.out.println("Total Amount Due = $" +sum);
System.out.println("Amount Tendered = $100.00");
System.out.println("Change Returned = $" +(amountTendered - sum));
}
	
				
	}  



Is This A Good Question/Topic? 0
  • +

Replies To: printing a bill

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: printing a bill

Posted 29 October 2009 - 10:21 AM

first, explain what do you expect your program to do?
however, in first glance i noticed some problems in your code.
lets take milk in my example, but there are more variables that you repeat this mistake with.
you declare milk as a global variable and assigning it a value.
(better in such case to declare milk as a constant as: private final static double milk = 1.89;)
then you assign it the same value in your constructor.
also your constructor gets some parameters, however you do nothing with these parameter.
because you assign milk with "1.89" and not the value you get from the parameter double milk.

so my suggestion, is that you declare all variables which have a constant value as constants.
also your constructor shouldn't get parameters if you dont use your values.
and most importan. why do you return a value from your constructor?
Was This Post Helpful? 0
  • +
  • -

#3 xxshexx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-October 09

Re: printing a bill

Posted 29 October 2009 - 10:31 AM

i want my program to create a receipt of the items shown with milk priced at 1.89 with quantity 5 and no tax
sugar priced at .60 with quantity 10 at 8%tax, candy priced at .89 with quantity 20 at 8%tax, gloves at 5.99 with quantity 4 at 4%tax, and apples for 1.29 quantity 6.5 with no tax.

i tried to have a constructor with no parameters and no return but everytime i compile i keep getting an error that i need a return statement?
Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: printing a bill

Posted 29 October 2009 - 10:39 AM

the constructor should be without a return datatype, so remove the double from the line you declare it.
your constructor as i see it should be (assuming apple,milk etc are global and constants)
public TotalReceipt()
	{ 
		totalReceipt= milk+ sugar+candy+gloves+apples;
	}



as simple as that :)
Was This Post Helpful? 0
  • +
  • -

#5 xxshexx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-October 09

Re: printing a bill

Posted 29 October 2009 - 10:53 AM

okay great thanks that makes perfect sense! however, i still am confused in why my results are all 0.00.

i am supposed to have:
amount before tax = 64.95
+ tax=2.87
total amount= 67.82
amount tendered = 100.00
change returned=100.0

the only answer that i have correct is amount tendered because that is in my print statement. could you please tell me what im doing wrong to why my data isnt being printed?

here is my up to date code
public class TotalReceipt
{
	private final double milk=1.89;
	private final double sugar=0.60;
	private final double candy=0.89;
	private final double gloves=5.99;
	private final double apples=1.29;
	private double milkTotal;
	private double sugarTotal;
	private double candyTotal;
	private double glovesTotal;
	private double applesTotal;
	private double sum;
	private double taxRate;
	private double quantity;
	private double totalBeforeTax;
	private double taxRateSum;
	private double totalReceipt;
	private double amountTendered = 100.00;
	
	public TotalReceipt()
	{
		totalReceipt=milk+sugar+candy+gloves+apples;
	}
	
	public double getTotalBeforeTax()
	{
		return (milk+sugar+candy+gloves+apples);
	}
	public void setMilk()
	 {	
		quantity=5;
		taxRate=0.00;
	  }
	public void setSugar()
	{	
		quantity=10;
		taxRate=0.08;
	}
	public void setCandy()
	{
		quantity=20;
		taxRate=0.08;
	}
	public void setGloves()
	{
		quantity=4;
		taxRate=0.04;
	}
	public void setApples()
	{	
		quantity=6.5;
		taxRate=0.0;
	}

  public double calculateTaxRateSum()
	{
		taxRateSum= (sugar*taxRate+candy*taxRate+gloves*taxRate );
		return taxRateSum;
	}
  public double getMilkTotal()
		{
			milkTotal=(milk*taxRate + milk*quantity);
			return milkTotal;
		}
  public double getSugarTotal()
		{   sugarTotal=(sugar*taxRate + sugar*quantity);   
			return sugarTotal;
		}
  public double getCandyTotal()
		{
			candyTotal=(candy*taxRate + candy*quantity);
			return candyTotal;
		}
  public double getGlovesTotal()
		{ 
			glovesTotal=(gloves*taxRate + gloves*quantity);
			return glovesTotal;
		}
  public double getApplesTotal()
		{
			applesTotal = (apples*taxRate + apples*quantity);
			return applesTotal;
		}
  
			
  public double getSum()
			{
				  sum=(milkTotal+sugarTotal+candyTotal+glovesTotal+applesTotal);
			
			   return sum;
			}
		   
		
{	  
System.out.println("Total Before Tax = $" +totalBeforeTax);
System.out.println("+ tax = $" +taxRateSum);
System.out.println("Total Amount Due = $" +sum);
System.out.println("Amount Tendered = $100.00");
System.out.println("Change Returned = $" +(amountTendered - sum));
}
	
				
	}  

	

	

Was This Post Helpful? 0
  • +
  • -

#6 xxshexx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-October 09

Re: printing a bill

Posted 29 October 2009 - 12:29 PM

can anyone please help??? my code is listed above with my question
Was This Post Helpful? 0
  • +
  • -

#7 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: printing a bill

Posted 29 October 2009 - 03:41 PM

that is because you change the variables values inside your methods, but when you want to print them, you print the variables. NOT the value returned from a method!
//in your code you print only the variables.
System.out.println("Total Before Tax = $" +totalBeforeTax);
System.out.println("+ tax = $" +taxRateSum);
System.out.println("Total Amount Due = $" +sum);


try change it to this code:
//here i print the value returned from your methods
System.out.println("Total Before Tax = $" +getTotalBeforeTax());
System.out.println("+ tax = $" +calculateTaxRateSum());
System.out.println("Total Amount Due = $" +getSum());


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1