Here is the code for the follwing Policy, Expiring Policy(inherits from policy),
ID: 3722573 • Letter: H
Question
Here is the code for the follwing Policy, Expiring Policy(inherits from policy), and Depreciating Policy(inherits from policy)
Completion == Thumbs up :)
(2) The Clients Object We will now define the following hierarchy: To begin, copy the code from the following Client class import java.util.*; public abstract class Client Client IndividualClient CompanyClient private static final int MAX POLICIES PER CLIENT10 private static int NEXTCLIENTID = 1; - - private String private int protected Policy[] protected int name id policies; numPolicies; public Client (String n) l name n ; id-NEXT CLIENT ID++ policiesnew Policy [MAX POLICIES PER CLIENT] numPolicies 0; public String getName) return name; public int getId) return id; public Policy[] getPolicies (return policies: public int getNumPolicies ) return numPolicies; public String toString) [ return string. forma t ("Client : %06d amount: %s", id, name); Create the following in the Client class: a public method called totalCoverage) which returns a float containing the total amount of coverage of all the client's policies a public method called addPolicy(Policy p) which adds the given Policy to the array of policies, provided that the limit has not been reached... and returns the Policy object, or null if not added. a public method called openPolicyFor(float amt) which creates a new Policy for the amount specified in the parameter and adds it to the client using (and returning the result from) the addPolicy() methodExplanation / Answer
Hey, I have completed all the methods, and also mentioned comments in the code itself, please follow along, in case of doubts you may ask in me in the comments section.
Policy.java
public class Policy {
private static int NEXT_POLICY_NUMBER = 1;
public int policyNumber;
protected float amount;
public Policy(float amt) {
amount = amt;
policyNumber = NEXT_POLICY_NUMBER++;
}
public int getPolicyNumber() {
return policyNumber;
}
public float getAmount() {
return amount;
}
public String toString() {
return String.format("Policy: %04d amount: $%1.2f", policyNumber, amount);
}
public boolean isExpired() {
return false;
}
public float handleClaim() {
return amount;
}
}
DepreciatingPolicy.java
public class DepreciatingPolicy extends Policy {
private float rate;
public DepreciatingPolicy(float a, float r) {
super(a);
this.rate = r;
}
public float getRate() {
return rate;
}
@Override
public String toString() {
return "Depreciating Policy: 000" + getPolicyNumber() + " amount: $" + getAmount() + " rate: " + getRate() * 100f
+ "%";
}
public boolean isExpired() {
if (getAmount() == 0) {
return true;
} else {
return false;
}
}
public void depreciate() {
float amountTakeaway;
amountTakeaway = amount * rate;
amount -= amountTakeaway;
}
public float handleClaim() {
float amtBeforeDep = amount; // temporary store the amount as we need to return the amount of policy, before it was deprecated..
depreciate();
return amtBeforeDep;
}
}
ExpiringPolicy.java
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class ExpiringPolicy extends Policy {
private Date expiryDate;
public ExpiringPolicy(float a, Date d) {
super(a);
expiryDate = d;
}
public ExpiringPolicy(float a) {
super(a);
GregorianCalendar aCalendar = new GregorianCalendar();
aCalendar.add(Calendar.YEAR, 1);
expiryDate = aCalendar.getTime();
}
public Date getExpiryDate() {
return expiryDate;
}
public String toString() {
SimpleDateFormat dateFormatter = new SimpleDateFormat("MMMM d, yyyy '('h:mma')'");
return "Expiring Policy: 000" + getPolicyNumber() + " amount: $" + amount + " expires: "
+ dateFormatter.format(getExpiryDate());
}
public boolean isExpired() {
Date x = new Date();
if (x.after(this.expiryDate)) {
return true;
} else if (x.before(this.expiryDate)) {
return false;
} else {
return false;
}
}
public float handleClaim() {
if (isExpired()) {
return 0;
}
return amount;
}
}
Client.java
import java.util.Date;
public abstract class Client {
private static final int MAX_POLICIES_PER_CLIENT = 10;
private static int NEXT_CLIENT_ID = 1;
private String name;
private int id;
protected Policy[] policies;
protected int numPolicies;
public Client(String n) {
name = n;
id = NEXT_CLIENT_ID++;
policies = new Policy[MAX_POLICIES_PER_CLIENT];
numPolicies = 0;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public Policy[] getPolicies() {
return policies;
}
public int getNumPolicies() {
return numPolicies;
}
@Override
public String toString() {
return String.format(this.getClass().getName() + ": %06d amount: %s", id, name);
}
public float totalCoverage() {
float totalCoverage = 0;
for (Policy policy : policies) {
if (policy == null) // as policy can be null,
break;
totalCoverage += policy.getAmount();
}
return totalCoverage;
}
public Policy addPolicy(Policy p) {
boolean isAdded = false;
for (int i = 0; i < policies.length; i++) {
if (policies[i] == null) {
policies[i] = p;
numPolicies++;
isAdded = true;
break;
}
}
if (isAdded) {
return p;
} else {
return null;
}
}
public Policy openPolicyFor(float amt) {
Policy policy = new Policy(amt);
return addPolicy(policy);
}
public Policy openPolicyFor(float amt, float rate) {
Policy policy = new DepreciatingPolicy(amt, rate);
return addPolicy(policy);
}
public Policy openPolicyFor(float amt, Date expire) {
Policy policy = new ExpiringPolicy(amt, expire);
return addPolicy(policy);
}
public Policy getPolicy(int polNum) {
for (int i = 0; i < policies.length; i++) {
if (policies[i] != null && policies[i].getPolicyNumber() == polNum) {
return policies[i];
}
}
return null;
}
public boolean cancelPolicy(int polNum) {
Policy policy = getPolicy(polNum);
if (policy == null) {
return false;
}
Policy lastPolicy = getLastPolicy();
int indexOfLastPolicy = indexOfLastPolicy();
for (int i = 0; i < policies.length; i++) {
if (policies[i] == policy) {
policies[i] = lastPolicy;
policies[indexOfLastPolicy] = null;
numPolicies--;
return true;
}
}
return true;
}
private Policy getLastPolicy() {
for (int i = 0; i < policies.length; i++) {
if (policies[i] == null) {
return policies[i - 1];
}
}
return policies[policies.length - 1];
}
private int indexOfLastPolicy() {
for (int i = 0; i < policies.length; i++) {
if (policies[i] == null) {
return (i - 1);
}
}
return policies.length - 1;
}
public abstract float makeClaim(int polNum);
}
IndividualClient.java
public class IndividualClient extends Client {
public IndividualClient(String n) {
super(n);
}
@Override
public float makeClaim(int polNum) {
Policy policy = getPolicy(polNum);
if (policy == null || policy.isExpired()) {
return 0;
}
// if policy is of type normal policy, remove it from the array of
// policies for this customer, and return the entire claim amount
if (policy.getClass().getName().equals(Policy.class.getName())) {
cancelPolicy(polNum);
return policy.getAmount();
} else if (policy.getClass().getName().equals(DepreciatingPolicy.class.getName())) {
((DepreciatingPolicy) policy).depreciate(); // need to downcast policy object in order to call depreciate..
return policy.getAmount();
} else if (policy.getClass().getName().equals(ExpiringPolicy.class.getName())) {
return policy.getAmount();
}
return 0;
}
}
CompanyClient.java
public class CompanyClient extends Client {
public CompanyClient(String n) {
super(n);
}
@Override
public float makeClaim(int polNum) {
Policy policy = getPolicy(polNum);
if (policy == null) {
return 0;
}
// No need to check if the policy is the one belonging to this client,
// because this makeClaim method will be invoked only when if the object
// is of type CompanyClient,
// so if the object is of type CompanyClient, than all the policies in the policies array will
// also belong to this client
return policy.handleClaim();
}
}
Thanks and Happy Learning :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.