Good Morning,
Having a problem with my program not calculating. I have wrote a program CashRegisterA Class that should be used with the RetailItem Class. The CashRegisterA Class should stimulate the sale of the retail item. It should have a constructor that accepts a RetailItem object as an argument. The constructor should also accept an integer that represents the quantity of items being purchased.

CashRegisterA

import java.util.Scanner;

   public class CashRegisterA
   {
   
   //Attributes
      public double retailCost;
      public int quantity;
      final double TAX_RATE = 0.06;
      double subtotal;	
      double Tax;
      double total;	
   //	File salesReceipt;
   	
      RetailItem myRetail = new RetailItem();
   	   
   //Constructors
      public CashRegisterA()
      {

      }
   	//default constructor
   
      public CashRegisterA(int q)
      {
         quantity = q;
      }
   
   //Methods
   
      public double getSubTotal()
      {
         subtotal = retailCost * quantity;
         return subtotal;
      }
      
      public double getTax()
      {
         Tax = retailCost * TAX_RATE;
         return Tax;
      }
   
      public double getTotal()
      {
         total = subtotal + Tax;
         return total;			
      }
    
   }

RetailItem

import java.util.Scanner;


public class RetailItem {
	
	private String description;
	private int unitsOnHand;
	private double price;
	
	public RetailItem(RetailItem i) {
		this.description = i.description;
		this.unitsOnHand = i.unitsOnHand;
		this.price = i.price;
	}
	
	public RetailItem() {
		description = null;
		unitsOnHand = 0;
		price = 0.00;
	}
	
	public RetailItem(String d, int u, double p) {
		this.description = d;
		this.unitsOnHand = u;
		this.price = p;
	}
	
	public RetailItem(String d) {
		this.description = d;
		this.unitsOnHand = 0;
		this.price = 0.00;
	}
	
	public RetailItem(int u) {
		this.description = null;
		this.unitsOnHand = u;
		this.price = 0.00;
	}
	
	public RetailItem(double p) {
		this.description = null;
		this.unitsOnHand = 0;
		this.price = p;
	}
	
	public void setDescription(String d) {
		this.description = d;
	}
	
	public void setUnitsOnHand(int u) {
		this.unitsOnHand = u;
	}
	
	public void setPrice(double p) {
		this.price = p;
	}
	
	public String getDescription() {
		return description;
	}
	
	public int getUnitsOnHand() {
		return unitsOnHand;
	}
	
	public double getPrice() {
		return price;
	}
	
	public String toString() {
		return "Description: " + description + "\nUnits on hand: " + unitsOnHand + "\nPrice: $" + price;
	}
}

Transaction

import java.util.Scanner;
   import java.text.DecimalFormat;


   public class Transaction
   {
   
   //Methods
   
      public static void main(String[] args)
		{
         String itemName = " ";
         double itemCost = 0.00;
            
      
         Scanner sc = new Scanner(System.in);
         DecimalFormat df = new DecimalFormat("#0.00");
      
         RetailItem myRetailItem = new RetailItem();
         CashRegisterA myCashRegisterA = new CashRegisterA();
      
         System.out.print("What is the name of the item being purchased? : " );
         itemName = sc.nextLine();
            
         System.out.print("How many " + itemName + "(s) are you buying? ");
         myCashRegisterA.quantity = sc.nextInt();
      
      
         System.out.println();
         

         System.out.print("Subtotal:     " + df.format(myCashRegisterA.getSubTotal()));
         System.out.println();
			
         System.out.print("Salestax:     " + df.format(myCashRegisterA.getTax())); 
			System.out.println();
			
         System.out.print("Total:        " + df.format(myCashRegisterA.getTotal()));
        
      
      }
    }

What problem exactly are you having - please give expected vs actual results.

ps: Shouldn't tax depend on the quantity * price, not just the unit price?

I am expecting to get the subtotal, tax, and total. I am getting 0.00 for all of them. Tax is dependent on the 0.06 * the retail cost. Quantity is used in the subtotal.

OK, if you say that's how you want to calculate tax that's up to you. It's just not how tax is calculated in real life.
Anyway. You don't seem to set price anywhere, and its default is 0, so I would expect all your results to be zero as well.

My setPrice is in the code below:

RetailItemDemo

import java.util.Scanner;

public class RetailItemDemo
{
   public static void main(String[] args)
   {
      // Create the first item. Use the no-arg constructor.
		RetailItem item1 = new RetailItem();
      item1.setDescription("Jacket");
      item1.setUnitsOnHand(12);
      item1.setPrice(59.95);
      
      // Create the second item. Use the constructor
		// to initialize the fields.
		RetailItem item2 = 
		     new RetailItem("Designer Jeans", 40, 34.95);

      // Create the third item. Use the no-arg constructor.
		RetailItem item3 = new RetailItem();
      item3.setDescription("Shirt");
      item3.setUnitsOnHand(20);
      item3.setPrice(24.95);
      
      // Display the info for item1.
      System.out.println("Item #1");
      System.out.println("Description: " + item1.getDescription());
      System.out.println("Units on hand: " + item1.getUnitsOnHand());
      System.out.println("Price: " + item1.getPrice());

      // Display the info for item2.
      System.out.println("\nItem #2");
      System.out.println("Description: " + item2.getDescription());
      System.out.println("Units on hand: " + item2.getUnitsOnHand());
      System.out.println("Price: " + item2.getPrice());

      // Display the info for item3.
      System.out.println("\nItem #3");
      System.out.println("Description: " + item3.getDescription());
      System.out.println("Units on hand: " + item3.getUnitsOnHand());
      System.out.println("Price: " + item3.getPrice());
   }
}

no its not working. Some how its not calling the item to make the calculations! The user inputs one of the items and it should call the item and make the calculations the output the subtotal, tax, and total

Edited 6 Years Ago by blknmld69: n/a

I now see two classes, each with its own main(...) method. One main sets prices etc, but doesn't call any calculations. The other calls the calculations, but doesn't set the prices. I don't know which one you are running, but either way it's not enough.
You need to combine both sets of code into one main method that sets prices/descriptions etc, then prompts for the order and prints the calculation.

First off, you never set retailPrice , which presumably should be getting it from myRetail.price , or else from a method of some sort.

