I need help solving this assignment Below are the directions to the assignment.
ID: 3589444 • Letter: I
Question
I need help solving this assignment
Below are the directions to the assignment. Also there will be a starting code that i have to build my code around.
The code must be done in the C# programming language, and must be EXTREAMLY easy/ basic as I am very new to programming
Program must be a GUI (Graphic user interface)
You help would be much appreciated.
-------------------------------------------------------------------------------------
Directions to assignment
You must use a simple GUI with radio buttons to select a business or economy class.
Use separate classes for seats selection logic and GUI front-end.
This is your old friend and extra so far, but VERY desirable to complete. Must use one or two (better) arrays of booleans.
true a seat is selected, otherwise false.
below is a copy of the code that you will need to start with.
On Form load, make radio button Economy selected (default).
You will use only one button named Reserve that should be disabled after all seats are reserved.
What is new here is how seats are represented: you have to use checkboxes. At the beginning, no checkboxes are selected.
After each click on the Reserve button a corresponding checkbox should be selected and disabled.
Switching radiobuttons should change which checkboxes are selected. Selection in each class should be sequential.
No Random seat selection allowed.
You can add a background image.
You must have these methods implemented in a business class Reserver that is a separate class from your Form:
public bool IsFirstClassAvailable()
public bool IsEconomyClassAvailable()
public int ReserveFirstClassAnySeat()
public int ReserveEconomyClassAnySeat()
public bool[] GetAllSeats()
public bool[] GetFirstClassSeats()
public bool[] GetEconomyClassSeats()
public void DisplayAllSeats() //this may be in your Form
-------------------------------------------------------------------------------------
The code that you will start with
AirlineReservation.cs
using System;
//Version with Display
namespace AirlineReservationV2
{
class AirlineReservation
{
private static int NO_CHOICE = 0;
private static int FIRST_CLASS_CHOICE = 1;
private static int ECONOMY_CLASS_CHOICE = 2;
private static int EXIT_CHOICE = 3;
private Reserver reserver = new Reserver(); //class-level field. Instantiate Reserver to use in this class
public static void Main(string[] args)
{
AirlineReservation airline = new AirlineReservation();
airline.processReservations();
//pause
Console.ReadKey();
}
private void processReservations()
{
int userChoice = NO_CHOICE;
while (userChoice != EXIT_CHOICE) //
{
userChoice = GetUserInput();
if (userChoice == FIRST_CLASS_CHOICE)
{
HandleFirstClass();
}
else if (userChoice == ECONOMY_CLASS_CHOICE) //very similar to First class
{
HandleEconomyClass();
}
else //otherwise EXIT
{
Console.WriteLine("Thank you!");
}
}
}
private int GetUserInput()
{
int input = 0; //user input
bool IsInputValid = false; //start with this, so loop will proceed at least once
while (IsInputValid == false)
{
if (reserver.IsFirstClassAvailable() && reserver.IsEconomyClassAvailable())
{
Console.WriteLine("1. Reserve FIRST Class");
Console.WriteLine("2. Reserve ECONOMY Class");
}
else if (reserver.IsFirstClassAvailable() && !reserver.IsEconomyClassAvailable())
{
Console.WriteLine("ECONOMY class is full. FIRST class is still available");
Console.WriteLine("1. Reserve FIRST Class");
}
else if (!reserver.IsFirstClassAvailable() && reserver.IsEconomyClassAvailable())
{
Console.WriteLine("FIST class is full. ECONOMY class is still available");
Console.WriteLine("2. Reserve ECONOMY Class");
}
else if (!reserver.IsFirstClassAvailable() && !reserver.IsEconomyClassAvailable())
{
Console.WriteLine("This plane is full. Next flight in 3 hours. Goodbye!");
Console.WriteLine("2. Reserve ECONOMY Class");
}
Console.WriteLine("3. EXIT");
try
{
input = Convert.ToInt32(Console.ReadLine());
if (input == FIRST_CLASS_CHOICE || input == ECONOMY_CLASS_CHOICE || input == EXIT_CHOICE)
{
IsInputValid = true; //out of the loop
}
else
{
Console.WriteLine("Invalid Selection. Re-enter");
}
}
catch (Exception)
{
Console.WriteLine("Not a number.");
}
}
return input;
}
private void HandleFirstClass()
{
bool isEmptySeatInFirstClass = reserver.IsFirstClassAvailable();
if (isEmptySeatInFirstClass == true)
{
int firstClassSeatReserved = reserver.ReserveFirstClassAnySeat(); //reserve a seat
Console.WriteLine("You reserved FIRST class seat {0}", firstClassSeatReserved + 1); //+1 because return starts with 0
DisplaySeats();
}
}
private void HandleEconomyClass()
{
bool isEmptySeatInEconomytClass = reserver.IsEconomyClassAvailable();
if (isEmptySeatInEconomytClass == true)
{
int economyClassSeatReserved = reserver.ReserveEconomyClassAnySeat();
Console.WriteLine("You reserved ECONOMY class seat {0}", economyClassSeatReserved + 1); //+1 because return starts with 0
DisplaySeats();
}
}
//simple all display
private void DisplaySeats()
{
bool[] fseats = reserver.GetFirstClassSeats();
DisplaySeats("Fisrt", fseats, 'x');
bool[] eseats = reserver.GetEconomyClassSeats();
DisplaySeats("Economy", eseats, 'o');
}
//title: First or Economy
//seats: fseats or eseats
//seatChar: x (for First class) o (for Economy)
private void DisplaySeats(String title, bool[] seats, char seatChar)
{
Console.WriteLine("{0} Class Seats:", title);
char isTaken = '0';
HorizontalLine(seats.Length);
//seat numbers
SeatNumbers(seats.Length);
Console.WriteLine();
HorizontalLine(seats.Length);
//seelction
for (int i = 0; i < seats.Length; i++)
{
if (seats[i] == true)
{
isTaken = seatChar;
}
else if (seats[i] == false)
{
isTaken = '-';
}
Console.Write("| {0} ", isTaken);
}
Console.WriteLine("|");
HorizontalLine(seats.Length);
Console.WriteLine();
}
private void HorizontalLine(int times)
{
for (int i = 0; i < times; i++)
{
Console.Write(" -- ");
}
Console.WriteLine();
}
private void SeatNumbers(int times)
{
for (int i = 0; i < times; i++)
{
Console.Write("| {0} ", i + 1);
}
Console.Write("|");
}
}
}
Reserver.cs
using System;
namespace AirlineReservationV2
{
public class Reserver /*: IReservable*/
{
private static int FIRST_CLASS_NUM_SEATS = 5;
private static int ECONOMY_CLASS_NUM_SEATS = 5;
private bool[] fseats = new bool[FIRST_CLASS_NUM_SEATS]; //FIRST class seats
private bool[] eseats = new bool[ECONOMY_CLASS_NUM_SEATS]; //ECONOMY class seats
public bool IsFirstClassAvailable()
{
foreach (var seat in fseats) //using foreach loop
{
if (seat == false)
{
return true; //available
}
}
return false; //otherwise unavailable
}
//same as first class, just different syntax
public bool IsEconomyClassAvailable()
{
for (int i = 0; i < eseats.Length; i++) //using for loop
{
if (eseats[i] == false)
{
return true; //available
}
}
return false; //otherwise unavailable
}
private bool[] GetAllFirstSeats()
{
return fseats;
}
private bool[] GetAllEconomySeats()
{
return eseats;
}
//should be preceeded with IsFirstClassAvailable to avoid -1
//returns reserved seat
public int ReserveFirstClassAnySeat()
{
for (int i = 0; i < fseats.Length; i++)
{
if (fseats[i] == false)
{
fseats[i] = true;
return i; //available seat number
}
}
return -1; //otherwise unavailable
}
//should be preceeded with IsEconomyClassAvailable to avoid -1
//returns reserved seat
public int ReserveEconomyClassAnySeat()
{
for (int i = 0; i < eseats.Length; i++)
{
if (eseats[i] == false)
{
eseats[i] = true;
return i; //available seat number
}
}
return -1; //otherwise unavailable
}
//for displying seats
public bool[] GetFirstClassSeats()
{
return fseats;
}
//for displying seats
public bool[] GetEconomyClassSeats()
{
return eseats;
}
}
}
---------------------------------------------------------------------
Thank you so much for all of your help!
Explanation / Answer
using System;
//Version with Display
namespace AirlineReservationV2
{
class AirlineReservation
{
private static int NO_CHOICE = 0;
private static int FIRST_CLASS_CHOICE = 1;
private static int ECONOMY_CLASS_CHOICE = 2;
private static int EXIT_CHOICE = 3;
private Reserver reserver = new Reserver(); //class-level field. Instantiate Reserver to use in this class
public static void Main(string[] args)
{
AirlineReservation airline = new AirlineReservation();
airline.processReservations();
//pause
Console.ReadKey();
}
private void processReservations()
{
int userChoice = NO_CHOICE;
while (userChoice != EXIT_CHOICE) //
{
userChoice = GetUserInput();
if (userChoice == FIRST_CLASS_CHOICE)
{
HandleFirstClass();
}
else if (userChoice == ECONOMY_CLASS_CHOICE) //very similar to First class
{
HandleEconomyClass();
}
else //otherwise EXIT
{
Console.WriteLine("Thank you!");
}
}
}
private int GetUserInput()
{
int input = 0; //user input
bool IsInputValid = false; //start with this, so loop will proceed at least once
while (IsInputValid == false)
{
if (reserver.IsFirstClassAvailable() && reserver.IsEconomyClassAvailable())
{
Console.WriteLine("1. Reserve FIRST Class");
Console.WriteLine("2. Reserve ECONOMY Class");
}
else if (reserver.IsFirstClassAvailable() && !reserver.IsEconomyClassAvailable())
{
Console.WriteLine("ECONOMY class is full. FIRST class is still available");
Console.WriteLine("1. Reserve FIRST Class");
}
else if (!reserver.IsFirstClassAvailable() && reserver.IsEconomyClassAvailable())
{
Console.WriteLine("FIST class is full. ECONOMY class is still available");
Console.WriteLine("2. Reserve ECONOMY Class");
}
else if (!reserver.IsFirstClassAvailable() && !reserver.IsEconomyClassAvailable())
{
Console.WriteLine("This plane is full. Next flight in 3 hours. Goodbye!");
Console.WriteLine("2. Reserve ECONOMY Class");
}
Console.WriteLine("3. EXIT");
try
{
input = Convert.ToInt32(Console.ReadLine());
if (input == FIRST_CLASS_CHOICE || input == ECONOMY_CLASS_CHOICE || input == EXIT_CHOICE)
{
IsInputValid = true; //out of the loop
}
else
{
Console.WriteLine("Invalid Selection. Re-enter");
}
}
catch (Exception)
{
Console.WriteLine("Not a number.");
}
}
return input;
}
private void HandleFirstClass()
{
bool isEmptySeatInFirstClass = reserver.IsFirstClassAvailable();
if (isEmptySeatInFirstClass == true)
{
int firstClassSeatReserved = reserver.ReserveFirstClassAnySeat(); //reserve a seat
Console.WriteLine("You reserved FIRST class seat {0}", firstClassSeatReserved + 1); //+1 because return starts with 0
DisplaySeats();
}
}
private void HandleEconomyClass()
{
bool isEmptySeatInEconomytClass = reserver.IsEconomyClassAvailable();
if (isEmptySeatInEconomytClass == true)
{
int economyClassSeatReserved = reserver.ReserveEconomyClassAnySeat();
Console.WriteLine("You reserved ECONOMY class seat {0}", economyClassSeatReserved + 1); //+1 because return starts with 0
DisplaySeats();
}
}
//simple all display
private void DisplaySeats()
{
bool[] fseats = reserver.GetFirstClassSeats();
DisplaySeats("Fisrt", fseats, 'x');
bool[] eseats = reserver.GetEconomyClassSeats();
DisplaySeats("Economy", eseats, 'o');
}
//title: First or Economy
//seats: fseats or eseats
//seatChar: x (for First class) o (for Economy)
private void DisplaySeats(String title, bool[] seats, char seatChar)
{
Console.WriteLine("{0} Class Seats:", title);
char isTaken = '0';
HorizontalLine(seats.Length);
//seat numbers
SeatNumbers(seats.Length);
Console.WriteLine();
HorizontalLine(seats.Length);
//seelction
for (int i = 0; i < seats.Length; i++)
{
if (seats[i] == true)
{
isTaken = seatChar;
}
else if (seats[i] == false)
{
isTaken = '-';
}
Console.Write("| {0} ", isTaken);
}
Console.WriteLine("|");
HorizontalLine(seats.Length);
Console.WriteLine();
}
private void HorizontalLine(int times)
{
for (int i = 0; i < times; i++)
{
Console.Write(" -- ");
}
Console.WriteLine();
}
private void SeatNumbers(int times)
{
for (int i = 0; i < times; i++)
{
Console.Write("| {0} ", i + 1);
}
Console.Write("|");
}
}
}
Reserver.cs
using System;
namespace AirlineReservationV2
{
public class Reserver /*: IReservable*/
{
private static int FIRST_CLASS_NUM_SEATS = 5;
private static int ECONOMY_CLASS_NUM_SEATS = 5;
private bool[] fseats = new bool[FIRST_CLASS_NUM_SEATS]; //FIRST class seats
private bool[] eseats = new bool[ECONOMY_CLASS_NUM_SEATS]; //ECONOMY class seats
public bool IsFirstClassAvailable()
{
foreach (var seat in fseats) //using foreach loop
{
if (seat == false)
{
return true; //available
}
}
return false; //otherwise unavailable
}
//same as first class, just different syntax
public bool IsEconomyClassAvailable()
{
for (int i = 0; i < eseats.Length; i++) //using for loop
{
if (eseats[i] == false)
{
return true; //available
}
}
return false; //otherwise unavailable
}
private bool[] GetAllFirstSeats()
{
return fseats;
}
private bool[] GetAllEconomySeats()
{
return eseats;
}
//should be preceeded with IsFirstClassAvailable to avoid -1
//returns reserved seat
public int ReserveFirstClassAnySeat()
{
for (int i = 0; i < fseats.Length; i++)
{
if (fseats[i] == false)
{
fseats[i] = true;
return i; //available seat number
}
}
return -1; //otherwise unavailable
}
//should be preceeded with IsEconomyClassAvailable to avoid -1
//returns reserved seat
public int ReserveEconomyClassAnySeat()
{
for (int i = 0; i < eseats.Length; i++)
{
if (eseats[i] == false)
{
eseats[i] = true;
return i; //available seat number
}
}
return -1; //otherwise unavailable
}
//for displying seats
public bool[] GetFirstClassSeats()
{
return fseats;
}
//for displying seats
public bool[] GetEconomyClassSeats()
{
return eseats;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.