Question

In: Computer Science

a) Design a Java Class which represents a Retail Item. It is to have the following fields

 a) Design a Java Class which represents a Retail Item. It is to have the following fields

 Item Name

 Item Serial No

 Item Unit Price

 Item Stock Level

 Item Reorder Level

 It is to have at least the following methods an Observer, Mutator and a Display method for each field. It is also to have a buy and a restock method and a method to issue a warning if the stock level goes below the re-order level.

 

 b) Extend the Retail Item class of part a) above to represent a Perishable Item.

 This subclass will have extra fields to represent expiry date, storage instructions and a consume deadline i,e the number of days in which the item must be consumed once opened.

 Again have at least the following methods for these new fields, namely an Observer, Mutator and a Display method. Also have a method which displays the number of days until the expiry date.

 

 c) Write a control program which creates five instances of standard retail items and five instances of perishable items.

 1. Buy 2 Retail Items and 3 perishable items

 2. Display the Stock levels of any two items

 3. Calculate the total value of Items in Stock including perishable items Note Do not include any perishable items which are past their sell-by date

 4. Re-order Stock for any three Items

 

 

Solutions

Expert Solution

Screenshot of the code:

RetailItem.java:

PerishableItems.java:

Test.java:

Output:

Code to copy:

RetailItem.java:

//Define the class.

public class RetailItem

{

       //Define the variables.

        private String itemName;

        private String serialNumber;

        private int unitPrice;

        private int stockLevel;

        private int reorderLevel;

       

        //Define the constructor.

        public RetailItem(String itemName,

              String serialNumber, int unitPrice,

              int stockLevel, int reorderLevel)

        {

             //Initialize the data members.

            this.itemName = itemName;

            this.serialNumber = serialNumber;

            this.unitPrice = unitPrice;

            this.stockLevel = stockLevel;

            this.reorderLevel = reorderLevel;

        }

       

        //Define the method to return

        //item name.

        public String getterItemName()

        {

            return itemName;

        }

       

        //Define the method to set the item name.

        public void setterItemName(String itemName)

        {

            this.itemName = itemName;

        }

       

        //Define the method to return

        //serial number.

        public String getterSerialNumber()

        {

            return serialNumber;

        }

       

        //Define the method to set serial number.

        public void setterSerialNumber(String serialNumber)

        {

            this.serialNumber = serialNumber;

        }

       

        //Define the method to return

        //unit price.

        public int getterUnitPrice()

        {

            return unitPrice;

        }

       

        //Define the method to set the unit price.

        public void setterUnitPrice(int unitPrice)

        {

            this.unitPrice = unitPrice;

        }

       

        //Define the method to return

        //the stockLevel.

        public int getterStockLevel()

        {

            return stockLevel;

        }

       

        //Define the method to set the stock level

        public void setterStockLevel(int stockLevel)

        {

            this.stockLevel = stockLevel;

        }

       

        //Define the method to return

        //the reorder levels.

        public int getterReorderLevel()

        {

            return reorderLevel;

        }

        //Define the method to set the reorder level.

        public void setterReorderLevel(int reorderLevel)

        {

            this.reorderLevel = reorderLevel;

        }

        //Define the method to update the

        //Stock level after buying the stock.

        public void buy(int b)

        {

            setterStockLevel(getterStockLevel() + b);

        }

        //Define the method to set the re-stock

        //levels.

        public void restock(int stock)

        {

            setterStockLevel(stock);

        }

       

        //Define the method to display the warning.

        public void warning()

        {  

             //Compare the stock level.

             //Display error if stock is insufficient.

            if(getterStockLevel()

                System.out.println("WARNING: Stock "

             + "level is below the reorder level!");

            else

                System.out.println("Stock level "

                                    + "looks good!");

        }

       

        //Define the method to return the value

        //of data members of the class.

        @Override

        public String toString() {

            return "RetailItem [itemName=" + itemName +

                 ", serialNumber=" + serialNumber + ","

                         + " unitPrice=" + unitPrice +

                    ", stockLevel=" + stockLevel + ","

                 + " reorderLevel=" + reorderLevel + "]";

        }

     }

PerishableItems.java:

//Import for input.

import java.util.*;

//Import for time.

import java.util.concurrent.TimeUnit;

public class PerishableItems extends RetailItem

{