Also, the price for myRetailItem isn't being set in the Transaction class' main() function. Furthermore, the myRetailItem object in Transaction is not the same as the one in the myCashRegisterA object (and change the default c'tor so that you initialized an empty myRetail there instead of at the declaration), so setting that price has no effect on the one which would be used to calculate the total.

Most likely, you need to set up a non-default constructor for CashRegisterA which can take a RetailItem object as an argument, which would then be used as the value of myRetail :

public CashRegisterA(RetailItem ri, int q) {
        myRetail = ri;
        quantity = q;
        retailCost = myRetail.getPrice();
    }

    public CashRegisterA(int q) {
        this(new RetailItem(), q);
    }

    public CashRegisterA() {
        this(0);
    }

Edited 6 Years Ago by Schol-R-LEA: n/a

ok so i need the Transaction class to call the retailItemDemo and the CashregisterA Class. So I should combine retailItemDemo and CashregisterA. How could I have the Transaction call those to after I combine them?

I don't think you would need RetailItemDemo in Transaction , but you will definitely need RetailItem and CashRegisterA . What I would suggest is to create the c'tor I mentioned above, and then create myCashRegisterA like so:

RetailItem myRetailItem = new RetailItem("Widget", 10, 10.00);
         CashRegisterA myCashRegisterA = new CashRegisterA(myRetailItem, 4);

With the name and amounts set to whatever you feel suitable.

Edited 6 Years Ago by Schol-R-LEA: n/a

the price should be coming from the RetailItemDemo. Ok i did this with the code:

import java.util.Scanner;

   public class CashRegisterA
   {
   
   //Attributes
      public double retailCost;
      public int quantity;
      final double TAX_RATE = 0.06;
      double subtotal;	
      double Tax;
      double total;	
  
   	
      RetailItem myRetail = new RetailItem();
   	   
   //Constructors
      public CashRegisterA(RetailItem ri, int q) 
      {
         myRetail = ri;
         quantity = q;
         retailCost = myRetail.getPrice();
      }
   	//default constructor
   
      public CashRegisterA(int q) 
      {
         this(new RetailItem(), q);
      }
      
   
      public CashRegisterA() 
      {
         this(0);
      }
   
   //Methods
   
      public double getSubTotal()
      {
         subtotal = retailCost * quantity;
         return subtotal;
      }
      
      public double getTax()
      {
         Tax = retailCost * TAX_RATE;
         return Tax;
      }
   
      public double getTotal()
      {
         total = subtotal + Tax;
         return total;			
      }
     
   }

Output still not calculating

What is the name of the item being purchased? : Shirt
How many Shirt(s) are you buying? 2

Subtotal: 0.00
Salestax: 0.00
Total: 0.00

You still need to change Transaction so that it gives a price for the RetailItem in question. Here's one way to do it:

import java.util.Scanner;
import java.text.DecimalFormat;

public class Transaction  {
   
    //Methods
   
    public static void main(String[] args)    {
        String itemName = " ";
        double itemCost = 10.00;
        
        Scanner sc = new Scanner(System.in);
        DecimalFormat df = new DecimalFormat("#0.00");
     
        System.out.print("What is the name of the item being purchased? : " );
        itemName = sc.nextLine();
           
        System.out.print("How many " + itemName + "(s) are you buying? ");
        int quantity = sc.nextInt();
      
        System.out.println();
        
        RetailItem myRetailItem = new RetailItem(itemName, quantity, itemCost);
        CashRegisterA myCashRegisterA = new CashRegisterA(myRetailItem, quantity);

        System.out.print("Subtotal:     " + df.format(myCashRegisterA.getSubTotal()));
        System.out.println();
        
        System.out.print("Salestax:     " + df.format(myCashRegisterA.getTax())); 
        System.out.println();
            
        System.out.print("Total:        " + df.format(myCashRegisterA.getTotal()));
    }
}

Edited 6 Years Ago by Schol-R-LEA: n/a

But the cost should be read from the RetailItemDemo or RetailItem where The price of the Jacket, Shirt, and Designer jeans:

// Create the first item. Use the no-arg constructor.
		RetailItem item1 = new RetailItem();
      item1.setDescription("Jacket");
      item1.setUnitsOnHand(12);
      item1.setPrice(59.95);
      
      // Create the second item. Use the constructor
		// to initialize the fields.
		RetailItem item2 = 
		     new RetailItem("Designer Jeans", 40, 34.95);

      // Create the third item. Use the no-arg constructor.
		RetailItem item3 = new RetailItem();
      item3.setDescription("Shirt");
      item3.setUnitsOnHand(20);
      item3.setPrice(24.95);
      
      // Display the info for item1.
      System.out.println("Item #1");
      System.out.println("Description: " + item1.getDescription());
      System.out.println("Units on hand: " + item1.getUnitsOnHand());
      System.out.println("Price: " + item1.getPrice());

      // Display the info for item2.
      System.out.println("\nItem #2");
      System.out.println("Description: " + item2.getDescription());
      System.out.println("Units on hand: " + item2.getUnitsOnHand());
      System.out.println("Price: " + item2.getPrice());

      // Display the info for item3.
      System.out.println("\nItem #3");
      System.out.println("Description: " + item3.getDescription());
      System.out.println("Units on hand: " + item3.getUnitsOnHand());
      System.out.println("Price: " + item3.getPrice());

My program works compiles but when I enter different quantities its give the same output.

CashRegisterMain

import java.util.*;
   import java.text.DecimalFormat;


   public class CashRegisterMain
   {
      public static void main(String[] args) 
      {
 			int quantity = 0;
			double itemCost = 0;

			
			//Create a RetailItem object
			RetailItem item2 = new RetailItem("Designer Jeans", 40, 34.95);
			
			RetailItem myRetailItem = new RetailItem("Designer Jeans", 40, 34.95);
			CashRegister myCashRegister = new CashRegister(myRetailItem, 1);
         Scanner keyboard = new Scanner(System.in);
			DecimalFormat df = new DecimalFormat("#0.00");

      	// Display the current items.
			System.out.println("The item available for sale is: " + "\n" + item2); 
			
			
      	System.out.println();
         // Get the number of items.      
         System.out.println("How many item(s) would you like to purchase: ");
         quantity = keyboard.nextInt();
      
			// Create a RetailItem object for the transaction
			CashRegister buy = new CashRegister(item2, quantity);
   					   
		   System.out.println();
         System.out.println("Subtotal:  " + df.format(myCashRegister.getSubTotal()));
			System.out.println("     Tax:   " + df.format(myCashRegister.getTax())); 
		   System.out.println("   Total:  " + df.format(myCashRegister.getTotal()));

      }
   }

CashRegister

import java.util.Scanner;
   import java.text.DecimalFormat;
   
   public class CashRegister
   {
      private RetailItem item;
      private int quantity;
      private double subTotal;
      private double salesTax;
      private double total;
   	
   	/**
   		Constructor
   		@param RetailItem The item to purchase'
   		@param quantity   The number of items.
   	*/
   	
      public CashRegister(RetailItem i, int q) 
      {
      //Create a copy of the object referenced by 
      //Retail Item.
         this.item = new RetailItem(i);
         quantity = q;
      }
   	/**
   		getItem method
   		@return A copy of the RetailItem object 
   		for the item being purchased.
   	*/
   
      public RetailItem getRetailItem()
      {
      //Retun a copy of the object referenced by item
		RetailItem item2 = new RetailItem("Designer Jeans", 40, 34.95);
      return new RetailItem();
      }
   
   	/**
   		getUnit method
   		@return The number of items being puchased.
   	*/
   
      public int getQuantity()
      {
         return quantity;
      }
   
   	/**
   		getsubtotal method
   		@return The subtotal of the item puchased
   	*/
   
      public double getSubTotal() 
      {
         subTotal = item.getPrice() * quantity;
         return subTotal;
      }
   
   	/**
   		getSalesTax method
   		@return The sales tax of the items puchased
   	*/
		
      public double getTax() 
		{
         salesTax = this.getSubTotal() * .06;
         return salesTax;
      }
		
		/**
			getTotal method
			@return The total of the items puchased
		*/

      public double getTotal() 
		{
         total = this.getSubTotal() + this.getTax();
         return total;
      }
   }

Output

The item available for sale is:
Description: Designer Jeans
Units on hand: 40
Price: $34.95

How many item(s) would you like to purchase:
2

Subtotal: 34.95
Tax: 2.10
Total: 37.05

You have a problem with excessive use of the "new" keyword.
You keep creating new RetailItems with the same data, but these will all be different items, and it's hopeless trying to keep track the state and contents of them all.
You should create your three different RetailItems just once, right at the start of your program, then every reference to a RetailItem after that should be a reference to one of those three. You should not have any "new RetailItem(" code anywhere else in your program.

Ok so how can I get my program to take the user input(quantity) and use it in my CashRegister class for the getSubTotal method?

CashRegister Class

import java.util.Scanner;
   import java.text.DecimalFormat;
   
   public class CashRegister
   {
      private RetailItem item;
      private int quantity;
		private double price;
      private double subTotal;
      private double salesTax;
      private double total;
	
   	
   	/**
   		Constructor
   		@param RetailItem The item to purchase.
   		@param quantity   The number of items.
			@param price		The price of item.
   	*/
   	
      public CashRegister(RetailItem i, int q) 
      {
      //Create a copy of the object referenced by 
      //Retail Item.
         this.item = new RetailItem(i);
         quantity = q;
      }
   	/**
   		getItem method
   		@return A copy of the RetailItem object 
   		for the item being purchased.
   	*/
   
      public RetailItem getRetailItem()
      {
		
      //Retun a copy of the object referenced by item
		RetailItem myRetailItem = new RetailItem("Designer Jeans", 40, 34.95);
		CashRegister myCashRegister = new CashRegister(myRetailItem, 1);

		      return new RetailItem();
      }
   
		/**
			The getPrice method rerurns the item's price.
			@return The item's price.
		*/

		public double getPrice()
		{
			return price;
		}


	
   	/**
   		getUnit method
   		@return The number of items being puchased.
   	*/
   
      public int getQuantity()
      {
		     return quantity;
      }
   
   	/**
   		getsubtotal method
   		@return The subtotal of the item puchased
   	*/
   
      public double getSubTotal() 
      {
         subTotal = item.getPrice() * quantity;
         return subTotal;
      }
   
   	/**
   		getSalesTax method
   		@return The sales tax of the items puchased
   	*/
		
      public double getTax() 
		{
         salesTax = this.getSubTotal() * .06;
         return salesTax;
      }
		
		/**
			getTotal method
			@return The total of the items puchased
		*/

      public double getTotal() 
		{
         total = this.getSubTotal() + this.getTax();
         return total;
      }
   }

CashRegisterMain

import java.util.*;
   import java.text.DecimalFormat;


   public class CashRegisterMain
   {
      public static void main(String[] args) 
      {
			int quantity = 0;
			double itemCost = 0;
			String input;

			
			//Create a RetailItem object
						
			RetailItem myRetailItem = new RetailItem("Designer Jeans", 40, 34.95);
			CashRegister myCashRegister = new CashRegister(myRetailItem, quantity);
         Scanner keyboard = new Scanner(System.in);
			DecimalFormat df = new DecimalFormat("#0.00");

      	// Display the current items.
			System.out.println("The item available for sale is: " + "\n" + myRetailItem); 
			
			
      	System.out.println();
         // Get the number of items.      
         System.out.println("How many item(s) would you like to purchase: ");
         quantity = keyboard.nextInt();
      
			// Create a RetailItem object for the transaction

			CashRegister buy = new CashRegister(myRetailItem, quantity);
   					   
		   System.out.println();
         System.out.println("Subtotal:  " + df.format(myCashRegister.getSubTotal()));
			System.out.println("     Tax:   " + df.format(myCashRegister.getTax())); 
		   System.out.println("   Total:  " + df.format(myCashRegister.getTotal()));

      }
   }

You haven't looked at your "new" problem, have you. Just look at this piece of code for example:

public RetailItem getRetailItem()
{
 
//Retun a copy of the object referenced by item
RetailItem myRetailItem = new RetailItem("Designer Jeans", 40, 34.95);
CashRegister myCashRegister = new CashRegister(myRetailItem, 1);
 
return new RetailItem();
}

Line 5 creates a new RetailItem "designer jeans" (regardless of what the item should be)
Line 6 creates a new myCashRegister, but that doesn't matter because you never use it, and it goes out of scope 2 lines later and just gets garbage collected
Line 8 ignores all that a returns yet another new RetailItem with default values.

Until you sort all your incorrect "new"s out you will obviously have problems.

Ok I deleted the CashRegister myCashRegister = new CashRegister(myRetailItem, 1); then when I compile the CashRegisterMain...the output shows:

How many item(s) would you like to purchase:
2

Subtotal: 0.00
Tax: 0.00
Total: 0.00
when I removed line 8 which you say is garbage I get the error missing return!

Obviously you can't leave the return out. Be sensible. It's a question of what you return.
You still have other errors, which you will find by inserting lots of print statements.
It's time to take a little step back and look at what these methods are doing, just deleting things because they are wrong is not the whole answer. Try drawing a couple of diagrams for your classes showing what data is where and how it is accessed. And finally - no new RetailItems except in the initialisation!

Edited 6 Years Ago by JamesCherrill: n/a

I just need to know if there is a way I can string the input to the int quantity?

This article has been dead for over six months. Start a new discussion instead.