-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicket.java
145 lines (136 loc) · 5.32 KB
/
Ticket.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
140
141
142
143
144
145
/**
* @file Ticket.java
*
* @author Malia Cherry
*
* @date Nov. 2022
**/
import java.text.SimpleDateFormat;
import java.util.*;
/**
* The Ticket class contains the methods to create a ticket for a flight.
*/
public class Ticket {
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("MMMM dd, yyyy");
String name, flightNum, ticketNum, seatNum;
ArrayList<Ticket> tickets = new ArrayList<Ticket>();
/**
* Default constructor for the Ticket class.
*
* @param none
*/
public Ticket() {
name = "";
flightNum = "";
ticketNum = "";
seatNum = "";
}
/**
* Overloaded constructor for the Ticket class used to create a ticket.
*
* @param user The name of the user
* @param flight The flight number
* @param ticket The ticket number
* @param seat The seat number
*/
public Ticket(String user, String flight, String ticket, String seat) {
name = user;
ticketNum = ticket;
flightNum = flight;
seatNum = seat;
}
/**
* Displays the information about the flights.
*
* @param none
*/
public void flightInfo() {
System.out.println("Available Flights:");
System.out.println("Green #100\t " + formatter.format(date) + "\nGSO - EWR | 6:00 AM - 7:30 AM\t(1 hr 30 min)\n");
System.out.println("Green #400\t " + formatter.format(date) + "\nGSO - SEA | 4:00 PM - 11:30 PM\t(10 hr 30 min)");
}
/**
* Finds the ticket in the array list from the ticket number.
* If found, the ticket is removed from the array list.
*
* @param num The ticket number
*/
public String returnTicket(String num) {
String seat = "";
for (int i = 0; i < tickets.size(); i++) {
if (num.equals(tickets.get(i).ticketNum)) {
seat = tickets.get(i).seatNum;
tickets.remove(i);
System.out.println("Ticket " + num + " has been cancelled.\n");
}
}
return seat;
}
/**
* Displays the ticket to the user after a flight is booked.
*
* @param seat The seat number
*/
public void confirmation(String seat) {
System.out.println("\n\n=====================================================");
System.out.println("Thank you for purchasing a ticket with Green Company!");
System.out.println("=====================================================");
int index = tickets.size() - 1;
if (tickets.get(index).seatNum.equals(seat)) {
System.out.println(tickets.get(index).name + "\nTicket: " + tickets.get(index).ticketNum);
System.out.println(
"Flight: Green #" + tickets.get(index).flightNum + "\tSeat: " + tickets.get(index).seatNum);
if (tickets.get(index).flightNum.equals("100")) {
System.out.println("\n" + formatter.format(date) + "\nGSO-EWR | 6:30 AM - 7:30 AM\t(1 hr 30 min)");
} else if (tickets.get(index).flightNum.equals("400")) {
System.out.println("\n" + formatter.format(date) + "\nGSO-SEA | 4:00 PM - 11:30 PM\t(10 hr 30 min)");
}
System.out.println("==================================================\n");
}
}
/**
* Adds the ticket to the array list.
*
* @param bookingInfo The array list of the ticket information
*/
public void generateTicket(Ticket bookingInfo) {
tickets.add(bookingInfo);
}
/**
* Finds the ticket in the array list from the ticket number and
* displays the ticket information to the user.
*
* @param num The ticket number
*/
public String getTicket(String num) {
// check if num is seat number or ticket number
// find ticket in tickets array that ticketNum matches num
// print ticket info\
num = num.toUpperCase();
System.out.println("\n===============================================");
if (num.matches("^[G][\\d]{7}$")) {
for (int i = 0; i < tickets.size(); i++) {
if (tickets.get(i).ticketNum.equals(num)) {
System.out.println(tickets.get(i).name + "\nTicket: " + tickets.get(i).ticketNum);
System.out.println(
"Flight: Green #" + tickets.get(i).flightNum + "\tSeat: " + tickets.get(i).seatNum);
if (tickets.get(i).flightNum.equals("100")) {
System.out.println("\n" + formatter.format(date) + "\nGSO-EWR | 6:30 AM - 7:30 AM\t(1 hr 30 min)");
} else if (tickets.get(i).flightNum.equals("400")) {
System.out.println("\n" + formatter.format(date) + "\nGSO-SEA | 4:00 PM - 11:30 PM\t(10 hr 30 min)");
}
System.out.println("===============================================\n");
return num;
}
}
System.out.println("Ticket not found.");
System.out.println("===============================================");
return "not found";
} else {
System.out.println("Invalid input. Please try again.");
System.out.println("===============================================\n");
return "";
}
}
}