Inventory program

initialization constructor

Page 1 of 1

8 Replies - 1002 Views - Last Post: 27 July 2007 - 08:06 PM Rate Topic: -----

#1 bigbluesky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 06-July 07

Inventory program

Post icon  Posted 27 July 2007 - 02:51 PM

I am working on an assignment that is due today and I know it is simple, but I am unsure how to proceed. I would like to see some examples of this, and have been searching for some on the Internet.

I am creating 2 files, one is Product.java and has information to be called to the other, Inventory1.java.

My instructor gave the following tip:

"Instantiate and initialize a Product object using and initialization constructor
NOTE: You will need a constructor that takes 4 parameters
Poduct p = new Product( :"DVD" )"

I think what I am supposed to do is call the information from the Product file to the Inventory file - using the constructor that is in the Product file, I am unsure how to do this and would like to see some examples of it.

I know this is a very easy assignment and I should be able to do it, please forgive me if I am asking a stupid question or if my question is against the policy of this website.

-Pam


Here is my Product.java file:


Product.java (the data class)

public class Product
{
private String name;
//the product name.
private int number;
//the item number
private int units;
//number of available units.
private double price;
//the price of each unit.

public Product ( ) //Default constructor
{
//Do nothing
}//End default constructor Product


//Initialization Constructor
public Product ( String nameIn, int numberIn, int unitsIn, double priceIn, double valueIn )
{
setName (nameIn);
	setNumber (numberIn);
	setUnits (unitsIn);
	setPrice (priceIn);
	setValue (valueIn);

} //End initialization constructor Product


//Declare Set/Get Methods
public void setName( String nameInput )
{
name = nameInput;


} //End method setName

}
public void setNumber ( int numberInput )
	{
	[code]number = numberInput;

} //End method setNumber

public void setUnits ( int unitsInput )
	{
	units = unitsInput;
	}
	public void setPrice ( double priceInput )
	{
	price = priceInput;
	} //End method setPrice

	public String getName ( )
	{
	return ( name);
	} //End method getName

	public double getNumber ( )
	{
	return ( number );
	} //End method getNumber

	public double getUnits ( )
	{
	return ( units );
	} //End method getUnits

	public double getPrice ( )
	{
	return ( price );
	} //End method getPrice

	public double stockValueInDollars()
	{
	return ( units * price );

} //End method stockValueInDollars

//toString()Method
//Returns a formatted String for output purposes
public String toString()
	 {
		 String formatString = "Identification Number : %d\n";
		 formatString	   += "Product Name		  : %s\n";
		 formatString	   += "Units In Stock		: %d\n";
		 formatString	   += "Unit Price			: $%.2f\n";
		 formatString	   += "Stock Value		   : $%.2f\n\n";

		  return String.format ( name(),identificationNumber(), unitsInStock(),unitPriceInDollars(),stockValueInDollars() );


}//End toString()


}//End Product

Is This A Good Question/Topic? 0
  • +

Replies To: Inventory program

#2 bigbluesky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 06-July 07

Re: Inventory program

Posted 27 July 2007 - 02:57 PM

View Postbigbluesky, on 27 Jul, 2007 - 02:51 PM, said:

I am working on an assignment that is due today and I know it is simple, but I am unsure how to proceed. I would like to see some examples of this, and have been searching for some on the Internet.

I am creating 2 files, one is Product.java and has information to be called to the other, Inventory1.java.

My instructor gave the following tip:

"Instantiate and initialize a Product object using and initialization constructor
NOTE: You will need a constructor that takes 4 parameters
Poduct p = new Product( :"DVD" )"

I think what I am supposed to do is call the information from the Product file to the Inventory file - using the constructor that is in the Product file, I am unsure how to do this and would like to see some examples of it.

I know this is a very easy assignment and I should be able to do it, please forgive me if I am asking a stupid question or if my question is against the policy of this website.

-Pam


Here is my Product.java file:


Product.java (the data class)

