我需要基于控制台的飞机座位预订代码(用c#编写)的帮助。
尝试实施:
以下是我认为与我问的问题相关的飞机类的代码,如果有必要我可以提供更多(座位方法已经简化,因为它非常大),谢谢:
public static void seating()
{
Console.Write("Key: " + "* - Available " + " X - Occupied" + "\n" + "\n");
//FC
Console.Write(" \tA\tB\tC\tD\tE\tF");
Console.Write("\n");
Console.Write("Row 1\t");
Console.Write(airchar[0, 0]);
Console.Write("\t");
Console.Write(airchar[0, 1]);
Console.Write("\t");
Console.Write(airchar[0, 2]);
Console.Write("\t");
Console.Write(airchar[0, 3]);
Console.Write("\n");
Console.Write("Row 2\t");
Console.Write(airchar[1, 0]);
Console.Write("\t");
Console.Write(airchar[1, 1]);
Console.Write("\t");
Console.Write(airchar[1, 2]);
Console.Write("\t");
Console.Write(airchar[1, 3]);
Console.Write("\n");
//EC
Console.Write("Row 3\t");
Console.Write(airchar[2, 0]);
Console.Write("\t");
Console.Write(airchar[2, 1]);
Console.Write("\t");
Console.Write(airchar[2, 2]);
Console.Write("\t");
Console.Write(airchar[2, 3]);
Console.Write("\t");
Console.Write(airchar[2, 4]);
Console.Write("\t");
Console.Write(airchar[2, 5]);
Console.Write("\n");
Console.Write("Row 4\t");
Console.Write(airchar[3, 0]);
Console.Write("\t");
Console.Write(airchar[3, 1]);
Console.Write("\t");
Console.Write(airchar[3, 2]);
Console.Write("\t");
Console.Write(airchar[3, 3]);
Console.Write("\t");
Console.Write(airchar[3, 4]);
Console.Write("\t");
Console.Write(airchar[3, 5]);
Console.Write("\n");
Console.Write("Row 5\t");
Console.Write(airchar[4, 0]);
Console.Write("\t");
Console.Write(airchar[4, 1]);
Console.Write("\t");
Console.Write(airchar[4, 2]);
Console.Write("\t");
Console.Write(airchar[4, 3]);
Console.Write("\t");
Console.Write(airchar[4, 4]);
Console.Write("\t");
Console.Write(airchar[4, 5]);
Console.Write("\n");
public static void passengers()
{
if (ticket == "FC")
{
Console.WriteLine("Enter number of passengers traveling together (1 or 2 for First Class): ");
numPassenger = Convert.ToInt32(Console.ReadLine());
if (numPassenger == 1)
{
Console.WriteLine("Passenger 1 Full Name (BLOCK CAPITAL): ");
Console.ReadLine();
}
else if (numPassenger == 2)
{
Console.WriteLine("Passenger 1 Full Name (BLOCK CAPITAL): ");
Console.ReadLine();
Console.WriteLine("Passenger 2 Full Name (BLOCK CAPITAL): ");
Console.ReadLine();
}
else
{
return;
}
}
else if (ticket == "EC")
{
Console.WriteLine("Enter number of passengers traveling together (1 to 3 for Economy): ");
numPassenger = Convert.ToInt32(Console.ReadLine());
if (numPassenger == 1)
{
Console.WriteLine("Passenger 1 Full Name (BLOCK CAPITAL): ");
Console.ReadLine();
}
else if (numPassenger == 2)
{
Console.WriteLine("Passenger 1 Full Name (BLOCK CAPITAL): ");
Console.ReadLine();
Console.WriteLine("Passenger 2 Full Name (BLOCK CAPITAL): ");
Console.ReadLine();
}
else if (numPassenger == 3)
{
Console.WriteLine("Passenger 1 Full Name (BLOCK CAPITAL): ");
Console.ReadLine();
Console.WriteLine("Passenger 2 Full Name (BLOCK CAPITAL): ");
Console.ReadLine();
Console.WriteLine("Passenger 3 Full Name (BLOCK CAPITAL): ");
Console.ReadLine();
}
else
{
return;
}
}
return;
}
答案 0 :(得分:1)
我在表单上快速思考以改进您的解决方案建模。我相信它可以以更好的方式完成,但这可以是一个良好的开端。
似乎您不仅需要存储座位信息,还需要随意快速找到它。所以我制作了一系列物品来帮助按位置或乘客名称存储,更换和查找座位。希望它有所帮助。
帮助程序类代码:
public class AirplaneSeating
{
private List<Seat> seats;
public AirplaneSeating()
{
seats = new List<Seat>();
// Filling FC Rows
for (byte row = 1; row <= 5; row++)
for (byte column = 1; column <= 4; column++)
seats.Add(new Seat(row, column));
// Filling EC Rows
for (byte row = 6; row <= 35; row++)
for (byte column = 1; column <= 6; column++)
seats.Add(new Seat(row, column));
}
// You can use this indexed property to search for a seat by it's address
public Seat this[string address]
{
get
{
try
{
var start = 0;
var end = address.Length - 1;
var row = byte.Parse(address.Substring(start, end));
var col = address[end];
return seats.First(
s => s.Position.Row == row && s.Position.Column.Name == col);
}
catch
{
return null;
}
}
}
// You can use this indexed property if you need to loop through seats
public Seat this[byte row, byte column]
{
get
{
return seats.FirstOrDefault(
s => s.Position.Row == row && s.Position.Column.Number == column);
}
}
// This method can be used to find a seat occupied by a passenger
public Seat FindSeatOccupiedBy(string passengerName)
{
return seats.FirstOrDefault(
s => s.Passenger.ToUpper().Contains(passengerName.ToUpper()));
}
public void ReserveSeat()
{
char ticketType = char.MinValue;
byte passengersTogether = 0;
int maxPassengersTogether;
bool inputOk = false;
while (ticketType != 'F' && ticketType != 'S')
{
Console.Write("Enter the ticket type (F = First Class, S = Second Class): ");
ticketType = char.ToUpper(Console.ReadLine()[0]);
}
if (ticketType == 'F')
maxPassengersTogether = 2;
else
maxPassengersTogether = 3;
while (!inputOk && (passengersTogether < 1 ||
passengersTogether > maxPassengersTogether))
{
Console.Write("Enter the number of passengers traveling together (1 or {0}): ",
maxPassengersTogether);
inputOk = byte.TryParse(Console.ReadLine(), out passengersTogether);
}
for (byte passengerNumber = 1;
passengerNumber <= passengersTogether;
passengerNumber++)
{
Console.Write("Passenger {0} full name (BLOCK CAPITAL): ", passengerNumber);
string passengerName = Console.ReadLine();
Seat seat = null;
string lastSeatAddress = null;
// Choosen seat address must be valid (or else it will return null)
// Choosen seat must match ticket type
// Choosen seat must not be occupied
while (seat == null ||
seat.Class != (ClassType)ticketType ||
seat.Status == SeatStatus.Occupied)
{
if (seat != null)
{
if (seat.Class != (ClassType)ticketType)
Console.WriteLine("Seat does not match ticket type.");
if (seat.Status == SeatStatus.Occupied)
Console.WriteLine("This seat is occupied by {0}.",
seat.Passenger);
}
else
{
if (lastSeatAddress != null)
Console.WriteLine("Invalid seat address.");
}
Console.Write("Enter passenger seat (example: 1A): ");
lastSeatAddress = Console.ReadLine().ToUpper();
seat = this[lastSeatAddress];
}
seat.Passenger = passengerName;
Console.WriteLine("Seat {0} reserved for passenger {1}.",
seat.Position, seat.Passenger);
}
Console.WriteLine("Reservation complete.");
Console.WriteLine();
}
public void PrintPassengersToConsole()
{
var occupiedSeats = seats.Where(s => s.Status == SeatStatus.Occupied);
if (occupiedSeats.Any())
{
Console.WriteLine("Seating\tPassenger Name");
foreach (var seat in occupiedSeats
.OrderBy(s => s.Position.Row)
.ThenBy(s => s.Position.Column.Number))
{
Console.WriteLine("{0} \t{1}",
seat.Position,
seat.Passenger);
}
}
else
{
Console.WriteLine("The airplane does not have any reservation.");
}
Console.WriteLine();
}
// This method can be called to print the seating to console, like you needed
public void PrintToConsole()
{
Console.Write("Key: " + "* - Available " + " X - Occupied" + "\n" + "\n");
Console.Write(" \tA\tB\tC\tD\tE\tF");
Console.WriteLine();
foreach (var row in seats.GroupBy(s => s.Position.Row)
.OrderBy(r => r.Key))
{
Console.Write(string.Format("Row {0}\t", row.Key));
foreach (var column in row.GroupBy(s => s.Position.Column.Number)
.OrderBy(c => c.Key))
{
foreach (var seat in column)
{
Console.Write((char)seat.Status);
Console.Write("\t");
}
}
Console.WriteLine();
}
Console.WriteLine();
}
}
// This class represents your Seat
public class Seat
{
public Seat(byte row, char column)
{
Position = new SeatPosition(row, column);
}
public Seat(byte row, byte column)
{
Position = new SeatPosition(row, column);
}
// Set Passenger to null to make the seat available.
public string Passenger { get; set; }
public SeatPosition Position { get; private set; }
public SeatStatus Status
{
get
{
// When you assign a passenger to a seat, status will change
if (Passenger == null)
return SeatStatus.Available;
else
return SeatStatus.Occupied;
}
}
public ClassType Class
{
get
{
// Remember to change this if you want more rows of FC
if (Position.Row <= 5)
return ClassType.FirstClass;
else
return ClassType.SecondClass;
}
}
}
// This enum represents the status of the Seat
public enum SeatStatus
{
Available = '*',
Occupied = 'X'
}
// This enum represents the class type of the Seat
public enum ClassType
{
FirstClass = 'F',
SecondClass = 'S'
}
// This class represents a seat position
public class SeatPosition
{
public SeatPosition(byte row, char column)
{
Row = row;
Column = new SeatColumn(column);
}
public SeatPosition(byte row, byte column)
{
Row = row;
Column = new SeatColumn(column);
}
public byte Row { get; private set; }
public SeatColumn Column { get; private set; }
public override string ToString()
{
return string.Format("{0}{1}", Row, Column.Name);
}
}
// This class is to help defining and printing seat columns
public class SeatColumn
{
public SeatColumn(byte number)
{
Number = number;
}
public SeatColumn(char name)
{
Name = name;
}
public byte Number { get; set; }
public char Name
{
get { return NumberToName(Number); }
set { Number = NameToNumber(value); }
}
private static char NumberToName(byte number)
{
switch (number)
{
case 1:
return 'A';
case 2:
return 'B';
case 3:
return 'C';
case 4:
return 'D';
case 5:
return 'E';
case 6:
return 'F';
}
throw new Exception("Invalid column number.");
}
private static byte NameToNumber(char name)
{
switch (name)
{
case 'A':
return 1;
case 'B':
return 2;
case 'C':
return 3;
case 'D':
return 4;
case 'E':
return 5;
case 'F':
return 6;
}
throw new Exception("Invalid column name.");
}
}
下面我做了一个关于如何使用这些类的快速演示代码。我想这可以让您在实施应用程序时更轻松。您只需要像输入一样从输入中获取数据。
static void Main(string[] args)
{
var seating = new AirplaneSeating();
// Use PrintToConsole to print seating to screen.
seating.PrintToConsole();
// You can find a specific seat by its position.
var seat1 = seating["1A"]; // Gives me seat 1A
var seat2 = seating[1, 1]; // Also gives me seat 1A
// Then you can put a passenger on this seat if you want.
seat2.Passenger = "Jhon";
// When you set a passenger name to the seat, its status will change.
Console.WriteLine("Seat 1A status is {0}.", seat1.Status); // Occupied
// You can find a specific seat by passenger name.
// This line of code gives me seat 1A, wich is now Jhon's seat.
var occupiedSeat1 = seating.FindSeatOccupiedBy("Jhon");
// You can find by typing a part of the passenger name,
// and you don't need to worry about capitalization
var occupiedSeat2 = seating.FindSeatOccupiedBy("JHO");
// You can print seat information on console.
Console.WriteLine("Seat {0} is occupied by {1}.",
occupiedSeat2.Position, occupiedSeat2.Passenger);
Console.ReadLine();
}
这是您的应用程序的实际示例。将此代码放入您的Program类(使用Main方法的类)。
static AirplaneSeating seating;
static void Main(string[] args)
{
seating = new AirplaneSeating();
Options();
}
static void Options()
{
bool optionOk = false;
byte option = 0;
while (!optionOk)
{
Console.Clear();
Console.WriteLine("Select from the following menu:");
Console.WriteLine("1. To Add Passenger");
Console.WriteLine("2. To View Seating");
Console.WriteLine("3. To View Passenger List");
Console.WriteLine("4. Quit Application" + "\n");
byte.TryParse(Console.ReadLine(), out option);
if (option >= 1 && option <= 4)
{
optionOk = true;
}
}
switch (option)
{
case 1:
Console.Clear();
seating.ReserveSeat();
Console.WriteLine();
Console.Write("Please press enter to return to main menu.");
Console.ReadLine();
Console.Clear();
Options();
break;
case 2:
Console.Clear();
seating.PrintToConsole();
Console.WriteLine();
Console.Write("Please press enter to return to main menu.");
Console.ReadLine();
Console.Clear();
Options();
break;
case 3:
Console.Clear();
seating.PrintPassengersToConsole();
Console.WriteLine();
Console.Write("Please press enter to return to main menu.");
Console.ReadLine();
Console.Clear();
Options();
break;
case 4:
Environment.Exit(0);
break;
}
}