-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReservationSystem.java
More file actions
293 lines (276 loc) · 9.47 KB
/
Copy pathReservationSystem.java
File metadata and controls
293 lines (276 loc) · 9.47 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.Double.parseDouble;
import static java.lang.Integer.parseInt;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
public class ReservationSystem extends Reservation {
List<Reservation> Reservations;
String fileName;
int id;
/**
* ReservationSystem constructor
* @param Reservations
*/
public ReservationSystem(List<Reservation> Reservations) {
this.Reservations = Reservations;
this.id = 1;
}
/**
* Method Name: addReservation
* Purpose: get user input to add a new reservation to the array of Reservations
* @param sc
*/
public void addReservation(Scanner sc) {
String LName;
String FName;
int nights;
double rate;
try {
System.out.println("Enter last name");
sc.nextLine();
LName = sc.nextLine();
System.out.println("Enter first name");
FName = sc.nextLine();
System.out.println("Enter number of nights");
nights = sc.nextInt();
System.out.println("Enter the daily rate");
rate = sc.nextDouble();
try {
addReservation(LName, FName, nights, rate);
}
catch (NullPointerException e) {
addReservation(LName, FName, nights, rate);
}
}
catch (InputMismatchException e) {
System.err.println("Not valid input, try again");
addReservation(sc);
}
}
/**
* Method Name: addReservation
* Purpose: Make a new reservation and add it to the array of reservations
* @param lName
* @param fName
* @param numOfNights
* @param dailyRate
*/
public void addReservation(String lName, String fName, int numOfNights, double dailyRate) {
int IDNum = this.id;
Reservation temp = new Reservation(IDNum, lName, fName, numOfNights, dailyRate);
Reservations.add(temp);
nextID();
}
/**
* Method Name: nextID
* Purpose: Take the most recent inputted ID and increase it by one
* @param IDNum
*/
public void nextID() {
this.id++;
}
/**
* Method Name: uploadReservations
* Purpose: Prompt user for file path and load records into Reservations array
* @param sc
*/
public void uploadReservations(Scanner sc) {
try {
String line = null;
try {
System.out.println("Enter file name for uploaded reservations EX.'C:\\Users\\username\\Desktop\\test.txt'");
sc.nextLine();
}
catch (InputMismatchException e) {
System.err.println("Not valid input, try again");
uploadReservations(sc);
}
String filePath = sc.nextLine();
this.fileName = filePath;
BufferedReader in = new BufferedReader(
new FileReader(filePath));
while ((line = in.readLine()) != null) {
int i;
String[] pieces = new String[5];
for (i = 0; i < 4; i++) {
pieces = line.split("\\s+");
}
String LName = pieces[i - 3];
String FName = pieces[i - 2];
int nights = parseInt(pieces[i - 1]);
double rate = parseDouble(pieces[i]);
addReservation(LName, FName, nights, rate);
}
System.out.println("File Uploaded");
}
catch (IOException e) {
System.err.println("IOException thrown");
}
}
/**
* Method Name: updateReservation
* Purpose: Get the ID of desired reservation and call findReservation
* @param sc
*/
public void updateReservation(Scanner sc) {
try {
System.out.println("Enter the ID of the reservation you would like to update");
sc.nextLine();
}
catch (InputMismatchException e) {
System.err.println("Not valid input, try again");
}
int searchID = sc.nextInt();
findReservation(searchID, sc);
System.out.println("Reservation Updated\n");
}
/**
* Method Name: findReservation
* Purpose: Loop through Reservation array to find the desired entry and update it
* @param searchID
* @param sc
*/
public void findReservation(int searchID, Scanner sc) {
int i = 0;
for (Reservation Reservation : Reservations) {
int n = Reservation.getID();
if (n == searchID) {
try {
System.out.println("Update Last Name: ");
sc.nextLine();
Reservation.lName = sc.nextLine();
System.out.println("Update First Name: ");
Reservation.fName = sc.nextLine();
System.out.println("Update Number of nights: ");
Reservation.numOfNights = sc.nextInt();
System.out.println("Update Daily Rate: ");
Reservation.dailyRate = sc.nextDouble();
}
catch (InputMismatchException e) {
System.err.println("Not valid input, try again");
}
}
i++;
if (i > Reservations.size()) {
System.err.println("Entry not found");
}
}
}
/**
* Method Name: findReservation
* Purpose: Get the name of the reservation holder and find that reservation in the array
*/
public void findReservation() {
Scanner sc = new Scanner(System.in);
try {
System.out.println("Enter the last name of the reservation holder ");
String LName = sc.nextLine();
System.out.println("Enter the first name of the reservation holder ");
String FName = sc.nextLine();
for (Reservation Reservation : Reservations) {
if (Reservation.lName.equalsIgnoreCase(LName) && Reservation.fName.equalsIgnoreCase(FName)) {
print(Reservation);
return;
}
}
}
catch (InputMismatchException e) {
System.err.println("Not valid input, try again");
}
System.err.println("No Reservation found under that name");
}
/**
* Method Name: downloadReservations
* Purpose: Download the Reservation array to a file along with other statistics
*/
public void downloadReservations() {
createStatsFile();
try {
File reservationFile = new File(this.fileName);
reservationFile.createNewFile();
PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter(reservationFile, true))
, true);
for (int i = 0; i <= Reservations.size() - 1; i++) {
String record = printToFile(Reservations.get(i));
out.print(record);
out.print(" Total Cost: $");
out.printf("%.2f", getTotalCost(Reservations.get(i)));
out.println("");
}
out.println("Longest Stay: " + getLongestStay() + " nights");
System.out.println("File Downloaded");
out.close();
}
catch (IOException e) {
System.err.println("File not found. Try again");
downloadReservations();
}
}
/**
* Method Name: createStatsFile
* Purpose: Create the statistics file
*/
public void createStatsFile() {
int index = this.fileName.length() - 4;
this.fileName = this.fileName.substring(0, index);
this.fileName += ("_stats.txt");
File reservationFile = new File(this.fileName);
try {
reservationFile.createNewFile();
}
catch (IOException e) {
System.err.println("File Not Created");
downloadReservations();
}
}
/**
* Method Name:getTotalCost
* Purpose: get the total cost for each Reservation
* @param temp
* @return Total Cost
*/
public double getTotalCost(Reservation temp) {
return temp.dailyRate * temp.numOfNights;
}
/**
* Method Name: getLongestSTay
* Purpose: Find the Reservation with the largest numOfNights
* @return longestStay
*/
public int getLongestStay() {
int longestStay = 0;
for (Reservation Reservation : Reservations) {
if (Reservation.numOfNights > longestStay) {
longestStay = Reservation.numOfNights;
}
}
return longestStay;
}
/**
* Method Name: viewReservations
* Purpose: Loop through Reservation array and call the print method for each entry
*/
public void viewReservations() {
for (int i = 0; i <= Reservations.size() - 1; i++) {
print(Reservations.get(i));
}
}
/**
* Method Name: getID
* Purpose: Returns the ID number of the Reservation
* @return ID
*/
@Override
public int getID() {
return this.ID;
}
}