Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create the following in the Client class: a public method called totalCoverage()

ID: 3722910 • Letter: C

Question

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() method

a public method called openPolicyFor(float amt, float rate) which creates a new DepreciatingPolicy for the amount and rate specified in the parameters and adds it to the client using (and returning the result from) the addPolicy() method.

a public method called openPolicyFor(float amt, Date expire) which creates a new ExpiringPolicy for the amount and expiry date specified in the parameters and adds it to the client using (and returning the result from) the addPolicy() method above.

a public method called getPolicy(int polNum) which examines all Policy objects in policies. If one is found whose policyNumber matches the input parameter, then it should be returned by the method. Otherwise null is returned.

a public method called cancelPolicy(int polNum) which removes the police from the client's list with the given policy number, if found. It should return true if the policy was found, otherwise it should return false. When a policy is removed from the array, the empty location in the array should be replaced by the last policy in the array.

a public abstract method called makeClaim(int polNum) that returns a float.

Now create the two subclasses of Client, one called IndividualClient and the other called CompanyClient. Try compiling them. Notice that your test code will not compile. That's because subclasses DO NOT inherit constructors and java requires a constructor to be available. Do the following:

Create a constructor (taking a single String argument) for each of these subclasses that calls the one in Client.

Go back and alter your toString() method in Client class so that the proper client type is displayed. For example: CompanyClient 0001: Bob B. Pins To do this, you will want to figure out how to get the name of the class as a String. (hint: type this. and then let IntelliJ show a list of available methods. You need to find out what class you have and then find out the name of the class).

Implement the makeClaim(int polNum) method in the IndividualClient class. If the policy with that number is not found or has expired, then nothing is to be done, just return 0. IndividualClients are allowed to make only 1 claim per regular Policy but DepreciatingPolicys and ExpiringPolicys can have multiple claims. So, you should cancel a regular policy after a claim is made on it. If the claim is being made for a DepreciatingPolicy, then you must make sure to depreciate the policy. This method should return the amount of the policy (amount after depreciating in the case of DepreciatingPolicys) if the claim was made ok, otherwise 0 is returned.

In the Policy class, implement a method called handleClaim() that returns the amount of the policy.

In the DepreciatingPolicy class, implement handleClaim() so that it returns the amount of the policy but also depreciates the policy. Note that the amount returned is the amount of the policy before it was depreciated.

In the ExpiringPolicy class, implement handleClaim() so that it returns the amount of the policy as long as the policy has not yet expired, otherwise it returns 0.

In the CompanyClient class, implement the makeClaim(int polNum) method so that it first checks to make sure the policy is one belonging to this client and then uses the double-dispatching technique by calling the handleClaim() method. You MUST NOT use any IF statements to determine the policy type ... that's would undo the advantages of double-dispatching. Note that CompanyClients DO NOT have their policy removed afterwards, and so you should make use of the getPolicy() method. If no policy is found with the given polNum, return 0;

Test it with the code below:

Here is the expected output Here are the Individual Client's policies: Policy: 0001 amount: $100.00 DepreciatingPolicy: 0002 amount: $200.00 rate: 10.0% ExpiringPolicy: 0003 amount: $300.00 expires January 02, 2020 (11: 59PM) ExpiringPolicy: 0004 amount: $400.00 expired on: June 04, 2009 (12:00PM) Making claims: Claim for policy 0001: $100.00 Claim for policy 0002: $180.00 Claim for policy 0003: $300.00 Claim for policy 0004:$ 0.00 Claim for policy 0005: $0.00 Here are the Individual Client's policies after claims: ExpiringPolicy: 0004 amount: $400.00 expired on: June 04, 2009 (12:00PM) DepreciatingPolicy: 0002 amount: $180.00 rate: 10.0% ExpiringPolicy: 0003 amount: $300.00 expires: January 02, 2020 (11: 59PM) The total policy coverage for this client: $880.00 Here are the Company C1ient's policies: Policy: 0006 amount: $1000.00 DepreciatingPolicy: 0007 amount: $2000.00 rate : 10.0% ExpiringPolicy: 0008 amount: $3000.00 expires: January 02, 2020 (11:59PM) ExpiringPolicy: 0009 amount: $4000.00 expired on: June 04, 2009 (12:00PM) Making claims: Claim for policy 0006: $1000.00 Claim for policy 0007: $2000.00 Claim for policy 0008: $3000.00 Claim for policy 0009:$0.00 Claim for policy 0005:$0.00 Here are the Company Client' s policies after claims: Policy:0006 amount: $1000.00 DepreciatingPolicy: 0007 amount: $1800.00 rate: 10.0% ExpiringPolicy: 0008 amount: $3000.00 expires: January 02, 2020 (11:59PM) ExpiringPolicy: 0009 amount: $4000.00 expired on: June 04, 2009 (12:00PM) The total policy coverage for this client: $9800.00 Cancelling policy #12 did it work: false Cancelling policy #8 did it work: true The total policy coverage for this client: $6800.00

Explanation / Answer

Policy .java

public class Policy {
    private static int NEXT_POLICY_NUMBER = 1;

