Question

In: Computer Science

Write a Java application that implements the following class(es) as per business requirements mentioned below: Create...

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

Solutions

Expert Solution

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:


Related Solutions

Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm. Create a second Java Application that implements an Insertion sort algorithm to sort the same array. Again, output the array in its original order, then output the array after it has been sorted...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm.
Write a java application that implements ArrayStack in chapter 16 of your textbook.
Write a java application that implements ArrayStack in chapter 16 of your textbook. Your application should have two files: 1. ArrayStack.java: This file will have the implementation of your stack. 2. TestStack.java: This file will have the main method where you declare your stack object and test the different stack operations. In addition to the ArrayStack methods given in the book, you are to write a toString method. This will allow you to print the Stack object from main. You...
USING JAVA and NOTTT using BufferedReader or BufferedWriter Write an application that implements a simple text...
USING JAVA and NOTTT using BufferedReader or BufferedWriter Write an application that implements a simple text editor. Use a text field and a button to get the file. Read the entire file as characters and display it in a TextArea. The user will then be able to make changes in the text area. Use a Save button to get the contents of the text area and write that over the text in the original file. Hint: Read each line from...
Write a program in java that does the following: Create a StudentRecord class that keeps the...
Write a program in java that does the following: Create a StudentRecord class that keeps the following information for a student: first name (String), last name (String), and balance (integer). Provide proper constructor, setter and getter methods. Read the student information (one student per line) from the input file “csc272input.txt”. The information in the file is listed below. You can use it to generate the input file yourself, or use the original input file that is available alone with this...
Eclipse Exercise 1 Write a Java application that implements different types of insurance policies for employees...
Eclipse Exercise 1 Write a Java application that implements different types of insurance policies for employees of an organization. Let Insurance be an abstract superclass and Health and Life two of its subclasses that describe respectively health insurance and life insurance. The Insurance class defines an instance variable of type String to describe the type of insurance and an instance variable of type double to hold the monthly cost of that insurance. Implement the get methods for both variables of...
Write a Java program that implements a song database. The SongsDatabase class keeps tracks of song...
Write a Java program that implements a song database. The SongsDatabase class keeps tracks of song titles by classifying them according to genre (e.g., Pop, Rock, etc.). The class uses a HashMap to map a genre with a set of songs that belong to such a genre. The set of songs will be represented using a HashSet. Your driver output should sufficiently prove that your code properly implements the code below. public class SongsDatabase { private Map<String, Set<String>> genreMap =...
Create a Java application that meets the following specifications. Write whatever public and private methods are...
Create a Java application that meets the following specifications. Write whatever public and private methods are necessary. Put all of these classes in a package called persistence1. Student has a String name, a double GPA, and a reasonable equals() method. Course has a String name, an ArrayList of Students, and a reasonable equals() method. It will also need a method to add a Student. StudentPersister uses binary file I/O to save and retrieve Students and Lists of Students. CoursePersister does...
Create a Java application that meets the following specifications. Write whatever public and private methods are...
Create a Java application that meets the following specifications. Write whatever public and private methods are necessary. Put all of these classes in a package called persistence1. The student has a String name, a double GPA, and a reasonable equals() method. The course has a String name, an ArrayList of Students, and a reasonable equals() method. It will also need a method to add a Student. StudentPersister uses the binary file I/O to save and retrieve Students and Lists of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT