Write a class called Item. The Item class represents all that is common between
various multimedia items. It should have the following attributes (fields):
a) a String name to store the name of the item.
b) a String comment to store a comment about the item.
c) a double value to store how much the item cost.
It should have appropriate constructors and at least the following methods
a) getters and setters for the attributes.
b) a public String toString() method that returns a String representation of the
Item.
c) a method void depreciate() that decreases the value of the item by 5%.
public class Item {
private String name;
private String comment;
private double itemCost;
public Item(String name, String comment, double itemCost) {
this.name = name;
this.comment = comment;
this.itemCost = itemCost;
}
public void depreciate() {
setItemCost(0.95 * getItemCost()); // 100% - 5% = 95%
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getComment() {
return this.comment;
}
public void setItemCost(double itemCost) {
this.itemCost = itemCost;
}
public String getItemCost() {
return this.itemCost;
}
@Override
public String toString() {
return "{name: " + name + ", comment: " + comment + ", item cost: " + itemCost + "}";
}
}
Comments
Leave a comment