    private     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 void setAmount(float amount) {
       this.amount = amount;
   }

   public String toString() {
        return String.format("Policy: %04d amount: $%1.2f", policyNumber, amount);
    }

    public boolean isExpired() {
        return false;
    }
  
    public float handleCalim()
    {
       return amount;
    }
}

DepreciatingPolicy .java


public class DepreciatingPolicy extends Policy {

   private float rate; // variable

   // constructor
   public DepreciatingPolicy(float amt, float rate) {
       super(amt);
       this.rate = rate;
   }

   public float getRate() {
       return rate;
   }

   // instance method

   public boolean isExpired() {
       return Math.signum(rate) == 0;
   }

   public void depreciate() {
       setAmount(getAmount() - getAmount() * rate);
   }

   @Override
   public String toString() {
       return "Depreciating" + super.toString() + " Rate : " + getRate() * 100 + " % ";
   }

   public float handleCalim() {
       float amt = amount;
       depreciate();
       return amt;
   }

}

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 amt) {
       super(amt);
       GregorianCalendar aCalendar = new GregorianCalendar();
       aCalendar.add(Calendar.YEAR, 1);
       this.expiryDate = aCalendar.getTime();
   }

   public ExpiringPolicy(float amt, Date expiryDate) {
       super(amt);
       this.expiryDate = expiryDate;
   }

   public Date getExpiryDate() {
       return expiryDate;
   }

   public boolean isExpired()
   {
       Date now=new Date();
       return expiryDate.before(now);
   }
   @Override
   public String toString() {
       boolean isExpired=isExpired();
       String s=isExpired?" expired":" expires";
       return "Expiring"+super.toString()+s+" on "+expiryDate;
   }

}

Client .java

import java.util.*;

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;
   }

   public String toString() {

       String s = null;
       if (this instanceof IndividualClient) {
           s = "Individual";
       }
       if (this instanceof CompanyClient) {
           s = "Company";
       }
       return String.format(s + "Client: %06d amount: %s", id, name);
   }

   // totalCoverage method
   public float totalCoverage() {
       float amount = 0;
       for (Policy policy : policies) {
           if(policy!=null)
           amount += policy.getAmount();
       }
       return amount;

   }

   // add policy
   public Policy add(Policy p) {
       boolean added = false;
       // find first available position in array and put at that position
       for (int i = 0; i < MAX_POLICIES_PER_CLIENT; i++) {
           if (policies[i] == null) {
               policies[i] = p;
               added = true;
               numPolicies++;
               break;
           }

       }
       if (added) {
           return p;
       }
       return null;
   }

   public void openPolicyFor(float amt) {
       Policy p = new Policy(amt);
       add(p);
   }

   public void openPolicyFor(float amt, float rate) {
       DepreciatingPolicy p = new DepreciatingPolicy(amt, rate);
       add(p);
   }

   // openPolicyFor
   public void openPolicyFor(float amt, Date expiryDate) {
       ExpiringPolicy p = new ExpiringPolicy(amt, expiryDate);
       add(p);
   }

   // getPolicy
   public Policy getPolicy(int polNum) {
       for (Policy policy : policies) {
           if (policy.getPolicyNumber() == polNum) {
               return policy;
           }
       }
       return null;
   }

   // cancelPolicy
   public boolean cancelPolicy(int polNum) {
       // find first available position in array and put at that position
       for (int i = 0; i < MAX_POLICIES_PER_CLIENT; i++) {
           if (policies[i]!=null && policies[i].getPolicyNumber() == polNum) {
               policies[i] = null;
               return true;
           }
       }
       return false;
   }

   // abstract method
   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[] policies=this.getPolicies();
       for (Policy policy : policies) {
           if(policy.isExpired())
           {
               return 0;  
           }
           else if(policy.getPolicyNumber()==polNum)
           {
               if(policy instanceof DepreciatingPolicy)
               {
                   ((DepreciatingPolicy) policy).depreciate();
                   return ((DepreciatingPolicy) policy).getAmount();
               }
           }
       }
       return 0;
   }

}

CompanyClient .java


public class CompanyClient extends Client{

   public CompanyClient(String n) {
       super(n);
   }

   @Override
   public float makeClaim(int polNum) {
       Policy [] policeis=this.getPolicies();
       for (Policy policy : policeis) {
           if(policy!=null && policy.getPolicyNumber()==polNum)
           {
               return policy.handleCalim();
           }
       }
       return polNum;
   }

}

ClientTester .java

import java.util.GregorianCalendar;

