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