// Display Time Table Command - Displays the time table private static void DisplayTimeTableCommand() { try { Console.WriteLine("Ferry Time Table:"); Console.WriteLine("-----------------"); foreach (var port in ManagementSystem.GetAllPorts()) { PrintPortHeader(port.Name); // Every Journey is based on an entry in the timetable, use them to display timetable foreach (var journey in ManagementSystem.GetAllJourneys().OrderBy(x => x.Origin.Name).ThenBy(x => x.Departure)) { if (journey.Origin.Id == port.Id) { Console.WriteLine("| {0} | {1} | {2} | {3} | {4} |", journey.Departure.ToString().PadRight(8), journey.Destination.Name.PadRight(13), journey.Travel.ToString().PadRight(13), journey.Ferry.Name.PadRight(18), journey.Arrival.ToString().PadRight(8) ); } } } } catch (Exception) { DisplayTimeTableError(); } }
// Search Command - Displays available journeys found in search private static void SearchCommand() { try { string[] parts = _command.Split(' '); int originPortId = int.Parse(parts[1]); int destinationPortId = int.Parse(parts[2]); TimeSpan time = TimeSpan.Parse(parts[3]); List <Journey> availableJourneys = ManagementSystem.GetAvailableJourneys(originPortId, destinationPortId, time); foreach (var result in availableJourneys) { Console.WriteLine(); Console.WriteLine("Journey Id: " + result.Id); Console.WriteLine("{1} to {2} departing at {0}", result.Departure.ToString("hh':'mm"), result.Origin.Name, result.Destination.Name ); Console.WriteLine("(Ferry: {0}, Passenger Tickets Left: {1}, Vehicle Tickets Left: {2}, Capacity Remaining: {3} tons)", result.Ferry.Name, result.Seats, result.Vehicles, result.Weight ); Console.WriteLine(); } } catch (Exception) { SearchError(); } }
// Book Command - Books an available journey private static void Book() { try { string[] parts = _command.Split(' '); int journeyId = Convert.ToInt32(parts[1]); int passengers = Convert.ToInt32(parts[2]); int vehicles = Convert.ToInt32(parts[3]); int weight = Convert.ToInt32(parts[4]); bool booked = ManagementSystem.Book(journeyId, passengers, vehicles, weight); if (booked) { Console.WriteLine("Booked"); } else { Console.WriteLine("Cannot book that journey"); } } catch (Exception) { BookingError(); } }
// Returns a list of available journeys public List <Journey> GetAvailableJourneys(int fromPort, int toPort, TimeSpan time) { List <Journey> available = new List <Journey>(); foreach (var journey in _journeys) { if (toPort == journey.Destination.Id && fromPort == journey.Origin.Id) { if (journey.Departure >= time) { List <Booking> bookings = ManagementSystem.GetBookings(journey.Id); var seatsLeft = journey.Ferry.Passengers - bookings.Sum(x => x.Passengers); var vehiclesLeft = journey.Ferry.Vehicles - bookings.Sum(x => x.Vehicles); var weightLeft = journey.Ferry.Weight - bookings.Sum(x => x.Weight); if (seatsLeft > 0) { journey.Seats = seatsLeft; journey.Vehicles = vehiclesLeft; journey.Weight = weightLeft; available.Add(journey); } } } } return(available); }
// Creates a list of journeys to travel on private static void CreateJourneys() { var timetable = ManagementSystem.GetFullTimeTable().OrderBy(x => x.Time).ThenBy(x => x.OriginId); var ports = ManagementSystem.GetAllPorts(); foreach (var entry in timetable) { Port fromPort = ports.Where(x => x.Id == entry.OriginId).Single(); Port toPort = ports.Where(x => x.Id == entry.DestinationId).Single(); Ferry ferry = ManagementSystem.GetNextAvailableFerry(fromPort, toPort, entry.Time); Journey journey = new Journey { Id = entry.Id, Origin = fromPort, Destination = toPort, Departure = entry.Time, Travel = entry.JourneyTime, Arrival = entry.Time + entry.JourneyTime, Ferry = ferry, Seats = ferry.Passengers, Vehicles = ferry.Vehicles, Weight = ferry.Weight }; _journeys.Add(journey); ManagementSystem.SetFerryJourney(ferry, journey); ManagementSystem.MoveFerry(entry.OriginId, entry.DestinationId, ferry); } }
// Assign ferries to their ports private void PortFerries() { foreach (var port in _ports) { foreach (var ferry in ManagementSystem.GetAllFerries()) { if (ferry.HomePortId == port.Id) { port.Ferries.Add(ferry); } } } }
// Assign a time table to a port private void PortTimeTable() { foreach (var port in _ports) { foreach (var entry in ManagementSystem.GetFullTimeTable()) { if (entry.TimeTableId == port.Id) { entry.OriginId = entry.TimeTableId; port.TimeTable.Add(entry); } } } }
// Books a journey if possible public bool Book(int journeyId, int passengers, int vehicles, int weight) { if (CanBook(journeyId, passengers, vehicles, weight)) { _bookings.Add(new Booking { Journey = ManagementSystem.GetJourney(journeyId), Passengers = passengers, Vehicles = vehicles, Weight = weight }); return(true); } return(false); }
// Determines if a user can book a particular journey based off of what they need (# passengers, # tickets, weight) private bool CanBook(int journeyId, int passengers, int vehicles, int weight) { foreach (var journey in ManagementSystem.GetAllJourneys()) { if (journey.Id == journeyId) { var bookings = _bookings.Where(x => x.Journey.Id == journeyId); var seatsLeft = journey.Ferry.Passengers - bookings.Sum(x => x.Passengers); var vehiclesLeft = journey.Ferry.Vehicles - bookings.Sum(x => x.Vehicles); var weightLeft = journey.Ferry.Weight - bookings.Sum(x => x.Weight); return(seatsLeft >= passengers && vehiclesLeft >= vehicles && weightLeft >= weight); } } return(false); }
// List Ports Command - Displays all ports private static void ListPortsCommand() { try { Console.WriteLine("Ports:"); Console.WriteLine("------"); foreach (var port in ManagementSystem.GetAllPorts()) { Console.WriteLine("Port {0} - {1}", port.Id, port.Name); } } catch (Exception) { ListPortError(); } }
// List Bookings Command - Displays all bookings private static void ListBookingsCommand() { try { Console.WriteLine("Bookings:"); Console.WriteLine("---------"); foreach (var b in ManagementSystem.GetAllBookings()) { Console.WriteLine("Journey {0} from {1} to {2} has {3} passengers and {4} vehicles weighing a total of {5} tons.", b.Journey.Id, b.Journey.Origin.Name, b.Journey.Destination.Name, b.Passengers, b.Journey.Vehicles, b.Journey.Weight ); } } catch (Exception) { ListBookingsError(); } }
// Main - Start reading commands public static void Main() { ManagementSystem managementSystem = new ManagementSystem(); CommandLoop(); }