public class ClientTester {
    public static void main(String args[]) {
        // Create an individual client, open some policies and then make some claims
        IndividualClient ic = new IndividualClient("Bob B. Pins");
        ic.openPolicyFor(100);
        ic.openPolicyFor(200, 0.10f);
        ic.openPolicyFor(300, new GregorianCalendar(2020, 0, 2, 23, 59).getTime());
        ic.openPolicyFor(400, new GregorianCalendar(2009, 5, 4, 12, 00).getTime());
        Policy p = new Policy(500);
        System.out.println("Here are the Individual Client's policies:");
        for (int i=0; i<ic.getNumPolicies(); i++)
            System.out.println(" " + ic.getPolicies()[i]);

        System.out.println("Making claims:");
        System.out.println(String.format(" Claim for policy 0001: $%6.2f",ic.makeClaim(1)));
        System.out.println(String.format(" Claim for policy 0002: $%6.2f",ic.makeClaim(2)));
        System.out.println(String.format(" Claim for policy 0003: $%6.2f",ic.makeClaim(3)));
        System.out.println(String.format(" Claim for policy 0004: $%6.2f",ic.makeClaim(4)));
        System.out.println(String.format(" Claim for policy 0005: $%6.2f",ic.makeClaim(5)));
        System.out.println("Here are the Individual Client's policies after claims:");
        for (int i=0; i<ic.getNumPolicies(); i++)
            System.out.println(" " + ic.getPolicies()[i]);
        System.out.println(String.format("The total policy coverage for this client: $%1.2f",ic.totalCoverage()));

        // Create a company client, open some policies and then make some claims
        CompanyClient cc = new CompanyClient("The Pillow Factory");
        cc.openPolicyFor(1000);
        cc.openPolicyFor(2000, 0.10f);
        cc.openPolicyFor(3000, new GregorianCalendar(2020, 0, 2, 23, 59).getTime());
        cc.openPolicyFor(4000, new GregorianCalendar(2009, 5, 4, 12, 00).getTime());
        System.out.println(" Here are the Company Client's policies:");
        for (int i=0; i<cc.getNumPolicies(); i++)
            System.out.println(" " + cc.getPolicies()[i]);

        System.out.println("Making claims:");
        System.out.println(String.format(" Claim for policy 0006: $%7.2f",cc.makeClaim(6)));
        System.out.println(String.format(" Claim for policy 0007: $%7.2f",cc.makeClaim(7)));
        System.out.println(String.format(" Claim for policy 0008: $%7.2f",cc.makeClaim(8)));
        System.out.println(String.format(" Claim for policy 0009: $%7.2f",cc.makeClaim(9)));
        System.out.println(String.format(" Claim for policy 0005: $%7.2f",cc.makeClaim(5)));

        System.out.println("Here are the Company Client's policies after claims:");
        for (int i=0; i<cc.getNumPolicies(); i++)
            System.out.println(" " + cc.getPolicies()[i]);
        System.out.println(String.format("The total policy coverage for this client: $%1.2f",cc.totalCoverage()));
        System.out.println("Cancelling policy #12 ... did it work: " + cc.cancelPolicy(12));
        System.out.println("Cancelling policy #8 ... did it work: " + cc.cancelPolicy(8));
        System.out.println(String.format("The total policy coverage for this client: $%1.2f",cc.totalCoverage()));
    }
}

Output

Here are the Individual Client's policies:
Policy: 0001 amount: $100.00
DepreciatingPolicy: 0002 amount: $200.00 Rate : 10.0 %
ExpiringPolicy: 0003 amount: $300.00 expires on Thu Jan 02 23:59:00 IST 2020
ExpiringPolicy: 0004 amount: $400.00 expired on Thu Jun 04 12:00:00 IST 2009
Making claims:
Claim for policy 0001: $ 0.00
Claim for policy 0002: $180.00
Claim for policy 0003: $ 0.00
Claim for policy 0004: $ 0.00
Claim for policy 0005: $ 0.00
Here are the Individual Client's policies after claims:
Policy: 0001 amount: $100.00
DepreciatingPolicy: 0002 amount: $180.00 Rate : 10.0 %
ExpiringPolicy: 0003 amount: $300.00 expires on Thu Jan 02 23:59:00 IST 2020
ExpiringPolicy: 0004 amount: $400.00 expired on Thu Jun 04 12:00:00 IST 2009
The total policy coverage for this client: $980.00

Here are the Company Client's policies:
Policy: 0006 amount: $1000.00
DepreciatingPolicy: 0007 amount: $2000.00 Rate : 10.0 %
ExpiringPolicy: 0008 amount: $3000.00 expires on Thu Jan 02 23:59:00 IST 2020
ExpiringPolicy: 0009 amount: $4000.00 expired on Thu Jun 04 12:00:00 IST 2009
Making claims:
Claim for policy 0006: $1000.00
Claim for policy 0007: $2000.00
Claim for policy 0008: $3000.00
Claim for policy 0009: $4000.00
Claim for policy 0005: $   5.00
Here are the Company Client's policies after claims:
Policy: 0006 amount: $1000.00
DepreciatingPolicy: 0007 amount: $1800.00 Rate : 10.0 %
ExpiringPolicy: 0008 amount: $3000.00 expires on Thu Jan 02 23:59:00 IST 2020
ExpiringPolicy: 0009 amount: $4000.00 expired on Thu Jun 04 12:00:00 IST 2009
The total policy coverage for this client: $9800.00
Cancelling policy #12 ... did it work: false
Cancelling policy #8 ... did it work: true
The total policy coverage for this client: $6800.00

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote