In: Computer Science
Java Programming language.
this program contains 3 classes:
1. item
2. refrigerator
3. test
Item.java
import java.util.Date;
public class Items {
// Items variables
private int id;
private String itemName;
private double price;
private Date storageDate;
private Date expiryDate;
private String itemLocation;
// default constructor
public Items() {
}
// parametereised constructor
public Items(int id, String itemName, double price, Date storageDate, Date expiryDate, String itemLocation) {
this.id = id;
this.itemName = itemName;
this.price = price;
this.storageDate =
storageDate;
this.expiryDate = expiryDate;
this.itemLocation =
itemLocation;
}
// getters and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Date getStorageDate() {
return storageDate;
}
public void setStorageDate(Date storageDate)
{
this.storageDate =
storageDate;
}
public Date getExpiryDate() {
return expiryDate;
}
public void setExpiryDate(Date expiryDate) {
this.expiryDate = expiryDate;
}
public String getItemLocation() {
return itemLocation;
}
public void setItemLocation(String itemLocation)
{
this.itemLocation =
itemLocation;
}
// to string method
@Override
public String toString() {
return "Items [id=" + id + ",
itemName=" + itemName + ", price=" + price + ", storageDate=" +
storageDate
+ ", expiryDate=" + expiryDate + ",
itemLocation=" + itemLocation + "]";
}
}
------------------------------------------------------------------------------------------------------------------------------
Refrigerator.java
import java.util.Arrays;
import java.util.Date;
public class Refrigerator {
private String companyName;
private Date manufacturingDate;
Items[] item;
public Refrigerator() {
}
public Refrigerator(String companyName, Date
manufacturingDate, Items[] item) {
this.companyName =
companyName;
this.manufacturingDate =
manufacturingDate;
this.item = item;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName)
{
this.companyName =
companyName;
}
public Date getManufacturingDate() {
return manufacturingDate;
}
public void setManufacturingDate(Date
manufacturingDate) {
this.manufacturingDate =
manufacturingDate;
}
public Items[] getItem() {
return item;
}
public void setItem(Items[] item) {
this.item = item;
}
// method to add item in refrigerator and returns true if
added
// new change : instead of passing array in parameter
, using this key word
// array was used.
// note : the problem was since we are not returning
any array, the array
// specified in test was not changed. if we use item
array using this keyword
// it changes in every where, ie power of oop.
// same change done in remove method also
public boolean addItemToRefrigerator(Items items)
{
int n = this.item.length;
Items newarr[] = new Items[n +
1];
for (int i = 0; i < n;
i++)
newarr[i] =
this.item[i];
newarr[n] = items;
this.item = newarr;
return true;
}
// method to remove item
// if item found in array it returns true else false
public boolean removeItemFromRefrigerator(Items item)
{
int n = this.item.length;
boolean isFound = false;
Items newarr[] = new Items[n -
1];
for (int i = 0; i < n - 1; i++)
{
newarr[i] =
this.item[i];
if
(this.item[i].getId() == item.getId()) {
newarr[i] = this.item[i + 1];
isFound = true;
}
}
this.item = newarr;
return isFound;
}
// method to get number of items in
refrigerator
public int getNumberOfItems() {
return this.item.length;
}
// method to get expiry date of all items
public void getExpiryDateOfItems() {
for (int i = 0; i <
this.item.length; i++) {
System.out.println(this.item[i].getId() + " . " +
this.item[i].getItemName() + " : will expire on "
+
this.item[i].getExpiryDate().toString());
}
}
// method to get location of items
public void getLocation() {
for (Items i : this.item) {
System.out.println(i.getId() + " . " + i.getItemName() + " : is
placed in " + i.getItemLocation());
}
}
// method to get total price of all items
public double getTotalPrice() {
double total = 0;
for (Items i : this.item) {
total = total +
i.getPrice();
}
return total;
}
@Override
public String toString() {
return "Refrigerator [companyName="
+ companyName + ", manufacturingDate=" + manufacturingDate + ",
item="
+ Arrays.toString(item) + "]";
}
}
--------------------------------------------------------------------------------------------------------------------
test.java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class test {
public static void main(String[] args) throws
ParseException {
// create items object and entered
item with parameterized constructor
Items item1 = new Items(1, "Milk",
20, new SimpleDateFormat("dd/MM/yyyy").parse("20/03/2020"),
new
SimpleDateFormat("dd/MM/yyyy").parse("25/03/2020"), "door");
Items item2 = new Items(2, "Apple",
20, new SimpleDateFormat("dd/MM/yyyy").parse("25/08/2020"),
new
SimpleDateFormat("dd/MM/yyyy").parse("30/08/2020"), "shelf");
Items item3 = new Items(3, "Meat",
20, new SimpleDateFormat("dd/MM/yyyy").parse("2/04/2020"),
new
SimpleDateFormat("dd/MM/yyyy").parse("2/04/2020"),
"freezer");
Items item4 = new Items(4, "egg",
20, new SimpleDateFormat("dd/MM/yyyy").parse("21/06/2020"),
new
SimpleDateFormat("dd/MM/yyyy").parse("29/06/2020"), "door");
// created two items array with
different number of items
Items[] items1 = { item1, item2,
item3 };
Items[] items2 = { item4, item3
};
// create refrigerator
object
Refrigerator refrigerator1 = new
Refrigerator("Samsung", new
SimpleDateFormat("dd/MM/yyyy").parse("2/04/2020"),
items1);
Refrigerator refrigerator2 = new
Refrigerator("LG", new
SimpleDateFormat("dd/MM/yyyy").parse("20/04/2018"),
items2);
// print elements in
refrigerators
System.out.println(refrigerator1.toString());
refrigerator2.toString();
// add one more item to
refrigerator 2 details
boolean status =
refrigerator2.addItemToRefrigerator(item1);
refrigerator2.addItemToRefrigerator(item1);
if (status == true) {
System.out.println(item1.getItemName() + ": Item added
successfully");
} else {
System.out.println(item1.getItemName() + ": Sorry . Cannot add the
item.");
}
// remove item from refrigerator
1
status =
refrigerator1.removeItemFromRefrigerator(item2);
// status =
refrigerator2.removeItemFromRefrigerator( item1);
// status =
refrigerator2.removeItemFromRefrigerator( item1);
if (status == true) {
System.out.println(item2.getItemName() + ": Item removed
successfully");
} else {
System.out.println(item2.getItemName() + "Sorry . Cannot find the
item.");
}
// get number of items in
refrigerator 1 and refrigerator 2
System.out.println("\nrefrigerator
1 :");
System.out.println("Number of items
in Refrigerator 1 :" + refrigerator1.getNumberOfItems());
System.out.println("\nrefrigerator
2 :");
System.out.println("Number of items
in Refrigerator 2 :" + refrigerator2.getNumberOfItems());
// get Expiry date of all items
in refrigerator 1 and refrigerator 2
System.out.println("\nrefrigerator
1 :");
refrigerator1.getExpiryDateOfItems();
System.out.println("\nrefrigerator 2 :");
refrigerator2.getExpiryDateOfItems();
// get location of all items in
refrigerator 1 and refrigerator 2
System.out.println("\nrefrigerator
1 :");
refrigerator1.getLocation();
System.out.println("\nrefrigerator
2 :");
refrigerator2.getLocation();
// get total price of all items
in refrigerator 1 and refrigerator 2
System.out.println("\nrefrigerator
1 :");
System.out.println("Total price of
items : " + refrigerator1.getTotalPrice());
System.out.println("\nrefrigerator
2 :");
System.out.println("Total price of
items : " + refrigerator2.getTotalPrice());
}
}