Question

In: Computer Science

Java Programming language. Proof of concept class design based on the following ideas Look at your...

Java Programming language.

  • Proof of concept class design based on the following ideas
  • Look at your refrigerator and think about how you would model it as a class. Considerations include:
    • A refrigerator is made by a company on a manufacturing date and has an overall size based on length, width, and height
    • A refrigerator contains a number of shelves and drawers for storing dairy, meats, and vegetables
    • A refrigerator also has storage areas on the door for things like bottled items, condiments, jars, etc.
    • Some refrigerators have a freezer side or section as well for storing meats, frozen food products, etc.
    • Some refrigerators have an ice maker and drinking water filter
  • There are also different types of items you can store in the refrigerator. Considerations here include:
    • All items at least have some kind of id, name, and price
    • All items are put in the refrigerator on a specific date, and some items are perishable that expire on a specific date
  • For this assignment, the minimum requirements you must demonstrate are:
    • Create or use an existing Date class for handling refrigerator manufacturing date and item storage and expiration dates
    • Create a Refrigerator class and demonstrate use of an array (or other collection data structure) within it to model storing the different items in a refrigerator
  • We have not talked about advanced OO concepts yet such as Inheritance and Polymorphism but if you are familiar with the concepts, you may see how they can be applied here so long as the minimum class design requirements are met
  • Minimum testing requirements are as follows:
    • Create at least 2 refrigerator objects and an appropriate number of different items to store into each refrigerator to properly test your class design
    • Design methods in your classes to be able to perform and report on the following situations:
      • Simulate adding and removing items to and from the refrigerator
      • Be able to see how many items are in a refrigerator and which items are expire and when
      • Be able to show where different items in the refrigerator are located (i.e. on which shelf, drawer, or door, in the freezer, etc.)
      • Report how much money has been spent on all the items in the refrigerator

Solutions

Expert Solution

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());
   }

}


Related Solutions

In the programming language java: Write a class encapsulating the concept of a telephone number, assuming...
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming a telephone number has only a single attribute: aString representing the telephone number. Include a constructor, the accessor and mutator, and methods 'toString' and 'equals'. Also include methods returning the AREA CODE (the first three digits/characters of the phone number; if there are fewer than three characters in the phone number of if the first three characters are not digits, then this method should...
Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following properties: • Name of company • Number of shares owned • Value of each share • Total value of all shares and the following operations: • Acquire stock in a company • Buy more shares of the same stock • Sell stock • Update the per-share value of a stock • Display information about the holdings • The StockB class should have the proper...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A double data field named price (for the price of a ticket from this machine). • A double data field named balance (for the amount of money entered by a customer). • A double data field named total (for total amount of money collected by the machine). • A constructor that creates a TicketMachine with all the three fields initialized to some values. • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A string data field named symbol1 for the stock’s symbol. • A string data field named name for the stock’s name. • A double data field named previousClosingPrice that stores the stock price for the previous day. • A double data field named currentPrice that stores the stock price for the current time. • A constructor that creates a stock with the specified symbol and...
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle....
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area of...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
program language: JAVA For this project, you get to design and write a WeightedCourseGrade class to...
program language: JAVA For this project, you get to design and write a WeightedCourseGrade class to keep track of a student's current grade. You also get to design and write WeightedCourseGradeDriver class that requests input from the user and interacts with the WeightedCourseGrade class. Your WeightedCourseGrade class should store the following information: Weighted subtotal (the sum of all of the categories multiplied by the grade category weight) Total category weights (the sum of all the grade category weights) Provide the...
In JAVA Language Please! Programming Exercise 3.20 required you to design a PID manager that allocated...
In JAVA Language Please! Programming Exercise 3.20 required you to design a PID manager that allocated a unique process identifier to each process. Exercise 4.20 required you to modify your solution to Exercise 3.20 by writing a program that created a number of threads that requested and released process identifiers. Now modify your solution to Exercise 4.20 by ensuring that the data structure used to represent the availability of process identifiers is safe from race conditions. Use Pthreads mutex locks....
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data field named id for the account (default 0). 2. A private double data field named balance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A no-arg constructor that creates a default account. 6....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT