COMP 182 CSUN The Maintaining an Inventory Application Program Programming assignment deliverables consist of specification and design, a UML class diagram

COMP 182 CSUN The Maintaining an Inventory Application Program Programming assignment deliverables consist of specification and design, a UML class diagram, finished programs source code, and evidence of adequate testing, and a copy of the results. 284
Chapter 5
Linked Lists
Application. Maintaining an Inventory
285
Dutput
3. Set the preceding reference in the nude that is to follow the new node so
that it references the new node.
4. Set the next reference in the node that is to precede the new node so that
it references the new node.
The following Java statements accomplish these four steps, assuming that’s
newNode references the new nexte:
tile that contains the updated inventory. (Note that you remove from
wie in entory all items whose have values and want values are zero and
whose wait lists are empty. Thus, such items do not appear in the file.)
Output as specified by the individual commands.
i program should be able to execute the following commands:
Inserting a node
11 insert the new node that newNode references before
11 the node referenced by curr
newNode. next – curr;
newNode.preceding – curr.preceding;
curr.preceding – newNode;
newNode. preceding.next = newNode;
You should convince yourself that these statements work even when you insert
the node into the beginning of a list; at the end of a list, in which case curr
references the head node; or into an empty list, in which case curr also refer
ences the head node.
()
5.4 Application: Maintaining an Inventory
Imagine that you have a part-time job at the local movie rental store. Realizing
that you know a good deal about computers, the store owner asks you to write
an interactive program that will maintain the store’s inventory of DVDs that
are for sale. The inventory consists of a list of movie titles and the following
information associated with cach title:
• Have value: number of DVDs currently in stock.
Want value: number of DVDs that should be in stock. (When the have
value is less than the want value, more DVDs are ordered.)
Wait list: list of names of people waiting for the title if it is sold out.
Because the owner plans to turn off the power to the computer when the store
is closed, your inventory program will not be running at all times. Therefore,
the program must save the inventory in a file before execution terminates and
later restore the inventory when it is run again.
Program input and output are as follows:
Input
• A file that contains a previously saved inventory.
• A file that contains information on an incoming shipment of DVDs. (Sca
command D.)
• Single-letter commands-with arguments where necessary—that inquire
about or modify the inventory and that the user will enter interactively.
(help) Provide a summary of the available commands. Program commands
ul>
(inquire) Display the inventory information for a specified title.
(list) List the entire inventory (in alphabetical order by
title)
(add)
Add a new citle to the inventory. Prompt for initial
want value.
4 (modifi) Modify the want value for a specified title.
(delivery) Take delivery of a shipment of DVDs, assuming that
the clerk has entered the shipment information (titles
and counts) into a file. Read the file, reserve DVDs
for the people on the wait list, and update the have
values in the inventory accordingly. Note that the
program must add an item to the inventory if a deliv-
ered title is not present in the current inventory.
(order) Write a purchase order for additional DVDs based on
a comparison of the have and want values in the
inventory, so that the have value is brought up to the
want value.
R
{return)
Write a return order based on a comparison of the
have and want values in the inventory and decrease the
have values accordingly (make the return). The
purpose is to reduce the have value to the want value.
(sell) Decrease the count for the specified title by 1. If the
title is sold out, put a name on the wait list for the title.
Q (quit) Save the inventory and wait lists in a file and termi-
nate execution.
The problem-solving process that starts with a statement of the problem
and ends with a program that effectively solves the problem–that is, a
program that meets its specification-has three main stages:
1. The design of a solution
2. The implementation of the solution
3. The final set of refinements to the program
286
Chapter 5 Linked Lists
Application Minimalisting ist, M 1:0″//
287
rivate String firstName;
private String phone;
public Customer (String first, String last, String phone)!
to be implemented
end constructor
public String tostring() {
to be implemented
Operations on the
inventory
end toString
end class Customer
Realize, however, that you cannot complete one stage in total isolation from
the others. Also realize that at many steps in the development of a solution
you must make choices. Although the following discussion may give the
impression that the choices are clear-cut, this is not always the case. In reality
both the trade-offs between choices and the false starts (wrong choices conside
ered) are often numerous.
This problem primarily involves data management and requires certain
program commands. These commands suggest the following operations on the
imentory:
• List the inventory in alphabetical order by title (L command).
• Find the inventory item associated with a title (I, M, D, O, and commands)
• Replace the inventory item associated with a title (M, D, R, and Scommands).
• Insert new inventory items (A and D commands).
Recall that each title might have an associated wait list of people who are
waiting for that title. You must be able to
Add new people to the end of the wait list when they want a DVD that
sold out (S command).
Delete people from the beginning of the wait list when new DVDs are
delivered (D command).
Display the names on a wait list for a particular title (I and L commands).
In addition, you must be able to
• Save the current inventory and associated wait lists when program execuo
tion terminates (Q command).
• Restore the current inventory and associated wait lists when program exe-
cution begins again.
You could think of these operations as part of an ADT inventory. Your
next step should be to specify each of the operations fully. Since this chapter is
about linked lists and implementation issues, the completion of the specifica-
tions will be left as an exercise. We will turn our attention to a data structure
that could implement the inventory.
Each data item in the ADT inventory represents a movie and contains a title,
the number of DVDs in stock (a have value), the number desired (a want value).
and a wait list. How will you represent the wait list? First, you need to decide
what information you want to store in the wait list. For example, you might want
to keep track of the full name and phone number of each person, and create
class Customer containing data fields for the first and last names along with the
telephone number of the person. This class might be structured as follows:
I’lis definition contains the minimum number of methods required to use
aces of the Customer class in our inventory problem. You may also decide
svou want to keep additional information about a person, such as their
tress. The toString method is provided for printing purposes.
Now that you have decided what information to keep in the wait list,
* will you implement the wait list itself in the ABT inventory? Could
use any of the implementations of the ADT list we developed previously?
make this decision, you must review the requirements of the wait list
ated in the inventory problem and then see if the ADT list will be able
neet these requirements. The inventory problem requires you to be able
d to the end of the wait list and delete from the beginning of the wait
• Clearly; removing an item from the beginning of the list is casy: You
.” simply use the ADT list operation remove with an index value of 1.
ding an item to the end of the list is also fairly easy. You know the size of
ist from the ADT list operation size(), and you could use the ADT list
:cration add with an index value of size() + 1 to place an item at the end of
wait list.
One of the requirements of the inventory problem is that the L command Tre FCTS
the inventory in alphabetical order by movie title. Will you be able to use the SSE COS”,
..ADT list in a way that will support this requirement? Not easily, since the
alpha 125
I list is based on index position, not on a sorted order. A better choice is
ADT sorted list. Not only does it maintain the data in a sorted order foor
– but it also provides an operation locateIndex(item) that can be used to
arch for an item in the sorted list.
If the ADT sorted list is not yet implemented, should you use an array.
paved implementation or a reference-based implementation? If you use an
array to contain the items, you can use a binary search. Inserting and deleting
items, however, requires you to shift array elements. Using a linked list for the
items avoids these data shifts but makes a binary search impractical. (How do
** quickly locate the middle item in a linked list?) Weighing these trade-offs,
We choose a linked list to implement the ADT sorted list.
dala ra!” SCH
A customer in the
wait list
public class Customer
private String lastName;
Application: Maintaining an Inventory
288
289
Chapter 5 Linked Lists
A sorted list
represents the
inventory
public void addToWaitingList(String lastName,
String firstName, String phone) {
/ add a person to the waiting list
waitingList.addSorted
new Customer (lastName, firstName, phone);
} // end addToWaitingList
To summarize, we have made the following choices:
• The inventory is a sorted list of data items (the AlYT seorted list implemented
as a linked list of data items), sorted by the title that cach item represents.
• Each inventory item contains a title, a have value, a want value, and a list
of customers (the wait list).
Figure 5-30 and the following lasa statements summarize choices:
public class StockItem implements java.lang.Comparable !
private String title;
private int have, want;
private ListReferenceBased waitingList;
// various constructors for StockItem
public String toString() {
11 for displaying StockItem instances
> 11 end tostring
public int compareTo (Object rhs) {
define how StockItems are compared, only by title
return title.compareTo(((StockItem)rhs).title);
} 11 end compare To
mutator and accessor methods for other data fields
Inventory list node
waitingList node
1/ end class StockItem
a.
(b)
iten
You declare the inventory as follows:
1
title
Person
have want waitingList
StockItem
SortedList inventory = new SortedList();
item
next
1
next
Inventory list
(c)
Before you can proceed with the implementation, you must consider how
sou will save the inventory in a file. Java provides object serialization, a process
that transforms an object into a stream of bytes that you can save and restore
trom a file. The most powerful aspect of object scrialization is that when you
write any object to a file, any other objects that are referenced by that object are
also written to the file. As mentioned in Chapter 1, to enable this feature, you
place an implements Serializable clause in cach class that has instances that
will be written to the file. Thus, to write the inventory successfully, you would
include the clause in the classes ListReferenceBased, Node, SortedList,
StockItem, and Customer. Then, when you write an inventory object to a file,
all of the stock items in the inventory list and their wait lists are also be placed in
the file. Here is the code that accomplishes that task:
Wat list
HD
try {
FileOutputStream fos – new
PileOutputStream(“inventory.dat”);
ObjectOutputStream oos = new ObjectOutputStream( fos);
oos.writeObject(inventory);
fos.close();
} // end try
catch (Exception e) {
FIGURE 5-30
(a) Inventory list node: (b) wait list node: (C) orthogonal structure for the inventory

Purchase answer to see full
attachment

Don't use plagiarized sources. Get Your Custom Essay on
COMP 182 CSUN The Maintaining an Inventory Application Program Programming assignment deliverables consist of specification and design, a UML class diagram
For $10/Page 0nly
Order Essay
Calculator

Calculate the price of your paper

Total price:$26

Need a better grade?
We've got you covered.

Order your paper