rework the airline service to have it return a dictionary

This commit is contained in:
Andreas Zweili 2018-08-28 20:46:56 +02:00
parent 50f30c328c
commit 0e1ef33782
3 changed files with 31 additions and 35 deletions

View File

@ -43,16 +43,14 @@ namespace AirlineServer
return flight; return flight;
} }
} }
public ICollection<IEnumerable<Flight>> GetFlights(DateTime startTime, public Dictionary<string, List<Flight>> GetFlights(DateTime startTime,
DateTime endTime, DateTime endTime,
string origin,
string destination, string destination,
int numberOfSeats) int numberOfSeats)
{ {
ICollection<IEnumerable<Flight>> freeFlights = new List<IEnumerable<Flight>>(); Dictionary<string, List<Flight>> freeFlights = new Dictionary<string, List<Flight>>();
freeFlights = FindFlight.Search(this.flights, startTime, freeFlights = FindFlight.Search(this.flights, startTime,
endTime, origin, endTime, destination, numberOfSeats);
destination, numberOfSeats);
return freeFlights; return freeFlights;
} }
@ -97,7 +95,7 @@ namespace AirlineServer
DateTime zurichStartDate = new DateTime(2018, 08, 15, 12, 00, DateTime zurichStartDate = new DateTime(2018, 08, 15, 12, 00,
00, 00); 00, 00);
DateTime baselStartDate = new DateTime(2018, 08, 15, 12, 00, DateTime baselStartDate = new DateTime(2018, 08, 19, 12, 00,
00, 00); 00, 00);
DateTime genfStartDate = new DateTime(2018, 08, 19, 12, 00, DateTime genfStartDate = new DateTime(2018, 08, 19, 12, 00,
00, 00); 00, 00);

View File

@ -8,61 +8,60 @@ namespace AirlineServer.Helper
{ {
public static class FindFlight public static class FindFlight
{ {
public static ICollection<IEnumerable<Flight>> Search( public static Dictionary<string, List<Flight>> Search(
ICollection<Flight> flights, List<Flight> flights,
DateTime startTime, DateTime startTime,
DateTime endTime, DateTime endTime,
string destination, string destination,
string origin,
int numberOfSeats) int numberOfSeats)
{ {
ICollection<IEnumerable<Flight>> free_flights = Dictionary<string, List<Flight>> free_flights =
new List<IEnumerable<Flight>>(); new Dictionary<string, List<Flight>>();
var flightsWithSeats = FindFlight var flightsWithSeats = FindFlight
.FlightsWithSeats(flights, numberOfSeats); .FlightsWithSeats(flights, numberOfSeats);
var flightsTo= FindFlight var flightsTo= FindFlight
.FlightsTo(flightsWithSeats, destination, origin, startTime); .FlightsTo(flightsWithSeats, destination, startTime);
var flightsBack = FindFlight var flightsBack = FindFlight
.FlightsBack(flightsWithSeats, destination, origin, endTime); .FlightsBack(flightsWithSeats, flightsTo, endTime);
free_flights.Add(flightsTo); free_flights.Add("To", flightsTo);
free_flights.Add(flightsBack); free_flights.Add("From", flightsBack);
return free_flights; return free_flights;
} }
public static IEnumerable <Flight> FlightsWithSeats( private static List<Flight> FlightsWithSeats(
ICollection<Flight> raw_flights, List<Flight> raw_flights,
int numberOfSeats) int numberOfSeats)
{ {
IEnumerable<Flight> flights = new List<Flight>(); List<Flight> flights = new List<Flight>();
flights = raw_flights.Where(f => (f.MaxSeats - f.BookedSeats) flights = raw_flights.Where(f => (f.MaxSeats - f.BookedSeats)
> numberOfSeats); > numberOfSeats).ToList();
return flights; return flights;
} }
public static IEnumerable <Flight> FlightsTo(IEnumerable<Flight> raw_flights, private static List<Flight> FlightsTo(IEnumerable<Flight> raw_flights,
string destination, string destination,
string origin,
DateTime startTime) DateTime startTime)
{ {
IEnumerable<Flight> flights = new List<Flight>(); List<Flight> flights = new List<Flight>();
flights = raw_flights.Where(f => flights = raw_flights.Where(f =>
f.Destination.City.Name == destination f.Destination.City.Name == destination
& f.Origin.City.Name == origin & f.StartTime == startTime).ToList();
& f.StartTime == startTime);
return flights; return flights;
} }
// Since this function searches for a flight back the arguments // Since this function searches for a flight back the arguments
// destination and origin get used in reverse. // destination and origin get used in reverse.
public static IEnumerable<Flight> FlightsBack(IEnumerable<Flight> raw_flights, private static List<Flight> FlightsBack(List<Flight> raw_flights,
string destination, List<Flight> flightsTo,
string origin, DateTime endTime)
DateTime endTime)
{ {
IEnumerable<Flight> flights = new List<Flight>(); List<Flight> flights = new List<Flight>();
flights = raw_flights.Where(f => foreach (var flight in flightsTo)
f.Destination.City.Name == origin {
& f.Origin.City.Name == destination flights.AddRange(raw_flights.Where(f =>
& f.StartTime == endTime); f.Destination.City.Name == flight.Origin.City.Name
& f.Origin.City.Name == flight.Destination.City.Name
& f.StartTime == endTime).ToList());
}
return flights; return flights;
} }
} }

View File

@ -15,9 +15,8 @@ namespace AirlineServer
public interface IAirlineService public interface IAirlineService
{ {
[OperationContract] [OperationContract]
ICollection<IEnumerable<Flight>> GetFlights(DateTime startTime, Dictionary<string, List<Flight>> GetFlights(DateTime startTime,
DateTime endTime, DateTime endTime,
string origin,
string destination, string destination,
int numberOfSeats); int numberOfSeats);
[OperationContract] [OperationContract]