In: Computer Science
Write a Java application that implements the following class(es) as per business requirements mentioned below:
Create an abstract Insurance class (Insurance.java) that has the following instance variables:
- Insurance number,
- customer name,
- start date of policy ( This should be represented by an object of predefined date class in java),
- customer address [( Create a new Address class ( Address.java ) having following instance data members: House number , Street name, City, Province, Zip code. Implement getter and setter and toString() method for Address class. )]
- Define getter and setter along with validations for all the above instance variables.
- Insurance class should have defined two overloaded constructors: o One for initializing all the instance data members to their default values
o Second for initializing only Insurance number, customer name, start date of policy and address
- Declare an abstract public method double MonthlyPayments() which is used for calculating monthly Insurance installment amount.
- Define toString() to display the values
Create following two subclasses of Insurance class:
• a) LifeInsurance (LifeInsurance.java)
• b) VehicleInsurance (VehicleInsurance .java)
For LifeInsurance class, implement the following:
- Define an instance variable – age of the person. Minimum is 1 and Max is 100.
- Define another Boolean variable – smoker.
- Define getters and setters for the above and validations.
- A constructor for initializing all the instance variables with appropriate values.
- Overriding the method - double MonthlyPayments () which calculates monthly insurance installment.
- Business requirements for calculating the yearly insurance – if the person is <= 25 years of age, then yearly insurance amount is $1500.00. if between 26 and 45, then it is 1.5 times and if above 45, then it is two times of the amount. And if the person is smoker, then add $250.00 to yearly insurance amount but if a person is non-smoker, then give 5% discount on yearly insurance amount before calculating the monthly installment. You need to add GST also which is 13%.
- You need to override toString() method to display the object’s data.
For VehicleInsurance class, implement the following:
- Define an instance variable – model year. It cannot be more than 2020 which is current year. And vehicle cannot be older than 15 years. So if current year is 2020 then model year should >= 2005.
- Define another Boolean instance variable – greenVehicle. (yes – means – electric, no means gasoline)
- Constructor for initializing all the instance variables
- Overriding the method - double MonthlyPayments () which calculates monthly vehicle insurance installment
- Business requirements for calculating the yearly insurance – if the vehicle is <= 5, then yearly insurance amount is $3000.00. if between 6 and 10, then 1.5 times and if above 10, then it two times of the amount. And if the vehicle is not green, then add $300.00 as environment fee to yearly insurance amount but if a vehicle is green, then give 10% discount on yearly insurance amount. You need to add GST also which is 13%.
- You need to override toString() method to display the object’s data.
Create a driver class – InsuranceTest (InsuranceTest.java) which tests above classes by at least creating two objects each of the LifeInsurance and VehicleInsurance classes and then processing them normally and polymorphically
All the java files to implement the above functionality is as below:
Address.java
public class Address {
private int houseNumber;
private String streetName;
private String city;
private String province;
private String zipCode;
public Address(){
}
public Address(int houseNumber, String streetName, String city, String province, String zipCode) {
this.houseNumber = houseNumber;
this.streetName = streetName;
this.city = city;
this.province = province;
this.zipCode = zipCode;
}
public int getHouseNumber() {
return houseNumber;
}
public String getStreetName() {
return streetName;
}
public String getCity() {
return city;
}
public String getProvince() {
return province;
}
public String getZipCode() {
return zipCode;
}
public void setHouseNumber(int houseNumber) {
this.houseNumber = houseNumber;
}
public void setStreetName(String streetName) {
this.streetName = streetName;
}
public void setCity(String city) {
this.city = city;
}
public void setProvince(String province) {
this.province = province;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public String toString(){
return this.houseNumber+", "+this.streetName+", "+this.city+", "+this.province+", "+this.zipCode;
}
}
Insurance.java
import java.text.SimpleDateFormat;
import java.util.Date;
public abstract class Insurance {
private int insuranceNumber;
private String customerName;
private Date startDateOfPolicy;
private Address customerAddress;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
public Insurance() {
this.insuranceNumber=0;
this.customerName="";
this.startDateOfPolicy= new Date();
this.customerAddress= new Address();
}
public Insurance(int insuranceNumber, String customerName, Date startDateOfPolicy, Address customerAddress) {
this.insuranceNumber = insuranceNumber;
this.customerName = customerName;
this.startDateOfPolicy = startDateOfPolicy;
this.customerAddress = customerAddress;
}
public int getInsuranceNumber() {
return insuranceNumber;
}
public String getCustomerName() {
return customerName;
}
public Date getStartDateOfPolicy() {
return startDateOfPolicy;
}
public Address getCustomerAddress() {
return customerAddress;
}
public void setInsuranceNumber(int insuranceNumber) {
this.insuranceNumber = insuranceNumber;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public void setStartDateOfPolicy(Date startDateOfPolicy) {
this.startDateOfPolicy = startDateOfPolicy;
}
public void setCustomerAddress(Address customerAddress) {
this.customerAddress = customerAddress;
}
public String toString(){
return this.customerName+" "+this.insuranceNumber+" "+sdf.format(this.startDateOfPolicy)+" "+this.customerAddress.toString();
}
public abstract double monthlyPayments();
}
LifeInsurance.java
import java.text.SimpleDateFormat;
import java.util.Date;
public class LifeInsurance extends Insurance {
private int age;
private boolean smoker;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
public LifeInsurance(int insuranceNumber, String customerName, Date startDateOfPolicy, Address customerAddress,int age, boolean smoker) throws Exception {
super(insuranceNumber,customerName,startDateOfPolicy, customerAddress);
if(age<0 || age>100)
throw new Exception("Invalid Age");
this.age = age;
this.smoker = smoker;
}
public int getAge() {
return age;
}
public boolean isSmoker() {
return smoker;
}
public void setAge(int age) throws Exception {
if(age<0 || age>100)
throw new Exception("Invalid Age");
this.age = age;
}
public void setSmoker(boolean smoker) {
this.smoker = smoker;
}
public double monthlyPayments() {
double yearlyInsAmount;
int age = getAge();
if(age<=25)
yearlyInsAmount=1500;
else if(age>=26 && age<=45)
yearlyInsAmount=1.5 * 1500;
else
yearlyInsAmount=2*1500;
if(smoker)
yearlyInsAmount=yearlyInsAmount+0.08*yearlyInsAmount; //GST - NONSMOKER discount
else
yearlyInsAmount=yearlyInsAmount+0.13*yearlyInsAmount; //GST
return yearlyInsAmount/12;
}
public String toString(){ //you can modify to any format you needed
return this.getCustomerName()+" "+this.getInsuranceNumber()+" "+sdf.format(this.getStartDateOfPolicy())+" "+this.getCustomerAddress().toString() +" has to pay monthly $"+this.monthlyPayments();
}
}
VehicleInsurance.java
import java.text.SimpleDateFormat;
import java.util.Date;
public class VehicleInsurance extends Insurance {
int modelYear;
boolean greenVehicle;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
public VehicleInsurance(int insuranceNumber, String customerName, Date startDateOfPolicy, Address customerAddress,int modelYear, boolean greenVehicle) throws Exception {
super(insuranceNumber,customerName,startDateOfPolicy, customerAddress);
if(modelYear<2005 || modelYear>2020)
throw new Exception("Invalid model year");
this.modelYear = modelYear;
this.greenVehicle = greenVehicle;
}
public double monthlyPayments() {
int modelYear=this.modelYear;
double yearlyInsuAmount;
if(modelYear>2015)
yearlyInsuAmount=3000;
else if(modelYear>2010)
yearlyInsuAmount=1.5 * 3000;
else
yearlyInsuAmount=2 * 3000;
if(greenVehicle)
yearlyInsuAmount=yearlyInsuAmount+0.03*yearlyInsuAmount; //GST - Green Vehicle discount
else
yearlyInsuAmount=yearlyInsuAmount+0.13*yearlyInsuAmount; //GST
return yearlyInsuAmount/12;
}
public String toString(){ //you can modify to any format you needed
return this.getCustomerName()+" "+this.getInsuranceNumber()+" "+sdf.format(this.getStartDateOfPolicy())+" "+this.getCustomerAddress().toString() +" has to pay monthly $"+this.monthlyPayments();
}
}
InsuranceTest.java
import java.text.SimpleDateFormat;
import java.util.Date;
public class InsuranceTest {
public static void main(String[] args) throws Exception{
LifeInsurance l1 = new LifeInsurance(1, "Angel", new Date("11/11/2016"), new Address(21,"xy Street","Washington","yy","36722"), 43, true);
System.out.println(l1.toString());
LifeInsurance l2 = new LifeInsurance(2, "Luis", new Date("11/11/2006"), new Address(89,"MNB Street","Aleaska","dja","36782"), 67, true);
System.out.println(l2.toString());
VehicleInsurance v1 = new VehicleInsurance(3, "Lopez", new Date("11/11/2018"), new Address(81,"xy Street","Washington","yy","36722"), 2013, true);
System.out.println(v1.toString());
VehicleInsurance v2 = new VehicleInsurance(4, "Arun", new Date("11/11/2007"), new Address(341,"MNB Street","Aleaska","dja","36782"), 2007, true);
System.out.println(v2.toString());
}
}
The output screenshots is as below: