-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAirport.java
255 lines (213 loc) · 6.6 KB
/
Airport.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
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
public class Main {
public static void main(String[] args) {
// Write your main program here. Implementing your own classes will be useful.
TextUserInterface ui = new TextUserInterface();
ui.start();
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author kdost
*/
public class Plane {
private String name;
private int capacity;
// private Flights flights;
public Plane(String name, int capacity) {
this.name = name;
this.capacity = capacity;
// this.flights = new Flights();
}
@Override
public String toString() {
return this.name + " (" + this.capacity + " ppl)";
}
public String getPlane() {
return this.name;
}
public int getCapacity() {
return this.capacity;
}
// public void addFlight(Plane plane, String fromAndTo) {
// plane.flights.addFlight(fromAndTo);
// }
}
/**
*
* @author kdost
*/
public class Flights {
private String depart;
private String arrive;
public Flights(String depart, String arrive) {
this.depart = depart;
this.arrive = arrive;
}
@Override
public String toString() {
return "(" + this.depart + "-" + this.arrive + ")";
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author kdost
*/
public class PlaneDirectory {
private ArrayList<Plane> planes;
private ArrayList<Flights> flights;
private Map<Plane, ArrayList<Flights>> database;
public PlaneDirectory(){
this.planes = new ArrayList<Plane>();
this.flights = new ArrayList<Flights>();
this.database = new HashMap<Plane, ArrayList<Flights>>();
}
public void addPlane(String id, int capacity){
ArrayList<Flights> init = new ArrayList<Flights>();
Plane plane = new Plane(id, capacity);
this.planes.add(plane);
this.database.put(plane, init);
}
public void addFlight(String id, String depart, String arriv){
Plane plane = new Plane("",0);
for(Plane p : this.planes){
if(p.getPlane().equals(id)){
plane = p;
}
}
Flights flight = new Flights(depart, arriv);
this.database.get(plane).add(flight);
}
public void printPlanes(){
for(Plane p : this.planes){
System.out.println(p);
}
}
public void printFlights(){
for(Plane p : this.database.keySet()){
ArrayList<Flights> flights = this.database.get(p);
for(Flights f : flights){
System.out.println(p + " " + f);
}
}
}
public void printPlaneInfo(String id){
for(Plane p : this.planes){
if(p.getPlane().equals(id)){
System.out.println(p);
}
}
}
}
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author kdost
*/
public class TextUserInterface {
private Scanner reader;
private PlaneDirectory planeDirectory;
public TextUserInterface() {
reader = new Scanner(System.in);
planeDirectory = new PlaneDirectory();
}
public void start() {
airportPanel();
flightService();
}
public void airportPanel() {
System.out.println("Airport panel");
System.out.println("--------------------");
System.out.println("");
while (true) {
String userInput;
System.out.println("Choose operation:");
System.out.println("[1] Add airplane");
System.out.println("[2] Add flight");
System.out.println("[x] Exit");
System.out.print("> ");
userInput = reader.nextLine();
if ("x".equals(userInput)) {
break;
} else if ("1".equals(userInput)) {
addAirplane();
} else if ("2".equals(userInput)) {
addFlight();
} else {
System.out.println("Unknown command!");
}
}
}
public void flightService() {
System.out.println("Flight service");
System.out.println("------------");
System.out.println("");
while (true) {
String userInput;
System.out.println("Choose operation:");
System.out.println("[1] Print planes");
System.out.println("[2] Print flights");
System.out.println("[3] Print plane info");
System.out.println("[x] Quit");
System.out.print("> ");
userInput = reader.nextLine();
if ("x".equals(userInput)) {
break;
} else if ("1".equals(userInput)) {
planeDirectory.printPlanes();
} else if ("2".equals(userInput)) {
planeDirectory.printFlights();
} else if ("3".equals(userInput)) {
printPlaneInfo();
} else {
System.out.println("Unknown command!");
}
}
}
public void addAirplane() {
System.out.print("Give plane ID: ");
String userPlaneID = reader.nextLine();
System.out.print("Give plane capacity: ");
int planeCapacity = Integer.parseInt(reader.nextLine());
planeDirectory.addPlane(userPlaneID, planeCapacity);
}
public void addFlight() {
System.out.print("Give plane ID: ");
String userResponse = reader.nextLine();
System.out.print("Give departure airport code: ");
String departureCode = reader.nextLine();
System.out.print("Give destination airport code: ");
String destinationCode = reader.nextLine();
planeDirectory.addFlight(userResponse, departureCode, destinationCode);
}
//
// public void printPlanes() {
//
// }
//
// public void printFlights() {
//
// }
public void printPlaneInfo() {
System.out.print("Give plane ID: ");
String userInput = reader.nextLine();
planeDirectory.printPlaneInfo(userInput);
}
}