The inverntory is a sortled list of Data items(the ADT sorted list implemented a
ID: 3542654 • Letter: T
Question
The inverntory is a sortled list of Data items(the ADT sorted list implemented as a linked list of data ITEMS, sorted that each item represents.
Each inverntory item contains a title, a have value, a want value and a list of customers(the wait list)
The problem is that i have inventory.dat file which contains the below
title and want value
I have a movie inventory:
Iroman 3
Momento 2
Life of Pi 2
Superman 2
The Crazies 1
I want that to be used by the program and i dont know how to implement
public interface BasicListInterface {
public int size();
public boolean isEmpty();
public void removeAll();
public Object get(int index)
throws ListIndexOutOfBoundsException;
} // end BasicListInterface
////////////////////////////////////////////////////////////////////////////////
public class ListException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 1L;
public ListException(String s) {
super(s);
} // end constructor
} // end ListException
////////////////////////////////////////////////////////////////////////////////////
public class ListIndexOutOfBoundsException
extends IndexOutOfBoundsException {
public ListIndexOutOfBoundsException(String s) {
super(s);
} // end constructor
} // end ListIndexOutOfBoundsException
//////////////////////////////////////////////////////////
// ************************************************
// Interface for the ADT list
// ************************************************
public interface ListInterface {
public boolean isEmpty();
public int size();
public void add(int index, Object item)
throws ListIndexOutOfBoundsException;
public void remove(int index) throws ListIndexOutOfBoundsException;
public Object get(int index) throws ListIndexOutOfBoundsException;
public void removeAll();
}
///////////////////////////////////////////////////////////////
import java.io.Serializable;
// **********************************************
// Reference-based implementation of ADT list.
// **********************************************
public class ListReferenceBased implements ListInterface, Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Node head;
private Node tail;
private int numItems;
public ListReferenceBased() {
numItems = 0;
head = null;
tail = null;
} // end default constructor
public boolean isEmpty() {
return numItems == 0;
} // end isEmpty
public int size() {
return numItems;
} // end size
private Node find(int index) {
Node curr = head;
for (int skip = 1; skip < index; skip++ )
{
curr = curr.getNext();
} // end for
return curr;
} // end find
public Object get(int index) throws ListIndexOutOfBoundsException {
if (index >= 1 && index <= numItems) {
// get reference to node, then data in node
Node curr = find(index);
Object dataItem = curr.getItem();
return dataItem;
}
else {
throw new ListIndexOutOfBoundsException(
"List index out of bounds on get");
} // end if
} // end get
public void add(int index, Object item)
throws ListIndexOutOfBoundsException {
if (index >= 1 && index <= numItems+1) {
if (index == 1) {
// insert the new node containing item at
// beginning of list
Node newNode = new Node(item, head);
head = newNode;
}
else {
Node prev = find(index-1);
// insert the new node containing item after
// the node that prev references
Node newNode = new Node(item, prev.getNext());
prev.setNext(newNode);
} // end if
numItems++;
}
else {
throw new ListIndexOutOfBoundsException(
"List index out of bounds on add");
} // end if
// tail updation
// Note: we know that head is not null at this point.
if ( tail == null ) tail = head;
while( tail.getNext() != null ) tail = tail.getNext();
} // end and
public void remove(int index)
throws ListIndexOutOfBoundsException {
if (index >= 1 && index <= numItems) {
if (index == 1) {
// delete the first node from the list
// If tail == head, the list is going to be empty.
// So, set tail to be null.
if ( tail == head ) tail = null;
head = head.getNext();
}
else {
Node prev = find(index-1);
// delete the node after the node that prev
// references, save reference to node
Node curr = prev.getNext();
prev.setNext(curr.getNext());
if ( curr == tail ) tail = prev;
} // end if
numItems--;
} // end if
else {
throw new ListIndexOutOfBoundsException(
"List index out of bounds on remove");
} // end if
} // end remove
public void removeAll() {
// setting head to null causes list to be
// unreachable and thus marked for garbage
// collection
head = null;
tail = null;
numItems = 0;
} // end removeAll
//
// queue function implementation
//
public void addLast(Object item) {
if ( tail == null )
{
add( 1, item );
}
else {
//
// add it next to the tail
//
Node prev = tail;
Node newNode = new Node(item, null );
prev.setNext(newNode);
while( tail.getNext() != null ) tail = tail.getNext();
numItems++;
}
}
public Object removeFirst() {
if ( numItems == 0 ) {
//
// if there is no item to remove, just return null.
//
return null;
}
Object o = head.getItem();
remove(1);
return o;
}
//
// getHead: returns the head node; for list iteration
//
public Node getHead() {
return head;
}
//
// toString implementation
//
public String toString() {
Node iter = head;
String result = "";
while ( iter != null )
{
result = result + iter.getItem() + " ";
iter = iter.getNext();
}
return result;
}
}
///////////////////////////////////////////////////////////////////////////////////////////
import java.io.*;
import java.util.StringTokenizer;
public class Main {
static SortedList inventory;
static BufferedReader in;
public static StockItem getStockItem( String title )
{
int idx;
StockItem newItem = new StockItem( title );
//
// Determine whether the video with the title is in the inventory.
//
idx = inventory.locateIndex( newItem );
if ( idx <= inventory.size() )
{
StockItem oldItem = (StockItem)inventory.get( idx );
if ( oldItem.compareTo(newItem) == 0 )
return oldItem;
}
//
// If not, insert.
//
inventory.sortedAdd(newItem);
return newItem;
}
//
// If the stock item has no information, remove it from the inventory.
//
public static void VerifyStockItem( StockItem item )
{
if ( item.getHave() == 0 && item.getWant() == 0 &&
item.getWaitingList().isEmpty() )
{
inventory.sortedRemove( item );
}
}
public static void cmd_I( String title )
{
StockItem item = getStockItem( title );
System.out.println( item );
VerifyStockItem( item );
}
public static void cmd_L()
{
System.out.println( inventory );
}
public static void cmd_A( String title )
{
StockItem item = getStockItem( title );
System.out.println( "Input the initial want value:" );
while ( true )
{
try {
item.setWant( Integer.parseInt( in.readLine() ) );
break;
}
catch ( IOException e ) {
continue;
}
}
VerifyStockItem( item );
}
public static void cmd_M( String title )
{
StockItem item = getStockItem( title );
System.out.println( "The original want value was " + item.getWant() +
"." );
System.out.println( "Input the new want value:" );
while ( true )
{
try {
item.setWant( Integer.parseInt( in.readLine() ) );
break;
}
catch ( IOException e ) {
continue;
}
}
VerifyStockItem( item );
}
public static void cmd_D()
{
try {
BufferedReader fin =
new BufferedReader( new FileReader( "incoming.dat" ) );
int lines = Integer.parseInt( fin.readLine() );
for ( int i = 0; i < lines; i++ )
{
StringTokenizer st = new StringTokenizer( fin.readLine() );
String title = st.nextToken();
int count = Integer.parseInt( st.nextToken() );
StockItem item = getStockItem( title );
ListReferenceBased waitingList = item.getWaitingList();
System.out.println ( "Arrival: " + title + " x " + count );
while( count > 0 && !waitingList.isEmpty() )
{
Person p = (Person)waitingList.removeFirst();
System.out.println( "" + p + " received the video." );
count--;
}
if ( count > 0 ) {
item.setHave( item.getHave() + count );
}
VerifyStockItem( item );
}
}
catch( FileNotFoundException fnfe ) {
System.out.println( "incoming.dat should exist." );
return;
}
catch( IOException e ) {
System.out.println(
"The operation is aborted due to an IO error." );
}
}
public static void cmd_O()
{
Node iter = inventory.getHead();
while ( iter != null ) {
Node next = iter.getNext();
StockItem item = (StockItem)iter.getItem();
int count = item.getWant()
+ item.getWaitingList().size()
- item.getHave();
if ( count > 0 ) {
System.out.println( item.title + ": " + count );
}
iter = next;
}
}
public static void cmd_R()
{
Node iter = inventory.getHead();
while ( iter != null ) {
Node next = iter.getNext();
StockItem item = (StockItem)iter.getItem();
int count = item.getHave()
- item.getWant()
- item.getWaitingList().size();
if ( count > 0 ) {
System.out.println( item.title + ": " + count + " returned" );
item.setHave( item.getHave() - count );
}
VerifyStockItem( item );
iter = next;
}
}
public static void cmd_S( String title )
{
StockItem item = getStockItem( title );
if ( item.getHave() > 0 )
{
item.setHave( item.getHave() - 1 );
System.out.println( title + ": You took one. " +
item.getHave() + " video(s) left." );
}
else
{
System.out.println( title + " is sold out." );
System.out.println( "Write your name please." );
while (true) {
try {
Person p = new Person( in.readLine().trim() );
item.getWaitingList().addLast( p );
break;
}
catch( IOException e )
{
continue;
}
}
}
VerifyStockItem( item );
}
public static void main(String[] args)
{
//
// Loading from the inventory.dat
//
try {
FileInputStream fis = new
FileInputStream("inventory.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
Object o = ois.readObject();
inventory = (SortedList)o;
}
catch (FileNotFoundException fnfe) {
inventory = new SortedList();
}
catch (Exception e) {
System.out.println(e);
}
in = new BufferedReader(new InputStreamReader(System.in));
while(true)
{
String cmdLine;
char cmd;
String arg = "";
System.out.print( "Input a command: " );
try {
cmdLine = in.readLine();
}
catch(Exception e) {
break;
}
if (cmdLine.length() == 0) continue;
//
// parse the command line.
//
cmd = cmdLine.charAt(0);
if ( cmdLine.length() >= 3 ) arg = cmdLine.substring(2);
if ( cmd == 'Q' ) break;
//
// dispatch
//
switch( cmd )
{
case 'H':
System.out.println(
"Help on commands " +
"---------------- " +
"H (help) " +
"I <title> (inquire) " +
"L (list) " +
"A <title> (add) " +
"M <title> (modify) " +
"D (delivery) " +
"O (order) " +
"R (return) " +
"S <title> (sell) " +
"Q (quit) " );
break;
case 'I':
cmd_I( arg );
break;
case 'L':
cmd_L();
break;
case 'A':
cmd_A( arg );
break;
case 'M':
cmd_M( arg );
break;
case 'D':
cmd_D();
break;
case 'O':
cmd_O();
break;
case 'R':
cmd_R();
break;
case 'S':
cmd_S( arg );
break;
}
}
//
// Saving into the inventory.dat
//
try {
FileOutputStream fos = new
FileOutputStream("inventory.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(inventory);
fos.close();
// end try
}
catch (Exception e) {
System.out.println(e);
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.io.Serializable;
public class Node implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Object item;
private Node next;
public Node(Object newItem) {
item = newItem;
next = null;
} // end constructor
public Node(Object newItem, Node nextNode) {
item = newItem;
next = nextNode;
} // end constructor
public void setItem(Object newItem) {
item = newItem;
} // end setItem
public Object getItem() {
return item;
} // end getItem
public void setNext(Node nextNode) {
next = nextNode;
} // end setNext
public Node getNext() {
return next;
} // end getNext
} // end class Node
/////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.io.Serializable;
import java.util.StringTokenizer;
public class Person implements Serializable {
private String lastName;
private String firstName;
public Person(String first, String last) {
firstName = new String(first);
lastName = new String(last);
} // end constructor
//
// Automatically parse
//
public Person(String fullname) {
StringTokenizer st = new StringTokenizer( fullname );
firstName = st.nextToken();
lastName = st.nextToken();
} // end constructor
public String toString() {
// to be implemented
return firstName + " " + lastName;
} // end toString
} // end class Person
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.io.Serializable;
public class SortedList extends ListReferenceBased
implements SortedListInterface, Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public SortedList() {
// invokes default constructor of superclass
// end default constructor
}
public void sortedAdd(Comparable newItem) {
// Adds an item to the list.
// Precondition: None.
// Postcondition: The item is added to the list in
// sorted order.
int newPosition = locateIndex(newItem);
add(newPosition, newItem);
// end sortedAdd
}
public void sortedRemove(Comparable anItem)
throws ListException {
// Removes an item from the list.
// Precondition: None.
// Postcondition: The item is removed from the list
// and the sorted order maintained.
int position = locateIndex(anItem);
if (anItem.compareTo(get(position))==0) {
remove(position);
}
else {
throw new ListException("Sorted remove failed");
} // end if
// end sortedRemove
}
public int locateIndex(Comparable anItem) {
// Finds an item in the list.
// Precondition: None.
// Postcondition: If the item is in the list, its
// index position in the list is returned. If the
// item is not in the list, the index of where it
// belongs in the list is returned.
int index = 1;
// Loop invariant: anItem belongs after all the
// elements up to the element referenced by index
while ( (index <= size()) &&
(anItem.compareTo(get(index)) > 0 ) ) {
++index;
} // end while
return index;
} // end locateIndex
} // end SortedList
/////////////////////////////////////////////////////////////////////////////////////
public interface SortedListInterface extends BasicListInterface {
public void sortedAdd(Comparable newItem) throws ListException;
public int locateIndex(Comparable anItem);
public void sortedRemove(Comparable anItem) throws ListException;
}
///////////////////////////////////////////////////////////////////////////////////
import java.lang.Comparable;
import java.io.Serializable;
public class StockItem implements Comparable, Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public String title;
private int have, want;
private ListReferenceBased waitingList;
// various constructors for StockItem
public StockItem()
{
have = 0;
want = 0;
waitingList = new ListReferenceBased();
}
public StockItem( String title )
{
this();
this.title = new String(title);
}
public void addToWaitingList(String lastName, String firstName) {
// add a person to the waiting list
waitingList.add(
waitingList.size(), (Object)(new Person(lastName, firstName)));
} // end addToWaitingList
public String toString() {
// for displaying StockItem instances
return
title +
" Have Value: " + have + " Want Value: " + want + " " +
"::Waiting List { " + waitingList + "} ";
} // end toString
public int compareTo(Object rhs) {
// define how StockItems are compared
return title.compareTo(((StockItem)rhs).title);
} // end compareTo
// mutator and accessor pethods for other data fields
public int getHave() { return have; }
public int getWant() { return want; }
public ListReferenceBased getWaitingList() { return waitingList; }
public void setHave( int newHave ) { have = newHave; }
public void setWant( int newWant ) { want = newWant; }
} // end class StockItem
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Explanation / Answer
Hi,
U need to pass :
A <MovieTitle>
e.g. A Titanic
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.