/* This application reads data in from a file and stores it in a class.
*/
public class StoreList
{
public static void main (String [] args)
{
ProductList
productList = new ProductList ();
productList.readList
();
productList.displayList
();
productList.findProduct
();
} // main method
} // class Store
class Product
{
protected String type, id;
protected double price;
protected Product next;
protected Product (String type) {this.type
= type;}
protected Product getNext () {return next;}
protected void setNext (Product n) {next =
n;}
protected String getId () {return id;}
public void readData (BufferedReader infile)
throws IOException
{
try
{
id = infile.readLine ();
price = Double.parseDouble (infile.readLine ());
} catch (NumberFormatException
e)
{
System.out.print ("Number Format Exception in the price for ");
System.out.println (type + " " + id);
}
} // readData
public void displayData ()
{
System.out.println
("Type: " + type);
System.out.println
("ID: " + id);
System.out.println
("Price: $" + decimals (price));
} // displayData
private String decimals (double num)
{
DecimalFormat
decFor = new DecimalFormat ();
decFor.setMaximumFractionDigits
(2);
decFor.setMinimumFractionDigits
(2);
return decFor.format
(num);
} // decimals
} // Product
class Fruit extends Product
{
private String name;
private String variety;
Fruit (String type)
{
super (type);
} // constructor
public void readData (BufferedReader infile)
throws IOException
{
super.readData
(infile);
name = infile.readLine
();
variety = infile.readLine
();
} // readData
public void displayData ()
{
super.displayData
();
System.out.println
("Name: " + name);
System.out.println
("Variety: " + variety);
} // displayData
} // Fruit
class Vegetable extends Product
{
private String name;
private String color;
Vegetable (String type) {super (type);}
public void readData (BufferedReader infile)
throws IOException
{
super.readData
(infile);
name = infile.readLine
();
color = infile.readLine
();
} // readData
public void displayData ()
{
super.displayData
();
System.out.println
("Name: " + name);
System.out.println
("Color: " + color);
} // displayData
} // Vegetable
class ProductList
{
Product listHead, listRear;
ProductList ()
{
listHead = null;
listRear = null;
} // constructor
public void readList ()
{
Product product;
try
{
BufferedReader infile = new BufferedReader (new InputStreamReader (new
FileInputStream ("products.txt")));
String type = infile.readLine ();
while (type != null)
{
if (type.equals ("Fruit"))
product = new Fruit (type);
else product = new Vegetable (type);
product.readData (infile);
addToList (product);
type = infile.readLine ();
}
infile.close ();
} catch (IOException
e) {System.out.println ("File not found");}
} // readList
private void addToList (Product product)
{
if (listHead
== null)
{
listHead = product;
listRear = product;
}
else
{
listRear.setNext (product);
listRear = product;
}
} // addToList
public void displayList ()
{
Product tempNode
= listHead;
while (tempNode
!= null)
{
System.out.println ();
tempNode.displayData ();
tempNode = tempNode.getNext ();
}
} // displayList
public void findProduct ()
{
try
{
BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
System.out.println ();
System.out.print ("Enter ID of product: ");
String keyId = stdin.readLine ();
Product product = findId (keyId);
if (product != null) product.displayData ();
else System.out.println ("ID not in list");
} catch (IOException
e) { System.out.println ("IO Exception");}
} // findProduct
private Product findId (String keyId)
{
Product tempNode
= listHead;
while ((tempNode
!= null) && !keyId.equals (tempNode.getId ()))
tempNode = tempNode.getNext ();
if (tempNode
== null) return null;
else return
tempNode;
} // findId
} // ProductList