public class Product
{
private String name;
//the product name.
private int number;
//the item number
private int units;
//number of available units.
private double price;
//the price of each unit.

public Product ( ) //Default constructor
{
//Do nothing
}//End default constructor Product


//Initialization Constructor
public Product ( String nameIn, int numberIn, int unitsIn, double priceIn, double valueIn )
{
setName (nameIn);
	setNumber (numberIn);
	setUnits (unitsIn);
	setPrice (priceIn);
	setValue (valueIn);

} //End initialization constructor Product


//Declare Set/Get Methods
public void setName( String nameInput )
{
name = nameInput;


} //End method setName

}
public void setNumber ( int numberInput )
	{
	[code]number = numberInput;

} //End method setNumber

public void setUnits ( int unitsInput )
	{
	units = unitsInput;
	}
	public void setPrice ( double priceInput )
	{
	price = priceInput;
	} //End method setPrice

	public String getName ( )
	{
	return ( name);
	} //End method getName

	public double getNumber ( )
	{
	return ( number );
	} //End method getNumber

	public double getUnits ( )
	{
	return ( units );
	} //End method getUnits

	public double getPrice ( )
	{
	return ( price );
	} //End method getPrice

	public double stockValueInDollars()
	{
	return ( units * price );

} //End method stockValueInDollars

//toString()Method
//Returns a formatted String for output purposes
public String toString()
	 {
		 String formatString = "Identification Number : %d\n";
		 formatString	   += "Product Name		  : %s\n";
		 formatString	   += "Units In Stock		: %d\n";
		 formatString	   += "Unit Price			: $%.2f\n";
		 formatString	   += "Stock Value		   : $%.2f\n\n";

		  return String.format ( name(),identificationNumber(), unitsInStock(),unitPriceInDollars(),stockValueInDollars() );


}//End toString()


}//End Product


opps! I signed my name instead of my user name! sorry about that. duh! I forgot to preview my post - I would have caught that! LOL I am tired - yesterday I got 4 hrs sleep then worked 10 hrs - night shift and got another 4 in today - must go to work again tonight.
-bigbluesky
Was This Post Helpful? 0
  • +
  • -

#3 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Inventory program

Posted 27 July 2007 - 03:16 PM

Hi bigbluesky,

First I am going to teach you how to use code tags.
You know how to do it but you use the code tags many times, you only need to use the once, and put the entire code there,
you did this
private String name;


private int number;


but what you really need to do is this:
this is your entire program
public class Product
{

private String name;
//the product name.

private int number;
//the item number

private int units;
//number of available units.

private double price;
//the price of each unit.

public Product ( ) //Default constructor
{
//Do nothing
}//End default constructor Product


//Initialization Constructor
public Product ( String nameIn, int numberIn, int unitsIn, double priceIn, double valueIn )
{

setName (nameIn);
	setNumber (numberIn);
	setUnits (unitsIn);
	setPrice (priceIn);
	setValue (valueIn);

} //End initialization constructor Product


//Declare Set/Get Methods
public void setName( String nameInput )
{

name = nameInput;


} //End method setName

}

public void setNumber ( int numberInput )
	{
	[]number = numberInput;

} //End method setNumber


public void setUnits ( int unitsInput )
	{
	units = unitsInput;
	}
	public void setPrice ( double priceInput )
	{
	price = priceInput;
	} //End method setPrice

	public String getName ( )
	{
	return ( name);
	} //End method getName

	public double getNumber ( )
	{
	return ( number );
	} //End method getNumber

	public double getUnits ( )
	{
	return ( units );
	} //End method getUnits

	public double getPrice ( )
	{
	return ( price );
	} //End method getPrice

	public double stockValueInDollars()
	{
	return ( units * price );

} //End method stockValueInDollars

//toString()Method
//Returns a formatted String for output purposes

public String toString()
	 {
		 String formatString = "Identification Number : %d\n";
		 formatString	   += "Product Name		  : %s\n";
		 formatString	   += "Units In Stock		: %d\n";
		 formatString	   += "Unit Price			: $%.2f\n";
		 formatString	   += "Stock Value		   : $%.2f\n\n";

		  return String.format ( name(),identificationNumber(), unitsInStock(),unitPriceInDollars(),stockValueInDollars() );


}//End toString()


}//End Product


Now the code is more readable :)


Now about your code, your second constructor takes five parameters, shouldn't they be four like your assignment's request. There are other errors but you said that you want so see examples. I would recommend that you search this Java forum right here and I am pretty much confident that you'll find similar if not exact posts like yours.
Was This Post Helpful? 0
  • +
  • -

#4 bigbluesky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 06-July 07

Re: Inventory program

Posted 27 July 2007 - 03:42 PM

View PostPennyBoki, on 27 Jul, 2007 - 03:16 PM, said:

Hi bigbluesky,

First I am going to teach you how to use code tags.
You know how to do it but you use the code tags many times, you only need to use the once, and put the entire code there,
you did this
private String name;


private int number;


but what you really need to do is this:
this is your entire program
public class Product
{

private String name;
//the product name.

private int number;
//the item number

private int units;
//number of available units.

private double price;
//the price of each unit.

public Product ( ) //Default constructor
{
//Do nothing
}//End default constructor Product


//Initialization Constructor
public Product ( String nameIn, int numberIn, int unitsIn, double priceIn, double valueIn )
{

setName (nameIn);
	setNumber (numberIn);
	setUnits (unitsIn);
	setPrice (priceIn);
	setValue (valueIn);

} //End initialization constructor Product


//Declare Set/Get Methods
public void setName( String nameInput )
{

name = nameInput;


} //End method setName

}

public void setNumber ( int numberInput )
	{
	[]number = numberInput;

} //End method setNumber


public void setUnits ( int unitsInput )
	{
	units = unitsInput;
	}
	public void setPrice ( double priceInput )
	{
	price = priceInput;
	} //End method setPrice

	public String getName ( )
	{
	return ( name);
	} //End method getName

	public double getNumber ( )
	{
	return ( number );
	} //End method getNumber

	public double getUnits ( )
	{
	return ( units );
	} //End method getUnits

	public double getPrice ( )
	{
	return ( price );
	} //End method getPrice

	public double stockValueInDollars()
	{
	return ( units * price );

} //End method stockValueInDollars

//toString()Method
//Returns a formatted String for output purposes

public String toString()
	 {
		 String formatString = "Identification Number : %d\n";
		 formatString	   += "Product Name		  : %s\n";
		 formatString	   += "Units In Stock		: %d\n";
		 formatString	   += "Unit Price			: $%.2f\n";
		 formatString	   += "Stock Value		   : $%.2f\n\n";

		  return String.format ( name(),identificationNumber(), unitsInStock(),unitPriceInDollars(),stockValueInDollars() );


}//End toString()


}//End Product


Now the code is more readable :)


Now about your code, your second constructor takes five parameters, shouldn't they be four like your assignment's request. There are other errors but you said that you want so see examples. I would recommend that you search this Java forum right here and I am pretty much confident that you'll find similar if not exact posts like yours.


Thank you - I did show this to my instructor and asked if it looked ok - he gave me a few suggestions which I followed - but there must be other things wrong as well . . . like the five parameters (which he said nothing about, might have thought he shouldn't have to) - I think I will change that. If I might ask - what other errors do you see?

I looked on the forum and found one example - I will look again.

Yes, I am feeling awkward with the code tags, and figured I was using too many. Thank you for the help.

-bigbluesky
Was This Post Helpful? 0
  • +
  • -

#5 bigbluesky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 06-July 07

Re: Inventory program

Posted 27 July 2007 - 03:55 PM

View Postbigbluesky, on 27 Jul, 2007 - 03:42 PM, said:

View PostPennyBoki, on 27 Jul, 2007 - 03:16 PM, said:

Hi bigbluesky,

First I am going to teach you how to use code tags.
You know how to do it but you use the code tags many times, you only need to use the once, and put the entire code there,
you did this
private String name;


private int number;


but what you really need to do is this:
this is your entire program
public class Product
{

private String name;
//the product name.

private int number;
//the item number

private int units;
//number of available units.

private double price;
//the price of each unit.

public Product ( ) //Default constructor
{
//Do nothing
}//End default constructor Product


//Initialization Constructor
public Product ( String nameIn, int numberIn, int unitsIn, double priceIn, double valueIn )
{

setName (nameIn);
	setNumber (numberIn);
	setUnits (unitsIn);
	setPrice (priceIn);
	setValue (valueIn);

} //End initialization constructor Product


//Declare Set/Get Methods
public void setName( String nameInput )
{

name = nameInput;


} //End method setName

}

public void setNumber ( int numberInput )
	{
	[]number = numberInput;

} //End method setNumber


public void setUnits ( int unitsInput )
	{
	units = unitsInput;
	}
	public void setPrice ( double priceInput )
	{
	price = priceInput;
	} //End method setPrice

	public String getName ( )
	{
	return ( name);
	} //End method getName

	public double getNumber ( )
	{
	return ( number );
	} //End method getNumber

	public double getUnits ( )
	{
	return ( units );
	} //End method getUnits

	public double getPrice ( )
	{
	return ( price );
	} //End method getPrice

	public double stockValueInDollars()
	{
	return ( units * price );

} //End method stockValueInDollars

//toString()Method
//Returns a formatted String for output purposes

public String toString()
	 {
		 String formatString = "Identification Number : %d\n";
		 formatString	   += "Product Name		  : %s\n";
		 formatString	   += "Units In Stock		: %d\n";
		 formatString	   += "Unit Price			: $%.2f\n";
		 formatString	   += "Stock Value		   : $%.2f\n\n";

		  return String.format ( name(),identificationNumber(), unitsInStock(),unitPriceInDollars(),stockValueInDollars() );


}//End toString()


}//End Product


