In: Computer Science
Program in Java using Inheritence
The purpose of this assignment is to practice OOP programming covering Inheritance.
Core Level Requirements (up to 6 marks)
The scenario for this assignment is to design an online shopping system for a local supermarket (e.g., Europa Foods Supermarket or Wang Long Oriental Supermarket). The assignment is mostly concentrated on the product registration system. Design and draw a UML diagram, and write the code for the following classes: The first product category is a fresh product, it contains the following attributes. - ID: The identification number must start with 1 and follows with 6 numbers, e.g., “1123456”. - Description, e.g. “Banana” - Recommended Price per unit, e.g. 5.54. Note that this is the suggested price, it is not the actual price in the online shopping system. - Unit (by default is 1) - Weight (in gram) - Unit Type, e.g. “Each, Kilogram, Bunch, Box and Pack” - The packing or baking date (it should include the date, month, year and time), this information is not required for some product, hence, it default may be set to 1/1/1900. - The expiry date is (it should include the date, month, year) The purpose of this class is to maintain the general information of fresh or daily product such as meat, seafood, bread, milk and vegetable. The second product category is a packaged product such as can, source, snack, etc. It contains the following attributes. - ID: The identification number must start with 2 and follows with 6 numbers, e.g., “2123456”. - Description, e.g. “Corn cream can” - Recommended Price per unit, e.g. 5.54. - Unit (by default is 1) - Weight (in gram) - The dimension of the package (including height, width, deep). If a product doesn’t provide this information, then it set to (0, 0, 0) as a default value. CSIT121 Spring 2019 - Nutrition Information o Quantity per 100 g or 100 ml o Energy/kilojoules o Protein o Fat o Sugars(mg) o Sodium/salt(mg) - Unit Type for this class is limited to only “Box, Bottle and Pack” - Packing or manufacturing date (it should include the date, month, year and time), this information is not required for some product, hence, it default may be set to 1/1/1900. - Expire date is (it should include the date, month, year), its default may be set to 1/1/3000 for the product that doesn’t have an expiry date. The purpose of this class is to maintain the general information of package product such as can, sauce, instant noodle, flour and snack. Noted that all classes should override and implement the toString methods to transform the information of the object into the string variable. Ensure that your classes are well designed with practical attributes (either primitive type, class object or Enumerated Data type) and have proper methods to handle all tasks (Encapsulation). Students are allowed to add any classes to the design, it is necessary to perfect the design. Task 1: Class design (Code and UML). 1. Design and write the java code for the above two categories. 2. Design 2 subclass with 2 additional attributes for each category. Task 2: Your classes should be able to provide methods allowing one to 1. (verifyUnitType) Check whether the input arguments (a unit type) passed to the method matches with the user type stored in the object or not. 2. (getAttributeDescriptionForSavingTofile) output the description of all attributes to a String format that can be used to save it to a CVS format file directly, e.g. “ID, Description, …. , Expire date”. 3. (getInformationForSavingToFile) Take all attributes in the object and output them to a String format that can be used to save it to a CVS format file directly, e.g. “1222344, Corn Cream,…. , 20/11/2019”. (0.5 marks for each feature) Choose the appropriate input or/and output type for each method. Test Code: write the test code (a separate class/file called “TestCode” with the Main method in it), create the object of each class, and create an ArrayList to keep all the above classes. For each object, call methods in Task 2 to check whether the method works properly. Display the output of the methods to the console. Finally, display information of all the object in the ArrayList to the console. (0.5 marks)
package inheritance;
import java.util.*;
// Defines super class Product
class Product
{
// Instance variable to store product information
String id;
String description;
double price;
double weight;
Date packingDate;
Date expiryDate;
// Default constructor to assign default values to instance variables
@SuppressWarnings("deprecation")
Product()
{
id = description = "";
price = weight = 0.0;
packingDate.setDate(1);
packingDate.setMonth(1);
packingDate.setYear(1900);
expiryDate.setDate(0);
expiryDate.setMonth(0);
expiryDate.setYear(0);
}// End of default constructor
// Parameterized constructor to assign parameter values to instance variables
Product(String ID, String des, double pr, double we, Date pd, Date ex)
{
id = ID;
description = des;
price = pr;
weight = we;
packingDate = pd;
expiryDate = ex;
}// End of parameterized constructor
// Method to set id
void setId(String ID)
{
id = ID;
}// End of method
// Method to set price
void setPrice(double pr)
{
price = pr;
}// End of method
// Method to set weight
void setWeight(double we)
{
weight = we;
}// End of method
// Method to set packing date
void setPackingDate(Date pd)
{
packingDate = pd;
}// End of method
// Method to set expiry date
void setExpiryDate(Date ed)
{
expiryDate = ed;
}// End of method
// Method to return id
String getId()
{
return id;
}// End of method
// Method to return price
double getPrice()
{
return price;
}// End of method
// Method to return weight
double getWeight()
{
return weight;
}// End of method
// Method to return packing date
Date getPackingDate()
{
return packingDate;
}// End of method
// Method to return expiry date
Date getExpiryDate()
{
return expiryDate;
}// End of method
// Overrides toString method to return product information
public String toString()
{
return "\n\n Product Id: " + id + "\n Price: " + price +
"\n Weight: " + weight + "\n Packing Date: " + packingDate +
"\n Expiry Date: " + expiryDate;
}// End of method
}// End of class Product
// Derived a class DailyProduct from super class Product
class DailyProduct extends Product
{
// Instance variable to store daily product information
String unitType;
// Default constructor to assign default values to instance variables
DailyProduct()
{
// Calls the base class constructor
super();
unitType = "";
}// End of default constructor
// Parameterized constructor to assign parameter values to instance variables
DailyProduct(String ID, String des, double pr, double we,
Date pd, Date ex, String ut)
{
// Calls the base class constructor
super(ID, des, pr, we, pd, ex);
unitType = ut;
}// End of parameterized constructor
// Method to set unit type
void setUnitType(String ut)
{
unitType = ut;
}// End of method
// Method to return unit type
String getUnitType()
{
return unitType;
}// End of method
// Overrides toString method to return daily product information
public String toString()
{
return super.toString() + "\n Unit Type: " + unitType;
}// End of method
}// End of class DailyProduct
// Derived a class PackageProduct from super class Product
class PackageProduct extends Product
{
// Instance variable to store package product information
int packageWdith;
int packageHeight;
int packageDeep;
String unitType;
String nutritionInfo;
// Default constructor to assign default values to instance variables
PackageProduct()
{
// Calls the base class constructor
super();
packageWdith = packageHeight = packageDeep = 0;
unitType = nutritionInfo = "";
}// End of default constructor
// Parameterized constructor to assign parameter values to instance variables
PackageProduct(String ID, String des, double pr, double we,
Date pd, Date ex, int pw, int ph, int pde, String ut, String ni)
{
// Calls the base class constructor
super(ID, des, pr, we, pd, ex);
packageWdith = pw;
packageHeight = ph;
packageDeep = pde;
unitType = ut;
nutritionInfo = ni;
}// End of parameterized constructor
// Method to set width
void setPackageWdith(int pw)
{
packageWdith = pw;
}// End of method
// Method to set height
void setPackageHeight(int ph)
{
packageHeight = ph;
}// End of method
// Method to set depth
void setPackageDeep(int pd)
{
packageDeep = pd;
}// End of method
// Method to set unit type
void setUnitType(String ut)
{
unitType = ut;
}// End of method
// Method to set nutrition
void setNutritionInfo(String ni)
{
nutritionInfo = ni;
}// End of method
// Method to return width
int getPackageWdith()
{
return packageWdith;
}// End of method
// Method to return height
int getPackageHeight()
{
return packageHeight;
}// End of method
// Method to return depth
int getPackageDeep()
{
return packageDeep;
}// End of method
// Method to return unit type
String getUnitType()
{
return unitType;
}// End of method
// Method to return nutrition
String getNutritionInfo()
{
return nutritionInfo;
}// End of method
// Overrides toString method to return packing product information
public String toString()
{
return super.toString() + "\n Packing Width: " + packageWdith +
"\n Packing Height: " + packageHeight +
"\n Packing Deepth: " + packageDeep +
"\n Unit Type: " + unitType + "\n Nutrition: " + nutritionInfo;
}// End of method
}// End of class PackageProduct
// Defines driver class FoodsSupermarket
public class FoodsSupermarket
{
// main method definition
@SuppressWarnings("deprecation")
public static void main(String st[])
{
// Declares an array list to store products
ArrayList Product = new ArrayList();
// Declares an array of objects for DailyProduct
DailyProduct dp[] = new DailyProduct[3];
// Declares an array of objects for PackageProduct
PackageProduct pp[] = new PackageProduct[3];
// Creates daily product using parameterized constructor
dp[0] = new DailyProduct("1123456", "Banana", 2.56, 1.2, new Date(1, 1, 1998),
new Date(5,1,198), "Each");
dp[1] = new DailyProduct("1123458", "Orange", 89.36, 22.23,
new Date(1, 3, 1999), new Date(5,3,198), "Kilogram");
// Creates package product using parameterized constructor
pp[0] = new PackageProduct("2123477", "noodle", 2.56, 1.2,
new Date(1, 1, 1998), new Date(5,1,198), 12, 10, 5, "Each", "Protein");
pp[1] = new PackageProduct("2123488", "snack", 45.44, 31.11,
new Date(1, 1, 1998), new Date(5,1,198), 22, 12, 3, "Box", "Sodium");
// Adds the daily product objects
Product.add(dp[0]);
Product.add(dp[1]);
// Adds the package product objects
Product.add(pp[0]);
Product.add(pp[1]);
// Loops to display all product information
for(int c = 0; c < Product.size(); c++)
System.out.println(Product.get(c));
}// End of main method
}// End of driver class FoodsSupermarket
Sample Output:
Product Id: 1123456
Price: 2.56
Weight: 1.2
Packing Date: Sun Jul 22 00:00:00 IST 1906
Expiry Date: Thu Aug 17 00:00:00 IST 1905
Unit Type: Each
Product Id: 1123458
Price: 89.36
Weight: 22.23
Packing Date: Thu Sep 20 00:00:00 IST 1906
Expiry Date: Sun Oct 15 00:00:00 IST 1905
Unit Type: Kilogram
Product Id: 2123477
Price: 2.56
Weight: 1.2
Packing Date: Sun Jul 22 00:00:00 IST 1906
Expiry Date: Thu Aug 17 00:00:00 IST 1905
Packing Width: 12
Packing Height: 10
Packing Deepth: 5
Unit Type: Each
Nutrition: Protein
Product Id: 2123488
Price: 45.44
Weight: 31.11
Packing Date: Sun Jul 22 00:00:00 IST 1906
Expiry Date: Thu Aug 17 00:00:00 IST 1905
Packing Width: 22
Packing Height: 12
Packing Deepth: 3
Unit Type: Box
Nutrition: Sodium