finalize the AirLineService Project

This commit is contained in:
Andreas Zweili 2018-08-16 22:10:50 +02:00
parent 12b7a7e11b
commit c82ebb8953
4 changed files with 109 additions and 83 deletions

View File

@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Collections.Generic;
using AirlineServer.Models;
using AirlineServer.Helper;
@ -16,20 +13,50 @@ namespace AirlineServer
// Explorer and start debugging.
public class AirlineService : IAirlineService
{
public Flight BookFlight(Flight flight, int numberOfSeats)
public List<Flight> flights = new List<Flight>();
public Flight BookFlight(Flight flight,
int numberOfSeats)
{
throw new NotImplementedException();
Flight flightToBook = this.flights.Single(f => f == flight);
if (flightToBook != null)
{
flightToBook.BookedSeats += numberOfSeats;
return flightToBook;
}
else
{
return flight;
}
}
public Flight CancelFlight(Flight flight, int numberOfSeats)
{
throw new NotImplementedException();
Flight flightToBook = this.flights.Single(f => f == flight);
if (flightToBook != null)
{
flightToBook.BookedSeats -= numberOfSeats;
return flightToBook;
}
else
{
return flight;
}
}
public ICollection<ICollection<Flight>> GetFlights(DateTime startTime,
DateTime endTime,
string cityName,
int numberOfSeats)
public ICollection<IEnumerable<Flight>> GetFlights(DateTime startTime,
DateTime endTime,
string origin,
string destination,
int numberOfSeats)
{
ICollection<ICollection<Flight>> free_flights = new List<ICollection<Flight>>();
ICollection<IEnumerable<Flight>> freeFlights = new List<IEnumerable<Flight>>();
freeFlights = FindFlight.Search(this.flights, startTime,
endTime, origin,
destination, numberOfSeats);
return freeFlights;
}
public AirlineService(){
List<Flight> flights = new List<Flight>();
Airline lufthansa = new Airline { Name = "Lufthansa" };
@ -51,31 +78,31 @@ namespace AirlineServer
{
ShortName = "BSL",
Name = "Basel Airport",
City = zurich
City = basel
};
Airport agenf = new Airport
{
ShortName = "GNF",
Name = "Geneva Airport",
City = zurich
City = genf
};
Airport abelp = new Airport
{
ShortName = "BLP",
Name = "Belp Airport",
City = zurich
City = belp
};
DateTime zurichStartDate = new DateTime(2018, 06, 15, 12, 00,
DateTime zurichStartDate = new DateTime(2018, 08, 15, 12, 00,
00, 00);
DateTime baselStartDate = new DateTime(2018, 06, 16, 16, 00,
00, 00);
DateTime genfStartDate = new DateTime(2018, 06, 19, 16, 00,
00, 00);
DateTime belpStartDate = new DateTime(2018, 06, 09, 16, 00,
00, 00);
DateTime baselStartDate = new DateTime(2018, 08, 15, 12, 00,
00, 00);
DateTime genfStartDate = new DateTime(2018, 08, 19, 12, 00,
00, 00);
DateTime belpStartDate = new DateTime(2018, 08, 09, 12, 00,
00, 00);
Flight zurich_basel = new Flight
{
@ -83,19 +110,19 @@ namespace AirlineServer
Name = "LFH1206",
StartTime = zurichStartDate,
Duration = 2.50F,
FromAirport = azurich,
ToAirport = abasel,
Origin = azurich,
Destination = abasel,
MaxSeats = 10,
BookedSeats = 0
};
Flight basel_belp = new Flight
Flight basel_zurich = new Flight
{
Airline = easyjet,
Name = "ESZ666",
StartTime = baselStartDate,
Duration = 7.20F,
FromAirport = abasel,
ToAirport = abelp,
Origin = abasel,
Destination = azurich,
MaxSeats = 10,
BookedSeats = 0
};
@ -105,8 +132,8 @@ namespace AirlineServer
Name = "ESZ666",
StartTime = genfStartDate,
Duration = 7.20F,
FromAirport = agenf,
ToAirport = azurich,
Origin = agenf,
Destination = azurich,
MaxSeats = 10,
BookedSeats = 0
};
@ -116,17 +143,15 @@ namespace AirlineServer
Name = "ESZ666",
StartTime = belpStartDate,
Duration = 7.20F,
FromAirport = abelp,
ToAirport = azurich,
Origin = abelp,
Destination = azurich,
MaxSeats = 10,
BookedSeats = 0
};
flights.Add(zurich_basel);
flights.Add(basel_belp);
flights.Add(genf_zurich);
flights.Add(belp_zurich);
FindFlight.Search(flights, startTime, endTime,
cityName);
this.flights.Add(zurich_basel);
this.flights.Add(basel_zurich);
this.flights.Add(genf_zurich);
this.flights.Add(belp_zurich);
}
}
}

