Question: TO DO in JAVA: Finish the compareTo() method in the Insurance class. package itp120mod6; import java.text.*; // note that this is an abstract class (see
TO DO in JAVA: Finish the compareTo() method in the Insurance class.
package itp120mod6;
import java.text.*;
// note that this is an abstract class (see p455 of your text)
// I do not want any one to create a generic Insurance instance
// (we only sell Auto, Life, and Health) but so as not to repeat
// common fields in those , we move them up one hierarchy level
// and make a generic Inurance class
// implements the interface Comparable so I can define how to order
// or sort Insurance objects
public abstract class Insurance implements Comparable
{
// NOTE: these fields are protected!!!!! If I am to inherit
// these , they must be protected (private allows NO other classes
// to use them) - protected means this class and any class that inherits
// from this one can use these
protected Customer customer;
protected double yearlyRate;
protected int policyNumber;
// for currency output
NumberFormat currency = NumberFormat.getCurrencyInstance();
// a static variable used to make the insurance id unique
public static int num = 1000;
// full constructor - automatically creates a new Insurance id
public Insurance (Customer cust)
{
customer = cust;
policyNumber=num;
num++;
}
// used for reading from a file where policy number and rate already set
public Insurance (Customer cust, int polNum, double yrRate)
{
customer = cust;
policyNumber=polNum;
yearlyRate = yrRate;
}
// the empty constructor but still sets a unique id
public Insurance ()
{
policyNumber=num;
num++;
}
// the calcRate method is used to set the yearlyRate
// NOTE: this is an abstract class. That means it can not be used (has no body)
// but by putting here, we require any class that inherits from the Insurance class
// to fully implement this method if they want to be a concrete class
public abstract void calcRate();
// toString method. Note - we let the customer write themself out since they have their own toString method
// a class should only write out their fields in the toString method
public String toString ()
{
return (customer.toString() + " with policy number " + policyNumber + " has a yearly rate of " + currency.format(yearlyRate));
}
// getters and setters
public Customer getCustomer() {
return customer;
}
public int getPolicyNumber() {
return policyNumber;
}
public double getYearlyRate() {
return yearlyRate;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public void setPolicyNumber(int policyNumber) {
this.policyNumber = policyNumber;
}
public void setYearlyRate(double yearlyRate) {
this.yearlyRate = yearlyRate;
}
// required by the Comparable interface. Describes how to compare two Insurance instances
// In this one, we want to compare policy numbers
// Look at how we did the one in the Customer blueprint
// Look at the one in the search_sort.Account class
// Note the difference if you are sorting on primitives versus objects
public int compareTo(Insurance ins)
{
// you get to write this!!
return -1;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------
TO DO in JAVA: Finish the code for the methods in TrackInsurance so that all the menu items work.
package itp120mod6;
import java.util.*;
public class TrackInsurance extends Object {
public static Scanner scan = new Scanner(System.in);
// method that runs first
public static void main(String[] args) throws Exception {
// make an ArrayList of customers and insurance policies
ArrayList
// note - the ArrayList below can hold Insurance objects
// but with inheritance, that includes Auto, Health, Life and Art
ArrayList
// create some fake customers (used for testing the program)
Customer c = new Customer("Duck", "Donald");
Customer c1 = new Customer("Mouse", "Minnie");
Customer c2 = new Customer("Mouse", "Mickey");
// add the customers to the array list
cust.add(c2);
cust.add(c1);
cust.add(c);
// make and add some insurance policies to the ArrayList
ins.add(new AutoInsurance(c, 2));
ins.add(new AutoInsurance(c1, 3));
/*ins.add(new HealthInsurance(c, 5));
ins.add(new HealthInsurance(c2, 1));
ins.add(new LifeInsurance(c, 30000, 65));
ins.add(new LifeInsurance(c1, 400000, 34));*/
// add your ArtInsurance instances here
//ins.add(new ArtInsurance(.....));
int choice = 0;
while (choice >= 0) {
choice = menu();
if (choice == 1)
printAllCustomers(cust);
else if (choice == 2)
printAllInsurance(ins);
else if (choice == 3) {
System.out
.println("Now lets find the information for a certain policy number");
System.out.println("What policy number do you want to find?");
int num = scan.nextInt();
printPolicy(ins, num);
} else if (choice == 4) {
System.out
.println("Now let's find all of the policies for a given customer");
System.out.println("What is the customer id?");
int custNum = scan.nextInt();
getCustomer(ins, custNum);
} else if (choice == 5)
sortCustNum(ins);
else if (choice == 6)
sortPolNum(ins);
else if (choice == 7) {
System.out.println("Bye!!!!!");
choice = -1;
}
} // end while
}
public static int menu() {
System.out.println("Choice:");
System.out
.println(" 1. Print all customers (call the toString method)");
System.out
.println(" 2. Print all insurance information (call the toString method)");
System.out
.println(" 3. Given a policy number, print the policy information");
System.out
.println(" 4. Find all of the policies for a given customer");
System.out
.println(" 5. Sort the insurance policy information by customer number");
System.out
.println(" 6. Sort the insurance policy information by policy number");
System.out.println(" 7. QUIT!! ");
System.out.println(" CHOICE:");
int value = scan.nextInt();
return value;
}
// write a printAllCusts method that prints out the toString method for all
// of the customers
public static void printAllCustomers(ArrayList
}
// write a printAllInsurance method that prints out the toString method for
// all of the insurance policies
public static void printAllInsurance(ArrayList
// print out all of the information
for (Insurance ins : insure)
System.out.println(ins.toString());
}
// write a printPolicy method that prints the information for the policy
// number
// passed in or the statement "That policy does not exist" if it is not
// present
public static void printPolicy(ArrayList
}
// write a getCustomer method that prints the information for all of the
// policies for a given customer
// that customer number is passed in. If none, have it print
// "There are no policies for that customer"
public static void getCustomer(ArrayList
}
// write a method that sorts the policies by policy number
// look at the example in the search_sort package
public static void sortPolNum(ArrayList
Collections.sort(insure);
}
// write a method that sorts the policies by customer number
// this one is tougher since you can not use the Collections.sort() method
// so you need to just slug out some code.
// Look at the bubble sort from the SortByHand in the search_sort package
// You will want to do something similar
// Here is some pseudocode to help
//
public static void sortCustNum(ArrayList
{
for (int out = insure.size() - 1; out > 1; out--)
for (int in = 0; in < out; in++) {
// get the first insurance policy
// get the customer from that insurance policy
// get the customer number from that insurance policy
// get the second insurance policy
// get the customer from that insurance policy
// get the customer number from that insurance policy
// We want to check to see if the second customer number is
// less than the first one
// NOTE: When comparing customer numbers:
// SortByHand uses Strings so it uses the compareTo()
// method.
// We are comparing integers so we can just use <
// if the second customer number is less than the first one
// swap the two insurance policies in the original "insure"
// ArrayList
// check out the SortByHand to see how to swap.
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
