Question: The files RateEstimator.java and RateUtility.java have been added at the bottom - Hi - could someone at least help me with the format for the
The files RateEstimator.java and RateUtility.java have been added at the bottom -
Hi - could someone at least help me with the format for the six test cases listed below? I'm not sure what to do when writing test cases when there is an arraylist.
Please help me write six test cases in the LifeRatesManagerTest class. They are:
public void testAddRate()
public void testGetRatebyPos()
public void testGetRatebyName()
public void testDeleteRatebyPos()
public void testGetMinTermQuote()
public void testGetMaxTermQuote()
I have included three classes. LifeRates and LifeRatesManager are finished, I just need help with the LifeRatesManagerTest class.
LifeRatesManagerTest:
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* The test class LifeRatesManagerTest.
*
* @author
* @version
*/
public class LifeRatesManagerTest
{
// instance fields
private LifeRatesManager quotes;
private LifeRates lifeRates0;
private LifeRates lifeRates1;
private LifeRates lifeRates2;
private LifeRates lifeRates3;
private LifeRates lifeRates4;
private LifeRates lifeRates5;
private int count;
/**
* Tolerance variable when comparing floating point values.
*/
public static final double DELTA = .01;
/**
* Default constructor for test class LifeRatesManagerTest.
*/
public LifeRatesManagerTest()
{
// not used for this lab
}
/**
* Sets up the test fixture.
*
* Called before every test case method.
*/
@Before
public void setUp()
{
this.quotes = new LifeRatesManager();
// Non smoker, Male, 25, Low Risk, 0 tickets, Good health
lifeRates0 = new
LifeRates("Gutterman, Donald",
new RateEstimator(false, 'M', 25, false, 0, "Good"),
100000);
this.quotes.addRate(lifeRates0);
count++;
// Smoker, Male, 55, High Risk, 1 ticket, Fair health
lifeRates1 = new
LifeRates("Van Camp, Fred",
new RateEstimator(true, 'M', 55, true, 1, "Fair"),
500000);
this.quotes.addRate(lifeRates1);
count++;
// Non smoker, female, 24, Low Risk, 2 tickets, Poor health
lifeRates2 = new
LifeRates("Speigel, Lisa",
new RateEstimator(false, 'F', 24, false, 2, "Bad"),
110000);
this.quotes.addRate(lifeRates2);
count++;
// Non smoker, Male, 25, Low Risk, 3 tickets, Good health
lifeRates3 = new
LifeRates("Lee, Kim",
new RateEstimator(false, 'M', 25, false, 3, "Good"),
110000);
this.quotes.addRate(lifeRates3);
count++;
// Smoker, Male, 79, Low Risk, 0 tickets, Poor health
lifeRates4 = new
LifeRates("Flynn, Thomas",
new RateEstimator(true, 'M', 79, false, 0, "Poor"),
405000);
this.quotes.addRate(lifeRates4);
count++;
// Non smoker, Female, 62, High Risk, 1 ticket, Good health
lifeRates5 = new
LifeRates("Tyler, Rachel",
new RateEstimator(false, 'F', 62, true, 1, "Good"),
325000);
this.quotes.addRate(lifeRates5);
count++;
}
/**
* Tears down the test fixture.
*
* Called after every test case method.
*/
@After
public void tearDown()
{
// not used for this lab
}
/**
* Test class constructor.
*/
@Test
public void testConstructor()
{
LifeRatesManager emptyQuotes = new LifeRatesManager();
assertNotNull(emptyQuotes);
assertEquals(0, emptyQuotes.getCount());
}
/**
* Test get count.
*/
@Test
public void testGetCount()
{
LifeRatesManager emptyRatesTest = new LifeRatesManager();
assertEquals(0, emptyRatesTest.getCount());
}
/**
* Test add rate.
*/
@Test
public void testAddRate()
{
// begin: sample code for exercising addRate() method
LifeRates rateTest = new LifeRates();
quotes.addRate(rateTest);
count++;
assertEquals(count, this.quotes.getCount());
assertNotNull(quotes.getRate(count - 1));
assertNotSame(rateTest, quotes.getRate(count - 1));
// end: sample code for exercising addRate() method
// REPLACE these comments and the assert
// with your coding of the remainder of the method
assertTrue(false);
}
/**
* Test get rate by postion.
*/
@Test
public void testGetRateByPos()
{
LifeRates rateTest = quotes.getRate(0);
assertNotNull(rateTest);
assertNotSame(rateTest, lifeRates0);
assertFalse(rateTest.getEstimator().isSmoker());
// REPLACE these comments with additional tests
// of the values of rateTest
rateTest = quotes.getRate(quotes.getCount() - 1);
assertNotNull(rateTest);
assertNotSame(rateTest, lifeRates5);
assertFalse(rateTest.getEstimator().isSmoker());
// REPLACE these comments with additional tests
// of the values of rateTest
// test invalid positions
int invalidIndex1 = -1;
int invalidIndex2 = quotes.getCount();
// REPLACE these comments with your code
}
/**
* Test get rate by name.
*/
@Test
public void testGetRateByName()
{
LifeRates rateTest = quotes.getRate("Van Camp, Fred");
assertNotNull(rateTest);
assertNotSame(rateTest, lifeRates1);
// REPLACE these comments and the assert
// with your coding of this method
rateTest = this.quotes.getRate("Lee, Kim");
assertNotNull(rateTest);
assertNotSame(rateTest, lifeRates3);
// REPLACE these comments and the assert
// with your coding of this method
assertTrue(false);
}
/**
* Test delete rate by postion.
*/
@Test
public void testDeleteRateByPos()
{
this.quotes.deleteRate(2);
count--;
String name = "Speigel, Lisa";
// test the existence of quote for "Lisa Speigel"
// test the number of quotes after delete
// test the quote at index 2 after delete
// test deleting with an invalid index
quotes.deleteRate(quotes.getCount());
//test the number of quotes after delete with an invalid index
assertEquals(1, -1);
// test deleting a rate from an empty collection
LifeRatesManager emptyQuotes = new LifeRatesManager();
emptyQuotes.deleteRate(0);
assertEquals(1, -1);
}
/**
* Test delete rate by name.
*/
@Test
public void testDeleteRateByName()
{
String name = "Speigel, Lisa";
this.quotes.deleteRate(name);
count--;
this.quotes.deleteRate(name);
assertNull(this.quotes.getRate(name));
assertEquals(1, -1);
LifeRates rateTest = this.quotes.getRate(2);
assertNotNull(rateTest);
// What's the name of the person at index 2?
assertEquals("Mickey Mouse", rateTest.getName());
// test deleting with name not in collection
quotes.deleteRate("Mickey Mouse");
assertEquals(1, -1);
// test deleting a rate from an empty collection
LifeRatesManager emptyQuotes = new LifeRatesManager();
emptyQuotes.deleteRate("Mickey Mouse");
assertEquals(1, -1);
}
/**
* Test sort by rate.
*/
@Test
public void testSortByRate()
{
LifeRatesManager sortedManager = this.quotes.sortValidRates();
assertEquals(5, sortedManager.getCount());
assertEquals("Gutterman, Donald", sortedManager.getRate(0).getName());
assertEquals("Lee, Kim", sortedManager.getRate(1).getName());
assertEquals("Tyler, Rachel", sortedManager.getRate(2).getName());
assertEquals("Van Camp, Fred", sortedManager.getRate(3).getName());
assertEquals("Flynn, Thomas", sortedManager.getRate(4).getName());
// test sorting an empty collection
LifeRatesManager emptyQuotes = new LifeRatesManager();
sortedManager = emptyQuotes.sortValidRates();
assertEquals(0, sortedManager.getCount());
}
/**
* Test get minimum term rate.
*/
@Test
public void testGetMinTermQuote()
{
// REPLACE these comments and the assert
// with your coding of this method
assertTrue(false);
// test on an empty collection
}
/**
* Test get maximum term rate.
*/
@Test
public void testGetMaxTermQuote()
{
// REPLACE these comments and the assert
// with your coding of this method
assertTrue(false);
// test on an empty collection
}
/**
* Test toString.
*/
@Test
public void testToString()
{
String rateString = quotes.toString();
assertTrue(rateString.contains("has 6"));
assertFalse(rateString.contains("0:"));
assertTrue(rateString.contains("Gutterman, Donald"));
assertTrue(rateString.contains("52.0"));
assertTrue(rateString.contains("Van Camp, Fred"));
assertTrue(rateString.contains("798.75"));
assertTrue(rateString.contains("Tyler, Rachel"));
assertTrue(rateString.contains("374.01"));
assertTrue(rateString.contains("6:"));
assertTrue(rateString.contains("cannot quote"));
// test on an empty collection
LifeRatesManager emptyQuotes = new LifeRatesManager();
rateString = emptyQuotes.toString();
assertTrue(rateString.contains("has 0"));
}
}
__________________________________________________________________________________________________________
LifeRatesManager
import java.util.ArrayList;
/** * Creates and manages a collection of LifeRates. * * @author * @version */ public class LifeRatesManager { // instance variables private ArrayList rates;
// constants /** * Tolerance variable when comparing floating point values in sort method. */ public static final double SORT_DELTA = .001;
/** * Constructor for objects of class LifeRatesManager. */ public LifeRatesManager() { this.rates = new ArrayList(); }
/** * Return the the number of LifeRates objects in the collection. * * @return int the number of LifeRates objects in the collection. * */ public int getCount() { return rates.size(); }
/** * Add a LifeRates object to the collection. * * @param inRate the LifeRates object. * */ public void addRate(LifeRates inRate) { LifeRates tmp = new LifeRates(inRate); this.rates.add(tmp); }
/** * Obtain a LifeRates object from the collection based on index. * * @param inIndex the index of the element in the collection. * @return LifeRates the LiferRates object from the collection, * (returns null if index not valid). * */ public LifeRates getRate(int inIndex) { if (inIndex > 0 || inIndex < rates.size() - 1) { return this.rates.get(inIndex); } return null; }
/** * Obtain a LifeRates object from the collection based on name. * * @param inName the name of candidate policy holder. * @return LifeRates an object in the collection with that name. * (returns null if name not found).
* */ public LifeRates getRate(String inName) { for (int i = 0; i < rates.size() - 1; i++) { if (this.rates.get(i).getName().equalsIgnoreCase(inName)) { return this.rates.get(i); } } return null; }
/** * Remove a LifeRates object from the collection based on index. * * @param inIndex the index of the LifeRates to remove * (do nothing if index is invalid). * */ public void deleteRate(int inIndex) { if (inIndex >= 0 && inIndex < rates.size()) { this.rates.remove(inIndex); } }
/** * Remmove a LifeRates object from the collection based on name. * * @param inName the name associated with the object to remove * (do nothing if name not found). * */ public void deleteRate(String inName) { for (int i = 0; i < rates.size(); i++) { if (this.rates.get(i).getName().equalsIgnoreCase(inName)) { this.rates.remove(inName); } } }
/** * Return a new LifeRatesManager with the valid LifeRates of the * collection ordered by ascending value of term rate. * * @return LifeRatesManager a new collection containing LifeRates in * the collection with valid term rates, * sorted in ascending order by term rate. * * This method is based on the bblSort3 method * presented in Module 11. */ public LifeRatesManager sortValidRates() { ArrayList sortedRates = new ArrayList();
for (int i = 0; i < rates.size(); i++) { if (this.rates.get(i).calcTermRate() != RateUtility.INVALID) { sortedRates.add(this.rates.get(i)); } }
for (int x = 0; x < sortedRates.size() - 1; x++) { for (int y = 0; y < sortedRates.size() - 1; y++) { if (sortedRates.get(y).calcTermRate() > sortedRates.get(y + 1).calcTermRate()) { LifeRates tmp = sortedRates.get(y); sortedRates.set(y, sortedRates.get(y + 1)); sortedRates.set(y + 1, tmp); } } }
LifeRatesManager sortedManager = new LifeRatesManager(); for (int z = 0; z < sortedRates.size(); z++) { sortedManager.addRate(sortedRates.get(z)); } return sortedManager; }
/** * Return a LifeRates object with the minimum term rate. * * @return LifeRates an object in the collection * with the min term rate * (null if collection is empty). */ public LifeRates getMinTermQuote() { // first sort the collection via a method call LifeRatesManager sortedManager = this.sortValidRates();
if (sortedManager.getCount() == 0) { return null; } else { // return the first (lowest rate) return new LifeRates(sortedManager.getRate(0)); } }
/** * Return a LifeRates object with the maximum term rate. * * @return LifeRates an object in the collection * with the maximum term rate * (null if collection is empty). */ public LifeRates getMaxTermQuote() { LifeRatesManager sortedManager = this.sortValidRates();
if (sortedManager.getCount() == 0) { return null; } else { return new LifeRates(sortedManager.getRate(this.rates.size() - 1)); } }
/** * Build a String containing the state of the collection * in the following format. *
There are 4 policyholders 1: Doe, Dan (quoted a term rate of 32.0) 2: Kay, Karen (cannot quote a term rate) 3: Lovelace, Ada (quoted a term rate of 74.482) 4: Babbage, George (quoted a term rate of 338.805) *
* * @return String The state of the object. */ public String toString() { String ratesString = " There are " + this.rates.size() + " policyholders ";
int count = 1; for (int i = 0; i < this.rates.size(); i++) { String name = this.rates.get(i).getName(); double quote = this.rates.get(i).calcTermRate(); if (this.rates.get(i).calcTermRate() == RateUtility.INVALID) { ratesString += count + ":" + name + "(cannot quote) "; } else if (this.rates.get(i).calcTermRate() != RateUtility.INVALID) { ratesString += count + ":" + name + "(Quoted a term rate of " + quote + ") "; } count++; } return ratesString; }
}
______________________________________________________________________________________________________________
LifeRates
/** * Class to generate quotes for Life Insurance. * * @author * @version */ public class LifeRates { // instance variables private String name; private RateEstimator estimator; private double faceValue;
// constants
/** * Term life base policy amount. */ public static final double BASE_FACEVALUE = 100000.00;
/** * Term life max policy amount. */ public static final double MAX_FACEVALUE = 600000.00;
/** * Term life max policy amount. */ public static final int MAX_MONTHS = 240;
/** * Term life to whole life conversion factor. */ public static final double CONVERT_FACTOR = 2.64;
/** * Surrender value compound rate. */ public static final double COMPOUND_RATE = .05;
/** * Months per year. */ public static final int MONTHS_PER_YEAR = 12;
/** * Table for determing discounts for term life policies. */ public static final double[][] TERM_DISCOUNT = {{100000.01, 100.0}, {200000.0, 95.0}, {300000.0, 88.0}, {400000.0, 84}, {500000.0, 80.0}, {600000.0, 75.0}};
/** * Table for determing surrender value percentages. */ public static final int[][] SURRENDER_PCTG = {{60, 0}, {120, 23}, {180, 48}, {240, 75}};
/** * First constructor for objects of class LifeRates. */ public LifeRates() { this.estimator = new RateEstimator(); this.faceValue = 0.0; this.name = null; }
/** * Second constructor for objects of class LifeRates. * * @param inName name of the candidate policy holder. * @param inEstimator a RateEstimator for the candidate policy holder. * @param inFaceValue the face value for rate quote. */ public LifeRates(String inName, RateEstimator inEstimator, double inFaceValue) { this.name = inName; this.estimator = inEstimator; this.faceValue = inFaceValue; }
/** * Third constructor for objects of class LifeRates * that creates a copy of the parameter. * * @param inRates an instance of LifeRates. */ public LifeRates(LifeRates inRates) { this.estimator = inRates.getEstimator(); this.faceValue = inRates.getFaceValue(); this.name = inRates.getName(); }
/** * Update name of the candidate policy holder. * * @param inName name of the candidate policy holder. * */ public void setName(String inName) { this.name = inName; }
/** * Return name of the candidate policy holder. * * @return String name of the candidate policy holder. * */ public String getName() { return this.name; }
/** * Update the estimator for the candidate policyholder. * * @param inEstimator Policy candidate RateEstimator. * */ public void setEstimator(RateEstimator inEstimator) { this.estimator = inEstimator; }
/** * Return the estimator for the candidate policyholder. * * @return RateEstimator estimator for the candidate policyholder. * */ public RateEstimator getEstimator() { return this.estimator; }
/** * Update the the face value for rate quote. * * @param inFaceValue the face value for rate quote. * */ public void setFaceValue(double inFaceValue) { this.faceValue = inFaceValue; }
/** * Return the face value for rate quote. * * @return double the face value for rate quote. * */ public double getFaceValue() { return this.faceValue; }
/** * Calculate term life rate. * * @return double monthly rate for a term life policy. * */ public double calcTermRate() { // Rate factor double rateFactor = 1.0; // Base rate double baseRate = 0;
// constants for use in accessing the term discount table final int category = 0; final int factor = 1;
// Check face value is valid if (this.faceValue < BASE_FACEVALUE || this.faceValue >= MAX_FACEVALUE) { return RateUtility.INVALID; }
// Get base rate baseRate = this.estimator.calcMonthlyRate();
if (baseRate == RateUtility.INVALID) { return RateUtility.INVALID; }
// Find the term discount factor int level = 0; while (this.faceValue >= TERM_DISCOUNT[level][category]) { level++; } rateFactor = TERM_DISCOUNT[level][factor] / 100.0;
// Adjust the base rate
baseRate = (baseRate + (baseRate * (faceValue - 100000) / 100000)) * rateFactor;
return baseRate; }
/** * Calculate the whole life rate. * * @return double monthly rate for a whole life policy. * */ public double calcWholeRate() { double tmp = this.calcTermRate(); if (tmp == RateUtility.INVALID) { return RateUtility.INVALID; } else { return CONVERT_FACTOR * tmp; } }
/** * Calculate whole life Surrender Value. * * @param inMonths months before before terminating; * @return double surrender Value for a whole life policy * */ public double getSurrenderVal(int inMonths) { // constants for use in accessing the surrender pctg table final int category = 0; final int pctg = 1;
// Surrender value percentage double surndrFactor = 0; // Base rate double baseRate = 0; // Surrender value double surndrVal = 0;
// Get whole life base rate baseRate = this.calcWholeRate();
// Test if rate is invalid if (baseRate == RateUtility.INVALID) { return RateUtility.INVALID; }
// Check if termination month is valid // If less than 0, return 0; // if > MAX_MONTHS, return face value if (inMonths < 0) { return 0; } else if (inMonths > MAX_MONTHS) { return this.faceValue; }
// Find the surrender value percentage int level = 0; while (inMonths > SURRENDER_PCTG[level][category]) { level++; } surndrFactor = SURRENDER_PCTG[level][pctg];
// Calculate surrender value surndrVal = (surndrFactor / 100) * baseRate * inMonths * Math.pow(1 + COMPOUND_RATE / MONTHS_PER_YEAR, inMonths);
// Adjust the surrender value if it's greater than face value if (surndrVal > this.faceValue) { surndrVal = this.faceValue; } return surndrVal; }
/** * Build a String containing object information. * * @return String The state of the object. */ public String toString()
{ String quote = "Policy quote information: "; quote += "Name: " + this.getName() + " "; quote += "Smoker: " + this.estimator.isSmoker() + " "; quote += "Gender: " + this.estimator.getGender() + " "; quote += "Age: " + this.estimator.getAge() + " "; quote += "High Risk: " + this.estimator.isHighRisk() + " "; quote += "Tickets: " + this.estimator.getNumTickets() + " "; quote += "Health: " + this.estimator.getHealth() + " "; quote += "Face Value: " + this.getFaceValue() + " "; quote += "Monthly premium term: " + String.format("%.3f", this.calcTermRate()) + " "; quote += "Monthly premium whole: " + String.format("%.3f", this.calcWholeRate()) + " ";
return quote; } }
RateEstimator.java
/** * Class to generate a Term Life Insurance Quote. * * @author * @version */ public class RateEstimator { // instance variables private boolean smoker; private char gender; private int age; private boolean highRisk; private int numTickets; private String health;
// constants
/** * base rate for a 100K face value policy. */ public static final double BASE_RATE = 45.0;
/** * First constructor for objects of class RateEstimator. */ public RateEstimator() { this.smoker = false; this.gender = ' '; this.age = 0; this.highRisk = false; this.numTickets = 0; this.health = null; }
/** * Second constructor for objects of class RateEstimator. * * @param inSmoker whether or not candidate is a smoker. * @param inGender gender (M/F) of candidate. * @param inAge age of candidate. * @param inHighRisk whether or not candidate is high risk. * @param inNumTickets number of traffic tickets for candidate. * @param inHealth candidate health status (Good, Fair, Poor). */ public RateEstimator(boolean inSmoker, char inGender, int inAge, boolean inHighRisk, int inNumTickets, String inHealth) { this.smoker = inSmoker; this.gender = inGender; this.age = inAge; this.highRisk = inHighRisk; this.numTickets = inNumTickets; this.health = inHealth; }
/** * Update whether or not the candidate is a smoker. * * @param inSmoker whether or not candidate is a smoker. */ public void setSmoker(boolean inSmoker) { this.smoker = inSmoker; }
/** * Return whether or not the candidate is a smoker. * * @return boolean True if smoker, false if not. */ public boolean isSmoker() { return this.smoker; }
/**Update gender of candidate. * * @param inGender gender of candidate */ public void setGender(char inGender) { this.gender = inGender; }
/**Returns the gender of the candidate. * * @return char M if male, F if female */ public char getGender() { return this.gender; }
/** Updates the age of the candidate. * * @param inAge age of candidate */ public void setAge(int inAge) { this.age = inAge; }
/**Returns the age of the candidate. * * @return int age age of candidate */ public int getAge() { return this.age; }
/** Updates whether or not candidate is high risk. * * @param inHighRisk whether or not high risk */ public void setHighRisk(boolean inHighRisk) { this.highRisk = inHighRisk; }
/**Return whether or not candidate is high risk. * * @return boolean True if candidate is high risk */ public boolean isHighRisk() { return this.highRisk; }
/** Updates the number of tickets the candidate has. * * @param inNumTickets number of tickets */ public void setNumTickets(int inNumTickets) { this.numTickets = inNumTickets; }
/** Returns the number of tickets the candidate has. * * @return int number of tickets */ public int getNumTickets() { return this.numTickets; }
/** Updates the health of the candidate. * * @param inHealth sets the health of the candidate */ public void setHealth(String inHealth) { this.health = inHealth; }
/** Returns the health of the candidate. * * @return String gets the health of the candidate */ public String getHealth() { return this.health; }
/** * Calculate monthly rate. * * @return double Monthly rate for a 100K face value policy */ public double calcMonthlyRate() { // The monthly rate starts with at the BASE_RATE and // is adjusted for each demographic.
// Use methods of RateUtility to find needed adjustment for each // demographic, and then update the monthly rate accordingly // Return the monthly rate after all adjustments are made
// NOTE: Some demographic data can be invalid - if the utility // method returns INVALID, then stop processing and return // INVALID as the monthly rate
// Variable to accumulate monthly rate double monthlyRate = BASE_RATE;
// Variable whose value will be adjustment for specific demographic data double rateAdjustment = 0.0;
// Adjust rate based on age rateAdjustment = RateUtility.calcAgeAdj(this.age);
if (rateAdjustment == RateUtility.INVALID) { return RateUtility.INVALID; } else { monthlyRate = monthlyRate + rateAdjustment; }
// Adjust rate based on whether or not a smoker rateAdjustment = RateUtility.calcSmokerAdj(this.smoker); monthlyRate = monthlyRate + rateAdjustment;
// Adjust rate based on gender rateAdjustment = RateUtility.calcGenderAdj(this.gender);
if (rateAdjustment == RateUtility.INVALID) { return RateUtility.INVALID; } else { monthlyRate = monthlyRate + rateAdjustment; }
// Adjust rate based whether high risk rateAdjustment = RateUtility.calcRiskAdj(this.highRisk); monthlyRate = monthlyRate + rateAdjustment;
// Adjust rate based on number of tickets rateAdjustment = RateUtility.calcTicketsAdj(this.numTickets);
if (rateAdjustment == RateUtility.INVALID) { return RateUtility.INVALID; } else { monthlyRate = monthlyRate + rateAdjustment; }
// Adjust rate based on health rateAdjustment = RateUtility.calcHealthAdj(this.health);
if (rateAdjustment == RateUtility.INVALID) { return RateUtility.INVALID; } else { monthlyRate = monthlyRate + rateAdjustment; }
// Adjust rate based on multiple factors // (using the calcDemogAdj method of RateUtility) rateAdjustment = RateUtility.calcDemogAdj( this.age, this.smoker, this.highRisk, this.numTickets, this.health); monthlyRate = monthlyRate + rateAdjustment;
return monthlyRate; }
/** * Display candidate information. * * @return String formatted candidate information. * *
* The resulting string has the format: * * Policy quote information: * Smoker: false * Gender: M * Age: 25 * # of tickets: 1 * Risky: false * Health: Good * Monthly premium: 50.0 **/ public String toString() { // initialize the variable that will hold the output string String quote = "Policy quote information: ";
quote += "Smoker: " + this.isSmoker() + " "; quote += "Gender: " + this.getGender() + " "; quote += "Age: " + this.getAge() + " "; quote += "# of tickets: " + this.numTickets + " "; quote += "Risky: " + this.isHighRisk() + " "; quote += "Health: " + this.getHealth() + " "; quote += "Monthly premium: " + calcMonthlyRate() + " ";
return quote; }
}
_____________________________________________________________________________________________________-
RateUtility.java
/** * A utility class to help calculate demographic impact on rates. * * @author * @version */ public class RateUtility { // This is a utility class, all methods are static, // thus objects are not created from this class. // Methods are called on the class name, similar // to how we call methods on the standard Java API // Math class i.e. Math.pow(5, 2), Math.sqrt(25) etc.
// constants
/** * Indicates that policy cannot be quoted due * to value of one of the rate factors. */ public static final double INVALID = -999.0;
/** * Calculate age-related rate adjustment. * * @param inAge age of candidate. * @return double monthly premium adjustment * (return INVALID if bad input). */ public static double calcAgeAdj(int inAge) { // Age-related adjustment if (inAge < 0) { return INVALID; } else if (inAge < 25) { return 0.00; } else if (inAge >= 25 && inAge < 35) { return 18.00; } else if (inAge >= 35 && inAge < 45) { return 23.00; } else if (inAge >= 45 && inAge < 55) { return 36.00; } else if (inAge >= 55 && inAge < 65) { return 51.00; } else if (inAge >= 65 && inAge < 80) { return 100.00; } else { return INVALID; } }
/** * Calculate smoker-related rate adjustment. * * @param inSmoker whether or not candidate is a smoker. * @return double monthly premium adjustment. * */ public static double calcSmokerAdj(boolean inSmoker) { if (inSmoker) { return 55.0; } else { return 0.0; } }
/** * Calculate gender-related rate adjustment. * * @param inGender gender (M/F) of candidate. * @return double monthly premium adjustment * (return INVALID if bad input). * */ public static double calcGenderAdj(char inGender) { if (inGender == 'M') { return 15.0; } else if (inGender == 'm') { return 15.0; } else if (inGender == 'F') { return 0.0; } else if (inGender == 'f') { return 0.0; } else { return INVALID; } }
/** * Calculate risk-related rate adjustment. * * @param inHighRisk whether or not candidate is high risk. * @return double monthly premium adjustment. * */ public static double calcRiskAdj(boolean inHighRisk) { if (inHighRisk) { return 42.0; } else { return -5.0; } }
/** * Calculate tickets-related rate adjustment. * * @param inNumTickets number of traffic tickets for candidate. * @return double monthly premium adjustment * (return INVALID if bad input). * */ public static double calcTicketsAdj(int inNumTickets) { if (inNumTickets == -1) { return INVALID; } else if (inNumTickets == 0) { return 0.0; } else if (inNumTickets == 1) { return 5.0; } else if (inNumTickets == 2) { return 9.0; } else { return 19.0; } }
/** * Calculate health-related rate adjustment. * * @param inHealth candidate health status (Good, Fair, Poor). * @return double monthly premium adjustment * (return INVALID if bad input). * */ public static double calcHealthAdj(String inHealth) { if (inHealth.equalsIgnoreCase("Good")) { return -6.00; } else if (inHealth.equalsIgnoreCase("Fair")) { return 0.00; } else if (inHealth.equalsIgnoreCase("Poor")) { return 60.00; } else { return INVALID; } }
/** * Calculate demographics-related rate adjustment. * * @param inSmoker whether or not candidate is a smoker. * @param inAge age of candidate. * @param inHighRisk whether or not candidate is high risk. * @param inNumTickets number of traffic tickets for candidate. * @param inHealth candidate health status (Good, Fair, Poor). * @return double monthly premium adjustment. * */ public static double calcDemogAdj(int inAge, boolean inSmoker, boolean inHighRisk, int inNumTickets, String inHealth) { // Variable to count factors that contribute to premium decrease int asset = 0;
// Variable to count factors that contribute to premium increase int liability = 0;
// Variable to hold rate adjustment double rateAdjustment = 0;
// Determine if demographic data counts toward increase/decrease
if (inAge < 25) { ++asset; }
if (inSmoker) { ++liability; } else { ++asset; }
if (inHighRisk) { ++liability; } else { ++asset; }
if (inHealth.equals("Poor")) { ++liability; }
if (inHealth.equals("Good")) { ++asset; }
if (inNumTickets == 0) { ++asset; }
if (inNumTickets == 3) { ++liability; }
// were there enough factors to decrease premium? if (asset >= 3) { rateAdjustment = -15.00; }
if (liability >= 3) { rateAdjustment = 65.00; } return rateAdjustment; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