View File

@ -8,63 +8,61 @@ namespace AirlineServer.Helper
{
public static class FindFlight
{
public static ICollection<ICollection<Flight>> Search(ICollection<Flight> flights,
public static ICollection<IEnumerable<Flight>> Search(
ICollection<Flight> flights,
DateTime startTime,
DateTime endTime,
string cityName)
string destination,
string origin,
int numberOfSeats)
{
ICollection<ICollection<Flight>> free_flights =
new List<ICollection<Flight>>();
ICollection<IEnumerable<Flight>> free_flights =
new List<IEnumerable<Flight>>();
var flightsWithSeats = FindFlight
.FlightsWithSeats(flights, numberOfSeats);
var flightsTo= FindFlight
.FlightsTo(flightsWithSeats, destination, origin, startTime);
var flightsBack = FindFlight
.FlightsBack(flightsWithSeats, destination, origin, endTime);
var flightsWithSeats = FindFlight.FlightsWithSeats(flights);
var flightsTo = FindFlight.FlightsTo(flightsWithSeats,
cityName, startTime);
var flightsFrom = FindFlight.FlightsFrom(flightsWithSeats,
cityName, endTime);
free_flights.Add(flightsTo);
free_flights.Add(flightsFrom);
free_flights.Add(flightsBack);
return free_flights;
}
public static ICollection<Flight> FlightsWithSeats(ICollection<Flight> raw_flights)
public static IEnumerable <Flight> FlightsWithSeats(
ICollection<Flight> raw_flights,
int numberOfSeats)
{
ICollection<Flight> flights = new List<Flight>();
foreach (var flight in raw_flights)
{
if (flight.MaxSeats > flight.BookedSeats)
{
flights.Add(flight);
}
}
IEnumerable<Flight> flights = new List<Flight>();
flights = raw_flights.Where(f => (f.MaxSeats - f.BookedSeats)
> numberOfSeats);
return flights;
}
public static ICollection<Flight> FlightsTo(ICollection<Flight> raw_flights,
string cityName,
public static IEnumerable <Flight> FlightsTo(IEnumerable<Flight> raw_flights,
string destination,
string origin,
DateTime startTime)
{
ICollection<Flight> flights = new List<Flight>();
foreach (var flight in raw_flights)
{
if (flight.ToAirport.City.Name == cityName
&& flight.StartTime == startTime)
{
flights.Add(flight);
}
}
IEnumerable<Flight> flights = new List<Flight>();
flights = raw_flights.Where(f =>
f.Destination.City.Name == destination
& f.Origin.City.Name == origin
& f.StartTime == startTime);
return flights;
}
public static ICollection<Flight> FlightsFrom(ICollection<Flight> raw_flights,
string cityName,
DateTime endTime)
// 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)
{
ICollection<Flight> flights = new List<Flight>();
foreach (var flight in raw_flights)
{
if (flight.ToAirport.City.Name == cityName
&& flight.StartTime == endTime)
{
flights.Add(flight);
}
}
IEnumerable<Flight> flights = new List<Flight>();
flights = raw_flights.Where(f =>
f.Destination.City.Name == origin
& f.Origin.City.Name == destination
& f.StartTime == endTime);
return flights;
}
}

View File

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

View File

@ -17,9 +17,9 @@ namespace AirlineServer.Models
[DataMember]
public float Duration { get; set; }
[DataMember]
public Airport FromAirport { get; set; }
public Airport Origin { get; set; }
[DataMember]
public Airport ToAirport { get; set; }
public Airport Destination { get; set; }
public int MaxSeats { get; set; }
public int BookedSeats { get; set; }
}