   //Define the variables.

   private Date expiryDate;

   private String storageInstructions;

   private int consumeDeadline;

  

   //Define the constructor.

   public PerishableItems(String itemName,

          String serialNumber, int unitPrice,

          int stockLevel, int reorderLevel,

          Date expiryDate, String storageInstructions,

          int consumeDeadline)

   {  

        //Use super for base class.

       super(itemName, serialNumber,

             unitPrice, stockLevel, reorderLevel);

     

       //Set the values of data members.

       this.expiryDate = expiryDate;

       this.storageInstructions = storageInstructions;

       this.consumeDeadline = consumeDeadline;

   }

   //Define the method to return the expiry date.

   public Date getterExpiryDate()

   {

       return expiryDate;

   }

  

   //Define the method to set the expiry date.

   public void setterExpiryDate(Date expiryDate)

   {

       this.expiryDate = expiryDate;

   }

  

   //Define the method to return the

   //storage instruction.

   public String getterStorageInstructions()

   {

       return storageInstructions;

   }

    //Define the method to set the storage

   //Instruction.

   public void setterStorageInstructions(String

                           storageInstructions)

   {

       this.storageInstructions = storageInstructions;

   }

   //Define the method to return the deadlines.

   public int getterConsumeDeadline()

   {

       return consumeDeadline;

   }

   //Define the method to set the deadlines.

   public void setterConsumeDeadline(int consumeDeadline)

   {

       this.consumeDeadline = consumeDeadline;

   }

   //Define the method to return the expiry date.

   public int daysExpiry()

   {

       int diff = (int) (new Date().getTime() -

                   getterExpiryDate().getTime());

        return (int) TimeUnit.DAYS.convert(diff,

                        TimeUnit.MILLISECONDS);

   }

   //Define the method to return the value of

   //data members.

   @Override

   public String toString()

   {

       return "PerishableItems [expiryDate=" + expiryDate +

             ", storageInstructions=" + storageInstructions

               + ", consumeDeadline=" + consumeDeadline +

               ", getItemName()=" + getterItemName() +

             ", getSerialNumber()="+ getterSerialNumber() +

               ", getUnitPrice()=" + getterUnitPrice() +

               ", getStockLevel()=" + getterStockLevel()+

        ", getReorderLevel()=" + getterReorderLevel()+ "]";

   }

}

Test.java:

//Import for date.

import java.util.Date;

//Define the class test.

public class Test {

   //Define the main method.

   public static void main(String[] args) {

        //Create 2 objects of retailitem class.

       RetailItem item1 = new RetailItem

       ("Iphone7", "XYZ001", 70000, 100, 50);

       RetailItem item2 = new RetailItem

       ("MacBook", "XYZ002", 90000, 120, 70);

      

       //Create 3 items of perishableitem class.

       PerishableItems item3 = new PerishableItems

       ("Peanuts", "Pe01", 40, 700, 200, new Date(),

                    "Use by time 30 days", 2);

       PerishableItems item4 = new PerishableItems

       ("Bread", "B001", 200, 9000, 1100, new Date(),

                              "Use by time 2 days", 5);

       PerishableItems item5 = new PerishableItems

       ("Butter", "B002", 90, 800, 200, new Date(),

                         "Store in refrigerator", 7);

      

       //Display the stock level of 2 items.

       System.out.println("Stock level for item 1= "

                          +item1.getterStockLevel());

       System.out.println("Stock level for item 2= "

                          +item2.getterStockLevel());

      

       //Calculate the total value of items in stock.

       int tValue = item1.getterUnitPrice() +

                       item2.getterUnitPrice() +

                       item3.getterUnitPrice() +

                       item4.getterUnitPrice() +

                       item5.getterUnitPrice();

      

       //Display the total value.

       System.out.println("Total Stock values = " +tValue);

       //Re-stock for 3 items.

       item1.restock(1000);

       item2.restock(2000);

       item3.restock(3000);

      

       //Display the record after re-stocking.

       System.out.println("After restocking:\n");

       System.out.println(item1);

       System.out.println(item2);

       System.out.println(item3);

   }

}


Related Solutions

a) Design a Java Class which represents a Retail Item. It is to have the following fields
 a) Design a Java Class which represents a Retail Item. It is to have the following fields Item Name Item Serial No Item Unit Price Item Stock Level Item Reorder Level It is to have at least the following methods an Observer, Mutator and a Display method for each field. It is also to have a buy and a restock method and a method to issue a warning if the stock level goes below the re-order level. b) Extend the Retail Item class of part a) above...
