Skip to content

Instantly share code, notes, and snippets.

@chaudharydeepanshu
Created November 18, 2022 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaudharydeepanshu/72fc17806abd78e3c2642f4cf8972525 to your computer and use it in GitHub Desktop.
Save chaudharydeepanshu/72fc17806abd78e3c2642f4cf8972525 to your computer and use it in GitHub Desktop.
import 'dart:io';
import 'dart:math';
void main() {
print("Enter no of planes to randomly generate:");
// Reading no of planes to randomly generate
int noOfPlanes = int.parse(stdin.readLineSync() ?? "0");
print("\n\nEnter total no of gates on airport:");
// Reading no of gates in airport
int noOfGates = int.parse(stdin.readLineSync() ?? "0");
List<Flight> generatedFlights = [];
for (int i = 0; i < noOfPlanes; i++) {
int r1 = 1 + Random().nextInt(10 - 1);
int r2 = 1 + Random().nextInt(10 - 1);
generatedFlights.add(
Flight(
flightName: "Flight ${i + 1}",
arrivalDateTime:
DateTime.now().add(Duration(hours: r1, minutes: r1 - 73)),
departureDateTime:
DateTime.now().add(Duration(hours: r1, minutes: r1 - 73)).add(
Duration(hours: r1 + r2),
),
),
);
}
print(
"\n\nRandomly generated Flights for testing:\n${generatedFlights.map((e) => "Flight Name -> ${e.flightName}\nFlight Arrival Time -> ${e.arrivalDateTime}\nFlight Departure Time -> ${e.departureDateTime}\n-----------------------------------------").join("\n")}");
List<AssignedGate> assignedGates = [];
for (int i = 0; i < noOfGates; i++) {
List<Flight> sortedFlights = List.from(generatedFlights);
sortedFlights
.sort((a, b) => a.arrivalDateTime.compareTo(b.arrivalDateTime));
if (sortedFlights.length > i) {
if (i < noOfGates) {
assignedGates
.add(AssignedGate(gateNo: i + 1, flight: sortedFlights[i]));
} else {
sortedFlights
.sort((a, b) => a.departureDateTime.compareTo(b.departureDateTime));
for (var element in assignedGates) {
if (element.flight.departureDateTime
.isBefore(sortedFlights[i].arrivalDateTime)) {
assignedGates.add(
AssignedGate(gateNo: element.gateNo, flight: sortedFlights[i]));
break;
}
}
}
}
}
List<int> gates = List.generate(noOfGates, (index) => index + 1);
List<int> unAssignedGates = gates
.where((element) => !assignedGates.map((e) => e.gateNo).contains(element))
.toList();
print("\n\nGates currently free:\n${unAssignedGates.join("\n")}");
List<Flight> unAssignedFlights = generatedFlights
.where((element) => !assignedGates
.map((e) => e.flight.flightName)
.contains(element.flightName))
.toList();
print(
"\n\nFlights for which gate not available:\n${unAssignedFlights.map((e) => "Flight Name -> ${e.flightName}\nFlight Arrival Time -> ${e.arrivalDateTime}\nFlight Departure Time -> ${e.departureDateTime}\n-----------------------------------------").join("\n")}");
print(
"\n\nFlights for which gate is available:\n${assignedGates.map((e) => "Flight Name -> ${e.flight.flightName}\nFlight Arrival Time -> ${e.flight.arrivalDateTime}\nFlight Departure Time -> ${e.flight.departureDateTime}\nGate No. -> ${e.gateNo}\n-----------------------------------------").join("\n")}");
}
class Flight {
final String flightName;
final DateTime arrivalDateTime;
final DateTime departureDateTime;
Flight({
required this.flightName,
required this.arrivalDateTime,
required this.departureDateTime,
});
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Flight &&
runtimeType == other.runtimeType &&
flightName == other.flightName &&
arrivalDateTime == other.arrivalDateTime &&
departureDateTime == other.departureDateTime;
@override
int get hashCode =>
flightName.hashCode ^
arrivalDateTime.hashCode ^
departureDateTime.hashCode;
// Implement toString to make it easier to see information
// when using the print statement.
@override
String toString() {
return 'Flight{flightName: $flightName, arrivalDateTime: $arrivalDateTime, departureDateTime: $departureDateTime}';
}
}
class AssignedGate {
final int gateNo;
final Flight flight;
AssignedGate({
required this.gateNo,
required this.flight,
});
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is AssignedGate &&
runtimeType == other.runtimeType &&
gateNo == other.gateNo &&
flight == other.flight;
@override
int get hashCode => gateNo.hashCode ^ flight.hashCode;
// Implement toString to make it easier to see information
// when using the print statement.
@override
String toString() {
return 'AssignedGate{gateNo: $gateNo, flight: $flight}';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment