In: Computer Science
Java
Create an abstract Product Class
Add the following private attributes:
name
sku (int)
price (double)
Create getter/setter methods for all attributes
Create a constructor that takes in all attributes and sets them
Remove the default constructor
Override the toString() method and return a String that represents
the building object that is formatted
nicely and contains all information (attributes)
Create a FoodProduct Class that extends Product
Add the following private attributes:
expDate (java.util.Date) for expiration date
refrigerationTemp (int) if 70 or above, none necessary, otherwise it will be a lower number
servingSize (int) in grams
caloriesPerServing (int)
allergens (ArrayList<String>)
Create getter/setter methods for all attributes
Create a constructor that takes in all attributes for product and food product. Call the super constructor and then set the expDate, refrigerationTemp, servingSize, caloriesPerServing and allergens.
Remove the default constructor
Override the toString() method and return a String that represents the food product object that is formatted nicely and contains all information (super toString and the private attributes)
Create a CleaningProduct Class that extends Product
Add the following private attributes:
String chemicalName
String hazards
String precautions
String firstAid
ArrayList<String> uses (e.g. kitchen, bath, laundry, etc)
Create getter/setter methods for all attributes
Create a constructor that takes in all attributes for product and food product. Call the super constructor and then set the chemical name, hazard, precautions, firstAid and uses. TIP: try generating the constructor, it will add the super stuff for you. (Also, see Lynda inheritance videos.)
Remove the default constructor
Override the toString() method and return a String that represents the cleaning product object that is formatted nicely and contains all information (super toString() and the private attributes)
Create the interface Edible which contains the following method signatures:
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(ArrayList<String> allergens) ;
Create the interface Chemical which contains the following method signatures
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<String> getUses() ;
public void setUses(ArrayList<String> uses) ;
public String getFirstAid() ;
public void setFirstAid(String firstAid) ;
Modify the FoodProduct Class to implement Edible, be sure to add the @Override before any methods that were there (get/set methods) that are also in the Edible interface.
Modify the CleaningProduct Class to implement Chemical, be sure to add the @Override before any methods that were there (get/set methods) that are also in the Chemical interface.
Create your main/test class to read products from a file, instantiate them, load them into an ArrayList and then print them nicely to the console at the end of your program. See instructor note about reading the file as the lengths of the lines may be different depending on the list of allergens. It is also comma delimited rather than by spaces. You will have to read an entire line, split its contents and then store the information into variables. The last element(s) are the allergens that you will have to store to an ArrayList.
2 note files:
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
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
/*****************************Edible.java********************/
import java.util.ArrayList;
import java.util.Date;
/**
* The Interface Edible.
*/
public interface Edible {
/**
* Gets the exp date.
*
* @return the exp date
*/
public Date getExpDate();
/**
* Sets the refrigeration temp.
*
* @param refrigerationTemp the new refrigeration
temp
*/
public void setRefrigerationTemp(int
refrigerationTemp);
/**
* Gets the refrigeration temp.
*
* @return the refrigeration temp
*/
public int getRefrigerationTemp();
/**
* Sets the exp date.
*
* @param expDate the new exp date
*/
public void setExpDate(Date expDate);
/**
* Gets the serving size.
*
* @return the serving size
*/
public int getServingSize();
/**
* Sets the serving size.
*
* @param servingSize the new serving size
*/
public void setServingSize(int servingSize);
/**
* Gets the calories per serving.
*
* @return the calories per serving
*/
public int getCaloriesPerServing();
/**
* Sets the calories per serving.
*
* @param caloriesPerServing the new calories per
serving
*/
public void setCaloriesPerServing(int
caloriesPerServing);
/**
* Gets the allergens.
*
* @return the allergens
*/
public ArrayList<String> getAllergens();
/**
* Sets the allergens.
*
* @param allergens the new allergens
*/
public void setAllergens(ArrayList<String>
allergens);
}
/***************************Chemical.java*****************************/
import java.util.ArrayList;
/**
* The Interface Chemical.
*/
public interface Chemical {
/**
* Gets the chemical name.
*
* @return the chemical name
*/
public String getChemicalName();
/**
* Sets the chemical name.
*
* @param chemicalName the new chemical name
*/
public void setChemicalName(String chemicalName);
/**
* Gets the hazards.
*
* @return the hazards
*/
public String getHazards();
/**
* Sets the hazards.
*
* @param hazards the new hazards
*/
public void setHazards(String hazards);
/**
* Gets the precautions.
*
* @return the precautions
*/
public String getPrecautions();
/**
* Sets the precautions.
*
* @param precautions the new precautions
*/
public void setPrecautions(String precautions);
/**
* Gets the uses.
*
* @return the uses
*/
public ArrayList<String> getUses();
/**
* Sets the uses.
*
* @param uses the new uses
*/
public void setUses(ArrayList<String> uses);
/**
* Gets the first aid.
*
* @return the first aid
*/
public String getFirstAid();
/**
* Sets the first aid.
*
* @param firstAid the new first aid
*/
public void setFirstAid(String firstAid);
}
/*******************************Product.java********************************/
/**
* The Class Product.
*/
public abstract class Product {
/** The name. */
private String name;
/** The sku. */
private int sku;
/** The price. */
private double price;
/**
* Instantiates a new product.
*
* @param name the name
* @param sku the sku
* @param price the price
*/
public Product(String name, int sku, double price)
{
super();
this.name = name;
this.sku = sku;
this.price = price;
}
/**
* Gets the name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Sets the name.
*
* @param name the new name
*/
public void setName(String name) {
this.name = name;
}
/**
* Gets the sku.
*
* @return the sku
*/
public int getSku() {
return sku;
}
/**
* Sets the sku.
*
* @param sku the new sku
*/
public void setSku(int sku) {
this.sku = sku;
}
/**
* Gets the price.
*
* @return the price
*/
public double getPrice() {
return price;
}
/**
* Sets the price.
*
* @param price the new price
*/
public void setPrice(double price) {
this.price = price;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Product name=" + name + ",
sku=" + sku + ", price=" + price;
}
}
/***************************FoodProduct.java************************/
import java.util.ArrayList;
import java.util.Date;
/**
* The Class FoodProduct.
*/
public class FoodProduct extends Product implements Edible {
/** The exp date. */
private Date expDate;
/** The refrigeration temp. */
private int refrigerationTemp;
/** The serving size. */
private int servingSize;
/** The calories per serving. */
private int caloriesPerServing;
/** The allergens. */
private ArrayList<String> allergens;
/**
* Instantiates a new food product.
*
* @param name the name
* @param sku the sku
* @param price the price
* @param expDate the exp date
* @param refrigerationTemp the refrigeration
temp
* @param servingSize the serving size
* @param caloriesPerServing the calories per
serving
* @param allergens the allergens
*/
public FoodProduct(String name, int sku, double price,
Date expDate, int refrigerationTemp, int servingSize,
int
caloriesPerServing, ArrayList<String> allergens) {
super(name, sku, price);
this.expDate = expDate;
this.refrigerationTemp =
refrigerationTemp;
this.servingSize =
servingSize;
this.caloriesPerServing =
caloriesPerServing;
this.allergens = allergens;
}
/*
* (non-Javadoc)
*
* @see Edible#getExpDate()
*/
@Override
public Date getExpDate() {
return expDate;
}
/*
* (non-Javadoc)
*
* @see Edible#setExpDate(Date)
*/
@Override
public void setExpDate(Date expDate) {
this.expDate = expDate;
}
/*
* (non-Javadoc)
*
* @see Edible#getRefrigerationTemp()
*/
@Override
public int getRefrigerationTemp() {
return refrigerationTemp;
}
/*
* (non-Javadoc)
*
* @see Edible#setRefrigerationTemp(int)
*/
@Override
public void setRefrigerationTemp(int
refrigerationTemp) {
this.refrigerationTemp =
refrigerationTemp;
}
/*
* (non-Javadoc)
*
* @see Edible#getServingSize()
*/
@Override
public int getServingSize() {
return servingSize;
}
/*
* (non-Javadoc)
*
* @see Edible#setServingSize(int)
*/
@Override
public void setServingSize(int servingSize) {
this.servingSize =
servingSize;
}
/*
* (non-Javadoc)
*
* @see Edible#getCaloriesPerServing()
*/
@Override
public int getCaloriesPerServing() {
return caloriesPerServing;
}
/*
* (non-Javadoc)
*
* @see Edible#setCaloriesPerServing(int)
*/
@Override
public void setCaloriesPerServing(int
caloriesPerServing) {
this.caloriesPerServing =
caloriesPerServing;
}
/*
* (non-Javadoc)
*
* @see Edible#getAllergens()
*/
@Override
public ArrayList<String> getAllergens() {
return allergens;
}
/*
* (non-Javadoc)
*
* @see Edible#setAllergens(java.util.ArrayList)
*/
@Override
public void setAllergens(ArrayList<String>
allergens) {
this.allergens = allergens;
}
/*
* (non-Javadoc)
*
* @see Product#toString()
*/
@Override
public String toString() {
return super.toString() + " " +
"expDate=" + expDate + ", refrigerationTemp=" +
refrigerationTemp
+ ", servingSize=" + servingSize + ",
caloriesPerServing=" + caloriesPerServing + ", allergens="
+ allergens;
}
}
/****************************CleaningProduct.java************************************/
import java.util.ArrayList;
/**
* The Class CleaningProduct.
*/
public class CleaningProduct extends Product implements Chemical
{
/** The chemical name. */
private String chemicalName;
/** The hazards. */
private String hazards;
/** The precautions. */
private String precautions;
/** The first aid. */
private String firstAid;
/** The uses. */
private ArrayList<String> uses;
/**
* Instantiates a new cleaning product.
*
* @param name the name
* @param sku the sku
* @param price the price
* @param chemicalName the chemical name
* @param hazards the hazards
* @param precautions the precautions
* @param firstAid the first aid
* @param uses the uses
*/
public CleaningProduct(String name, int sku, double
price, String chemicalName, String hazards, String
precautions,
String firstAid,
ArrayList<String> uses) {
super(name, sku, price);
this.chemicalName =
chemicalName;
this.hazards = hazards;
this.precautions =
precautions;
this.firstAid = firstAid;
this.uses = uses;
}
/* (non-Javadoc)
* @see Chemical#getChemicalName()
*/
@Override
public String getChemicalName() {
return chemicalName;
}
/* (non-Javadoc)
* @see
Chemical#setChemicalName(java.lang.String)
*/
@Override
public void setChemicalName(String chemicalName)
{
this.chemicalName =
chemicalName;
}
/* (non-Javadoc)
* @see Chemical#getHazards()
*/
@Override
public String getHazards() {
return hazards;
}
/* (non-Javadoc)
* @see Chemical#setHazards(java.lang.String)
*/
@Override
public void setHazards(String hazards) {
this.hazards = hazards;
}
/* (non-Javadoc)
* @see Chemical#getPrecautions()
*/
@Override
public String getPrecautions() {
return precautions;
}
/* (non-Javadoc)
* @see Chemical#setPrecautions(java.lang.String)
*/
@Override
public void setPrecautions(String precautions) {
this.precautions =
precautions;
}
/* (non-Javadoc)
* @see Chemical#getFirstAid()
*/
@Override
public String getFirstAid() {
return firstAid;
}
/* (non-Javadoc)
* @see Chemical#setFirstAid(java.lang.String)
*/
@Override
public void setFirstAid(String firstAid) {
this.firstAid = firstAid;
}
/* (non-Javadoc)
* @see Chemical#getUses()
*/
@Override
public ArrayList<String> getUses() {
return uses;
}
/* (non-Javadoc)
* @see Chemical#setUses(java.util.ArrayList)
*/
@Override
public void setUses(ArrayList<String> uses)
{
this.uses = uses;
}
/* (non-Javadoc)
* @see Product#toString()
*/
@Override
public String toString() {
return super.toString()+",
chemicalName=" + chemicalName + ", hazards=" + hazards + ",
precautions=" + precautions
+ ", firstAid=" + firstAid + ", uses=" +
uses;
}
}
/*********************************************TestProduct.java**********************************/
import java.io.File;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Date;
/**
* The Class TestProduct.
*/
public class TestProduct {
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
ArrayList<Product>
products = new ArrayList<>();
File file = new
File("food.txt");
File file2 = new
File("chemical.txt");
try {
Scanner scnr =
new Scanner(file);
Scanner scanner
= new Scanner(file2);
while (scnr.hasNextLine()) {
String line = scnr.nextLine();
String[] data = line.split(",");
String name = data[0];
int sku = Integer.parseInt(data[1]);
double price =
Double.parseDouble(data[2]);
Date date = null;
try {
date = new
SimpleDateFormat("MM/yyyy").parse(data[3]);
} catch (ParseException e) {
}
int refrigerationTemp =
Integer.parseInt(data[4]);
int servingSize =
Integer.parseInt(data[5]);
int caloriesPerServing =
Integer.parseInt(data[6]);
ArrayList<String> allergens = new
ArrayList<>();
for (int i = 7; i < data.length; i++) {
allergens.add(data[i]);
}
products.add(new FoodProduct(name, sku, price,
date, refrigerationTemp, servingSize, caloriesPerServing,
allergens));
}
while
(scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] data = line.split(",");
String name = data[0];
int sku = Integer.parseInt(data[1]);
double price = Double.parseDouble(data[2]);
String chemicalName = data[3];
String hazards = data[4];
String precautions = data[5];
String firstAid = data[6];
ArrayList<String> uses = new
ArrayList<>();
for (int i = 7; i < data.length; i++) {
uses.add(data[i]);
}
products.add(new CleaningProduct(name, sku,
price, chemicalName, hazards, precautions, firstAid, uses));
}
} catch (FileNotFoundException e)
{
System.out.println("File not found!");
}
for (Product product : products)
{
System.out.println(product.toString());
}
}
}
/***************************food.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
/************************************chemical.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
/***********************output****************************/
Product name=Frosted Mini Wheats, sku=233345667, price=4.99
expDate=Thu Oct 01 00:00:00 IST 2020, refrigerationTemp=70,
servingSize=54, caloriesPerServing=190, allergens=[Wheat
Ingredients Activa YoCrunch]
Product name=Yogurt, sku=33445654, price=2.5 expDate=Mon Oct 01
00:00:00 IST 2018, refrigerationTemp=35, servingSize=113,
caloriesPerServing=90, allergens=[Dairy, Peanuts]
Product name=Lysol, sku=2344454, price=4.99, chemicalName=Alkyl
(50%C14 40%C12 10%C16) dimethyl benzyl ammonium saccharinate,
hazards=Hazard to humans and domestic animals. Contents under
pressure, precautions=Causes eye irritation, firstAid=In case of
eye contact immediately flush eyes thoroughly with water, uses=[
kitchen, bath]
Product name=Windex, sku=4456765, price=3.99, chemicalName=Sodium
citrate (Trisodium citrate), hazards=To avoid electrical shock do
not spray at or near electric lines, precautions=Undiluted product
is an eye irritant, firstAid= if contact with eye occurs flush
immediately with plenty of water for at least 15 to 20 minutes,
uses=[ glass, upholstery, clothing]
/*********************************scrshot**********************/
Please let me know if you have any doubt or modify the answer, Thanks :)