abstract and concrete classes – inventory system for a grocery store
you are going to make abstract and concrete classes that complete a simple inventory system for a grocery store. You are going to create an abstract class called Food, and then three concrete subclasses called Fruit, Carbohydrate, and MeatFish. Fruit encompasses any kind of fruit, Carbohydrate encompasses breads and grains, and MeatFish encompasses meat and fish. You will see how easy such a situation is once you take advantage of polymorphism with an abstract class. Obtain the tester class, GroceryInventory, from Blackboard. You will define all of the other classes, as per the descriptions below.
The GroceryInventory class creates an array of Food items. It adds to this array a series of concrete Food objects, such as apples, oranges, chicken, and rice. See the provided code. Each of the concrete objects represents a type of food and the quantity of it that the store has. As a user, you can enter item names followed by the quantity to purchase, such as “Rice 50”, which removes 50 units of rice from the store inventory. When you are done removing items (it is in a loop so you can test multiple items easily), type STOP to quit the loop. Finally, it will print the details of your purchase. The values for fat, protein, and sugar are in terms of calories for the entire purchase. It then outputs the updated inventory. Sample output is at the end of this document.
Class Food:
The Food class has a String for name, a String for the unit type, a double for the price per unit, a double for the amount of stock (which is in terms of the unit type defined), and the number of calories per unit. Food has a single constructor, which takes the name, unit, price, stock amount, and calories per unit, and sets all of the values of the current object accordingly.
Food has getters for fat (in percentage of calories), sugar (in percentage of calories), protein (in percentage of calories), name, price, and calories per unit, and they are called getFat(), getSugar(), getProtein(), getName(), getPrice(), and getCaloriesPerUnit() respectively. Food also has a method called decreaseStock which takes the amount of stock (in the units of the food item) to remove from the inventory. If there is enough stock to remove, remove the required amount and return true. Otherwise, return false and do not remove any stock. Finally, Food has an override for toString which prints the name of the food item, followed by “, ”, followed by the price per unit in the format of “$” followed by the
price followed by “/” followed by the unit, which is followed by “, ” and the amount of stock remaining, followed by “ ” and the unit for this food item, followed by “ in stock.”.
Class Carbohydrate:
Has a constructor, accepting name, unit, price, amount of stock, and calories per unit. The toString method should show “Carbohydrate: ” followed by the toString() of Food. Carbohydrates have 20% calories from fat, 60% calories from sugar, and 20% calories from protein.
Class Fruit:
Constructor is the same as Carbohydrate. The toString method should show “Fruit: followed by the toString() of Food. Fruits have 100% calories from sugar, 0% calories from fat, and 0% calories from protein.
Class MeatFish:
Same constructor as Carbohydrate and Fruit. The toString method should show “MeatFish: followed by the toString() of Food. MeatFish have 70% calories from fat, 10% calories from sugar, and 20% calories from protein.
Sample Output:
Inventory:
Fruit: Apple, $1.89/kg, 200.35 kg in stock
Fruit: Orange, $2.36/dozen, 38.0 dozen in stock
Carbohydrate: Rice, $2.99/kg, 20.0 kg in stock
MeatFish: Chicken, $5.49/kg, 538.32 kg in stock
MeatFish: Lamb, $4.32/kg, 134.62 kg in stock
MeatFish: Shrimp, $9.65/kg, 74.52 kg in stock
Enter
Type STOP to finish
Rice 2
Orange 3
Apple 1
Lamb 0
Shrimp 2
STOP
Total price: $34.25
Total calories from fat: 1440.0 calories
Total calories from sugar: 1820.0 calories
Total calories from protein: 540.0 calories
Inventory:
Fruit: Apple, $1.89/kg, 199.35 kg in stock
Fruit: Orange, $2.36/dozen, 35.0 dozen in stock
Carbohydrate: Rice, $2.99/kg, 18.0 kg in stock
MeatFish: Chicken, $5.49/kg, 538.32 kg in stock
MeatFish: Lamb, $4.32/kg, 134.62 kg in stock
MeatFish: Shrimp, $9.65/kg, 72.52 kg in stock
Answer:
Carbohydrate Class
public class Carbohydrate extends Food{ public Carbohydrate(String name,String unit, double price,double stock,double calories) { this.name = name; this.price = price; this.unit=unit; this.CaloriesPerUnit=calories; this.stock=stock; } public double getPrice() { return price; } public double getFat() { return .2; } public double getSugar() { return .6; } public String getName() { return name; } public double getProtein(){ return .2; } public double getCaloriesPerUnit(){ return CaloriesPerUnit; } @Override public String toString() { // TODO Auto-generated method stub String s= super.toString(); return ("Carbohydrates: "+s); } }
Food Class
public abstract class Food { protected String name,unit; protected double price,stock,CaloriesPerUnit; protected double fat,sugar,protein; public Food (String name,String unit, double price,double stock,double calories) { this.price = price; this.unit=unit; this.CaloriesPerUnit=calories; this.name=name; this.stock=stock; } public Food (){} public abstract double getFat(); public abstract double getSugar(); public abstract double getProtein(); public abstract String getName(); public abstract double getPrice(); public abstract double getCaloriesPerUnit(); public boolean decreaseStock(double amount){ if (amount<=0){ return false; } else{ stock -= amount; return true; } } public String toString(){ if(stock<=0){ return name +" is Out of Stock "; } return (name + ", $" + price + "/"+ unit+ ","+stock + " "+ unit+" in stock" ); } }
Fruit Class
public class Fruit extends Food{ public Fruit(String name,String unit, double price,double stock,double calories) { this.sugar = 1.0; this.name = name; this.price = price; this.unit=unit; CaloriesPerUnit=calories; this.stock=stock; } public double getPrice() { return price; } public double getFat() { return 0.0; } public double getSugar() { return sugar; } public String getName() { return name; } public double getProtein(){ return 0.0; } public double getCaloriesPerUnit(){ return CaloriesPerUnit; } @Override public String toString() { String s; s= super.toString(); return "Fruit: " +s; } }
GroceryInventory Class
import java.util.Scanner; import java.util.StringTokenizer; public class GroceryInventory { public static final int maxFoodItems = 10; private Food[] foodItems = new Food[maxFoodItems]; // Keep track of the store's inventory private int totalFoodItems = 0; public static void main(String[] args) { // For input Scanner keyboard = new Scanner(System.in); String input; StringTokenizer inputTokenizer; // Stock up on items GroceryInventory inventory = new GroceryInventory(); /************************************* Name unit price stock caloriesPerUnit**/ inventory.addFoodItem(new Fruit( "Apple", "kg", 1.89, 200.35, 200)); inventory.addFoodItem(new Fruit( "Orange", "dozen", 2.36, 38, 300)); inventory.addFoodItem(new Carbohydrate("Rice", "kg", 2.99, 20, 450)); inventory.addFoodItem(new MeatFish( "Chicken", "kg", 5.49, 538.32, 800)); inventory.addFoodItem(new MeatFish( "Lamb", "kg", 4.32, 134.62, 1200)); inventory.addFoodItem(new MeatFish( "Shrimp", "kg", 9.65, 74.52, 900)); System.out.println("Inventory:"); System.out.println(""); inventory.printInventory(); System.out.println(""); System.out.println("Enter <item> <amount> to buy"); System.out.println("Type STOP to finish"); System.out.println(""); double totalPrice = 0; double totalFat = 0; double totalSugar = 0; double totalProtein = 0; // Keep parsing through input while( !(input = keyboard.nextLine()).equals("STOP") ) { inputTokenizer = new StringTokenizer(input); String name = inputTokenizer.nextToken(); double amount = Double.parseDouble(inputTokenizer.nextToken()); // Check if it matches any of the items in the inventory for(int i = 0; i < inventory.totalFoodItems; i++) { if(inventory.foodItems[i].getName().equals(name)) { inventory.foodItems[i].decreaseStock(amount); totalPrice += amount * inventory.foodItems[i].getPrice(); totalFat += amount * inventory.foodItems[i].getFat() * inventory.foodItems[i].getCaloriesPerUnit(); totalSugar += amount * inventory.foodItems[i].getSugar() * inventory.foodItems[i].getCaloriesPerUnit(); totalProtein += amount * inventory.foodItems[i].getProtein() * inventory.foodItems[i].getCaloriesPerUnit(); break; } } } keyboard.close(); // Print results System.out.println("Total price: $" + totalPrice); System.out.println("Total calories from fat: " + totalFat + " calories"); System.out.println("Total calories from sugar: " + totalSugar + " calories"); System.out.println("Total calories from protein: " + totalProtein + " calories"); System.out.println(""); System.out.println("Inventory:"); System.out.println(""); inventory.printInventory(); } // Add a food item to the inventory public boolean addFoodItem(Food f) { // List is full if(totalFoodItems >= maxFoodItems) { return false; } foodItems[totalFoodItems] = f; totalFoodItems++; return true; } public void printInventory() { for(int i = 0; i < totalFoodItems; i++) { System.out.println(foodItems[i].toString()); } } }
MeatFish Class
public class MeatFish extends Food{ public MeatFish(String name,String unit, double price,double stock,double calories) { this.name = name; this.price = price; this.unit=unit; this.CaloriesPerUnit=calories; this.stock=stock; } public double getPrice() { return price; } public double getFat() { return .7; } public double getSugar() { return .1; } public String getName() { return name; } public double getProtein(){ return .2; } public double getCaloriesPerUnit(){ return CaloriesPerUnit; } @Override public String toString() { String s= super.toString(); return ("MeatFish: " +s); } }
Leave a reply