-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transaction.java
executable file
·170 lines (137 loc) · 8.62 KB
/
Transaction.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
import java.util.ArrayList;
public class Transaction {
public static void book(Table<Flight> fTable, Table<Seat> sTable, Table<Passenger> pTable, Table<Reservation> rTable){
// temp table for free seats
Table<Seat> freeSeats = new Table<Seat>();
int randomPassenger = (int) (Math.random()*pTable.size()); // choosing random passenger [index] from passengers table
int randomFlight = (int) (Math.random()*fTable.size()); // choosing random flight [index] from flights table
for(int f = 0; f < sTable.size(); f++){ // check for all seats in sTable
if((sTable.get(f).flightId() == randomFlight+1) && (!sTable.get(f).isBooked())){ // if seat belongs to chosen flight and is not booked then
freeSeats.add(sTable.get(f)); // add seats to the freeSeats list
}
}
if (freeSeats.size() > 0){ // if there is free seats then
int randomFreeSeat = (int) (Math.random()*freeSeats.size()); // choose random seat from free seats list
if(freeSeats.size() == 1){ // if there is only one free seat
randomFreeSeat = 0; // then take that seat
}
Reservation res = new Reservation(pTable.get(randomPassenger), freeSeats.get(randomFreeSeat)); // create reservation
if (rTable.isEmpty()){ // if reservations table is empty
freeSeats.get(randomFreeSeat).bookSeat();
rTable.add(res); // add reservation to reservation table
} else { // if reservations table is NOT empty and
if( exists(res, rTable) ){ // if seat is booked or seat exists in rTable
//continue; // exit this loop
} else {
freeSeats.get(randomFreeSeat).bookSeat(); // else book that seat
rTable.add(res); // add to reservation table
}
}
}
}
// method for booking all seats
public static void bookAllSeats(Table<Flight> fTable, Table<Seat> sTable, Table<Passenger> pTable, Table<Reservation> rTable){
do {
// temp table for free seats
Table<Seat> freeSeats = new Table<Seat>();
int randomPassenger = (int) (Math.random()*pTable.size()); // choosing random passenger [index] from passengers table
int randomFlight = (int) (Math.random()*fTable.size()); // choosing random flight [index] from flights table
for(int f = 0; f < sTable.size(); f++){ // check for all seats in sTable
if((sTable.get(f).flightId() == randomFlight+1) && (!sTable.get(f).isBooked())){ // if seat belongs to chosen flight and is not booked then
freeSeats.add(sTable.get(f)); // add seats to the freeSeats list
}
}
if (freeSeats.size() > 0){ // if there is free seats then
int randomFreeSeat = (int) (Math.random()*freeSeats.size()); // choose random seat from free seats list
if(freeSeats.size() == 1){ // if there is only one free seat
randomFreeSeat = 0; // then take that seat
}
Reservation res = new Reservation(pTable.get(randomPassenger), freeSeats.get(randomFreeSeat)); // create reservation
if (rTable.isEmpty()){ // if reservations table is empty
freeSeats.get(randomFreeSeat).bookSeat();
rTable.add(res); // add reservation to reservation table
} else { // if reservations table is NOT empty and
if( exists(res, rTable) ){ // if seat is booked or seat exists in rTable
continue; // exit this loop
} else {
freeSeats.get(randomFreeSeat).bookSeat(); // else book that seat
rTable.add(res); // add to reservation table
}
}
}
}
while( !(sTable.size() == rTable.size()) ); // keep booking until reservations table size is equal to seats table size
}
// method for booking all seats
public static void bookAllSeatsWithComments(Table<Flight> fTable, Table<Seat> sTable, Table<Passenger> pTable, Table<Reservation> rTable){
do {
// temp table for free seats
Table<Seat> freeSeats = new Table<Seat>();
int randomPassenger = (int) (Math.random()*pTable.size()); // choosing random passenger [index] from passengers table
int randomFlight = (int) (Math.random()*fTable.size()); // choosing random flight [index] from flights table
for(int f = 0; f < sTable.size(); f++){ // check for all seats in sTable
if((sTable.get(f).flightId() == randomFlight+1) && (!sTable.get(f).isBooked())){ // if seat belongs to chosen flight and is not booked then
freeSeats.add(sTable.get(f)); // add seats to the freeSeats list
}
}
if (freeSeats.size() > 0){ // if there is free seats then
int randomFreeSeat = (int) (Math.random()*freeSeats.size()); // choose random seat from free seats list
if(freeSeats.size() == 1){ // if there is only one free seat
randomFreeSeat = 0; // then take that seat
}
Reservation res = new Reservation(pTable.get(randomPassenger), freeSeats.get(randomFreeSeat)); // create reservation
System.out.println("Passenger " + pTable.get(randomPassenger).passengerName() + " is trying to book a seat to " + freeSeats.get(randomFreeSeat).fDestination());
if (rTable.isEmpty()){ // if reservations table is empty
System.out.println(pTable.get(randomPassenger).passengerName() + " picked seat ID number " + freeSeats.get(randomFreeSeat).seatId());
freeSeats.get(randomFreeSeat).bookSeat();
rTable.add(res); // add reservation to reservation table
System.out.println("Passenger "+ pTable.get(randomPassenger).passengerName() +" has booked a seat number "+ freeSeats.get(randomFreeSeat).seatId() +" to " + fTable.get(randomFlight).flightDestination());
} else { // if reservations table is NOT empty
if( exists(res, rTable) ){ // if seat booked or seat exists in rTable
System.out.println("Already reserved => Searching new seat!"); // print message
continue; // and exit this loop
} else {
System.out.println(pTable.get(randomPassenger).passengerName() + " picked seat ID number " + freeSeats.get(randomFreeSeat).seatId());
freeSeats.get(randomFreeSeat).bookSeat(); // else book that seat
rTable.add(res); // add to reservation table
System.out.println("Passenger "+ pTable.get(randomPassenger).passengerName() +" has booked a seat number "+ freeSeats.get(randomFreeSeat).seatId() +" to " + fTable.get(randomFlight).flightDestination());
}
}
} else { // else if there is no free seats for that flight
System.out.println("There are no free seats available for this flight!"); // print message
}
}
while( !(sTable.size() == rTable.size()) ); // keep booking until reservations table size is equal to seats table size
System.out.println("All seats are booked!!!");
}
// check if reservation exists in table
private static boolean exists(Reservation res, Table<Reservation> rTable){
for(int r = 0; r < rTable.size(); r++){
if( (Transaction.sameDestination(rTable.get(r), res)) || (rTable.get(r).seatId() == res.seatId()) ){
return true;
}
}
return false;
}
// return total sum of all reservations
public static int totalReservations(ArrayList<Reservation> rTable) {
return rTable.size();
}
// show reservations for specific passenger
public static void myReservations(String p, ArrayList<Reservation> rTable) {
System.out.println("Reservations of passenger " + p + ":" );
for(int i = 0; i < rTable.size(); i++){
if(rTable.get(i).seatOwner() == p){
System.out.println(rTable.get(i));
}
}
}
// check if two reservations have same destination
private static boolean sameDestination(Reservation a, Reservation b){
if( (a.seatOwner() == b.seatOwner() ) && ( a.seatDestination() == b.seatDestination() ) ){
return true;
}else{
return false;
}
}
}