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;
}
}
public ICollection<IEnumerable<Flight>> GetFlights(DateTime startTime,
public Dictionary<string, List<Flight>> GetFlights(DateTime startTime,
DateTime endTime,
string origin,
string destination,
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,
endTime, origin,
destination, numberOfSeats);
endTime, destination, numberOfSeats);
return freeFlights;
}
@ -97,7 +95,7 @@ namespace AirlineServer
DateTime zurichStartDate = new DateTime(2018, 08, 15, 12, 00,
00, 00);
DateTime baselStartDate = new DateTime(2018, 08, 15, 12, 00,
DateTime baselStartDate = new DateTime(2018, 08, 19, 12, 00,
00, 00);
DateTime genfStartDate = new DateTime(2018, 08, 19, 12, 00,
00, 00);

View File

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

View File

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