Question: How to solve and code the following requirements (below) using the JAVA program? 1. Modify the FoodProduct Class to implement Edible , add the @Overrride

How to solve and code the following requirements (below) using the JAVA program?

1. Modify the FoodProduct Class to implement Edible, add the @Overrride before any methods that were there (get/set methods) that are also in the Edible interface.

2. Modify the CleaningProduct Class to implement Chemical, add the @Overrride before any methods that were there (get/set methods) that are also in the Chemical interface.

3. Create main class to read products from a file, instantiate them, load them into an ArrayList and then print them to the console.

4. The last element are the allergens that you will have to store to an ArrayList.

5. Please show the screenshot of the output.

1. Product.java

public abstract class Product { // attributes private String name; private int sku; private double price; // constructor public Product(String name, int sku, double price) { this.name = name; this.sku = sku; this.price = price; } // getters ans setters public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSku() { return sku; } public void setSku(int sku) { this.sku = sku; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } // toString override @Override public String toString() { return "Product{" + "name='" + name + '\'' + ", sku=" + sku + ", price=" + price + '}'; } }

2. FoodProduct.java

import java.util.ArrayList; import java.util.Date; public class FoodProduct extends Product { // constructors public FoodProduct(String name, int sku, double price, Date expDate, int refrigeration, int servingSize, int caloriesPerServing, ArrayList allergens) { super(name, sku, price); this.expDate = expDate; this.refrigeration = refrigeration; this.servingSize = servingSize; this.caloriesPerServing = caloriesPerServing; this.allergens = allergens; } // attributes private Date expDate; private int refrigeration; private int servingSize; private int caloriesPerServing; private ArrayList allergens; // getters and setters public Date getExpDate() { return expDate; } public void setExpDate(Date expDate) { this.expDate = expDate; } public int getRefrigeration() { return refrigeration; } public void setRefrigeration(int refrigeration) { this.refrigeration = refrigeration; } public int getServingSize() { return servingSize; } public void setServingSize(int servingSize) { this.servingSize = servingSize; } public int getCaloriesPerServing() { return caloriesPerServing; } public void setCaloriesPerServing(int caloriesPerServing) { this.caloriesPerServing = caloriesPerServing; } public ArrayList getAllergens() { return allergens; } public void setAllergens(ArrayList allergens) { this.allergens = allergens; } // toString override @Override public String toString() { return "FoodProduct{" + super.toString() + "expDate=" + expDate + ", refrigeration=" + refrigeration + ", servingSize=" + servingSize + ", caloriesPerServing=" + caloriesPerServing + ", allergens=" + allergens + '}'; } }
 

3. CleaningProduct.java

 
import java.util.ArrayList; public class CleaningProduct extends Product { // constructors public CleaningProduct(String name, int sku, double price, String chemicalName, String hazards, String precautions, String firstAid, ArrayList uses) { super(name, sku, price); this.chemicalName = chemicalName; this.hazards = hazards; this.precautions = precautions; this.firstAid = firstAid; this.uses = uses; } // attributes private String chemicalName; private String hazards; private String precautions; private String firstAid; private ArrayList uses; // getters and setters public String getChemicalName() { return chemicalName; } public void setChemicalName(String chemicalName) { this.chemicalName = chemicalName; } public String getHazards() { return hazards; } public void setHazards(String hazards) { this.hazards = hazards; } public String getPrecautions() { return precautions; } public void setPrecautions(String precautions) { this.precautions = precautions; } public String getFirstAid() { return firstAid; } public void setFirstAid(String firstAid) { this.firstAid = firstAid; } public ArrayList getUses() { return uses; } public void setUses(ArrayList uses) { this.uses = uses; } // toString override @Override public String toString() { return "CleaningProduct{" + super.toString() + "chemicalName='" + chemicalName + '\'' + ", hazards='" + hazards + '\'' + ", precautions='" + precautions + '\'' + ", firstAid='" + firstAid + '\'' + ", uses=" + uses + '}'; } }
 

4. Edible-Interface

 
import java.util.ArrayList; import java.util.Date; public interface Edible { public Date getExpDate(); public void setRefrigerationTemp(int refrigerationTemp); public int getRefrigerationTemp(); public void setExpDate(Date expDate); public int getServingSize(); public void setServingSize(int servingSize); public int getCaloriesPerServing(); public void setCaloriesPerServing(int caloriesPerServing); public String getAllergens(); public void setAllergens(ArrayListallergens); }
 

5. Chemical-Interface

 import java.util.ArrayList; public interface Chemical { public String getChemicalName(); public void setChemicalName(String chemicalName); public String getHazards(); public void setHazards(String hazards); public String getPrecautions(); public void setPrecautions(String precautions); public ArrayList getUses(); public void setUses(ArrayList uses ); public String getFirstAid(); public void setFirstAid(String firstAid); }

6. Main

import java.io.*; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Formatter; import java.util.Scanner; import java.util.ArrayList; public class ProductMain { public static void main (String[] args) throws FileNotFoundException{ File fileInput = new File ("src/FoodProduct"); Scanner sc = new Scanner(fileInput); String name; // product String sku; // product double price; // product Date expDate = new Date(); // food product int refrigerationTemperature; // food product int servingSize; // food product int caloriesPerServing; // food product ArrayList allergens = new ArrayList(); while(sc.hasNextLine()){ String line = sc.nextLine(); String[] productSpec = line.split(","); name = productSpec[0]; sku = productSpec[1]; price = Double.parseDouble(productSpec[2]); String testDate = productSpec[3]; SimpleDateFormat formatter = new SimpleDateFormat("MM/yyyy"); formatter.format(expDate); refrigerationTemperature = Integer.parseInt(productSpec[4]); servingSize = Integer.parseInt(productSpec[5]); caloriesPerServing = Integer.parseInt(productSpec[6]); if(productSpec.length > 5){ for (int i=7; ilength; i++){ allergens.add(productSpec[i]); } } System.out.println( name + " " + sku + " " + price + " " + testDate + " " + refrigerationTemperature + " " + servingSize + " " + caloriesPerServing + " " + allergens + " " ); } } }
 
7. FoodProduct.txt
 
Frosted Mini Wheats,233345667,4.99,10/2020,70,54,190,Wheat Ingredients Activa YoCrunch Yogurt,33445654,2.50,10/2018,35,113,90,Dairy,Peanuts

8. CleaningProduct.txt
Lysol,2344454,4.99,Alkyl (50%C14 40%C12 10%C16) dimethyl benzyl ammonium saccharinate,Hazard to humans and domestic animals. Contents under pressure,Causes eye irritation,In case of eye contact immediately flush eyes thoroughly with water, kitchen, bath Windex,4456765,3.99,Sodium citrate (Trisodium citrate),To avoid electrical shock do not spray at or near electric lines,Undiluted product is an eye irritant, if contact with eye occurs flush immediately with plenty of water for at least 15 to 20 minutes, glass, upholstery, clothing

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!