A small airline has just purchased a computer for its new au

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).

     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.]

How many seats do you need? 5

Name: Peter Chen

Name: Frank Chao

Name: Al Molin

Name: Walter Rotenberry

Name: Hong Cui

How many seats do you need? 3

Name: WitoldSieradzan

Name: Mary Orazem

Name: Hillary Paul

How many seats do you need? 4

Sorry! Only 2 seats left

How many seats do you need? 2

Name: Cindy Foster

Name: Man-Chi Leung

All seats are sold.

List of passengers:

Peter Chen

Frank Chao

Al Molin

Walter Rotenberry

Hong Cui

WitoldSieradzan

Mary Orazem

Hillary Paul

Cindy Foster

Man-Chi Leung

Press any key to continue . . .

Solution

public static void Main(string[] args)
{
int total = 10;
string[] passengers = new string[10];
int ticketsSold = 0;
for(int answer = 0; answer < passengers.Length; )
{
ticketsSold = 0;
while(ticketsSold == 0)
{
Console.WriteLine(\"How many seats do you need? \");
if(!int.TryParse(Console.ReadLine(), out ticketsSold))
Console.WriteLine(\"Invalid entry - whole numbers only\");
}
if(ticketsSold <= total)
{
for(int i = 0; i < ticketsSold; i++)
{
Console.Write(\"Name: \");
passengers[i + answer] = Console.ReadLine();
}
total -= ticketsSold;
answer += ticketsSold;
}
else if(ticketsSold > total)
{
Console.WriteLine(\"Sorry only {0} seats left\", total);
}
else if(ticketsSold == passengers.Length)
{
break;
}
}
Console.WriteLine(\"All seats are sold\ List of passengers:\ \");
for(int x = 0; x < passengers.Length; x++)
{
Console.WriteLine(\"{0}\", passengers[x]);
}
Console.ReadKey();
}

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 t
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 t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site