Grocery Store Inventory:
Enhance the grocery-store Inventory program created in a Chapter 13 project by providing a FileHandler class that contains write and read methods to write an object to a file in the local directory or read an object from a file in the local directory. Complete the following skeleton by providing the needed code fragments. You don’t actually need the Chapter 13 grocery store Inventory program to do this exercise. You can test this new class with any appropriately instantiated object. The class that defines that object must, however, include a certain special feature. How would you modify the Inventory class to incorporate that special feature?
/************************************************************** FileHandler.java* Dean & Dean** This stores and retrieves an object.*************************************************************/import java.util.*;import java.io.*;public class FileHandler{public static void write(Object object, String filename){ObjectOutputStream fileOut;try{<provide code fragment here>}catch (IOException e){System.out.println(e.getMessage());}} // end write//**********************************************************public static Object read(String filename){ObjectInputStream fileIn;Object object;try{<provide code fragment here>}catch (Exception e){System.out.println(e.getMessage());return new Object(); // to satisfy compiler}} // end read} // end FileHandler classNow, with the FileHandler class added to the slightly modified grocery-store Inventory program, the following driver shows how you can wipe out unwanted modifications by restoring previously saved data:
/************************************************************** InventoryDriver2.java* Dean & Dean** This demonstrates filing of grocery inventory.*************************************************************/public class InventoryDriver2{public static void main(String[] args){Inventory store = new Inventory("groceries");store.newItem("bread", 15, 9.99);store.newItem("SunnyDale", "milk", 2, 2.00);store.newItem("eggs", 3, 1.50);store.newItem("bread", 2, 1.25); // warning: in stockstore.stockReport();FileHandler.write(store, "Inventory.data");store.update("SunnyDale", "milk", .25); // raise price 25%store.update("eggs", -1); // lower quantity by 1store.update("beer", 3); // warning: not stockedstore.newItem("BrookSide", "milk", 4, 1.95);store.stockReport();store = (Inventory) FileHandler.read("Inventory.data");store.stockReport();} // end main} // end InventoryDriver2 classOutput:
Item already exists - breadbread - in stock: 15, price: $9.99SunnyDale milk - in stock: 2, price: $2.00eggs - in stock: 3, price: $1.50Total value: $158.35Cannot find specified item - beerbread - in stock: 15, price: $9.99SunnyDale milk - in stock: 2, price: $2.50eggs - in stock: 2, price: $1.50BrookSide milk - in stock: 4, price: $1.95Total value: $165.65bread - in stock: 15, price: $9.99SunnyDale milk - in stock: 2, price: $2.00eggs - in stock: 3, price: $1.50Total value: $158.35
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.