Now the code is more readable :)


Now about your code, your second constructor takes five parameters, shouldn't they be four like your assignment's request. There are other errors but you said that you want so see examples. I would recommend that you search this Java forum right here and I am pretty much confident that you'll find similar if not exact posts like yours.


Thank you - I did show this to my instructor and asked if it looked ok - he gave me a few suggestions which I followed - but there must be other things wrong as well . . . like the five parameters (which he said nothing about, might have thought he shouldn't have to) - I think I will change that. If I might ask - what other errors do you see?

I looked on the forum and found one example - I will look again.

Yes, I am feeling awkward with the code tags, and figured I was using too many. Thank you for the help.

-bigbluesky



I looked at the tips my instructor gave me - and he has 5 parameters listed: product number, product name, units in stock, unit price and stock value - if I remember how to count right - that is 5, not 4 - LOL.
Ok, I am confused about what he wants there - he lists 5 and says 4 - I will ask him about that.

thanks again!

-Pam
Was This Post Helpful? 0
  • +
  • -

#6 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Inventory program

Posted 27 July 2007 - 04:17 PM

well here we go:

you have an extra bracket here:
public void setName( String nameInput )
{

name = nameInput;


} //End method setName

}


then right below that code is this:
public void setNumber ( int numberInput )
	{
	[]number = numberInput;

}

which is wrong
you don't need those [ ].

and your toString function is not ok so I made little changes
see:
public class Product
{

private String name;
//the product name.

private int number;
//the item number

private int units;
//number of available units.

private double price;
//the price of each unit.

 public Product ( ) //Default constructor
{
//Do nothing
}//End default constructor Product


//Initialization Constructor
public Product ( String nameIn, int numberIn, int unitsIn, double priceIn)
{

	setName (nameIn);
	setNumber (numberIn);
	setUnits (unitsIn);
	setPrice (priceIn);
//	setValue (valueIn);

} //End initialization constructor Product


//Declare Set/Get Methods
public void setName( String nameInput )
{

name = nameInput;


} //End method setName



public void setNumber ( int numberInput )
	{
	int number = numberInput;

} //End method setNumber


public void setUnits ( int unitsInput )
	{
	units = unitsInput;
	}
	public void setPrice ( double priceInput )
	{
	price = priceInput;
	} //End method setPrice

	public String getName ( )
	{
	return ( name);
	} //End method getName

	public double getNumber ( )
	{
	return ( number );
	} //End method getNumber

	public double getUnits ( )
	{
	return ( units );
	} //End method getUnits

	public double getPrice ( )
	{
	return ( price );
	} //End method getPrice

	public double stockValueInDollars()
	{
	return ( units * price );

} //End method stockValueInDollars

//toString()Method
//Returns a formatted String for output purposes

public String toString()
	 {
		 String formatString	=  "Identification Number "+getNumber()+"\n";
		 formatString	 		+= " Product Name		 "+getName()+"\n";
		 formatString		   	+= " Units In Stock	   "+getUnits()+"\n";
		 formatString		   	+= " Unit Price		   "+getPrice()+"\n";
		 formatString	   	+= " Stock Value		  "+stockValueInDollars()+"\n";

		  return formatString;

}//End toString()


}//End Product

