Question #17517

Create a class named Product that contains private instance variables to hold the product's name, price, value, and quantity in stock. Create getters and setters that allow you to access and modify the name, price, value, and quantity of the product. Create a method named getProfit() to tell you the profit you would make if you sold one. The profit is the price less the value. Override the toString() method to return a string that contains the name of the product. Validation of the incoming data in the setters is not required.

Expert's answer

public class Product {
privateString name;
privatedouble price;
privatedouble value;
privateint quantity;

publicString getName() {
returnname;
}

publicvoid setName(String name) {
this.name = name;
}

publicdouble getValue() {
returnvalue;
}

publicvoid setValue(double value) {
this.value= value;
}

publicint getQuantity() {
returnquantity;
}

publicvoid setQuantity(int quantity) {
this.quantity= quantity;
}

publicdouble getPrice() {
returnprice;
}

publicvoid setPrice(double price) {
this.price= price;
}

publicdouble getProfit() {
returnthis.value - this.price;
}

@Override
publicString toString() {
return"Product name: " + name;
}

}
LATEST TUTORIALS
APPROVED BY CLIENTS