-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMenu.java
More file actions
191 lines (170 loc) · 6.44 KB
/
Copy pathMenu.java
File metadata and controls
191 lines (170 loc) · 6.44 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
import java.io.*;
import java.util.*;
/**
* The main class, with the main method that controls the whole project.
*/
public class Menu
{
static SortedArrayList<Event> listOfEvent = new SortedArrayList<>();
static SortedArrayList<Client> listOfClient = new SortedArrayList<>();
static TicketOffice ticketOffice = new TicketOffice(listOfEvent, listOfClient);
public static void main(String[] args) throws IOException
{
readInformation();
while(true)
{
System.out.println("f - to finish running the program.");
System.out.println("e - to display on the screen the information about all the events.");
System.out.println("c - to display on the screen the information about all the clients.");
System.out.println("b - to update the stored data when tickets are bought by one of the registered clients.");
System.out.println("r - to update the store data when a registered client cancels/returns tickets.");
System.out.println("----------------------------------------------------------------------------------------");
Scanner in = new Scanner(System.in);
String choice = in.nextLine();
switch (choice)
{
case "f":
save();
System.out.println("Exit to the menu and save the data to the output.");
System.exit(0);
break;
case "e":
e();
break;
case "c":
c();
break;
case "b":
b();
break;
case "r":
r();
break;
default:
System.out.println("Invalid input, please try it again.");
}
}
}
/**
* The method to read the file information
* @throws IOException
*/
public static void readInformation() throws IOException
{
FileReader in = new FileReader("input.txt");
BufferedReader reader = new BufferedReader(in);
String line = reader.readLine();
int eventNumbers = Integer.parseInt(line);
for (int i = 0; i < eventNumbers; i++)
{
String eventName = reader.readLine();
int numOfTicketLeft = Integer.parseInt(reader.readLine());
Event event = new Event(eventName, numOfTicketLeft);
ticketOffice.eventList.insertSorted(event);
}
int clientNumbers = Integer.parseInt(reader.readLine());
for (int i = 0; i < clientNumbers; i++)
{
String clientName = reader.readLine();
String[] split = clientName.split("\\s+");
Client client = new Client(split[0], split[1]);
ticketOffice.clientList.insertSorted(client);
}
reader.close();
}
/**
* The method to write the information in a file
* @throws IOException
*/
public static void save() throws IOException
{
FileWriter out = new FileWriter("output.txt");
BufferedWriter output = new BufferedWriter(out);
output.write(ticketOffice.outputInformation);
output.newLine();
for (Client client : ticketOffice.clientList)
{
output.write(client.toString());
}
output.newLine();
for (Event event : ticketOffice.eventList)
{
out.write(event.toString());
}
output.close();
}
public static void e() //display all the event information
{
for (Event event : ticketOffice.getEventList())
{
System.out.println(event.toString());
}
}
public static void c() //display all the client information
{
for (Client client : ticketOffice.getClientList())
{
System.out.println(client.toString());
}
}
public static void b() //update the store data when buy tickets
{
Scanner in = new Scanner(System.in);
try
{
System.out.println("Please enter the event you want to buy(capitalize the first letter):");
String eventName = in.next();
System.out.println("Please enter the first name of the client:");
String firstName = in.next();
System.out.println("Please enter the last name of the client:");
String lastName = in.next();
System.out.println("Please enter the number of tickets to buy:");
int numberOfTicketsBuy = in.nextInt();
if (numberOfTicketsBuy <= 0)
{
System.out.println("Wrong input, try it again!");
}
else
{
Client client = new Client(firstName, lastName);
Event event = new Event(eventName);
String ticketInformation = ticketOffice.buyTicket(client, event, numberOfTicketsBuy);
System.out.println(ticketInformation);
}
}
catch (Exception e)
{
System.out.println("The input data is wrong, please try again!");
}
}
public static void r() //update the store data cancel/return tickets
{
Scanner in = new Scanner(System.in);
try
{
System.out.println("Please enter the event you want to cancel/return(capitalize the first letter):");
String eventName = in.next();
System.out.println("Please enter the first name of the client:");
String firstName = in.next();
System.out.println("Please enter the last name of the client:");
String lastName = in.next();
System.out.println("Please enter the number of tickets to cancel/return:");
Integer numberOfTicketsCancel = in.nextInt();
if (numberOfTicketsCancel <= 0)
{
System.out.println("Wrong input, try it again!");
}
else
{
Client client = new Client(firstName, lastName);
Event event = new Event(eventName);
String ticketInformation = ticketOffice.cancelTicket(client, event, numberOfTicketsCancel);
System.out.println(ticketInformation);
}
}
catch (Exception e)
{
System.out.println("The input data is wrong, please try again!");
}
}
}