// Computer Science Example
// Insurance Policy – Inheritance Example

import java.io.*;

/* InsurancePolicy reads data about insurance policies from a file.  If the type of the policy is Life, it creates an instance of a LifePolicy class and displays the data.  However, if the type is Car, it creates a CarPolicy class. */
public class InsurancePolicy
{
     public static void main (String [] args)
     {
          Policy policy;
          try
          {
               BufferedReader infile = new BufferedReader (new InputStreamReader (new FileInputStream ("infile.txt")));
               String type = infile.readLine ();
               while (type != null)
               {
                    if (type.equals ("Life"))
                        policy = new LifePolicy (type);
                    else
                        policy = new CarPolicy (type);
                    policy.readPolicy (infile);
                    policy.displayPolicy ();
                    type = infile.readLine ();
               }
          } catch (IOException e) {}
     } // main
} // class InsurancePolicy

/* The Policy class is a super class of the Life and Car Policy classes.  It stores the type, id and owner of the policy. */
class Policy
{
     protected String type, id, owner;
 
     Policy (String t) {type = t;}
 
     public void readPolicy (BufferedReader infile) throws IOException
     {
          id = infile.readLine ();
          owner = infile.readLine ();
     } // method readPolicy
 
     public void displayPolicy ()
     {
          System.out.println (type + " Policy");
          System.out.println ("The owner is " + owner);
          System.out.println ("The ID is " + id);
     } // method displayPolicy
} // class Policy

/* The CarPolicy class inherits data from the Policy class and adds data for the make, model and year of the car. */
class CarPolicy extends Policy
{
     private String make, model;
     private int year;
 
     CarPolicy (String t) {super (t);}
 
     public void readPolicy (BufferedReader infile) throws IOException
     {
          super.readPolicy (infile);
          make = infile.readLine ();
          model = infile.readLine ();
          year = Integer.parseInt (infile.readLine ());
     } // method readPolicy
 
     public void displayPolicy ()
     {
          super.displayPolicy ();
          System.out.println ("The make of the car is " + make);
          System.out.println ("The model of the car is " + model);
          System.out.println ("The year is " + year);
          System.out.println ();
     } // method displayPolicy
} // class CarPolicy

/* The LifePolicy class inherits data from the Policy class and adds a beneficiary and policy value. */
class LifePolicy extends Policy
{
     private String beneficiary;
     private double policyValue;
 
     LifePolicy (String t) {super (t);}
 
     public void readPolicy (BufferedReader infile) throws IOException
     {
          super.readPolicy (infile);
          beneficiary = infile.readLine ();
          policyValue = Double.valueOf (infile.readLine ()).doubleValue ();
     } // method readPolicy
 
     public void displayPolicy ()
     {
          super.displayPolicy ();
          System.out.println ("The beneficiary is " + beneficiary);
          System.out.println ("The policy value is " + policyValue);
          System.out.println ();
     } // method displayPolicy
} // class LifePolicy