Skip to content

Commit c220ace

Browse files
author
Sean Thames
committed
Completed exercise 8
1 parent 89b1101 commit c220ace

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

m4/ex8/Health.java

+24
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,27 @@
1919
* displayed, and then create the appropriate object. Save the files as
2020
* Life.java, Health.java, Insurance.java, and UseInsurance.java.
2121
*/
22+
23+
import javax.swing.JOptionPane;
24+
25+
public class Health extends Insurance
26+
{
27+
// nobody likes "magic" numbers ;)
28+
private final double HEALTH_INSURANCE_COST = 196;
29+
30+
public Health()
31+
{
32+
super("Health");
33+
setCost();
34+
}
35+
36+
public void setCost()
37+
{
38+
monthlyCost = HEALTH_INSURANCE_COST;
39+
}
40+
41+
public void display()
42+
{
43+
JOptionPane.showMessageDialog(null, "Insurance type: " + getInsuranceType() + "\nMonthly Cost: $" + getMonthlyCost());
44+
}
45+
}

m4/ex8/Insurance.java

+24
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,27 @@
1919
* displayed, and then create the appropriate object. Save the files as
2020
* Life.java, Health.java, Insurance.java, and UseInsurance.java.
2121
*/
22+
23+
public abstract class Insurance
24+
{
25+
private String insuranceType;
26+
protected double monthlyCost;
27+
28+
public Insurance(String type)
29+
{
30+
insuranceType = type;
31+
}
32+
33+
public String getInsuranceType()
34+
{
35+
return insuranceType;
36+
}
37+
38+
public double getMonthlyCost()
39+
{
40+
return monthlyCost;
41+
}
42+
43+
public abstract void setCost();
44+
public abstract void display();
45+
}

m4/ex8/Life.java

+24
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,27 @@
1919
* displayed, and then create the appropriate object. Save the files as
2020
* Life.java, Health.java, Insurance.java, and UseInsurance.java.
2121
*/
22+
23+
import javax.swing.JOptionPane;
24+
25+
public class Life extends Insurance
26+
{
27+
// nobody likes "magic" numbers ;)
28+
private final double LIFE_INSURANCE_COST = 36;
29+
30+
public Life()
31+
{
32+
super("Life");
33+
setCost();
34+
}
35+
36+
public void setCost()
37+
{
38+
monthlyCost = LIFE_INSURANCE_COST;
39+
}
40+
41+
public void display()
42+
{
43+
JOptionPane.showMessageDialog(null, "Insurance type: " + getInsuranceType() + "\nMonthly Cost: $" + getMonthlyCost());
44+
}
45+
}

m4/ex8/UseInsurance.java

+25
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,28 @@
1919
* displayed, and then create the appropriate object. Save the files as
2020
* Life.java, Health.java, Insurance.java, and UseInsurance.java.
2121
*/
22+
23+
import javax.swing.JOptionPane;
24+
25+
public class UseInsurance
26+
{
27+
public static void main(String[] args)
28+
{
29+
String userInput;
30+
int selection;
31+
32+
userInput = JOptionPane.showInputDialog(null, "Show info for which insurance type?\n 1 - Life \n 2 - Health");
33+
selection = Integer.parseInt(userInput);
34+
35+
if(selection == 1)
36+
{
37+
Life lifeInsurance = new Life();
38+
lifeInsurance.display();
39+
}
40+
else
41+
{
42+
Health healthInsurance = new Health();
43+
healthInsurance.display();
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)