Project 5

advertisement
CS 162
Computer Science I
Fall 2001
Project 5
Objective

Get practice with arrays.
Scenario
This program will be an inventory control that allows the user to manage the store's inventory. An Inventory class
that will be an array of SaleItems will model the inventory with operations for

displaying a particular item,

displaying all of the items,

recording the sale of an item,

recording the restocking of an item,

adding an item to the store's inventory, and
 deleting an item from the inventory.
The user interface will be a text based menu driven interface.
This programming project is about a menu with menu options and an inventory of saleItems. The program user
enters a menu option from the menu to utilize or manipulate items in the inventory.
The project is comprised of the following classes:

SaleItem

Inventory

MenuItem

Menu

MerchandiseMenu

Proj5Main
class Inventory (You need to complete the methods of this class)
Static constants

MAX_INV_SIZE = 100, this is the size of the inventory array
 NOT_FOUND_INDEX = -1, see instance variable currentIndex below
Instance variables

inventory, an array of SaleItem objects

invSize, the number of SaleItem objects stored in the array

currentIndex, there is a search method, findItem, for looking up an item in the inventory array. The
findItem method sets this variable to the index where the item is located in the array or to
NOT_FOUND_INDEXif the item is not found in the array.
Constructor and Methods
Read the inventory. This involves getting the name of the inventory input file as SimpleIO input, and then
calling the read method for that object, passing it the BufferedReader object. Finally close the inventory input
file.
The class file Inventory.java contains comments above each method. See that file for documentation.
A Sample Data File
Below is a sample data file. The file starts with an integer value n that is followed by n four-line records each
containing the data for a SaleItem object. The sample file below contains 3 records but your program should be
capable of handling a file with any number of records. Don't fixate on 3. As you can see the format of the data
file for Project 5 is identical to that of Project 4. The difference is in how the data is used.
3
112345
Down All the Days
12.95
8
204562
A Thousand Clowns
6.98
16
313579
Sgt. Peppers Lonely Hearts Club Band
16.99
4
class Menu
We wish to have the program display a text-based menu that looks like the one on the left below. If we analyze
this menu we see that it is a sequence of items to choose from. Each item is defined by a character that the user is
to enter to designate that choice and a descriptive string. In this particular menu the choices are designated by the
numeric characters '1','2', '3', ..., '7' but in this or another menu we could have used letters of the alphabet, a - g, A
- G, or some other set of characters. This suggests that our Menu class have an array of MenuItem objects and that
a MenuItem object be defined by a choice character and a description. We also want our Menu class to be flexible
so that it can be used for a variety of menu structures. This will be the case if it is defined by data in a data file,
like the one on the right below. The data file for Project 5's menu is menu1.txt
===============Menu ===============
1) Display a sales item
2) Display all sales items
3) Record a sale
4) Record a restock
5) Add a new item
6) Delete an item
7)Quit
====================================
Enter choice:
7
1
Display a sales item
2
Display all sales items
3
Record a sale
4
Record a restock
5
Add a new item
6
Delete an item
7
Quit
Instance variables
numChoices, the number of choices in the menu items, an array of MenuItem objects
Methods
 a constructor method that reads the data file and builds the array items from the data in the file
 a toString method that concatenates the lines of the menu and the prompt message together
 a validSelection method that returns true if the user entered a valid choice character and false if he or she
entered an invalid choice character
 a method getDescription which takes a choice character as a parameter and returns the description
associated with that choice. For example, with the menu above if you passed '5' to getDescription it would
return "Add a new item"
The file Menu.java contains comments describing the code you must add.
class MenuItem
Instance variables
 description, a String that describes the menu selection
 choiceChr, the character that designates the menu selection
Methods
 two constructors, one without parameters and one with parameters for initializing description and
choiceChr
 sets and gets for description and choiceChr
 toString, returns a string which when printed will form a line of the menu. The string consists of the
choiceChr concatenated with " ) " and the description.
Main Method
The main method should only declare a Merchandise object.
class Merchandise inherits Menu
1.
2.
3.
Declares an Inventory object.
Process the inventory transactions. This will involve a loop in which you display the menu, get the user's
menu selection, perform the selected inventory transaction. These steps are repeated until the user selects
the menu selection for quitting.
Write out the updated inventory. This involves getting the name of the output file as keyboard input,
instantiating a PrintWriter object to write to the file, calling the write method for your Inventory object and
closing the file.
Sample run: proj5run.txt
Hand in:
1. A copy of your Inventory class
2. A copy of your Menu class
3. A copy of the class that contains your main method
4. A copy of your Merchandise method
5. A copy of the output file
Download