Skip to content

Commit d46e6a2

Browse files
author
Paul Rayner
committed
Update with refactored sample files.
1 parent 0a93782 commit d46e6a2

9 files changed

+399
-75
lines changed

src/warranty/Claim.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package warranty;
22

3-
import java.util.ArrayList;
43
import java.util.Date;
5-
import java.util.List;
64

75
public class Claim {
86

97
public Claim(int id, double amount, Date date) {
108
super();
11-
this.id = id;
129
this.amount = amount;
1310
this.date = date;
1411
}
1512
public int id;
1613
public double amount;
1714
public Date date = new Date();
18-
public List<RepairPO> repairPO = new ArrayList<RepairPO>();
15+
public ProductReplacementEvent productReplacement;
16+
public CustomerReimbursementEvent customerReimbursement;
1917
}

src/warranty/ClaimsAdjudicationService.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
package warranty;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
5-
63
import warranty.Contract.Status;
74

85
public class ClaimsAdjudicationService {
96

107
public void Adjudicate(Contract contract, Claim newClaim) {
11-
double claimTotal = 0;
12-
List<Claim> claims = new ArrayList<Claim>();
13-
claims.addAll(contract.getClaims());
14-
15-
for (Claim claim:claims)
16-
{
17-
claimTotal += claim.amount;
18-
}
198

20-
if (((contract.purchasePrice - claimTotal) * 0.8 > newClaim.amount) &&
21-
(newClaim.date.compareTo(contract.effectiveDate) >= 0) &&
22-
(newClaim.date.compareTo(contract.expirationDate) <= 0) &&
23-
(contract.status == Status.ACTIVE)) {
9+
if ((newClaim.amount < contract.limitOfLiability() &&
10+
contract.status(newClaim.date) == Status.ACTIVE)) {
11+
2412
contract.add(newClaim);
2513
}
2614
}

src/warranty/Contract.java

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package warranty;
22

3+
import java.text.ParseException;
34
import java.util.ArrayList;
45
import java.util.Date;
56
import java.util.List;
@@ -8,21 +9,34 @@
89

910
public class Contract {
1011

11-
public Contract(int id, double d) {
12-
this.purchasePrice = d;
13-
this.status = Status.PENDING;
12+
public Contract(int id, double purchasePrice, TermsAndConditions termsAndConditions) {
13+
this.id = id;
14+
this.purchasePrice = purchasePrice;
15+
this.termsAndConditions = termsAndConditions;
1416
}
1517

16-
public int id;
18+
public final int id;
1719
public double purchasePrice;
18-
public Date effectiveDate = new Date();
19-
public Date expirationDate = new Date();
20-
public Date purchaseDate = new Date();
21-
public int inStoreGuaranteeDays;
22-
public Status status;
23-
public Product product;
20+
public TermsAndConditions termsAndConditions;
21+
22+
public Status status(Date date)
23+
{
24+
if (termsAndConditions.isActive(date))
25+
{
26+
return Status.ACTIVE;
27+
}
28+
if (termsAndConditions.isPending(date))
29+
{
30+
return Status.PENDING;
31+
}
32+
if (termsAndConditions.isExpired(date))
33+
{
34+
return Status.EXPIRED;
35+
}
36+
return Status.INVALID;
37+
}
2438

25-
public enum Status { PENDING, ACTIVE, EXPIRED }
39+
public enum Status { PENDING, ACTIVE, EXPIRED, INVALID }
2640

2741
private List<Claim> Claims = new ArrayList<Claim>();
2842

@@ -40,4 +54,23 @@ public void remove(Claim Claim)
4054
{
4155
Claims.remove(Claim);
4256
}
57+
58+
public void extendAnnualSubscription() throws ParseException
59+
{
60+
this.termsAndConditions = this.termsAndConditions.annuallyExtended();
61+
}
62+
63+
public double limitOfLiability()
64+
{
65+
double claimTotal = 0;
66+
List<Claim> claims = new ArrayList<Claim>();
67+
claims.addAll(this.getClaims());
68+
69+
for (Claim claim:claims)
70+
{
71+
claimTotal += claim.amount;
72+
}
73+
74+
return (this.purchasePrice - claimTotal) * 0.8;
75+
}
4376
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package warranty;
2+
import java.util.Date;
3+
4+
public class CustomerReimbursementEvent {
5+
public final Date dateOfReimbursement;
6+
public final String reason;
7+
public final double amount;
8+
9+
public CustomerReimbursementEvent(Date dateOfReimbursement, String reason,
10+
double amount) {
11+
super();
12+
this.dateOfReimbursement = dateOfReimbursement;
13+
this.reason = reason;
14+
this.amount = amount;
15+
}
16+
17+
@Override
18+
public int hashCode() {
19+
final int prime = 31;
20+
int result = 1;
21+
long temp;
22+
temp = Double.doubleToLongBits(amount);
23+
result = prime * result + (int) (temp ^ (temp >>> 32));
24+
result = prime
25+
* result
26+
+ ((dateOfReimbursement == null) ? 0 : dateOfReimbursement
27+
.hashCode());
28+
result = prime * result + ((reason == null) ? 0 : reason.hashCode());
29+
return result;
30+
}
31+
32+
@Override
33+
public boolean equals(Object obj) {
34+
if (this == obj)
35+
return true;
36+
if (obj == null)
37+
return false;
38+
if (getClass() != obj.getClass())
39+
return false;
40+
CustomerReimbursementEvent other = (CustomerReimbursementEvent) obj;
41+
if (Double.doubleToLongBits(amount) != Double
42+
.doubleToLongBits(other.amount))
43+
return false;
44+
if (dateOfReimbursement == null) {
45+
if (other.dateOfReimbursement != null)
46+
return false;
47+
} else if (!dateOfReimbursement.equals(other.dateOfReimbursement))
48+
return false;
49+
if (reason == null) {
50+
if (other.reason != null)
51+
return false;
52+
} else if (!reason.equals(other.reason))
53+
return false;
54+
return true;
55+
}
56+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package warranty;
2+
import java.util.Date;
3+
4+
public class ProductReplacementEvent {
5+
public final Date replacementDate;
6+
public final String reason;
7+
8+
public ProductReplacementEvent(Date replacementDate, String reason) {
9+
super();
10+
this.replacementDate = replacementDate;
11+
this.reason = reason;
12+
}
13+
14+
@Override
15+
public int hashCode() {
16+
final int prime = 31;
17+
int result = 1;
18+
result = prime * result + ((reason == null) ? 0 : reason.hashCode());
19+
result = prime * result
20+
+ ((replacementDate == null) ? 0 : replacementDate.hashCode());
21+
return result;
22+
}
23+
24+
@Override
25+
public boolean equals(Object obj) {
26+
if (this == obj)
27+
return true;
28+
if (obj == null)
29+
return false;
30+
if (getClass() != obj.getClass())
31+
return false;
32+
ProductReplacementEvent other = (ProductReplacementEvent) obj;
33+
if (reason == null) {
34+
if (other.reason != null)
35+
return false;
36+
} else if (!reason.equals(other.reason))
37+
return false;
38+
if (replacementDate == null) {
39+
if (other.replacementDate != null)
40+
return false;
41+
} else if (!replacementDate.equals(other.replacementDate))
42+
return false;
43+
return true;
44+
}
45+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package warranty;
2+
3+
import java.text.ParseException;
4+
import java.text.SimpleDateFormat;
5+
import java.util.Calendar;
6+
import java.util.Date;
7+
8+
public class TermsAndConditions {
9+
10+
public TermsAndConditions(Date effectiveDate, Date expirationDate,
11+
Date purchaseDate, int inStoreGuaranteeDays) {
12+
super();
13+
this.effectiveDate = effectiveDate;
14+
this.expirationDate = expirationDate;
15+
this.purchaseDate = purchaseDate;
16+
this.inStoreGuaranteeDays = inStoreGuaranteeDays;
17+
}
18+
19+
@Override
20+
public int hashCode() {
21+
final int prime = 31;
22+
int result = 1;
23+
result = prime * result
24+
+ ((effectiveDate == null) ? 0 : effectiveDate.hashCode());
25+
result = prime * result
26+
+ ((expirationDate == null) ? 0 : expirationDate.hashCode());
27+
result = prime * result + inStoreGuaranteeDays;
28+
result = prime * result
29+
+ ((purchaseDate() == null) ? 0 : purchaseDate().hashCode());
30+
return result;
31+
}
32+
33+
@Override
34+
public boolean equals(Object obj) {
35+
if (this == obj)
36+
return true;
37+
if (obj == null)
38+
return false;
39+
if (getClass() != obj.getClass())
40+
return false;
41+
TermsAndConditions other = (TermsAndConditions) obj;
42+
if (effectiveDate == null) {
43+
if (other.effectiveDate != null)
44+
return false;
45+
} else if (!effectiveDate.equals(other.effectiveDate))
46+
return false;
47+
if (expirationDate == null) {
48+
if (other.expirationDate != null)
49+
return false;
50+
} else if (!expirationDate.equals(other.expirationDate))
51+
return false;
52+
if (inStoreGuaranteeDays != other.inStoreGuaranteeDays)
53+
return false;
54+
if (purchaseDate() == null) {
55+
if (other.purchaseDate() != null)
56+
return false;
57+
} else if (!purchaseDate().equals(other.purchaseDate()))
58+
return false;
59+
return true;
60+
}
61+
62+
private Date effectiveDate;
63+
private Date expirationDate;
64+
private Date purchaseDate;
65+
private int inStoreGuaranteeDays;
66+
67+
public int inStoreGuaranteeDays() {
68+
return inStoreGuaranteeDays;
69+
}
70+
public Date purchaseDate() {
71+
return purchaseDate;
72+
}
73+
public Date effectiveDate() {
74+
return effectiveDate;
75+
}
76+
public Date ExpirationDate() {
77+
return expirationDate;
78+
}
79+
80+
public boolean isPending(Date date)
81+
{
82+
return (date.compareTo(effectiveDate) < 0);
83+
}
84+
85+
public boolean isExpired(Date date)
86+
{
87+
return (date.compareTo(expirationDate) > 0);
88+
}
89+
90+
public boolean isActive(Date date)
91+
{
92+
return (date.compareTo(effectiveDate) >= 0) &&
93+
(date.compareTo(expirationDate) <= 0);
94+
}
95+
96+
public TermsAndConditions annuallyExtended() throws ParseException
97+
{
98+
return new TermsAndConditions(this.effectiveDate,
99+
increaseDateByOneYear(expirationDate),
100+
this.purchaseDate,
101+
this.inStoreGuaranteeDays);
102+
}
103+
104+
private Date increaseDateByOneYear(Date date) throws ParseException {
105+
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
106+
Calendar cal = Calendar.getInstance();
107+
cal.setTime(expirationDate);
108+
cal.add(Calendar.YEAR, 1); // Add 1 year to current date
109+
String newDate = dateFormat.format(cal.getTime());
110+
return dateFormat.parse(newDate);
111+
}
112+
}

0 commit comments

Comments
 (0)