how could I assign passenger name to a flight seat in c#

Viewed 387

I am working on a project for airline reservations in c# windows form. The user inputs their name and select seat by using two list box, one for rows (i.e.1 2 3 4 5) and one for columns (i.e A B C D).

When the user press book, the passenger will be added to seat list (i.e 1A - Sue). If the user press Show all seat, it displays all seat information may or may not have a passenger. (i.e. 1A - and 2A - David).

My problem is my code just record name once. if I press book again to record the second passenger, the second name will show on the second loop of seat information.

e.g First input name: Sue; Row:1 Seat: A.

The text box of "Show All Seats" will be:

1A - Sue
1B - 
....
5D -

Second input name: Steven; Row: 1 Seat C.

The text box of "Show All Seats" will be:

 1A - Sue
 1B - 
 ....
 5D -
 1A - 
 1B - 
 1C - Steven
 ....
 5D -

My code:

namespace airline
{
    public partial class Form1 : Form
    {
        //initialize seat in 2D array
        string[,] seat = new string[5, 4] {{"1A","1B","1C","1D" },
                                               {"2A","2B","2C","2D" },
                                               {"3A","3B","3C","3D" },
                                               {"4A","4B","4C","4D" },
                                               {"5A","5B","5C","5D" },};
        //initialize varialbes
        string row;
        string col;
        string selectedSeat;
        string output = String.Empty;
        public static List<Passenger> passengers;
        public Form1()
        {
            InitializeComponent();


        }

        private void showAllowSeatClick(object sender, EventArgs e)
        {

            seatTextBox.Text = output;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void bookClick(object sender, EventArgs e)
        {
            row = rowListBox.SelectedItem.ToString();
            col = seatListBox.SelectedItem.ToString();
            selectedSeat = row + col;
            for (int i = 0; i < seat.GetLength(0); i++)
            {
                for (int j = 0; j < seat.GetLength(1); j++)
                {
                    if (seat[i, j] == selectedSeat)
                    {
                        output += seat[i, j] + " - " + nameTextInput.Text +"\n";
                        nameTextInput.Clear();
                    }
                    else
                    {
                        output += seat[i, j] + " - " +"\n";
                    }

                }
            }


        }
    }
}
1 Answers

Instead of using an array of strings, I'd suggest building an array of an object:

public class Seat
{
    public string Id {get; set;}
    public string PassengerName {get; set;}
}

So an object would look like:

var seat = new Seat()
{
    Id = "1A",
    PassengerName = "John Doe"
}

And this way, when a user selects a seat you can assign the PassengerName property of that specific object.

Or, instead on an array, you could use a Dictionary object whose Key is the seat id, and the passenger name is the value.

Related