-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReservation.java
More file actions
182 lines (158 loc) · 5.22 KB
/
Copy pathReservation.java
File metadata and controls
182 lines (158 loc) · 5.22 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/**
* Represents a Reservation object that customers can make.
* Contains the essential details of a reservation.
* @author Beh Ming Jun
* @version 10
* @since 2021-11-13
*/
public class Reservation {
/**
* The date and Arrival time of the Reservation.
*/
private Date dateAndArrivalTime;
/**
* The number of pax per Reservation.
*/
private int numPax;
/**
* The name of the person who booked the Reservation.
*/
private String name;
/**
* The contact of the perso who booked the Reservation.
*/
private int contact;
/**
* Boolean attribute to check if the Reservation has expired.
*/
private boolean isExpired;
/**
* Date object to get the date and time that the Reservation expires.
*/
private Date expiryDateTime;
/**
* Creates a new Reservation with the given contact
* date and arrival time, number of pax and name of
* person who made the reservation.
* @param contact This Reservation's contact number (person who booked the Reservation).
* @param dateAndArrivalTime This Reservation's date and booking time.
* @param numPax This Reservation's number of pax.
* @param name This Reservation's name (person who booked the Reservation).
*/
public Reservation(int contact, String dateAndArrivalTime, int numPax, String name) {
// TODO - implement Reservations.Reservations
this.contact = contact;
this.dateAndArrivalTime = convertToDate(dateAndArrivalTime);
this.numPax = numPax;
this.name = name;
this.expiryDateTime = convertExpiryDateTime(this.dateAndArrivalTime);
//throw new UnsupportedOperationException();
}
/**
* Get the current date and time and check if the Reservation has expired.
* Reservation is considered expired if it has been more than 15
* minutes past the Reservation date and time.
* @return true if Reservation has expired.
*/
public boolean getIsExpired() {
//SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm");
Date curDateTime = new Date();
//curDateTime = formatter.format(curDateTime);
long minutesDiff = getTimeDiff(this.dateAndArrivalTime, curDateTime, TimeUnit.MINUTES);
if (minutesDiff > 15) {
this.isExpired = true;
}
else {
this.isExpired = false;
}
return this.isExpired;
}
/**
* Get the time difference between 2 Date objects in minutes.
* @param date1 First date to be compared.
* @param date2 Second date to be compared.
* @param timeUnit The timeunit that will be returned Eg. MINUTES.
* @return The time difference in minutes.
*/
public long getTimeDiff(Date date1, Date date2, TimeUnit timeUnit) {
long diffInMillies = date2.getTime() - date1.getTime();
return timeUnit.convert(diffInMillies,TimeUnit.MILLISECONDS);
}
/**
* Converts a String in the format "dd-MM-yyyy HH:mm" into a Date object.
* @param dateTime String to be converted into Date object.
* @return The date object after conversion.
*/
public Date convertToDate(String dateTime) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm");
try {
date = sdf.parse(dateTime);
return date;
}catch(ParseException e){
System.out.println("Incorrect date time format.");
System.out.println("program exiting...");
System.exit(0);
}
return date;
}
/**
* Adds 15minutes to a Date object to get the expiry date and time of a Reservation.
* @param dateTime The date and time of the Reservation.
* @return The expiry date and time.
*/
public Date convertExpiryDateTime(Date dateTime) {
long timeInSecs = dateTime.getTime();
Date Add15Mins = new Date (timeInSecs + (15*60*1000));
return Add15Mins;
}
/**
* Given an contactNumber input, check if this Reservation's contact
* matches the input.
* @param contactNumber The contact number to check with Reservation's contact.
* @return true if the contact number matches.
*/
public boolean checkContact(int contactNumber){
boolean contactMatch=false;
if(this.contact==contactNumber){
contactMatch=true;
}
return contactMatch;
}
/**
* Prints the Reservation details (name, contact number,
* date and arrival time, number of pax).
*/
public void printReservation() {
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm");
System.out.println("Reservation Details for " + name + ":\r\n"
+ "Contact Number: " + contact + "\r\n"
+ "Date and Arrival Time: " + formatter.format(dateAndArrivalTime) + "\r\n"
+ "Number of Pax: " + numPax + "\r\n");
}
/**
* Get the expiry date and time of Reservation.
* @return The expiry date and time.
*/
public Date getExpiryDateTime() {
return this.expiryDateTime;
}
/**
* Get the contact number of the person who made the Reservation.
* @return Contact number of person.
*/
public int getContact() {
return this.contact;
}
/**
* Get the number of pax of the Reservation.
* @return The number of pax.
*/
public int getNumPax(){
return this.numPax;
}
}