Computer Science 122
Polymorphism Example

import java.io.*;
import java.text.*;

/* Store reads and displays data about products in a store.   It then requests an ID and finds it in the list.*/
public class Store
{
     public static void main (String [] args)
     {
          ProductList productList = new ProductList ();
          productList.readList ();
          productList.displayList ();
          productList.findProduct ();
     } // main method
} // class Store

// Product is the super class for the products sold by the store.
class Product
{
     protected String type, id;
     protected double price;
 
     Product (String type) {this.type = type;}
     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

// Fruit is a Product.  It is used to read and display data.
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

// Vegetable is a Product used to read and display data.
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.readData (infile);
          System.out.println ("Name: " + name);
          System.out.println ("Color: " + color);
     } // displayData
} // Vegetable

// ProductList manages a list of products.  They can be either fruits or vegetables.
class ProductList
{
     Product [] list;
     int listSize;
     final int maxSize = 10;
 
     ProductList ()
     {
          list = new Product [maxSize];
          listSize = 0;
     } // constructor
 
     public void readList ()
     {
          Product product;
          try
          {
               BufferedReader infile = new BufferedReader (new InputStreamReader (new FileInputStream ("products.txt")));
               String type = infile.readLine ();
               while ((type != null) && (listSize < maxSize))
               {
                    if (type.equals ("Fruit"))  product = new Fruit (type);
                    else product = new Vegetable (type);
                    product.readData (infile);
                    list [listSize] = product;
                    listSize ++;
                    type = infile.readLine ();
               }
               infile.close ();
          } catch (IOException e) {System.out.println ("File not found");}
     } // readList
 
     public void displayList ()
     {
          for (int count = 0; count < listSize; count ++)
          {
               System.out.println ();
               list [count].displayData ();
          }
     } // 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)
     {
          int count = 0;
          while (count < listSize && !keyId.equals (list[count].getId ()))
                count ++;
          if (count == listSize) return null;
          else return list [count];
     } // findId
} // ProductList
 
products.txt

Fruit
AF136
1.50
Apples
Red Delicious
Vegetable
AV213
2.65
Broccoli
Green
Fruit
BF214
1.70
Oranges
Florida
Vegetable
BV247
1.25a
Carrots
Orange
Fruit
CF159
2.35
Pear
Bosc
Vegetable
CV364
3.47
Beans
Yellow
Fruit
DF162
0.89
Bananas
Small