Page 675 - Introduction to Programming with Java: A Problem Solving Approach
P. 675
}
// end InventoryDriver2 class
catch (Exception e)
{
}
System.out.println(e.getMessage());
return new Object();
// to satisfy compiler
} // end read
} // end FileHandler class
Now, 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)
{
}
store.newItem("BrookSide", "milk", 4, 1.95);
store.stockReport();
store = (Inventory) FileHandler.read("Inventory.data");
store.stockReport();
// end main
Inventory store = new Inventory("groceries");
Apago PDF Enhancer
store.newItem("SunnyDale", "milk", 2, 2.00);
store.newItem("bread", 15, 9.99);
store.newItem("eggs", 3, 1.50);
store.newItem("bread", 2, 1.25);
store.stockReport();
// warning: in stock
FileHandler.write(store, "Inventory.data");
store.update("SunnyDale", "milk", .25); // raise price 25%
store.update("eggs", -1);
store.update("beer", 3);
// lower quantity by 1
// warning: not stocked
Output:
Item already exists - bread
bread - in stock: 15, price: $9.99
SunnyDale milk - in stock: 2, price: $2.00
eggs - in stock: 3, price: $1.50
Total value: $158.35
Cannot find specified item - beer
Exercises 641
bread - in stock: 15, price: $9.99