can anybody helps me with this? its for java neatbeans.. thanks In a Supermarket
ID: 3798689 • Letter: C
Question
can anybody helps me with this? its for java neatbeans.. thanks
In a Supermarket, a product has id, description, and price. Write a class in java to represent a product as follows:
Declare/define the product id to be public and all other data members to be private.
Provide the following constructors Default constructor in which you set id to “p-00”, price to 1.0, and description to empty string.
A constructor that takes two parameters, namely, the id and the description and sets them accordingly.
Provide public setter for the product id field.
Provide public getter and setter for the price.
Make sure that the price is not negative all the time.
Provide toString method that returns the product in the below format: ID: 1223-443, Description: Java book, Price 31 AED Write the main method such that you do the following create a product p1 with id =1223-443, description= keyboard, price=4.5 create a product p2 with id =1433-77, description= Wooden Chair, price=42.1 Increment the price of p1 by 10 AED. Change the id of p2 the above object to “p-456”. Print the sum of both p1 and p2 prices. Print the id, description, and price of p2.
Explanation / Answer
//Product Class
public class Product {
public String productId;
private Double price;
private String description;
//Default constructor in which you set id to “p-00”, price to 1.0, and description to empty string
public Product(){
productId = "p-00";
price = 1.0;
description = "";
}
//A constructor that takes two parameters, namely, the id and the description and sets them accordingly.
public Product(String Id, String description){
this.productId = Id;
this.description = description;
}
//public setter for the product id field.
public void setProductId(String Id){
this.productId = Id;
}
//public getter and setter for the price.
public void setPrice(Double price){
//Make sure that the price is not negative all the time.
if (price > 0)
this.price = price;
}
public Double getPrice(){
return this.price;
}
// toString method that returns the product in the below format: ID: 1223-443, Description: Java book, Price 31 AED
public String toString(){
return "ID: "+ this.productId +
", Description: "+ this.description +
", Price "+ this.price +" AED";
}
}
//Main Class
public class main {
public static void main(String[] args) {
//create a product p1 with id =1223-443, description= keyboard, price=4.5
Product p1 = new Product("1223-443","keyboard");
p1.setPrice(4.5);
//create a product p2 with id =1433-77, description= Wooden Chair, price=42.1
Product p2 = new Product("1433-77","Wooden Chair");
p2.setPrice(42.1);
//Increment the price of p1 by 10 AED.
p1.setPrice(p1.getPrice()+10.0);
//Change the id of p2 the above object to “p-456”.
p1.setProductId("p-456");
//Print the sum of both p1 and p2 prices.
System.out.println(p1.getPrice() + p2.getPrice()); //OUTPUT: 56.6
//Print the id, description, and price of p2
System.out.println(p2.toString()); //OUTPUT: ID: 1433-77, Description: Wooden Chair, Price 42.1 AED
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.