Question
A small airline has just purchased a computer for its new automated reservation system. You have been asked to develop the new system. You are to write an app to assign seats on each flight of the airline’s only plane (capacity: 10 seats).Create a C# console application Your app should sell tickets until the plane is full. For each customer, ask how many seats he wants to buy. If the plane has enough seats available, ask the customer to enter the name of each passenger. If the plane does not have enough seats, tell the customer how many seats are available (please see the example below). When the plane is full, display the names of all passengers. [Hint: Use a string array to store the names of the passengers. Create a variable to store the number of tickets sold. That will help you to decide where in the array to store each passenger’s name.]
Explanation / Answer
using System; namespace FunnyConsoleApplication { public class Program { static void Main(string[] args) { Airplane plane = new Airplane(); bool reserve = true; while (reserve) { Console.Clear(); Console.WriteLine("Enter [1] to fly First Class ({0} vacant)", plane.VacantFirstClassSeats()); Console.WriteLine("Enter [2] to fly Economy Class ({0} vacant)", plane.VacantEconomySeats()); string input = Console.ReadLine(); switch (input) { case "1": if (plane.HasFirstClassSeats) { plane.ReserveFirstClassSeat(); } else if (plane.HasEconomySeats) { if (IsOk("No vacancy, enter [y] to fly Economy instead?")) plane.ReserveEconomySeat(); else reserve = false; } else { reserve = false; } break; case "2": if (plane.HasEconomySeats) { plane.ReserveEconomySeat(); } else if (plane.HasFirstClassSeats) { if (IsOk("No vacancy, enter [y] to fly First Class instead?")) plane.ReserveFirstClassSeat(); else reserve = false; } else { reserve = false; } break; } Console.WriteLine(); } Console.WriteLine("No can do, good bye!"); Console.ReadLine(); } private static bool IsOk(string question) { Console.WriteLine(question); return string.Compare(Console.ReadLine(), "y", StringComparison.OrdinalIgnoreCase) == 0; } } public class Airplane { private readonly bool[] _seats = new bool[10]; public bool HasFirstClassSeats { get { return HasSeats(0); } } public bool HasEconomySeats { get { return HasSeats(5); } } public int VacantFirstClassSeats() { return GetVacant(0); } public int VacantEconomySeats() { return GetVacant(5); } public void ReserveFirstClassSeat() { Reserve(0); } public void ReserveEconomySeat() { Reserve(5); } private bool HasSeats(int index) { for (int i = index; i