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 program will now be broken up into methods and store all the inventory in an ArrayList object.
The program will be able to run a report for all inventory details as well as a report for items that are low in stock. Low stock means that there are less than 5 items available for sale.
Grading details:
Correct usage of OOP concepts (80%)
Correct class design and usage of class instances (30%)
Correct implementation and usage of polymorphism (30%)
Other supporting code/logic (20%)
Program runs/correct results (10%)
Formatting/Indentation (10%)
Starter code:
import java.util.ArrayList; |
Product:
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
Book:
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
Food:
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; } }
Sample output:
Viewing Store Products:
name: Computer, description: No description entered., wholesalePrice: $1400.00, retailPrice: $1540.00, stock: 3
name: iPhone XR, description: No description entered., wholesalePrice: $400.00, retailPrice: $440.00, stock: 6
name: Samsung Galaxy S10, description: Phone with 128GB memory., wholesalePrice: $700.00, retailPrice: $770.00, stock: 8
name: Starting out with C++, description: No description entered., wholesalePrice: $150.00, retailPrice: $165.00, stock: 3, author: Author_Placeholder
name: Fuji Apples, description: No description entered., wholesalePrice: $3.50, retailPrice: $3.85, stock: 150, expiration: Dec. 1, 2019
Viewing items that need to be re-stocked:
name: Computer, description: No description entered., wholesalePrice: $1400.00, retailPrice: $1540.00, stock: 3
name: Starting out with C++, description: No description entered., wholesalePrice: $150.00, retailPrice: $165.00, stock: 3, author: Author_Placeholder
HI,
I just updated two classess, rest are same.i didnt updateed Book.java and Food.java.
Updated classes are below:
Product.java:
// You will need to add more methods to this file
public class Product {
private String name;
private String description;
private double wholeSalePrice;
private double retailPrice;
private int stock=0;
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 int getStock(){
return stock ;
}
public void setStock(int stock){
this.stock = stock ;
}
public String getName(){
return name ;
}
public void setName(String name){
this.name = name ;
}
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()+", stock: "+getStock();
}
else {//otherwise print description value
return "\nname: "+name+" , description: "+description+", wholesalePrice: $"+wholeSalePrice+", retailPrice $"+getRetailPrice()+", stock: "+getStock();
}//end else
}//end toString
}//end class
RetailInventory.java
import java.util.ArrayList;
public class RetailInventory {
static ArrayList<Product> inventory = new ArrayList<Product>();
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");
addNewProduct(computer);
addNewProduct(iPhone);
addNewProduct(Galaxy10);
addNewProduct(book);
addNewProduct(apple);
updateStock(computer, 3);
updateStock(iPhone, 6);
updateStock(Galaxy10, 8);
updateStock(book, 3);
updateStock(apple, 150);
viewInventoryDetails();
viewLowStock();
}//end of main
public static void addNewProduct(Product p){
inventory.add(p);
}
public static void updateStock(Product p, int quantity){
p.setStock(quantity);
}
public static void viewInventoryDetails(){
System.out.println("\nView store products: ");
for (Product prod : inventory) {
System.out.println(prod);
}
}
public static void viewLowStock(){
System.out.println("\nView low stock: ");
for (Product prod : inventory) {
if(prod.getStock()<5)
System.out.println(prod);
}
}
} //end of class
Output: output is as expected.
Thanks