11package warranty ;
22
3- import java .text .ParseException ;
4- import java .text .SimpleDateFormat ;
53import java .util .Calendar ;
6- import java .util .Date ;
4+
5+ import warranty .util .ImmutableDate ;
76
87public 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