Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Program Three Titanic Insurance company insures various types of assets. This program is a prototype for them to demonstrate the ability to work with
Program Three Titanic Insurance company insures various types of assets. This program is a prototype for them to demonstrate the ability to work with four types of assets, Automobile, Boat, House, and Jewelry. A true insurance program would be much more complex, but they just want to see a proof of concept. The insurance company wants to be able to load various types of these items into a Java program and then print some information. They want to be able to put all of their insured assets into an array and then to sort them and print them in various ways. In particular, they would like to see the items sorted by: A. Type B. Name C. Value D. Insurance Cost There should be a menu that allows the user to print the items sorted each of these ways as well as an option to quit. The list should show each of the four values mentioned above. The menu should also provide a means to quit the program. (The menu has actually been created for you; I'm certain that after CPT 167 you know how to do menus.) Implementation details Appropriate classes for the four types of assets have already been created for you. You should not modify these classes. There is also the beginning of the Insurance Manager class. The menu is there, but the functionality is missing or incomplete. There is other code completed as well, but a lot is left up to you. Insurable interface Start by completing the (currently empty) Insurable interface. The interface must extend Comparable . It must have the following methods: String getName() -- returns the name of the object String getType() -- returns the type of the object; this just means 'Automobile', 'Boat', etc. double getValue() -- returns the value of the object double getRate() -- returns the insurance rate for this object Get the interface completed first. If you start trying to 'fix' the program before correctly defining the Insurable interface, you'll just get mired down in unhelpful Eclipse suggestions. Again, the Insurable interface must extend Comparable . This is important. Insurable implementations For each of the classes mentioned above, there is an associated class, e.g. InsuredAutomobile. These classes must extend the appropriate base class (e.g. Automobile) and implement the Insurable interface. Let Eclipse help you as you implement the interface. All of these have an existing getName method. The Insured House class has been further completed, though once the Insurable interface is completed, you will need to do some more work. These classes should not need any new methods other than those File Edit Source Refactor Navigate Search Project Run Window Help D... 115 P... P... X > 2 MD UU N 3.3.23*0-2-2 Agency.java X Automobile.java BY ! > Boat.java > House.java > > > JRE System Library [Jav src (default package) > Agency.java 18 > Automobile.java 19 20 InsuranceMan... 10 11 public class Agency { 12 13 14 15 16 17 > > > InsuredJewelry.ja > Jewelry.java 21 22 Insurable.java 230 InsuranceManag 24 InsuredAutomob 25 InsuredBoat.java 26 InsuredHouse.jav 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 m private static final double HOUSE_RATE= .0075; private static final double AUTOMOBILE_RATE = .045; private static final double JEWELRY_RATE = .005; private static final double BOAT_RATE= .07; private ArrayList insured Items = new ArrayList (); private class Cost Comparer implements Comparator { // TODO } public Agency (String itemListPath) throws FileNotFoundException { loadItems (itemListPath); } public void loadItems (String path) throws FileNotFoundException { Scanner input; File file = new File(path); input = new Scanner (file); } while (input.hasNext()) { InsuredAutom... String record = input.nextLine(); Insurable item = createItemFromRecord (record); if (item != null) { } input.close(); } insuredItems.add(item); public double getInsurance Cost (Insurable item) { return item.getRate() * item.getValue(); _ InsuredBoat.... P Insured Hous... InsuredJewel... *Insurable.java A 8 0 0 0 0 File Edit Source Refactor Navigate Search Project Run Window Help B 6 UP UL N 33A32-0-2- InsuranceMan... Automobile.java *D... P... x > > > > JRE System Library [Jav 5 src 6 7 (default package) 80 > Agency.java 9 > Automobile.java 10 211 12 Boat.java House.java Insurable.java > InsuranceManag > InsuredAutomob > InsuredBoat.java > InsuredHouse.jav > InsuredJewelry.ja > Jewelry.java 3 public class Insured Jewelry extends Jewelry { 4 13 } 14 Boat.java private static final String type = "Jewelry"; double rate; } // TODO Problems X Debug Shell 13 errors, 3 warnings, 0 others Description House.java NO 1 public String getName() { return String.format("%s %s", getPrimaryMaterial(), getTypeOfPiece()); InsuredBoat.... Writable InsuredHous... Smart Insert InsuredJewel... X "Insurable.java 3 3:28 [14] Resource Location Q Type FY ** 8 2 (x)= 60 File Edit *D... Source Refactor Navigate Search Project Run Window Help B 6 MD UL . 2. N 3.3=2* InsuranceMan... P... x ESY : > JRE System Library [Jav src (default package) > Agency.java > Automobile.java > Boat.java 2 3 4 > House.java > Insurable.java > Insurance Manag > InsuredAutomob > > > InsuredJewelry.ja > Jewelry.java 5 6 7 8 90 10 11 12 13 14 15 160 InsuredBoat.java 17 InsuredHouse.jav 18 190 20 21 220 23 24 250 26 27 280 29 30 31 32 } 33 public class Jewelry { Boat.java private final String typeOfPiece; private final String primaryMaterial; private final String secondaryMaterial; private double appraisedValue; } House.java this.primaryMaterial = primaryMaterial; this.secondaryMaterial = secondaryMaterial; this.appraisedValue = appraisedValue; public double getAppraisedValue() { return appraisedValue; } public void setAppraisedValue (double appraisedValue) { this.appraisedValue = appraisedValue; } public String getTypeOfPiece() { return typeOfPiece; InsuredBoat.... } public String getPrimaryMaterial() { return primaryMaterial; public Jewelry (String typeOfPiece, String primaryMaterial, String secondaryMaterial, double appraisedValue) { this.typeOfPiece = typeOfPiece; } public String getSecondaryMaterial() { return secondaryMaterial; } Problems X Debug Shell 13 errors, 3 warnings, 0 others Description @ 1 T Insured Hous... Writable InsuredJewel... Smart Insert Jewelry.java X *Insurable.java 33:1:900 Resource 3 Location Q P Type R - 8 D 8 (x)= % required by the interface, though if you find the need to supply some private method to make the code cleaner, feel free to do so. Get these classes done next. Do not attempt to work on the Agency class until you have fixed the 'Insured' classes. The Agency class There is an Agency class that has a list of Insurable objects, ArrayList , that is to be loaded when the constructor is called. The constructor must accept the filename of the records to be loaded. This class will need the following: loadItems(string):void - loads the list of Insurables from the provided filename. This code for this method has been started but works only for House assets. It must be modified to load others. getItemList(): List - this method returns the (unmodifiable) list of items read from the file. This code is complete. Note that the list returned cannot be sorted, you will need to create a new list from the one returned so that you can sort it. CostComparer - a static nested class that compares two Insurable objects based on cost. It must implement the Comparator interface. See below for how it is to be used. This class also has four constants that hold the rates of each type of insured asset. These values are to be used when a new 'Insured' object is created. The Insurance Manager class There is be an Insurance Manager class where the main method resides. All sorting must all be done in the Insurance Manager, though not necessarily the main method. How the sorting is done must follow these directions precisely. This assignment is not an exercise in sorting, but in using interfaces and lambda expressions. Just because the list is sorted in the required order does not mean that credit will be awarded; again, the sorting methods must follow the instructions. As noted above, the menu must allow for four types of sorts: Name - The natural sort order, that is, the compare To method required when implementing the Insurable interface must be sorted by name. This means that Collections.sort will be called only with the list, nothing else. Each of your classes will be forced to implement the compare To method when the class implements the Insurable interface. Insurance Cost-Sorting by insurance cost must use a nested class in the Agency class. It must implement the Comparator interface. You will need to create a new one of these when you do your sorting. Create a new one of these in the getCostComparer method. Type - Sorting by type must be done using a lambda expression. Value - Sorting by value must use an anonymous class. The anonymous class must be created somewhere in your Insurance Manager class. The input file There will be four types of records in the file, one for each type of asset to be insured. The records start with the first letter of the asset type, (A)utomobile, (B)oat, (H)ouse, and (J)ewelry. Each line will have different comma separated fields, depending on the record type. Here is the order of each field, the Java type, and an example: JRE System Library [Jav src (default package) Agency.java Automobile.java > > > Boat.java > House.java > Insurable.java > Insurance Manag > InsuredAutomob InsuredBoat.java InsuredHouse.jav > InsuredJewelry.ja > > > Jewelry.java 3 public class Automobile { 4 5 6 7 8 90 10 11 12 13 14 15 160 17 18 19 200 21 22 23 240 25 26 27 280 29 30 31 32} 33 private final String make; private final String model; private final int year; private double value; public Automobile (String make, String model, int year, double value) { this.make make; this.model model; this.year = year; this.value= value; } public String getMake() { return make; } public String getModel() { return model; } = public int getYear() { return year; } public double getValue() { return value; } Problems X Debug Shell 13 errors, 3 warnings, 0 others Description Writable Smart Insert 7:22:187 Resource Location Type .. Automobile: Type char A Boat: Type char B House: Type char H Jewelry: Type char J Make String Pontiac Builder String Hobie Cat Style String Bungalow Article String Bracelet Model String Vibe Length int 16 Price Per SqFt SqFt double int 163.0 1445 Too few fields on a line Invalid numeric values primary material String diamond Invalid type characters Too many fields on a line Year int 2007 Year int 2019 Bedrooms Int 3 secondary material String silver Value double 1800 Value double 7800 Baths int 3 value Other Notes: Notice that the Agency and the other classes are in a package that ends with 'model'. These are classes that know how to do 'Agency' stuff. They should not know how to do any user stuff. This means that they should never communicate with the user, for example, using System.out. Any communication with the user must be in the Insurance Manager class, which is our user interface. double 9100 For a starting grade of 'B': (no 'C' on this one) Complete the code such that it meets the above requirements. You will need to create an interface, implement the interface in various classes, etc. Note that for a couple of classes, the getName method has been completed for you. You should not change this code. This assignment is mostly about using interfaces; in one case, an interface that you develop (Insurable), in other cases, interfaces provided by Java packages, e.g. Comparable. It is also about understanding how to use classes and lambda expressions to implement the behavior of those interfaces. For a starting grade of 'A': Add exception handling to the reading of the file. You must catch these types of errors at a minimum: *D... P... X > JRE System Library [Jav src (default package) > Agency.java > Automobile.java > Boat.java > House.java > Insurable.java > InsuranceManag > InsuredAutomob InsuredBoat.java InsuredHouse.jav > > > InsuredJewelry.ja > Jewelry.java InsuranceMan... 3 public class Boat { 4 5 6 7 80 9 10 : 8 8 11 12 13 14 150 16 17 18 190 20 21 22 22 230 24 25 26 27@ 28 29 30 310 32 33 34 35} 36 } private final int length; private final String builder; private final int year; private double value; public Boat(String builder, int length, int year, double value) { } public double getValue() { return value; } Agency.java } this.length= length; this.builder = builder; public void setValue (double value) { this.value= value; this.value = value; this.year = year; public int getLength() { return length; } public String getBuilder() { return builder; } public int getYear() { return year; Automobile.java Problems X Debug Shell 13 errors, 3 warnings, 0 others Description Boat.java X InsuredAutom... InsuredBoat.... InsuredJewel... Resource *Insurable.java Location 3 Type (x)= % 60 The first two errors must be caught using exceptions. You may catch the second two by throwing exceptions or by printing an appropriate message w/o throwing an exception. It is up to you. There are a few specific requirements: Reading of the file must NOT stop when an error occurs When an error is found, the line number of the error and the line itself must be printed, e.g. Error on line 12 - 'z' is not an appropriate type: Z, Belt, Brass, Silver, 2000 Each type of error must contain an error specific output; don't simply say invalid input on line 12. Tell what the error is. Output: Given the test file supplied with the program, sorting by Name should produce the following: Type Name Value Cost Boat 16' 2019 Hobie 7800.00 546.00 Boat 19 2010 Prindle 3900.00 273.00 Automobile 1957 Chevrolet Bel Air Automobile 1967 Chevrolet Covair House 2/2/0 Bungalow (1445 sqft) Automobile 2007 Pontiac Vibe Automobile 2019 Nisan Rogue Automobile 2020 Ford Mustang House House House 3/2/0 Bungalow (1445 sqft) 3/2/0 Ranch (1345 sqft) 3/3/0 Bungalow (1445 sqft) 3/3/0 Cottage (1345 sqft) 30' 1975 Catalina House Boat Boat 32' 1990 Hunter House 4/2/0 Ranch (1445 sqft) House 4/4/0 TwoStory (2045 sqft) Boat 42 1995 Pearson House 5/2/0 Split Level (2245 sqft) Jewelry diamond Bracelet Jewelry diamond Necklace Jewelry gold Nose Ring gold Watch Jewelry Jewelry Jewelry gold Wedding Ring plastic Cassio Watch 31000.00 1395.00 18900.00 850.50 258355.00 1937.66 2300.00 103.50 19200.00 864.00 41900.00 1885.50 1986.56 264875.00 248575.00 269765.00 253465.00 1864.31 2023.24 1900.99 696.50 9950.00 50000.00 3500.00 271395.00 2035.46 378975.00 2842.31 38000.00 2660.00 408315.00 3062.36 9100.00 45.50 19000.00 95.00 1200.00 6.00 29000.00 1100.00 29.00 145.00 5.50 0.15 A note on a cleaner implementation: As OOP concepts have become popular, professional programmers now tend to favor composition rather than inheritance. That is, the 'Insured' classes would not extend the associated class but would instead have the associated class as one of its members. That is, InsuredAutomobile would not extend Automobile. Instead, the InsuredAutomobile class would have as one of its members an Automobile. Any information that was needed from the Automobile would come from that object. If this paragraph makes sense to you and you would like to use composition rather BY: 3 public class House { 4 > JRE System Library [Jav src (default package) > Agency.java > Automobile.java > Boat.java > House.java > Insurable.java > > InsuredAutomob > > > > InsuranceManag Insured House.jav 5 6 7 InsuredJewelry.ja Jewelry.java 8 9 10 11 12 13 14 16 150 public House(String style, double baseBuilderCost, int square Feet, int numBedrooms, int numBaths) { this (style, baseBuilderCost, squareFeet, numBedrooms, numBaths, 0); 17 InsuredBoat.java 190 18 20 21 22 23 24 25 26 27 280 29 30 31 32 private static final double BED_UPCOST = 40.0; private static final double BATH_UPCOST = 30.0; private static final double HALF_BATH_UPCOST = 15.0; 33 34 35 360 private final String style; private double baseBuilderCost; private int numBedrooms; private int numBaths; private int numHalfBaths; private int squareFeet; } public House(String style, double baseBuilderCost, int square Feet, int numBedrooms, int numBaths, int numHalfBaths) { this.style= style; } this.baseBuilderCost = baseBuilderCost; this.numBedrooms numBedrooms; this.numBaths = numBaths; this.squareFeet = squareFeet; this.numHalfBaths = numHalfBaths; public double getValue() { double value = baseBuilderCost * squareFeet; value += (numBedrooms * BED_UPCOST) * baseBuilderCost; value += (numBaths * BATH_UPCOST)* baseBuilderCost; value += (numHalfBaths * HALF_BATH_UPCOST) * baseBuilderCost; return value; } public double getBaseBuilderCost() { Problems X Debug Shell 13 errors, 3 warnings, 0 others Description Writable Smart Insert 1:1:0 Resource Location Type (x)= 60 8 30 D... P... BY ! (default package) > Agency.java > Automobile.java > Boat.java > House.java > Insurable.java 4 > JRE System Library [Jav 5 src 6 7 } > InsuranceManag > InsuredAutomob InsuranceMan... > InsuredBoat.java > InsuredHouse.jav > InsuredJewelry.ja Jewelry.java > 2 3 Agency.java public interface Insurable { // TODO public void int getComparator(); Problems X Debug Shell 13 errors, 3 warnings, 0 others Description Automobile.java Boat.java House.java Writable InsuredBoat.... Smart Insert InsuredJewel... 5:37 [32] "Insurable.java X Resource Location Type 8 6 bo *D... P... X BY: > JRE System Library [Jav src (default package) > > > Boat.java > House.java > Insurable.java > InsuranceManag > InsuredAutomob InsuranceMan... XAgency.java 30 import java.io.FileNotFoundException; 4 import java.util.ArrayList; 5 import java.util.Collections; 6 import java.util.Comparator; 7 import java.util.List; 8 import java.util.Scanner; 9 Agency.java 10 import edu.tridenttech.cpt237.insurance agency.model.Agency; Automobile.java 11 import edu.tridenttech.cpt237.insuranceagency.model. Insurable; 12 13 public class InsuranceManager { 14 15 160 17 18 > InsuredBoat.java 19 > InsuredHouse.jav 20 > InsuredJewelry.ja 21 22 > Jewelry.java 23 24 25 26 27 28 29 30 31 232 33 34 35 36 Automobile.java private static Scanner input = new Scanner(System.in); public static void main(String[] args) throws FileNotFoundException { Agency agency= new Agency ("Put your path here"); String menu = "N) Sort by Name " + "V) Sort by Value " + Boat.java The method getType() is undefined for the type Insurable }break; case 'V': { // TODO Problems X Debug Shell 13 errors, 3 warnings, 0 others Description "T) Sort by Type " + "C) Sort by Cost " + "Q) Quit"; String valid Input = "NVTCQ"; char userInput = getValidatedSelection (menu, valid Input); while (userInput != 'Q') { List itemList = new ArrayList (agency.getInsured Items()); switch (userInput) { case 'N' : { // TODO System.out.println("Not Yet Implemented"); House.java InsuredBoat.... Writable Smart Insert InsuredJewel... 80:71 [7] *Insurable.java 3 Resource Location Type 8 8 8 (x)= % bo IS *D... P... x BY ! > > Agency.java > Automobile.java > Boat.java > > Insurable.java > > > 52 53 > JRE System Library [Jav 54 src (default package) House.java InsuranceManag Insured Automob InsuredBoat.java InsuredHouse.jav InsuredJewelry.ja N 33232-0-2-2 InsuranceMan... XAgency.java > > Jewelry.java 50 51 55 56 570 58 59 60 61 62 63 64 6 65 67 68 69 70 71 72 73 74 75 76 770 78 79 *80 81 82 83} } } } 660 private static char getMenuSelection (String prompt) { System.out.println(prompt); } user Input = getValidated Selection (menu, "NVTCQ"); System.out.println("DONE"); = } return selected; Automobile.java } private static char getValidated Selection (String prompt, String allowedChars) { char selected getMenuSelection (prompt); = while (allowedChars.indexOf(selected) < 0) { System.out.printf("%c is not a valid selection; please input a valid character.%n", selected); selected getMenuSelection (prompt); Boat.java String response = input.nextLine(); while (response.length() == 0 ) { System.out.println("Please input a value"); System.out.println(prompt); response input.nextLine(); = 13 errors, 3 warnings, 0 others Description } return Character.toUpperCase(response.charAt(0)); Problems X Debug Shell NOT House.java InsuredBoat.... private static void printList (Agency agency, List list) { System.out.printf("%-11s % - 30s%15s%10s%n", "Type", "Name", "Value", "Cost"); for (Insurable item: list) { System.out.printf("%-11s%-30s%15.2f%10.2f%n", item.getType(), item.getName(), item.getValue(), agency.getInsurance Cost (item)); } Writable M InsuredJewel... Smart Insert *Insurable.java 3 80:71 [7] Resource Location Q Type 8 2:00 - 8 D 18 69 File Edit Source Refactor Navigate Search Project Run Window Help = Q UD UU p D... P... BY: > Boat.java > > JRE System Library [Jav src (default package) > Agency.java Z9 > Automobile.java 10 } 11 House.java > Insurable.java > InsuranceManag > InsuredAutomob > InsuredBoat.java > InsuredHouse.jav N 33.252.02 - 2 InsuranceMan... Agency.java > InsuredJewelry.ja > Jewelry.java 1 2 3 public class InsuredBoat extends Boat { 4 50 public String getName() { 6 7 8 Automobile.java } // TODO Problems X Debug Shell Boat.java 13 errors, 3 warnings, 0 others Description Implicit super constructor Boat() is undefined for default constructor. Must define an explicit constructor NO 1 return String.format("%d' %d %s", getLength(), getYear(), getBuilder()); House.java Writable R InsuredBoat.... X InsuredJewel... Smart Insert 3:25 [11] *Insurable.java Resource T Location Type FY #* 00 2 (x)= % File Edit Source Refactor Navigate Search Project Run 00 *D... P... x > > > > 60 InsuranceMan... > > Jewelry.java 2 3 4 5 > JRE System Library [Jav src (default package) > Agency.java 9 > Automobile.java 10 > Boat.java 11 House.java 12 Insurable.java InsuranceManag 130 14 15 InsuredAutomob 16 > InsuredBoat.java 17 > InsuredHouse.jav 18 } InsuredJewelry.ja 6 7 80 public class Insured House extends House { private static final String type = "House"; private double rate; } Window Help Agency.java } // TODO Automobile.java Problems X Debug Shell 13 errors, 3 warnings, 0 others Description Boat.java House.java public Insured House (String style, double baseBuilderCost, int squareFeet, int numBedrooms, int numBaths, double rate) { super (style, baseBuilderCost, squareFeet, numBedrooms, numBaths); this.rate= rate; InsuredBoat..... Writable P public String getName() { String name = String.format("%d/%d/%d %s (%d sqft)", getNumBedrooms (), getNumBaths (), getNumHalfBaths (), getStyle(), getSquareFeet()); return name; InsuredHous... X Insurable.java Smart Insert 1:1:0 3 Resource Location Q Y Type 2:7 0 * 2 (x)= % 60 Q
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started