Skip to content

Commit 04c0088

Browse files
author
Paul Rayner
authored
Merge pull request #1 from biju74nair/refactored
Refactored to use Immutable Date
2 parents d46e6a2 + f12ff7a commit 04c0088

File tree

9 files changed

+218
-190
lines changed

9 files changed

+218
-190
lines changed

src/warranty/Claim.java

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

3-
import java.util.Date;
3+
import warranty.util.ImmutableDate;
44

55
public class Claim {
66

7-
public Claim(int id, double amount, Date date) {
7+
public Claim(int id, double amount, ImmutableDate date) {
88
super();
99
this.amount = amount;
1010
this.date = date;
1111
}
1212
public int id;
1313
public double amount;
14-
public Date date = new Date();
14+
public ImmutableDate date;
1515
public ProductReplacementEvent productReplacement;
1616
public CustomerReimbursementEvent customerReimbursement;
1717
}
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
package warranty;
22

3-
import warranty.Contract.Status;
4-
53
public class ClaimsAdjudicationService {
64

75
public void Adjudicate(Contract contract, Claim newClaim) {
8-
9-
if ((newClaim.amount < contract.limitOfLiability() &&
10-
contract.status(newClaim.date) == Status.ACTIVE)) {
11-
12-
contract.add(newClaim);
13-
}
6+
contract.add(newClaim);
147
}
158
}

src/warranty/Contract.java

Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,58 @@
11
package warranty;
22

3-
import java.text.ParseException;
43
import java.util.ArrayList;
5-
import java.util.Date;
4+
import java.util.Collections;
65
import java.util.List;
76

8-
import warranty.Claim;
9-
107
public class Contract {
118

12-
public Contract(int id, double purchasePrice, TermsAndConditions termsAndConditions) {
9+
private final int id;
10+
public final double purchasePrice;
11+
private TermsAndConditions termsAndConditions;
12+
private List<Claim> claims;
13+
14+
public Contract(int id, double purchasePrice,
15+
TermsAndConditions termsAndConditions) {
1316
this.id = id;
1417
this.purchasePrice = purchasePrice;
1518
this.termsAndConditions = termsAndConditions;
19+
this.claims = new ArrayList<Claim>();
1620
}
1721

18-
public final int id;
19-
public double purchasePrice;
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;
22+
public void add(Claim newClaim) {
23+
if (newClaim.amount < limitOfLiability()
24+
&& this.termsAndConditions.isActive(newClaim.date)) {
25+
claims.add(newClaim);
26+
} else {
27+
throw new ContractException(
28+
"Contract is not active or amount is less than LOL");
3529
}
36-
return Status.INVALID;
3730
}
38-
39-
public enum Status { PENDING, ACTIVE, EXPIRED, INVALID }
40-
41-
private List<Claim> Claims = new ArrayList<Claim>();
4231

43-
public void add(Claim Claim)
44-
{
45-
Claims.add(Claim);
46-
}
32+
public List<Claim> getClaims() {
33+
return Collections.unmodifiableList(claims);
34+
}
35+
36+
public void remove(Claim Claim) {
37+
claims.remove(Claim);
38+
}
39+
40+
public void extendAnnualSubscription() {
41+
this.termsAndConditions = this.termsAndConditions.annuallyExtended();
42+
}
4743

48-
public List<Claim> getClaims()
49-
{
50-
return Claims;
51-
}
44+
public TermsAndConditions termsAndConditions() {
45+
return termsAndConditions;
46+
}
5247

53-
public void remove(Claim Claim)
54-
{
55-
Claims.remove(Claim);
56-
}
57-
58-
public void extendAnnualSubscription() throws ParseException
59-
{
60-
this.termsAndConditions = this.termsAndConditions.annuallyExtended();
61-
}
62-
63-
public double limitOfLiability()
64-
{
48+
public double limitOfLiability() {
6549
double claimTotal = 0;
66-
List<Claim> claims = new ArrayList<Claim>();
67-
claims.addAll(this.getClaims());
68-
69-
for (Claim claim:claims)
70-
{
50+
51+
for (Claim claim : getClaims()) {
7152
claimTotal += claim.amount;
7253
}
73-
54+
7455
return (this.purchasePrice - claimTotal) * 0.8;
75-
}
56+
}
57+
7658
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package warranty;
2+
3+
public class ContractException extends RuntimeException {
4+
public ContractException(String message){
5+
super(message);
6+
}
7+
}
Lines changed: 34 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,44 @@
11
package warranty;
22

3-
import java.text.ParseException;
4-
import java.text.SimpleDateFormat;
53
import java.util.Calendar;
6-
import java.util.Date;
4+
5+
import warranty.util.ImmutableDate;
76

87
public class TermsAndConditions {
98

10-
public TermsAndConditions(Date effectiveDate, Date expirationDate,
11-
Date purchaseDate, int inStoreGuaranteeDays) {
9+
public final ImmutableDate effectiveDate;
10+
public final ImmutableDate expirationDate;
11+
public final ImmutableDate purchaseDate;
12+
public final int inStoreGuaranteeDays;
13+
14+
public TermsAndConditions(ImmutableDate effectiveDate,
15+
ImmutableDate expirationDate, ImmutableDate purchaseDate,
16+
int inStoreGuaranteeDays) {
1217
super();
1318
this.effectiveDate = effectiveDate;
1419
this.expirationDate = expirationDate;
1520
this.purchaseDate = purchaseDate;
1621
this.inStoreGuaranteeDays = inStoreGuaranteeDays;
1722
}
18-
23+
24+
public boolean isPending(ImmutableDate date) {
25+
return (date.compareTo(effectiveDate) < 0);
26+
}
27+
28+
public boolean isExpired(ImmutableDate date) {
29+
return (date.compareTo(expirationDate) > 0);
30+
}
31+
32+
public boolean isActive(ImmutableDate date) {
33+
return (date.compareTo(effectiveDate) >= 0)
34+
&& (date.compareTo(expirationDate) <= 0);
35+
}
36+
37+
public TermsAndConditions annuallyExtended() {
38+
return new TermsAndConditions(this.effectiveDate, expirationDate.add(
39+
Calendar.YEAR, 1), this.purchaseDate, this.inStoreGuaranteeDays);
40+
}
41+
1942
@Override
2043
public int hashCode() {
2144
final int prime = 31;
@@ -26,10 +49,10 @@ public int hashCode() {
2649
+ ((expirationDate == null) ? 0 : expirationDate.hashCode());
2750
result = prime * result + inStoreGuaranteeDays;
2851
result = prime * result
29-
+ ((purchaseDate() == null) ? 0 : purchaseDate().hashCode());
52+
+ ((purchaseDate == null) ? 0 : purchaseDate.hashCode());
3053
return result;
3154
}
32-
55+
3356
@Override
3457
public boolean equals(Object obj) {
3558
if (this == obj)
@@ -51,62 +74,12 @@ public boolean equals(Object obj) {
5174
return false;
5275
if (inStoreGuaranteeDays != other.inStoreGuaranteeDays)
5376
return false;
54-
if (purchaseDate() == null) {
55-
if (other.purchaseDate() != null)
77+
if (purchaseDate == null) {
78+
if (other.purchaseDate != null)
5679
return false;
57-
} else if (!purchaseDate().equals(other.purchaseDate()))
80+
} else if (!purchaseDate.equals(other.purchaseDate))
5881
return false;
5982
return true;
6083
}
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-
}
7984

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

0 commit comments

Comments
 (0)