Java... Design a Payroll class with the following fields: • name: a String containing the employee's...
Java... Design a Payroll class with the following fields: • name: a String containing the employee's name • idNumber: an int representing the employee's ID number • rate: a double containing the employee's hourly pay rate • hours: an int representing the number of hours this employee has worked The class should also have the following methods: • Constructor: takes the employee's name and ID number as arguments • Accessors: allow access to all of the fields of the Payroll...
Design a class named Pet, which should have the following fields: Name – The name field...
Design a class named Pet, which should have the following fields: Name – The name field holds the name of a pet. Type – The type field holds the type of animal that is the pet. Example values are “Dog”, “Cat”, and “Bird”. Age – The age field holds the pet’s age. The Pet class should also have the following methods: setName – The setName method stores a value in the name field. setType – The setType method stores a...
Write a java program using the following information Design a LandTract class that has two fields...
Write a java program using the following information Design a LandTract class that has two fields (i.e. instance variables): one for the tract’s length(a double), and one for the width (a double). The class should have:  a constructor that accepts arguments for the two fields  a method that returns the tract’s area(i.e. length * width)  an equals method that accepts a LandTract object as an argument. If the argument object holds the same data (i.e. length and...
Design an Inventory class that can hold information for an item in a retail store’s inventory.
C++! Becareful following the test case.7. Inventory ClassDesign an Inventory class that can hold information for an item in a retail store’s inventory.The class should have the following private member variables.Variable Name   DescriptionitemNumber   An int that holds the item’s number.quantity               An int that holds the quantity of the item on hand.cost    A double that holds the wholesale per-unit cost of the itemThe class should have the following public member functionsMember Function               Descriptiondefault constructor             Sets all the member variables...
(Java) Design a class named Person with fields for holding a person’s name, address, and telephone...
(Java) Design a class named Person with fields for holding a person’s name, address, and telephone number. Write one or more constructors and the appropriate mutator and accessor methods for the class’s fields. Next, design a class named Customer, which extends the Person class. The Customer class should have a field for a customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write one or more constructors and the appropriate mutator and...
JAVA - Design and implement a class called Flight that represents an airline flight. It should...
JAVA - Design and implement a class called Flight that represents an airline flight. It should contain instance data that represent the airline name, the flight number, and the flight’s origin and destination cities. Define the Flight constructor to accept and initialize all instance data. Include getter and setter methods for all instance data. Include a toString method that returns a one-line description of the flight. Create a driver class called FlightTest, whose main method instantiates and updates several Flight...
JAVA/Netbeans •Design a class named Person with fields for holding a person’s name, address and phone...
JAVA/Netbeans •Design a class named Person with fields for holding a person’s name, address and phone number (all Strings) –Write a constructor that takes all the required information. –Write a constructor that only takes the name of the person and uses this to call the first constructor you wrote. –Implement the accessor and mutator methods. Make them final so subclasses cannot override them –Implement the toString() method •Design a class named Customer, which extends the Person class. It should have...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape,...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape, Two Dimensional Shape, Three Dimensional Shape, Square, Circle, Cube, Rectangular Prism, and Sphere. Cube should inherit from Rectangular Prism. The two dimensional shapes should include methods to calculate Area. The three dimensional shapes should include methods to calculate surface area and volume. Use as little methods as possible (total, across all classes) to accomplish this, think about what logic should be written at which...
Design a class named Employee. The class should keep the following information in fields: ·         Employee...
Design a class named Employee. The class should keep the following information in fields: ·         Employee name ·         Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M. ·         Hire date Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that inherits from the Employee class. The ProductionWorker class should have fields...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT