1.(15 pts) Suppose that we are given the following paragraph describing the feat
ID: 3910202 • Letter: 1
Question
1.(15 pts) Suppose that we are given the following paragraph describing the features of a class. The menu items in a department store are represented by Menultem objects. Each Menultem stores a description, the stock (just a number, like 300, which could mean 300 chicken wings), and the price. A Menultem is created by passing its description, quantity on hand, and the price as shown below. In this case, the item is a hamburger with 500 items in stock and a price of $1.95. >Menultem hbg new Menultem("Hamburger", 500, 1.95); The class must be designed in such a way that it returns a reasonable representation of the Menultem object as the following example shows. >System.out.println(hbg); Menultem Hamburger stock 500 price 1.95 The class must have a method named sell, which is called when an item is sold. The method accepts as a parameter, the number of items to be sold. An item is sold only if the stock is not lower than the quantity to be sold. The stock must then be reduced appropriately. The method must return true if the item could be sold. Otherwise, it must return false. The following code sells 20 units of the hamburger hbg. >hbg.sell(20); >System.out.println(hbg); Menultem Hamburger stock 480 price 1.95 Write a class for the Menultem object using the information given above, Answer:Explanation / Answer
//The class for the MenuItem object can be defined as
class MenuItem{
//data member to hold the description, stocks, and price of each item
String desc;
int stock;
double price;
//default Constructor
MenuItem(){}
//Parmeterized Constructor
MenutItem(String description, int stk, double pr) {
desc = description;
stock = stk;
price = pr;
}
//Printing the Object of MenuItem
@Override
public String toString(){
return "MenuItem "+desc+" stock "+stock+" price "+price;
}
//Sell method
public Boolean sell(int stk){
if(stk>stock)
return false;
else{
stock = stock - stk;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.