2016-11-10 4 views
-2

私のコンソールベースの飛行機の座席予約コード(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

答え前の質問...これは学術的解決策ですか?私は思っていますから、あなたは記憶のすべてを店内に計画しているので、そうであるようです。だから私はあなたの現在のアーキテクチャに適した何かに答えることができます。 – Ismael

答えて

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; 
    } 
} 
+0

はい、2つのforループを変更するだけです。初期値は最初の行/ colで、最後の値は最後の行/ colです。私はそれをあなたの例のように見せました(第1クラス行1〜2、第2クラス行3〜5)。同じ原則が列に適用されます。 – Ismael

+0

ファーストクラスの行を1から5に、2番目のクラスの6から35を入れます。 – Ismael

+0

更新の種類は何ですか?私はあなたがクラスの初期化のすべての座席を埋める後、passangersを埋める必要があると思った。 – Ismael

関連する問題