TINFO200/Cs3Apps/Airline/Program.cs
2022-03-15 21:16:45 -07:00

255 lines
9.7 KiB
C#

/* Shaun Marquardt
* TINFO 200
* CS3: Airline
* ****************************************************
* Change History
* Date Developer Description
* 2020-02-18 marqusa File creation and initial implementation.
* I decided to split PurchaseFirstClassTicket
* and PurchaseEconomyTicket into two seperate functions to make it easier to call
* if one class of ticket was full and it was acceptable to buy the other seats.
* Created a SeatsRemaining function to inform the
* user if the flight's all booked up and check seats remaining in a given class
* if one type is full.
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Airline
{
class Program
{
//Make the seating chart a more globally available variable.
//REQ 1: Use a one-dimensional array of type bool
//to represent the 10 seats of the plane
static bool[] seatingChart = new bool[10];
/* This is a program for a small airline to manage its new
* reservation system. This program assigns seats on each
* flight of the airline's only plane (capacity: 10 seats).
* The user will be presented with a menu to choose
* first class (seats 1-5) or economy (seats 6-10).
*
* Requirements:
* 1) Use a one-dimensional array of type bool to represent
* the seating chart of the plane, initialized to false to
* indicate all seats are empty.
* 2) As each seat is assigned, set the corresponding element
* to true to indicate the seat is no longer available.
* 3) The program should never assign a seat that is already
* assigned.
* 4) When the economy section is full, the app should ask the
* person if it's acceptable to sit in first class or vice/versa.
* 4a) If acceptable, make the seat assignment. If not acceptable
* then display the message "Next flight leaves in 3 hours".
*/
static void Main(string[] args)
{
string mainMenuChoice;
//Initialize each seat to false.
for (int i = 0; i < seatingChart.Length; i++)
{
seatingChart[i] = false;
}
// user interface
// 1 - sell the user of the software to the user
// 2 - instructions (basic) on how to use the software
Console.WriteLine(@"
****************************************************
**** Welcome to the Airline Reservation System *****
****************************************************
This is a program for Small Airlines Incorporated
to assign seats for all flights on the airline's
only plane (capacity, 10 seats -- book now!).
You will be presented with a menu if you would like
to fly First Class or Economy. After choosing, you
will be assigned a seat if it is available. If no
seat is available, you will be asked if you would
like a seat in the other class of seats. If that is
still not satisfactory for your flying needs or all
seats are taken, we will inform you of our next flight
departure time.
Please watch the printed warnings about input types
carefully so that you don't crash the program!
****************************************************
");
//Enter main program loop to keep purchasing tickets
//like a ticketing agent.
do
{
//Input
//Display the menu.
Console.WriteLine("1) First Class");
Console.WriteLine("2) Economy");
Console.WriteLine("3) Exit Program");
Console.Write("What tickets would you like to purchase (1, 2, or 3): ");
//Get the input from the user and store it as a string
//as C# allows us to switch on a string, and does not
//risk crashing the program on int.Parse()
mainMenuChoice = Console.ReadLine();
switch (mainMenuChoice)
{
case "1":
PurchaseFirstClassTicket();
break;
case "2":
PurchaseEconomyTicket();
break;
case "3":
break;
default:
Console.WriteLine("Sorry, that's not a valid selection.");
break;
} //end switch mainMenuChoice
} //end do... while
while (mainMenuChoice != "3");
//salutation
Console.WriteLine("\nThank you for using this program! Good bye!");
} //END main
private static void PurchaseEconomyTicket()
{
string userAcceptsSeatClassChange = String.Empty;
//If there are seats remaining in the Economy class.
if (SeatsRemaining("Economy") > 0)
{
for (int i = 5; i < seatingChart.Length; i++)
{
/* Assign the next empty seat.
* Unfortunately I use break here because
* I don't know where the next index of False is
* in the range of seating area. Normally this is
* done with while, but since I am working in the
* middle of an array, it can't be done like that.
*/
if (!seatingChart[i])
{
seatingChart[i] = true;
break;
}
}
Console.WriteLine("Thank you for booking an Economy class ticket with us.");
}
else if(SeatsRemaining("First Class") > 0) //Economy is full, but First Class has seats.
{
Console.Write("Sorry, Economy section is full. " +
"Is it acceptable to be seated in First Class? (Y/N): ");
userAcceptsSeatClassChange = Console.ReadLine();
if(userAcceptsSeatClassChange.ToUpper() == "Y" )
{
PurchaseFirstClassTicket();
}
else //User does not want first class seats.
{
Console.WriteLine("Next flight leaves in 3 hours.");
}
}
else //The flight is full.
{
Console.WriteLine("Sorry, we're all booked up. Next flight leaves in 3 hours.");
}
} //END PurchaseEconomyTicket
private static void PurchaseFirstClassTicket()
{
string userAcceptsSeatClassChange = String.Empty;
//If there are seats remaining in First Class.
if (SeatsRemaining("First Class") > 0)
{
for (int i = 0; i < 5; i++)
{
/* Assign the next empty seat.
* Unfortunately I use break here because
* I don't know where the next index of False is
* in the range of seating area. Normally this is
* done with while, but since I am working in the
* middle of an array, it can't be done like that.
*/
if (!seatingChart[i])
{
seatingChart[i] = true;
break;
}
}
Console.WriteLine("Thank you for booking a First Class ticket with us.");
}
else if (SeatsRemaining("Economy") > 0) //First Class is full, but economy has seats.
{
Console.Write("Sorry, First Class section is full. " +
"Is it acceptable to be seated in Economy? (Y/N): ");
userAcceptsSeatClassChange = Console.ReadLine();
if (userAcceptsSeatClassChange.ToUpper() == "Y")
{
PurchaseEconomyTicket();
}
else
{
Console.WriteLine("Next flight leaves in 3 hours.");
}
}
else //The flight is full.
{
Console.WriteLine("Sorry, we're all booked up. Next flight leaves in 3 hours.");
}
} //END PurchaseFirstClassTicket
/* Gets the number of seats remaining in
* a given seating class, "Economy" or "First Class"
*
* Preconditions: not yet
* Inputs: string seatClass as "Economy" or "First Class"
* Outputs: integer
* Postconditions: not yet
*/
private static int SeatsRemaining(string seatClass)
{
//Setup variables.
int countOfSeatsRemaining = 0;
int start_index = 0;
int end_index = 0;
//Depending on seat class, we change where we
//start and end our for-loop to count the
//number of seats remaining in a given class.
switch(seatClass)
{
case "Economy":
start_index = 5;
end_index = 9;
break;
case "First Class":
start_index = 0;
end_index = 4;
break;
}
//For loop to count the number of seats remaining, seatClass agnostic.
for(int i=start_index; i<=end_index; i++)
{
if (!seatingChart[i])
countOfSeatsRemaining++;
}
//Return the number of seats remaining.
return countOfSeatsRemaining;
} //END SeatsRemaining
} //END class Program
}