Was This Post Helpful? 0
  • +
  • -

#7 bigbluesky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 06-July 07

Re: Inventory program

Posted 27 July 2007 - 04:45 PM

View PostPennyBoki, on 27 Jul, 2007 - 04:17 PM, said:

well here we go:

you have an extra bracket here:
public void setName( String nameInput )
{

name = nameInput;


} //End method setName

}


then right below that code is this:
public void setNumber ( int numberInput )
	{
	[]number = numberInput;

}

which is wrong
you don't need those [ ].

and your toString function is not ok so I made little changes
see:
public class Product
{

private String name;
//the product name.

private int number;
//the item number

private int units;
//number of available units.

private double price;
//the price of each unit.

 public Product ( ) //Default constructor
{
//Do nothing
}//End default constructor Product


//Initialization Constructor
public Product ( String nameIn, int numberIn, int unitsIn, double priceIn)
{

	setName (nameIn);
	setNumber (numberIn);
	setUnits (unitsIn);
	setPrice (priceIn);
//	setValue (valueIn);

} //End initialization constructor Product


//Declare Set/Get Methods
public void setName( String nameInput )
{

name = nameInput;


} //End method setName



public void setNumber ( int numberInput )
	{
	int number = numberInput;

} //End method setNumber


public void setUnits ( int unitsInput )
	{
	units = unitsInput;
	}
	public void setPrice ( double priceInput )
	{
	price = priceInput;
	} //End method setPrice

	public String getName ( )
	{
	return ( name);
	} //End method getName

	public double getNumber ( )
	{
	return ( number );
	} //End method getNumber

	public double getUnits ( )
	{
	return ( units );
	} //End method getUnits

	public double getPrice ( )
	{
	return ( price );
	} //End method getPrice

	public double stockValueInDollars()
	{
	return ( units * price );

} //End method stockValueInDollars

//toString()Method
//Returns a formatted String for output purposes

public String toString()
	 {
		 String formatString	=  "Identification Number "+getNumber()+"\n";
		 formatString	 		+= " Product Name		 "+getName()+"\n";
		 formatString		   	+= " Units In Stock	   "+getUnits()+"\n";
		 formatString		   	+= " Unit Price		   "+getPrice()+"\n";
		 formatString	   	+= " Stock Value		  "+stockValueInDollars()+"\n";

		  return formatString;

}//End toString()


}//End Product


ok - extra bracket - braces where they don't belong - kind of sloppy . . . I should have seen those things myself! LOL Thanks for your patience. I hope I don't cause your eyes to get stuck in the back of your head rolling them at my code and my questions! LOL

-bigbluesky
Was This Post Helpful? 0
  • +
  • -

#8 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Inventory program

Posted 27 July 2007 - 04:51 PM

:rolleyes: haha just kidding, not really because I had to go through the same thing just as you do regarding the programming, so a bit of hint:
If you have time try to consider the codes even to the slightest detail, ask your self questions like why this semicolon here or why is this function there? That way things would be more clear, and if you can't answer some of those questions, you can always drop by @ </dream.in.code> for help :)
Was This Post Helpful? 0
  • +
  • -

#9 bigbluesky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 06-July 07

Re: Inventory program

Posted 27 July 2007 - 08:06 PM

View PostPennyBoki, on 27 Jul, 2007 - 04:51 PM, said:

:rolleyes: haha just kidding, not really because I had to go through the same thing just as you do regarding the programming, so a bit of hint:
If you have time try to consider the codes even to the slightest detail, ask your self questions like why this semicolon here or why is this function there? That way things would be more clear, and if you can't answer some of those questions, you can always drop by @ </dream.in.code> for help :)



Thank you so much -

I feel like I just got run over by a bus - and my assignment isn't done. After working on it for 3 days. . . I am wondering why I thought I could do this stuff . . . This is an easy program - why can't I figure it out? all I have is a big mess! I wish I didn't have to work tonight! I could keep working on this.
I would post what I have done - but it is a joke. I posted it for my instructor to see what I have done so far. I think I should have learned how to program on my own first then took this class . . . it is too expensive to fail!

Thanks again for your help.

-Pam
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1