Introduction: Grocery Store Inventory
As your first job fresh out of college, youre hired on as a bagger at the local supermarket. In order to
impress the store manager, you decide to write an Inventory program to help with inventory tracking.
Because you have your heart set on moving into the exciting and fast-paced world of grocery
software, you hope that your Inventory program will be a prototype for many future grocery software
solutions.
Use an Inventory class with an ArrayList instance variable that will hold a list of objects,
which may be either of two kinds: (1) Generic items instances of an Item class that includes
three instance variables for: type description, quantity of that type, and price of one unit of that
type. (2) Branded items instances of a Brand class that is derived from the Item class and
aIDs an aIDitional instance variable to specify a brand name.
Your driver class will just be a testing driver, designed to make sure that the methods of the
classes work correctly.
Here some suggested UML class diagrams:
Inventory
inventory : ArrayList<Item>
category : String
+ Inventory(category : String)
+ newItem(brand : String, type : String, quantity : int, price : double) : boolean
+ setQuantity(brand : String, type : String, quantity : int) : void
+ setPrice(brand : String, type : String, price : double) : void
+ getQuantity(brand : String, type : String) : int
+ getPrice(brand : String, type : String) : double
+ update(brand : String, type : String, adjustmentFactor : double) : void
+ update(brand : String, type : String, qtyIncrease : int) : void
findItem(type : String) : ArrayList<Item>
findItem(brand : String, type : String) : ArrayList<Item>
+ stockReport() : void
Item
type : String
quantity : int
price : double
+ Item()
+ Item(type : String)
+ setQuantity(quantity : int) : Item
+ setPrice(price : double) : Item
+ getType() : String
+ getQuantity() : int
+ getPrice() : double
+ update(qtyIncrease : int) : Item
+ update(adjustmentFactor : double) : Item
BrandedItem (extends Item)
brand : String
+ BrandedItem()
+ BrandedItem(brand : String, type : String)
+ getBrand() : String
The Inventory class has lots of methods. Each one takes both an item type and a brand
name. If an item is generic then it has no brand name and the brand argument should be
null. Here are some explanations, but not in the order the methods appear in the UML
diagram.
The constructor should just make the category equal to the parameter value.
The findItem method is a helper method that performs a linear search of the
ArrayList, looking for items that match the item type (and brand, when the brand
argument is not null). It returns a new ArrayList containing all matching Items.
The assumption is that there is only one item of a particular brand and type, and
there is only one generic Item of a particular type. But when you search with just
Type, there may be multiple options and findItem will return all of them. You
have to write your program so that it cant happen that two matching items get
entered into the Inventory.
The newItem method is designed to create new items and put them in the
inventory. If a matching item already exists, it is not created and aIDed. If it
cannot be aIDed the method returns false. If it can be aIDed, the method returns
true.
In the setQuantity, setPrice, getQuantity, and getPrice methods,
start with a call to the findItem method. If findItem returns a single item,
then set or get the quantity or the price in or from the object. If findItem
returns zero items or more than one item: In the one-parameter getQuantity
method, return -1. In the getPrice methods, return Double.NaN (which
means not a number and makes a good sentinel value).
The update methods change items if they find them.
The error messages in the sample output are all produced by the corresponding
methods of the Inventory class. The driver could produce most of the messages,
but not all of them, so its easier to just put the error message code in the
Inventory class. (This is not the best OO design, but well live with it.)
In the stockReport method, use this format:
<type> in stock: <quantity>, price: $<price using #.##>
<brand> <type> in stock: < quantity>, price: $< price using #.##>
<type> in stock: < quantity>, price: $< price using #.##>
Total value: $<total value using #.##>
See the sample output below for example stock reports. To get and display the brand name
from instances of the Brand class, youll have to include something like this:
if (item instanceof Brand)
{
System.out.print(((Brand) item).getBrand() + );
}
For the most part, the Item class methods and the Brand class methods should be easy.
The only non-trivial activities are in the update methods in the Item class. When the
parameter is an int, the method should increase the quantity by this parameter. When the
parameter is a double, the method should multiply the price by 1 + this parameter. (A
negative value should automatically decrease the quantity or price.)
In InventoryDrivers main method, test your other classes with the following lines of code:
Inventory store = new Inventory(groceries);
store.newItem(null, bread, 15, 9.99);
store.newItem(SunnyDale, milk, 2, 2.00);
store.newItem(null, eggs, 3, 1.50);
store.newItem(null, bread, 2, 1.25); //warning: in stock
store.stockReport();
store.update(SunnyDale, milk, .25); // raise price 25%
store.update(null, eggs, -1); // lower quantity by 1
store.update(null, beer, 3); // warning: not stocked
store.newItem(BrookSide, milk, 4, 1.95);
store.stockReport();
store.setPrice(null, iguanas, 99); // warning: not stocked
store.setQuantity(SunnyDale, milk, 3);
store.setPrice(null, eggs, 2.00);
System.out.println(milk quantity: +
store.getQuantity(BrookRidge, milk)); // not stocked
System.out.println(milk quantity: +
store.getQuantity(null, milk)); // ambiguity
System.out.println(
eggs price: + store.getPrice(null, eggs));
System.out.println(
milk price: + store.getPrice(null, milk));
// ambiguity
Using the above lines of code, your program should generate the following output.
Output:
bread already exists
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 beer
bread in stock: 15, price: $9.99
SunnyDale milk in stock: 2, price: $2.50
eggs in stock: 2, price: $1.50
BrookSide milk in stock: 4, price: $1.95
Total value: $165.65
Cannot find iguanas
Cannot find BrookRidge milk
milk quantity: 0
Found more than one brand of milk
milk quantity: -1
eggs price: 2.0
Found more than one brand of milk
milk price: NaN
part one For this assignment you are to to watch: Shattered Glass Write a two…
Standard Project - WebServers. Instruction attached. Need all requirements, you do not have to make…
Read classmates post and respond with 100 words:The International Categorization of Diseases, Tenth Revision, Clinical…
Most Americans have at least 1 issue that is most important to them. Economic issues…
For this assignment, you are the court intake processor at a federal court where you…
Use a standard outline format to lay out how you are going to write your…