Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
153 lines (124 loc) · 5.05 KB
/
Copy pathMain.java
File metadata and controls
153 lines (124 loc) · 5.05 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
import airport.Flight;
import airport.Passenger;
import exceptions.*;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate date = LocalDate.parse("13/02/2022", formatter);
Flight f1 = new Flight(date, LocalTime.parse("13:00"), "isfahan", "tehran",
320.5, 55, 780.3, 2);
Flight f2 = new Flight(date, LocalTime.parse("11:00"), "isfahan", "mashhad",
900d, 110, 1570.5, 50);
Flight f3 = new Flight(date, LocalTime.parse("10:00"), "isfahan", "tehran",
320.5, 55, 780.3, 87);
String name1 = sc.next();
String name2 = sc.next();
String phone1 = null;
String phone2 = null;
String email1 = null;
String email2 = null;
double balance1 = 0;
double balance2 = 0;
Passenger p1 = new Passenger(name1, phone1, email1, balance1);
Passenger p2 = new Passenger(name2, phone2, email2, balance2);
Passenger p3 = new Passenger("ali", "9301112233", "abc@gmail.com", 2000.5);
Passenger p4 = new Passenger("zahra", "989301112233", "abcgl.com", 3000d);
phone1 = sc.next();
managePhone(p1, phone1);
phone2 = sc.next();
managePhone(p2, phone2);
email1 = sc.next();
manageEmail(p1,email1);
email2 = sc.next();
manageEmail(p2,email2);
balance1 = sc.nextDouble();
manageBalance(p1,balance1);
balance2 = sc.nextDouble();
manageBalance(p2,balance2);
managePhone(p3, p3.getPhoneNumber());
managePhone(p4, p4.getPhoneNumber());
manageEmail(p3,p3.getEmail());
manageEmail(p4, p4.getEmail());
manageBalance(p3,p3.getBalance());
manageBalance(p4,p4.getBalance());
management(p1, f1);
management(p1, f2);
management(p2, f3);
management(p2, f2);
management(p3, f1);
management(p3, f3);
management(p4, f1);
management(p4, f2);
}
public static void flightDetails(Flight flight) {
System.out.println("-------------------------------------------------");
System.out.println(flight.toString());
System.out.println("Passengers:");
for (Passenger passenger : flight.getPassengers()) {
System.out.println(passenger.toString());
System.out.println();
}
System.out.println("-------------------------------------------------");
}
public static void management(Passenger passenger, Flight flight) throws FailedReserve {
System.out.println("flight is reserving for " + passenger.getName());
try {
passenger.reserveFlight(flight);
} catch (SameOrginAndDestination sameOrginAndDestination) {
System.out.println(sameOrginAndDestination.toString());
} catch (SameTime sameTime) {
System.out.println(sameTime.toString());
} catch (IndexOutOfBoundsException indexOutOfBoundsException) {
System.out.println("It's full!");
} catch (NoBalance noBalance) {
System.out.println(noBalance.toString());
} finally {
flightDetails(flight);
}
}
public static void manageBalance(Passenger passenger,double balance) {
try {
passenger.checkInitialBalance(balance);
passenger.setBalance(balance);
} catch (InputMismatchException inputMismatchException) {
System.out.println(inputMismatchException.getMessage());
} catch (WrongBalance wrongBalance) {
System.out.println(wrongBalance.toString());
passenger.setBalance(0);
} finally {
System.out.println("Balance Done!");
}
}
public static void manageEmail(Passenger passenger,String email) {
try {
passenger.checkEmail(email);
passenger.setEmail(email);
} catch (InputMismatchException inputMismatchException) {
System.out.println(inputMismatchException.getMessage());
} catch (WrongEmail wrongEmail) {
System.out.println(wrongEmail.toString());
passenger.setEmail(null);
} finally {
System.out.println("Email Done!");
}
}
public static void managePhone(Passenger passenger, String phone) {
try {
passenger.checkPhone(phone);
passenger.setPhoneNumber(phone);
} catch (InputMismatchException inputMismatchException) {
System.out.println(inputMismatchException.getMessage());
} catch (WrongPhoneNumber wrongPhoneNumber) {
System.out.println(wrongPhoneNumber.toString());
passenger.setPhoneNumber(null);
} finally {
System.out.println("Phone number Done!");
}
}
}