7 Replies - 7691 Views - Last Post: 04 February 2011 - 11:56 AM Rate Topic: -----

#1 Guest_MCN*


Reputation:

Retail Store Java Program

Posted 04 February 2011 - 08:42 AM

RetailItem Class

Write a class named RetailItem that holds data about an item in a retail store. The class should have the following fields:

description. The description field references a String object that holds a brief description of the item.

unitsOnHand. The unitsOnHand field is an int variable that holds the numbers of units currently in inventory.

price. The price filed is a double that holds the item's retail price.
Write a constructor that accepts arguements for each field, appropriate mutator methods that store values in these fields, and accessor methods that returns the values in these fields. Once you have written the class, write a separate program that creates three RetailItem objects and stores the following data in them:

Description Units on Hand Price

Item #1 Jacket 12 59.95
Item #2 Designer Jeans 40 34.95
Item #3 Shirt 20 24.95


this is what I have for my retail item class

/**
Retail item class that holds data about the items
*/

public class RetailItem

{

	//fields
	private String description;		// description of item
	private int unitsOnHand;		// number of items
	private double retailPrice;		// price of item

	//constructor
	
	public RetailItem(String type, int unit, double price);

	{
		description = type;
		unitsOnHand = unit;
		retailPrice = price;

	}

	/**
	set methods to appropriate name
	*/

	public void setDescription(String type)

	{
		description = type;

	}

	public void setUnitsOnHand(int unit)
	
	{
		unitsOnHand = unit;

	}

	public void setRetailPrice(double price)

	{
		retailPrice = price;

	}
	
	/**
	get method and return value of each variable
	*/


	public String getDescription()

	{
		return description;

	}

	public int getUnitsOnHand()
	
	{
		return unitsOnHand;

	}
	
	public double getRetailPrice()

	{

		return retailPrice;

	}
}




and this is what I have for the program that creates the objects and stores their data.

public class RetailConstructor

	{

		public static void main (String[ ] args)
	
		{	
			//define object
			RetailItem jacket = new RetailItem();
			RetailItem jeans = new RetailItem();
			RetailItem shirt = new RetailItem();


			//set number of items and price	
			jacket.setDescription(12, 59.95);
			jeans.setDescription(40, 34.95);
			shirt.setDescription(20, 24.95);

	

			//display all data
			System.out.println("There are" jacket.getDescription() +
				          "at" jacket.getDescription() "each");


			System.out.println("There are" jeans.getDescription() +
				          "at" jeans.getDescription() "each");	


			System.out.println("There are" shirt.getDescription() +
				          "at" shirt.getDescription() "each");	

	}
}



I'm having my issues with the second portion of code. It will not display the data for each item.

Is This A Good Question/Topic? 0

Replies To: Retail Store Java Program

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

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

Re: Retail Store Java Program

Posted 04 February 2011 - 09:03 AM

Why do you call getDescription() twice?
System.out.println("There are" jacket.getDescription() +  
   "at" jacket.getDescription() "each");

Perhaps you mean to call getRetailPrice() instaed?
Was This Post Helpful? 0
  • +
  • -

#3 Guest_MCN*


Reputation:

Re: Retail Store Java Program

Posted 04 February 2011 - 09:06 AM

View Postjapanir, on 04 February 2011 - 09:03 AM, said:

Why do you call getDescription() twice?
System.out.println("There are" jacket.getDescription() +  
   "at" jacket.getDescription() "each");

Perhaps you mean to call getRetailPrice() instaed?



because in

jacket.setDescription(12, 59.95);




I declared the number of units and the price
Was This Post Helpful? 0

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

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

Re: Retail Store Java Program

Posted 04 February 2011 - 09:20 AM

I see now....
The problem is in setDescription method.
That is the way you declare it:
public void setDescription(String type)

it gets only a String object as parameter.
However when you call it in the class RetailConstructor, you try to pass it 2 parameters:
jacket.setDescription(12, 59.95);

non of them is String.

You should call rhe appropriate methods, and pass them the appropriate value.
So,to pass price call the setRetailPrice method, and to pass the number of units call setUnitsOnHand method, like so:
jacket.setUnitsOnHand(12);
jacket.setRetailPrice(59.95);

then you can call:
System.out.println("There are" jeans.getDescription() +  
"at" jeans.getRetailPrice() "each"); 


Note that your constructor is declared to accept 3 parameters. however when you call it you supply it no parameters.
so either delete that constructor, or when creating an object, pass it the appropriate parameters:
RetailItem jacket = new RetailItem("Jacket", 12, 59.95);

Then you don't have to call set methods since all data was passed( unless you want to change it).
Was This Post Helpful? 0
  • +
  • -

#5 Guest_MCN*


Reputation:

Re: Retail Store Java Program

Posted 04 February 2011 - 10:37 AM

okay I have changed that.

In the first class (retialitem()) I receive errors saying that it doesn't recognize the symbol
ex:
description = type;
unitsOnHand = units;
retailPrice = price;

Now that I have fixed what you suggested in the second program it doesn't call the retailitem() class, I'm assuming it's because of these errors?
Was This Post Helpful? 0

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

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

Re: Retail Store Java Program

Posted 04 February 2011 - 11:11 AM

Post your updated code along with the exact error messages your recieve.
Was This Post Helpful? 0
  • +
  • -

#7 Guest_MCN*


Reputation:

Re: Retail Store Java Program

Posted 04 February 2011 - 11:55 AM

From RetailItem class
Posted Image
Was This Post Helpful? 0

#8 Guest_MCN*


Reputation:

Re: Retail Store Java Program

Posted 04 February 2011 - 11:56 AM

View PostMCN, on 04 February 2011 - 11:55 AM, said:

From RetailItem class
Posted Image



sorry I forgot to mention the code is exactly the same as the first block of code in my original post.
Was This Post Helpful? 0

Page 1 of 1