Hi mate...This is Andy here.... I need help in JAVA...I know nothing abt JAVA...& also very poor in programming.....
The following is question for my assignment.Can you plzz help me out...

Problem Situation

You will write a small application which might be used by a shopkeeper to hold and maintain a list of items sold by the shop and allow some simple summary information to be obtained from the collection of items. The problem focuses on a container class of user-defined objects. The main features being assessed include your ability to handle several Java classes working together, the dynamic adding of new objects to a collection and searching the collection for particular objects.

*********************************************************
The Item Class

Item objects represent information about different items sold by the shop. Each object contains simple attributes and just the 'standard' methods.

Define the Item class in a file called 'Item.java' and include the following attributes as instance variables:

The name of the item, a String (for example 'Lite White Milk')
The quantity in stock, an integer
The current price of the item, a double.
The methods of class Item should include:

A constructor which accepts just the name of the item.
A constructor which accepts the name, the quantity and the price.
Getter methods for the name, the quantity and the price.
Setter methods for the name and the price but not for the quantity. The quantity will be changed only when some of the items are sold or some new ones are ordered.
A method called sell which accepts an integer and reduces the quantity of the Item by that amount. This method should not allow the quantity to fall below zero. This method simulates selling some of a Items.
A method called order which accepts an integer and increases the quantity by that amount. This method simulates more of the Item being stocked.
A method which returns the 'value' of an Item. This value is calculated by multiplying the quantity by the price.
A 'toString()' method which returns a String containing a description of the current state of the item.
It is recommended that once you have written the Item class you compile it and test it using BlueJ, by creating objects and calling their methods.

*******************************************************
The StockTake Container Class

A StockTake class is a container class which can manage a collection of Item objects.

The StockTake class should define the following attributes:

An array of Item objects
An integer to hold the maximum size of the array
An integer to keep track of how many Item objects are actually stored in the array at any time.
The StockTake class should define the following methods:

A constructor which requires the maximum number of Items which might be managed. This will be an integer. The StockTake constructor will check that the parameter is positive and if so use it as the length of an array of Item objects. If the argument is non-positive the constructor will allocate and create an array of 50 Items. The maximum array size and the current number of Items will also need to be initialized.

A method which accepts as a parameter an object of class Item. This method will attempt to store a reference to this Item object into the next available cell in the array if there is room and return true as a confirmation. If the array is full this method will return false. This method will be used by client code when ever a new Item is to be added to the collection.

A method which accepts a String argument representing the name of an Item. The method will search through the array of Items and return the first one found which has a name matching the argument. If no match is possible the method should return a null reference. This method will allow client code to search for Items.

A method which returns the current value of the entire collection of Items. This value is simply the sum of all the values of the individual Items in the array.

When you have written and compiled the StockTake class test it by creating a StockTake object and invoking the methods to create a small collection of Items.

Also add methods that:

Traverse the array of Items and collects the names (only) of any items which have zero quantity. These names (each a string) should be copied into an array of Strings and that array should be returned by the method. This method allows client codes to fetch a list of names of all Items which are out-of-stock.
Delete a named item from the collection
Sort the collection in descending order of value
Client Code and User-Interface

The aim of this class is to provide a user-interface for a modest application which uses a StockTake container class. The user-interface should be written as a 'console' application using the normal screen and keyboard to interact with a user via a simple text-based menu.

The menu must allow the following tasks to be performed:

Add a new Item to the StockTake collection
Display the full details of all Items in stock
Display the full details of an Item after specifying its name
Sell a specified quantity of an Item after first specifying its name
Purchase a specified quantity of an Item after first specifying its name
Display the total value of the entire stock of Items
Display a list of the names of all the Items which need reordering
Delete an item from the collection after specifying its name
The contents of the collection to be sorted

I need to use two utility classes (KeyboardReader.java & Script.java) provided by our faculty. I have also worked out on the code step by step whick I will send to you in my next posting...... I need to know how to make all the the five classes to get running (Item, StockTake & Console along with KeyboradReader & Script). KeyboardReader.java is used to accept an input from the user & I think Script.java too does the same(I think both have run together).

