In java create a Product class. A Product object represents a product stored in
ID: 3589049 • Letter: I
Question
In java create a Product class. A Product object represents a product stored in inventory.
The Product class should have the following private instance variables: productName, numberOnHand, numberOnOrder, and price.
Give the instance variables public 'get' and 'set' methods. The numberOnHand, numberOnOrder, and price 'set' methods will not accept a negative number.
Give the class a four argument constructor that populates the data in a Product object. Also, recreate the no-argument constructor. Remember that the numberOnHand, numberOnOrder, and price should not be allowed to have a value less than zero.
Also give the class an overridden toString method that reports on the data in a Product object.
Explanation / Answer
public class Product{
private String productName;
private int numberOnHand;
private int numberOnOrder;
private double price;
// no argument constructor
Product(){
this.productName = "";
this.numberOnHand = 0;
this.numberOnOrder = 0;
this.price = 0;
}
Product(String productName,int numberOnHand,int numberOnOrder, double price){
this.productName = productName;
if(numberOnHand>-1 && numberOnOrder>-1 && price>-1){
this.numberOnHand = numberOnHand;
this.numberOnOrder = numberOnOrder;
this.price = price;
}
else{
System.out.println("numberOnOrder,numberOnHand,price cannot be less than zero");
}
}
//setter methods
public void setProductName(String productName){
this.productName = productName;
}
public void setNumberOnHand(int numberOnHand){
if(numberOnHand>-1){
this.numberOnHand = numberOnHand;
}
else{
System.out.println("NumberOnHand cannot be less than zero");
}
}
public void setNumberOnOrder(int numberOnOrder){
if(numberOnOrder>-1){
this.numberOnOrder = numberOnOrder;
}
else{
System.out.println("NumberOnOrder cannot be less than zero");
}
}
public void setPrice(double price){
if(price>-1){
this.price = price;
}
else{
System.out.println("Price cannot be less than zero.");
}
}
//getter methods
public String getProductName(){
return this.productName;
}
public int getNumberOnHand(){
return this.numberOnHand;
}
public int getNumberOnOrder(){
return this.numberOnOrder;
}
public double getPrice(){
return this.price;
}
//toString method to retun String of the object.
public String toString(){
return "ProduceName:"+getProductName()+","+"NumberOnHand:"+getNumberOnHand()+","+"NumberOnOrder:"+getNumberOnOrder()+","+"Price:"+getPrice();
}
//main method
public static void main(String args[]){
Product p = new Product();
System.out.println(p.toString());
Product p1 = new Product("A",1,2,3.0);
System.out.println(p1.toString());
p.setProductName("Mobile");
p.setNumberOnHand(123);
p.setNumberOnOrder(150);
p.setPrice(200);
System.out.println(p.toString());
}
}/*
sample output
ProduceName:,NumberOnHand:0,NumberOnOrder:0,Price:0.0
ProduceName:A,NumberOnHand:1,NumberOnOrder:2,Price:3.0
ProduceName:Mobile,NumberOnHand:123,NumberOnOrder:150,Price:200.0
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.