/* InsurancePolicies illustrates inheritance and polymorphism in Java.
When data for a policy is read, it is stored in the proper class
for that policy.
And when it is displayed, the correct data will be used.
*/
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class PolicyFrame extends Frame
{
private Panel panel;
private Label areaLabel;
private TextArea area;
private Button displayCar, displayLife;
private Policies policies;
private Connection con;
public PolicyFrame ()
{
super ("Frame");
setSize (300,
300);
// Add a window
closer to the frame.
addWindowListener
(new WindowAdapter()
{public void windowClosing(WindowEvent e) {System.exit(0);}});
// Get a new
panel and set its background color.
panel = new
Panel ();
panel.setBackground
(Color.cyan);
// Get a text
area and add it to the panel.
areaLabel =
new Label ("Library Books");
area = new TextArea
(10, 30);
// Get a new
button and add an action listener to it.
displayCar =
new Button ("Display Car Policies");
displayCar.addActionListener
(new DisplayCarListener ());
// Get a new
button and add an action listener to it.
displayLife
= new Button ("Display Life Policies");
displayLife.addActionListener
(new DisplayLifeListener ());
panel.add (areaLabel);
panel.add (area);
panel.add (displayCar);
panel.add (displayLife);
add (panel);
try
{
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection ("jdbc:odbc:insurance");
policies = new Policies ();
} catch (ClassNotFoundException
e){System.out.println ("Database connection exception.");}
catch (SQLException e) {System.out.println ("SQL Exception");}
} // constructor
// An inner class that displays the library
data when the button is clicked.
class DisplayCarListener implements ActionListener
{
public void
actionPerformed (ActionEvent e)
{
try
{
// Display the data on the text area.
policies.displayCarPolicies (con, area);
} catch (SQLException ex) {System.out.println ("SQL Exception");}
} // actionPerformed
} // DisplayCarListener
// An inner class that displays the library
data when the button is clicked.
class DisplayLifeListener implements ActionListener
{
public void
actionPerformed (ActionEvent e)
{
try
{
// Display the data on the text area.
policies.displayLifePolicies (con, area);
} catch (SQLException ex) {System.out.println ("SQL Exception");}
} // actionPerformed
} // DisplayLifeListener
// The main method gets a new frame and shows
it.
public static void main (String [] args)
{
PolicyFrame
frame = new PolicyFrame ();
frame.setVisible
(true);
}
} // class PolicyFrame
class Policies
{
CarPolicy carPolicy;
LifePolicy lifePolicy;
public void displayCarPolicies (Connection
con, TextArea area) throws SQLException
{
// Create a
statement and query and display the car policy data.
Statement stmt
= con.createStatement ();
String query
= "Select * From CarInsurance";
ResultSet rs
= stmt.executeQuery (query);
while (rs.next
())
{
carPolicy = new CarPolicy ();
carPolicy.readPolicy (rs);
carPolicy.displayPolicy (area);
}
} // displayCarPolicies
public void displayLifePolicies (Connection
con, TextArea area) throws SQLException
{
// Create a
statement and query and display the life policy data.
Statement stmt
= con.createStatement ();
String query
= "Select * From LifeInsurance";
ResultSet rs
= stmt.executeQuery (query);
while (rs.next
())
{
lifePolicy = new LifePolicy ();
lifePolicy.readPolicy (rs);
lifePolicy.displayPolicy (area);
}
} // displayLifePolicies
} // Policies
// Policy is a super class that contains the data common to the two
types of policies.
class Policy
{
protected String id, owner;
public void readPolicy (ResultSet rs) throws
SQLException
{
id = rs.getString
("Id");
owner = rs.getString
("Owner");
} // method readPolicy
public void displayPolicy (TextArea area)
{
area.append
("The owner is " + owner + '\n');
area.append
("The ID is " + id + '\n');
} // method displayPolicy
} // class Policy
// CarPolicy extends Policy and adds data of its own that describes
a car.
class CarPolicy extends Policy
{
private String make, model;
private int year;
public void readPolicy (ResultSet rs) throws
SQLException
{
super.readPolicy
(rs);
make = rs.getString
("Make");
model = rs.getString
("Model");
year = rs.getInt
("Year");
} // method readPolicy
public void displayPolicy (TextArea area)
{
super.displayPolicy
(area);
area.append
("The make of the car is " + make + '\n');
area.append
("The model of the car is " + model + '\n');
area.append
("The year is " + year + "\n\n");
} // method displayPolicy
} // class CarPolicy
// LifePolicy extends Policy and adds data needed for a life insurance
policy.
class LifePolicy extends Policy
{
private String beneficiary;
private double policyValue;
public void readPolicy (ResultSet rs) throws
SQLException
{
super.readPolicy
(rs);
beneficiary
= rs.getString ("Beneficiary");
policyValue
= rs.getDouble ("PolicyValue");
} // method readPolicy
public void displayPolicy (TextArea area)
{
super.displayPolicy
(area);
area.append
("The beneficiary is " + beneficiary + '\n');
area.append
("The policy value is " + policyValue + "\n\n");
} // method displayPolicy
} // class LifePolicy