In Java POLICY CLASS POLICY TESTER CLIENT CLASS CLIENT TESTER CLASS (1) Policy C
ID: 3722868 • Letter: I
Question
In Java
POLICY CLASS
POLICY TESTER
CLIENT CLASS
CLIENT TESTER CLASS
(1) Policy Classes We will be creating this class hierarchy: To begin, copy the code from the following Policy class Object Policy public class Policy ( rivate static int NEXT POLICY NUMBER1 DepreciatingPolicyExpiringPolicy private int protected float amount; Poli public Policy (float amt) amount = amt; policyNumber = NEXT POLICY NUMBER++ ; public int getPolicyNumber return policyNumber: public float getAmounto return amount: ) public String toString)t return string . format("Policy : %04d amount: $%1.2f", policyNumber, amount); public boolean isExpired) return false Now define a DepreciatingPolicy class as a subclass of Policy which has the following: a private instance variable called rate of type float which represents the rate at which the policy depreciates each time a claim is made. For example, a rate of 0.10 means that the value of the policy (i.e., the amount) depreciates 10% each time a claim is made (we will discuss the making of claims in the next section).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);
}
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;
}
}
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;
}
}
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
//please walkthrough code once .
//There may be some error , due to time constraint i could not reverify .
Thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.