-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmartCard.java
139 lines (125 loc) · 4.02 KB
/
SmartCard.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.B5015845.CSC8002.Coursework;
import java.util.Calendar;
//import java.util.Calendar;
import java.util.Date;
/**
* The SmartCard class create unique smart cards. It stores and can retrieve components of the smart card, for example student name,
* date of birth, the smart card number, the student's ID, the issue date and the expiry date of the card.
* This class does not implement the factory design pattern because the smart card numbers are all unique, therefore each card created
* will also be unique as a result of this.
* @author Arianna Esposito
*/
public final class SmartCard {
private final Name studentName;
private final Date dob;
private final SmartCardNumber smartCardNumber;
private final StudentID sid;
private final Date issueDate;
private Date expiryDate;
//private static final Map<String, SmartCard> SMARTCARDS = new HashMap<String, SmartCard>();
/**
* Constructs and initialises the objects of the smart card.
* @param studentName
* @param dob
* @param sid
* @param studentType
*/
public SmartCard (Name studentName, Date dob, StudentID sid, StudentType studentType)
{
this.studentName = new Name(studentName.getFirstName(), studentName.getLastName());
this.dob = new Date(dob.getTime());
this.smartCardNumber = SmartCardNumber.getInstance(studentName);
this.issueDate = new Date();
this.sid = sid;
this.expiryDate = setExpiryDate(studentType);
}
/**
* Retrieves a defensive copy of the student's name.
* @return the student's name.
*/
public Name getStudentName()
{
return new Name(studentName.getFirstName(), studentName.getLastName());
}
/**
* Retrieves a defensive copy of the student's date of birth.
* @return the student's date of birth.
*/
public Date getDob()
{
return new Date(dob.getTime());
}
/**
* Retrieves the smart card number generated in the constructor.
* @return the smart card number.
*/
public SmartCardNumber getSmartCardNumber()
{
return smartCardNumber;
}
/**
* Retrieves the unique student ID generated in the constructor.
* @return the smart card number.
*/
public StudentID getStudentID()
{
return sid;
}
/**
* Retrieves a defensive copy of the issue date of the smart card.
* @return the smart card issue date.
*/
public Date getIssueDate()
{
return new Date(issueDate.getTime());
}
/**
* Private method to set and retrieve defensive copy of the expiry date for the smart card based on the student type.
* Code adapted from: https://mkyong.com/java/java-how-to-add-days-to-current-date/
* @return the smart card expiry date (privately).
*/
private Date setExpiryDate(StudentType studentType) throws IllegalArgumentException
{
Calendar cal = Calendar.getInstance();
cal.setTime(issueDate);
if (studentType == StudentType.UG)
{
cal.add(Calendar.YEAR, 4);
this.expiryDate = cal.getTime();
return new Date(expiryDate.getTime());
}
else if (studentType == StudentType.PGT)
{
cal.add(Calendar.YEAR, 2);
this.expiryDate = cal.getTime();
return new Date(expiryDate.getTime());
}
else if (studentType == StudentType.PGR)
{
cal.add(Calendar.YEAR, 5);
this.expiryDate = cal.getTime();
return new Date(expiryDate.getTime());
}
else
{
throw new IllegalArgumentException("This student's student type is not valid.");
}
}
/**
* Retrieves a defensive copy of the smart card expiry date that's accessible outside the smart card class.
* @return the smart card expiry date
*/
public Date getExpiryDate()
{
return new Date(expiryDate.getTime());
}
/**
* @Override
* @return the string representation of the smart card when printed.
*/
@Override
public String toString()
{
return "SmartCard: " + studentName.toString() + ", " + dob.toString() + ", " + smartCardNumber.toString() + ", " + issueDate.toString();
}
}