Thanking u.....Expecting a positive reply ASAP

Regards
Andy

Recommended Answers

All 5 Replies

It's against forum policy to do peoples homework for them. If you show a little effort we can help.

I have done my part & don't know from here ...I also want to know how to create an interface....i have created an interface by name ClientUI.java. Its all messy....

class Item
{
private String name;
private int quantity;
private double price;
double itemValue;


//This Method accepts the name , quantity & the price of an Item.


public Item(String n, int q, double p)
{
name = new String(n);
quantity = q;
price = p;
}


//Reader Method for Item Name


public Item(String name)
{
name = name;
}


//Reader Method for Item Name


public String getName()
{
return name;
}


//Reader Method for Item Quantity


public int getQuantity()
{
return quantity;
}


//Reader Method for Item Price


public double getPrice()
{
return price;
}


//Writer Method for Item Name


public void setName(String name)
{this.name = name;
}


//Writer Method for Item Price


public void setPrice(double price)
{
this.price = price;
}


// This Sell Method accepts an integer & reduces the quantity of that item by that number


public int sell(int sellQuant)
{
while (quantity>0)
{
quantity = quantity - sellQuant;
if (sellQuant > quantity)
System.out.print("Order exceeds stock limit.");
}
return quantity;



// This Order Method accepts an integer & increases the quantity of that item by that number


public int order(int newQuant)
{
quantity += newQuant;
return quantity;
}


//This Method returns the total 'value ' of an Item


public double value(double itemVal)
{
itemVal = (quantity * price);
return itemVal;
}


//This Method returns a string containing the description of the current state of the item.


public String toString(String n, int q, double p)
{
String a = "\nItem Name: " + getName() + "\nQuantity: " + getQuantity()+"\nPrice: " + getPrice() + "\n";
return a;
}
}



class StockTake
{
private static int DEFAULTSIZE = 50;
private static int MAXITEMS;
private int length=0;
private Item[] it;
static String name;
static int quantity;
static double price;
static String itemName;
public KeyboardReader kb=new KeyboardReader();
private void init()
{


it = new Item[MAXITEMS];
length = 0;
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////


//  Constructor that manages maximum number of items


//////////////////////////////////////////////////////////////////////////////////////////////////////////////



public StockTake()
{
MAXITEMS = 50;
init();
}



public StockTake( int length )
{
if( length <= 0 )
length = DEFAULTSIZE;
MAXITEMS = length;
init();
}


/////////////////////////////////////////////////////////////////////////////////////////////////


//  1. Add a new item to the collection


/////////////////////////////////////////////////////////////////////////////////////////////////


public boolean addItem(String n, int q, double p)
{
if (length == MAXITEMS)
{
return false;
}
else
{
it[length] = new Item(n, q, p);
length++;
return true;
}
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//  2. Searchs for items in the collection, by its name


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


public String findItem( String itemName )
{
int i = 0;
while( i < length )
{
if( it.getName().equals(itemName) )
itemName = it.getName();
i++;
return (itemName);
}
return null;
}


////////////////////////////////////////////////////////////////////////////////////////////


//  4.Displays the value of the found item


////////////////////////////////////////////////////////////////////////////////////////////


public double foundItemVal(double itemVal)
{
{
int i = 0;
while( i < length )
{
if( it.getName().equals(itemName) )
return (itemVal);
i++;
}
return '0';
}
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//  Displays the total value of all the items in the collection


////////////////////////////////////////////////////////////////////////////////////////////////////////////////


public int getTotalValue()
{
int totalValue=0;
for(int i =0; i < length;i++)
{
totalValue += it.getPrice();
}


return totalValue;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//  This Method returns a list of all items names which are out-of-stock


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


public void rePack()
{
for (int i=0; i <length; i++)
{
if (it == null)
{
it = it[i+1];
it[i+1] = null;
}
}
}



////////////////////////////////////////////////////////////////////////////////////////////


//  Deletes items by name from the collection


////////////////////////////////////////////////////////////////////////////////////////////


public boolean deleteItem(String itemName)
{
int i = 0;
int index = -1;
while( i < length )
{
if( it.getName().equals(itemName) )
index = i;
i++;
}
if (index >=0)
{
it[index] = null;
length--;
rePack();
return true;
}
else
return false;
}


///////////////////////////////////////////////////////////////////////////////////


//  Displays the current state of the item


///////////////////////////////////////////////////////////////////////////////////


public String toString()
{
String o = "";
for (int i=0; i < length; i++)
o += "\nItem Name: " + it.getName() + "\nQuantity: " +it.getQuantity()+"\nPrice: " + it.getPrice() + "\n";
return o;
}


}


// ClientUI.java
// The User Interface which interacts with the user via a simple text-based menu


public class ClientUI
{
public static void main(String args[])
{


KeyboardReader kb = new KeyboardReader();
StockTake ST = new StockTake();
Item IT = new Item();
boolean cond=true;
int i;
while(cond)
{
System.out.println("\n\t1. Add a new item to the collection");
System.out.println("\t2. Load some items into the collection");
System.out.println("\t3. Display list of items in the collection");
System.out.println("\t4. Find an item in the collection");
System.out.println("\t5. Display found items name");
System.out.println("\t6. Display 'value' of found item");
System.out.println("\t7. Sell an item");
System.out.println("\t8. Display item details");
System.out.println("\t9. Total value of stock items");
System.out.println("\t10. Display items to reorder");
System.out.println("\t11. Delete an item");
System.out.println("\t0. EXIT");


i=kb.getInt();
if(i>=12||i<0)
{
System.out.println("Sorry Wrong Input");
}
else
{
if(i==1)
{
ST.addItem();
}
if(i==2)
{
ST.findItem();
}
if(i==3)
{
//ST.display_item();
}
if(i==4)
{
ST.findItem();
}
if(i==5)
{
IT.sell();
}
if(i==6)
{
ST.foundItemVal();
}
if(i==7)
{
ST.total();
}
if(i==8)
{
ST.flist();
}
if(i==9)
{
ST.getTotalValue();
}
if(i==10)
{
ST.rePack();
}
if(i==11)
{
ST.deleteItem();
}
if(i==0)
{
cond=false;
}


}
}
}


}


// File: KeyboardReader.java
/* A class whose objects allow the interactive reading of
data from the user's keyboard.


Errol Chopping
[echopping@csu.edu.au]
Version 1.0, September 1999


Assumes Java 1.1.x
*/


import java.io.*;
import java.util.*;


class KeyboardReader
{
private BufferedReader br;
private Script sc;


public
KeyboardReader()
{
br = new BufferedReader( new InputStreamReader( System.in ) );
sc = null;
}


public
KeyboardReader( Script s )
{
br = new BufferedReader( new InputStreamReader( System.in ) );
sc = s;
}


public
String getString()
{
String temp;
try
{
temp = br.readLine();
}
catch( IOException ioe )
{
System.err.println( "KeyboardReader IOException in 'getString()'" );
return null;
}
if( sc != null )
sc.capture( temp );
return temp;
}


public
String getLine()   // for compatibility with older version
{
return getString();
}



public
char getChar()
{
String s = getString();
if( s.length() == 0 )
{
System.err.println( "\nKeyboardReader warning: zero length data entry in 'getChar()'" );
return '\0';
}


return s.charAt(0);
}


public
int getInt()
{
String s = getString();
if( s.length() == 0 )
System.err.println( "\nKeyboardReader warning: zero length data entry in 'getInt()'" );
return Integer.parseInt( s );
}


public
double getDouble()
{
String s = getString();
if( s.length() == 0 )
System.err.println( "\nKeyboardReader warning: zero length data entry in 'getDouble()'" );


return new Double( s ).doubleValue();
}


public
String getWord()
{
String s = getString();
StringTokenizer st = new StringTokenizer( s );
if( st.hasMoreTokens() )
return st.nextToken();
return null;
}



public
String toString()
{
String rtn = "\nKeyboardReader. A utility class for reading from the standard input.\n"
+ "by Errol Chopping\n"
+ "[echopping@csu.edu.au]\n"
+ "Version 1.0, September 1999.\n"
+ "Methods available:\n"
+ "  String getString()\n"
+ "  int getInt()\n"
+ "  double getDouble()\n"
+ "  char getChar()\n"
+ "  String getWord()\n";
return rtn;
}


}


// File: Script.java
/* Script objects provide a similar facility to the
UNIX 'script' utility. That is they are used to
capture into a text file the output from Java programs
which is normally sent to the standard ouput stream.


Data sent to a Script object is written to a text file
(named 'typescript.text' by default). The data is ALSO
written to stdout.


A Script object also can be used when constructing a KBReader
object (another Java utility I wrote) so that the standard input
read by the KBReader object is ALSO written to the same text file
as well as appearing on stdout.


Errol Chopping
[echopping@csu.edu.au]
Version 1.0, September 16, 1999
*/


import java.io.*;


class Script
{
private FileWriter fw;
private BufferedWriter bw;
private String filename;
private boolean append;
private boolean OK;


public
Script()
{
setFileName( "typescript.text" );
append = false;
init();
}


public
Script( String fn )
{
setFileName( fn );
append = false;
init();
}


public
Script( boolean app )
{
setFileName( "typescript.text" );
append = app;
init();
}


public
Script( String fn, boolean app )
{
setFileName( fn );
append = app;
init();
}


private
void setFileName( String fn )
{
if( fn == null || fn.length() == 0 )
filename = "typescript.text";
else
filename = fn;
}


private
void init()
{
try
{
fw = new FileWriter( filename, append );
}
catch( IOException e )
{
if( append )
System.err.println( "Script Error - can't append to the file '"
+ filename + "'");
else
System.err.println( "Script Error - can't create the file '"
+ filename + "'");


OK = false;
return;
}


bw = new BufferedWriter( fw );
OK = true;
}


private
boolean isWorking()
{
if( !OK )
System.err.println( "Script Warning: Script not correctly initialised." );
return OK;
}


public
void println()
{
if( !isWorking() ) return;


try
{
bw.newLine();
System.out.println();
}
catch( IOException e )
{
System.err.println( "Script Error: IoException in 'println()'" );
}
flush();


}


public
void println( String s )
{
if( !isWorking() ) return;


try
{
bw.write( s );
bw.newLine();
System.out.println( s );
}
catch( IOException e )
{
System.err.println( "Script Error: IoException in 'println( String )'" );
}
flush();
}


public
void capture( String s )
{
if( !isWorking() ) return;
try
{
bw.write( s );
bw.newLine();
}
catch( IOException e )
{
System.err.println( "Script Error: IoException in 'println( String )'" );
}
flush();
}


public
void println( int i )
{
if( !isWorking() ) return;
println( ""+i );
flush();
}


public
void println( char c )
{
if( !isWorking() ) return;
println( ""+c );
flush();
}


public
void println( double d )
{
if( !isWorking() ) return;
println( ""+d );
flush();
}


public
void println( boolean s )
{
if( !isWorking() ) return;
if( s )
println( "true" );
else
println( "false" );
flush();
}


public
void println( Object o )
{
if( !isWorking() ) return;
println( o.toString() );
}


public
void print( String s )
{
if( !isWorking() ) return;
try
{
bw.write( s );
System.out.print( s );
}
catch( IOException e )
{
System.err.println( "Script Error: IOException in 'print( String )'" );
}
flush();
}


public
void print( int s )
{
if( !isWorking() ) return;
print( ""+s );
flush();
}


public
void print( char s )
{
if( !isWorking() ) return;
print( ""+s );
flush();
}


public
void print( double s )
{
if( !isWorking() ) return;
print( ""+s );
flush();
}


public
void print( boolean s )
{
if( !isWorking() ) return;
if( s )
print( "true" );
else
print( "false" );
flush();
}


public
void flush()
{
if( !isWorking() ) return;
System.out.flush();
try
{
bw.flush();
}
catch( IOException e )
{
System.err.println( "Script warning: cannot flush buffer for '"
+ filename + "'" );
}
}


public
void close()
{
if( !isWorking() ) return;
try
{
bw.close();
}
catch( IOException e )
{
System.err.println( "Script warning: cannot close buffer for '"
+ filename + "'" );
}
}


public
String toString()
{
String rtn = "\nScript. A utility class for capturing output to a file.\n"
+ "by Errol Chopping\n"
+ "[echopping@csu.edu.au]\n"
+ "Version 1.0, September 1999.\n"
+ "Methods available:\n"
+ "  void print[ln]( String )\n"
+ "  void print[ln]( int )\n"
+ "  void print[ln]( double )\n"
+ "  void print[ln]( boolean )\n"
+ "  void print[ln]( char )\n"
+ "  void print[ln]( Object )\n";


return rtn;
}



}

next time, can you please use

tags? it makes your code much easier to read - making it much easier to help you.

I have worked on it & now there are no errors after complilation, but it is not doing what I exactly need.
the code goes on like this ............

// ClientUI.java
// The User Interface which interacts with the user via a simple text-based menu


class ClientUI
{
public static void main(String args[])
{


KeyboardReader kb = new KeyboardReader();
StockTake ST = new StockTake();
Item IT = new Item("a",1,2.0);
boolean cond=true;
int i=0, q=0;
double p=0;
String it=null;
while(cond)
{
System.out.println("\t1. Add a new item to the collection");
System.out.println("\t2. Find an item in the collection");
System.out.println("\t3. Display found items name");
System.out.println("\t4. Display 'value' of found item");
System.out.println("\t5. Sell an item");
System.out.println("\t6. Display item details");
System.out.println("\t7. Total value of stock items");
System.out.println("\t8. Display items to reorder");
System.out.println("\t9. Delete an item");
System.out.println("\t10.Sort the items in the collection by its name");
System.out.println("\t0. EXIT");


System.out.print("\n\nEnter choice: ")
i=kb.getInt();


if(i>11||i<0)
{
System.out.println("Please Enter a value between 0 to 10");
}
else
{
if(i==1)
{
boolean result;
result=ST.addItem(it, q, p );
}
if(i==2)
{
ST.findItem(it);
}
if(i==3)
{   ST.foundItem(it);
}
if(i==4)
{
ST.foundItemVal(p);
}
if(i==5)
{
IT.sell(q);
}
if(i==6)
{
ST.toString();
}
if(i==7)
{
ST.getTotalValue();//ST.total();
}
if(i==8)
{
ST.rePack();
}
if(i==9)
{
ST.deleteItem(it);
}
if(i==10)
{
;
}
if(i==11)
{
;
}
if(i==0)
{
cond=false;
}


}
}
}


}


===============================================================


class Item
{
private String name;
private int quantity;
private double price;
double itemValue;



//This Method accepts the name , quantity & the price of an Item.


public Item(String n, int q, double p)
{
name = new String(n);
quantity = q;
price = p;
}


//Reader Method for Item Name


public Item(String name)
{
name = name;
}


//Reader Method for Item Name


public String getName()
{
return name;
}


//Reader Method for Item Quantity


public int getQuantity()
{
return quantity;
}


//Reader Method for Item Price


public double getPrice()
{
return price;
}


//Writer Method for Item Name


public void setName(String name)
{this.name = name;
}


//Writer Method for Item Price


public void setPrice(double price)
{
this.price = price;
}


// This Sell Method accepts an integer & reduces the quantity of that item by that number


public int sell(int sellQuant)
{
while (quantity>0)
{
quantity = quantity - sellQuant;
if (sellQuant > quantity)
System.out.print("Order exceeds stock limit.");
}
return quantity;
}


// This Order Method accepts an integer & increases the quantity of that item by that number


public int order(int newQuant)
{
quantity += newQuant;
return quantity;
}


//This Method returns the total 'value ' of an Item


public double value(double itemVal)
{
itemVal = (quantity * price);
return itemVal;
}


//This Method returns a string containing the description of the current state of the item.


public String toString(String n, int q, double p)
{
String a = "\nItem Name: " + getName() + "\nQuantity: " + getQuantity()+"\nPrice: " + getPrice() + "\n";
return a;
}
}


================================================================


class StockTake
{
private static int DEFAULTSIZE = 50;
private static int MAXITEMS;
private int length=0;
private Item[] it;
static String name;
static int quantity;
static double price;
static String itemName;
public KeyboardReader kb=new KeyboardReader();
private void init()
{


it = new Item[MAXITEMS];
length = 0;
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////


//  Constructor that manages maximum number of items


//////////////////////////////////////////////////////////////////////////////////////////////////////////////



public StockTake()
{
MAXITEMS = 50;
init();
}



public StockTake( int length )
{
if( length <= 0 )
length = DEFAULTSIZE;
MAXITEMS = length;
init();
}


/////////////////////////////////////////////////////////////////////////////////////////////////


//  1. Add a new item to the collection


/////////////////////////////////////////////////////////////////////////////////////////////////


public boolean addItem(String n, int q, double p)
{
System.out.println("Enter item Name: ");
n=kb.getString();
System.out.println("Enter item Quantity: ");
q=kb.getInt();
System.out.println("Enter item Price: ");
p=kb.getDouble();


if (length == MAXITEMS)
{
return false;
}
else
{
it[length] = new Item(n, q, p);
length++;
return true;
}
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//  2. Searchs for items in the collection, by its name


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


public String findItem( String itemName )
{
int i = 0;
while( i < length )
{
if( it.getName().equals(itemName) )
itemName = it.getName();
i++;
return (itemName);
}
return null;
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//  3. Searchs for items in the collection, by its name


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


public String foundItem( String itemName )
{
int i = 0;
while( i < length )
{
if( it.getName().equals(itemName) )
itemName = it.getName();
i++;
return (itemName);
}
System.out.println("Item Found & its Name is : " +itemName);
return null;
}


////////////////////////////////////////////////////////////////////////////////////////////


//  4.Displays the value of the found item


////////////////////////////////////////////////////////////////////////////////////////////


public double foundItemVal(double itemVal)
{
{
int i = 0;
while( i < length )
{
if( it.getName().equals(itemName) )
return (itemVal);
i++;
}
return '0';
}
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//  Displays the total value of all the items in the collection


////////////////////////////////////////////////////////////////////////////////////////////////////////////////


public int getTotalValue()
{
int totalValue=0;
for(int i =0; i < length;i++)
{
totalValue += it.getPrice();
}


return totalValue;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//  This Method returns a list of all items names which are out-of-stock


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


public void rePack()
{
for (int i=0; i <length; i++)
{
if (it == null)
{
it = it[i+1];
it[i+1] = null;
}
}
}



////////////////////////////////////////////////////////////////////////////////////////////


//  Deletes item by name from the collection


////////////////////////////////////////////////////////////////////////////////////////////


public boolean deleteItem(String itemName)
{
int i = 0;
int index = -1;
while( i < length )
{
if( it.getName().equals(itemName) )
index = i;
i++;
}
if (index >=0)
{
it[index] = null;
length--;
rePack();
return true;
}
else
return false;
}


///////////////////////////////////////////////////////////////////////////////////


//  Displays the current state of the item


///////////////////////////////////////////////////////////////////////////////////


public String toString()
{
String o = "";
for (int i=0; i < length; i++)
o += "\nItem Name: " + it.getName() + "\nQuantity: " +it.getQuantity()+"\nPrice: " + it.getPrice() + "\n";
return o;
}


}


================================================================

The other two classes ( KeyboardReader.java & Script.java ) are the same ones as I posted earlier.

PLEASE USE CODE TAGS!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.