In: Computer Science
Problem statement:
You are tasked with writing a simple program that will keep track of items sold by a retail store. We need to keep track of the stock (or number of specific products available for sale).
Requirements:
The Food and Book items should inherit all the properties of the Product item in the previous assignment.
Foods cannot be added to the inventory without an expiration date.
Implement a toString method for Product, Food, and Book.
Grading details:
Correct usage of OOP concepts (80%)
Correct class design and usage of class instances (25%)
Correct implementation and usage of inheritance(25%)
Other supporting code/logic (30%)
Program runs/correct results (10%)
Formatting/Indentation (10%)
Starter code:
public class RetailInventory { public static void main(String[] args) { Product computer = new Product("Computer", 1400); Product iPhone = new Product("iPhone XR", 400, 600); Product Galaxy10 = new Product("Samsung Galaxy S10", 700, 900, "Phone with 128GB memory."); Product book = new Book("Starting out with C++", 150.00); Product apple = new Food("Fuji Apples", 3.50, "Dec. 1, 2019"); Product[] inventory = {computer, iPhone, Galaxy10, book, apple}; System.out.println("Viewing Store Products:"); for (Product item: inventory) { System.out.println(item); } } }
Sample output:
Viewing Store Products:
name: Computer, description: No description entered.,
wholesalePrice: $1400.00, retailPrice: $1540.00
name: iPhone XR, description: No description entered.,
wholesalePrice: $400.00, retailPrice: $440.00
name: Samsung Galaxy S10, description: Phone with 128GB memory.,
wholesalePrice: $700.00, retailPrice: $770.00
name: Starting out with C++, description: No description entered.,
wholesalePrice: $150.00, retailPrice: $165.00, author:
Author_Placeholder
name: Fuji Apples, description: No description entered.,
wholesalePrice: $3.50, retailPrice: $3.85, expiration: Dec. 1,
2019
Given problem will have below 4 files as solution where each class has its own file (for a better understanding and encapsulation)
PROGRAM: There are below files in solution:
i. RetailInventory.java: Same as shared in problem, no change in it
ii. Product.java: parent class for products
iii. Food.java: Subclass for Product type food.
iv. Book.java: subclass for product type book.
Product.java:
public class Product {
private String name;
private String description;
private double wholeSalePrice;
private double retailPrice;
public Product() {//default constructor
}
public Product(String name, double wholeSalePrice) {//constructor with name and price
this.name = name;//set the instance variables as per the value passed
this.wholeSalePrice= wholeSalePrice;
}
public Product(String name, double wholeSalePrice, double retailPrice) {//constructor with name and price
this.name = name;//set the instance variables as per the value passed
this.wholeSalePrice= wholeSalePrice;
this.retailPrice=retailPrice;
}
public Product(String name, double wholeSalePrice, double retailPrice, String description) {//constructor with name and price
this.name = name;//set the instance variables as per the value passed
this.wholeSalePrice= wholeSalePrice;
this.retailPrice=retailPrice;
this.description=description;
}
private double getRetailPrice() {//this method will return the retailPrice as per the sample outpu
retailPrice= wholeSalePrice+(wholeSalePrice*.10);//output shows that retailPrice is 10% addition to wholesaleprice
return retailPrice;
}
public String toString() {//print object details
if(description==null) {//if description is null, then print a message for description
return "\nname: "+name+" , description: No description entererd., wholesalePrice: $"+wholeSalePrice+", retailPrice $"+getRetailPrice();
}
else {//otherwise print description value
return "\nname: "+name+" , description: "+description+", wholesalePrice: $"+wholeSalePrice+", retailPrice $"+getRetailPrice();
}//end else
}//end toString
}//end class
SCRENSHOT for indentation:
Book.java:
public class Book extends Product {
private String author;
public Book(String name, double wholeSalePrice) {// constructor gets name , price
super(name, wholeSalePrice);//pass the name and price to the super class product
author="Author_Placeholder";
}
public String toString() {
return super.toString()+", Author: "+author;//super will return the product details and author is added here
}//end toString
}//end class
SCREENSHOT for indentation:
Food.java:
public class Food extends Product {
private String date;
public Food(String name, double wholeSalePrice, String date) { // constructor gets name , price and date
super(name, wholeSalePrice);//pass name and wholesale price to super class
this.date = date;//set date for local class instance variable date
}
public String toString() {//return the food details
return super.toString()+", expiration: "+date;//super will return the product details and expiration is added here
}
}
SCREENSHOT for indentation
OUTPUT
Kindly comment, if you face issue in understanding code flow.