Putting it out there that this is a school assignment.
I have nearly all of the code figured out, but the instructions make getting the output and variables to match up. If anyone can assist in seeing what the instructor intends for us to do, I would greatly appreciate it.
This is a multi-class program where an abstract class implements a class and is extended by three sub-classes.
the instructions are:
Problem Definiiton
Create a console application in a Java Package named UseInsurnace
Create an abstract Insurance class
Create Health and Life subclasses to display different types of insurance polices and the cost per month.
Create Auto Insurance class
Add the property VIN ( Vehicle Identification Number)
Add appropriate Set and get methods for VIN
Add Constructor that calls the super class constructor and initializes VIN
Use constructors in each of the classes with appropriate arguments.
Include get and set methods, at least one of which is abstract.
Prompt the user for the type of Insurance to be displayed, and then create the appropriate object.
Also create an interface for a print() method and use this interface with both subclasses.Implement Insurance class with the following properties and methods.
public abstract class Insurance implements Print
// class variables
int PolicyNumber;
double MonthlyPremium;
// public methods
Insurance(int n, double b)
int getPolicyNumber()
double getMonthlyPremium()
double getAnnualPremium()
void setPolicyNumber(int n)
abstract void setMonthlyPremium(double r);
Test for the following Input
//I input the underscores to space out to make easier to read. whitespace wasn't showing for some reason.
Insurance Policy Type______Health___Life____Auto
Policy Number____________3233___1234____3333
Monthly Premium__________$200___$100____$50
VIN_____________________N/A_____N/A___987654321A2008B
output should appear as :
What type of insurance do you wish?
Enter 1 for Health
2 for Life
3 for Auto
2
Life Insurance Policy number is 1234
Monthly Premium is $100.00
Annual Premium is $1,200.00
end quote.
I'm only getting one error in the Auto Insurance class that says that the Annual Premium Variable can not be resolved. I believe it has a lot to do with the variable declarations in the Insurance class, but it's difficult to follow the logic that instructor requires using variable names the way he does.
My output right now is close and has the same error no matter which type of insurance I choose:
What type of insurance do you wish?
Enter 1 for Health
2 for Life
3 for Auto
2
Life
Policy number is 0
Monthly Premium is .00
Annual Premium is 1,200.00
Here is the class that "uses" the others. (the class that is ran)
package useInsurnace;
import java.util.*;
public class UseInsurance
{
public static void main(String [] str) throws Exception
{
Insurance item;
char inputChar;
Scanner keyboard = new Scanner(System.in);
System.out.println("What type of insurance do you wish?");
System.out.println("Enter 1 for Health ");
System.out.println(" 2 for Life ");
System.out.println(" 3 for Auto ");
inputChar = keyboard.next().charAt(0);
if(inputChar == '1')
item = new HealthInsurance(3233,200,2400);
else if(inputChar == '2')
item = new LifeInsurance(1234,100,1200);
else if(inputChar == '3')
item = new AutoInsurance(3333, 50, 600, "987654321A2008B");
else
{
System.out.println("Invalid Insurance Choice");
return;
}
item.print();
}
}
The abstract Insurance class
package useInsurnace;
public abstract class Insurance implements Print
{
int PolicyNumber;
double MonthlyPremium;
public Insurance(int n,double b)
{
n = PolicyNumber;
b = MonthlyPremium;
}
public int getPolicyNumber()
{
return PolicyNumber;
}
public double getMonthlyPremium()
{
return MonthlyPremium;
}
public double getAnnualPremium()
{
return MonthlyPremium;
}
public void setPolicyNumber(int n)
{
PolicyNumber = n;
}
public void setAnnualPremium(double b)
{
MonthlyPremium = b;
}
public abstract void setMonthlyPremium(double r);
}
Auto Insurance class
package useInsurnace;
import java.text.DecimalFormat;
public class AutoInsurance extends Insurance
{
private String VIN;
public AutoInsurance(int n, double b, int i, String vin)
{
super(n, b);
VIN = vin;
}
public void setAnnualPremium(int i)
{
AnnualPremium = i;
}
public double getAnnualPremium()
{
return AnnualPremium;
}
public void setVIN(String vin)
{
VIN = vin;
}
public String getVIN()
{
return VIN;
}
public void setMonthlyPremium(double r)
{
MonthlyPremium = r;
}
public void print()
{
DecimalFormat dollar = new DecimalFormat("#,###.00");
System.out.println("Auto Insurance Policy number is "+ getPolicyNumber()
+"\nMonthly Premium is "+ dollar.format(getMonthlyPremium())
+"\nAnnual Premium is "+ dollar.format(getAnnualPremium())
+ "\nVIN: "+ getVIN());
}
}
The Health Insurance class
package useInsurnace;
import java.text.DecimalFormat;
public class HealthInsurance extends Insurance
{
private double AnnualPremium;
public HealthInsurance(int n, double b, int i)
{
super(n, b);
AnnualPremium = i;
}
public void setMonthlyPremium(double r)
{
MonthlyPremium = r;
}
public double getMonthlyPremium()
{
return MonthlyPremium;
}
public void setAnnualPremium(double i)
{
AnnualPremium = i;
}
public double getAnnualPremium()
{
return AnnualPremium;
}
public void print()
{
DecimalFormat dollar = new DecimalFormat("#,###.00");
System.out.println("Health Insurance Policy number is "+ getPolicyNumber()
+ "\nMonthly Premium is "+ dollar.format(getMonthlyPremium())
+ "\nAnnual Premium is "+ dollar.format(getAnnualPremium()));
}
}
The Life Insurance Class
package useInsurnace;
import java.text.DecimalFormat;
public class LifeInsurance extends Insurance
{
private double AnnualPremium;
public LifeInsurance(int n, double b, int i)
{
super(n, b);
AnnualPremium = i;
}
public void setMonthlyPremium(double r)
{
MonthlyPremium = r;
}
public double getMonthlyPremium()
{
return MonthlyPremium;
}
public void setAnnualPremium(double i)
{
AnnualPremium = i;
}
public double getAnnualPremium()
{
return AnnualPremium;
}
public void print()
{
DecimalFormat dollar = new DecimalFormat("#,###.00");
System.out.println("Life Insurance Policy number is "+ getPolicyNumber()
+ "\nMonthly Premium is "+ dollar.format(getMonthlyPremium())
+ "\nAnnual Premium is "+ dollar.format(getAnnualPremium()));
}
}
Print Class
package useInsurnace;
public interface Print
{
public abstract void print();
}