0

座席番号の値を取得して予約するテキストボックスを削除するコードに変更するには、リストボックスの座席表から希望の座席を選択して[予約する]をクリックするだけです。ここには、クリーンアップが必要な未使用のコードがいくつかあることを知っています。これを行うためにさまざまな方法で遊んでいるので、無視してください。

フォームコードは次のとおりです。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Reservations
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Flight curFlight;
        List<Flight> flightlist = new List<Flight>();

        Flight flight1 = new Flight("Cessna Citation X", "10:00AM", "Denver", 6, 2);
        Flight flight2 = new Flight("Piper Mirage", "10:00PM", "Kansas City", 3, 2);

        private void Form1_Load(object sender, EventArgs e)
        {
            MakeReservations();
            DisplayFlights();
            SetupFlights();


        }

        private void lstFlights_SelectedIndexChanged(object sender, EventArgs e)
        {
            curFlight = (Flight)lstFlights.SelectedItem;
            txtDepart.Text = curFlight.DepartureTime;
            txtDestination.Text = curFlight.Destination;
            string[] seatChart = curFlight.SeatChart;
            DisplaySeatChart(seatChart);
        }

        private void DisplaySeatChart(string[] seating)
        {
            lstSeatChart.Items.Clear();
            for (int seat = 0; seat <= seating.GetUpperBound(0); seat++)
            {
                lstSeatChart.Items.Add("Seat: " + (seat + 1) + "       " + seating[seat]);
            }
        }

        private void SetupFlights()
        {


            //flightlist.Add(flight1);
            //flightlist.Add(flight2);


        }

        private void MakeReservations()
        {
            flight1.MakeReservation("Dill", 12);
            flight1.MakeReservation("Deenda", 3);
            flight1.MakeReservation("Schmanda", 11);
            flight2.MakeReservation("Dill", 4);
            flight2.MakeReservation("Deenda", 2);
        }

        private void DisplayFlights()
        {
            lstFlights.Items.Clear();
            lstFlights.Items.Add(flight1);
            lstFlights.Items.Add(flight2);
        }

        private void btnMakeReservation_Click(object sender, EventArgs e)
        {
            string name;
            int seatNum;
            string[] seatChart = curFlight.SeatChart;

            if (txtCustomerName.Text != "" && txtSeatNum.Text != "")
            {
                name = txtCustomerName.Text;
                seatNum = Convert.ToInt16(txtSeatNum.Text);
                curFlight.MakeReservation(name, seatNum);

                lstSeatChart.Items.Clear();

                DisplaySeatChart(seatChart);
            }
            else
            {
                MessageBox.Show("Please Fill out Name and Seat Number.", "Reservation Error");
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            string name;
            int seatNum;
            string[] seatChart = curFlight.SeatChart;

            if (txtCustomerName.Text != "" && txtSeatNum.Text != "")
            {
                name = txtCustomerName.Text;
                seatNum = Convert.ToInt16(txtSeatNum.Text);
                curFlight.EditReservation(name, seatNum);

                lstSeatChart.Items.Clear();

                DisplaySeatChart(seatChart);
            }
            else
            {
                MessageBox.Show("Please Fill out Name and Seat Number.", "Reservation Error");
            }
        }
    }
}

ここにクラスコードがあります

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Reservations
{
    class Flight
    {
        private string mPlane;
        private string mDepartureTime;
        private string mDestination;
        private int mRows;
        private int mSeats;
        private string[] mSeatChart;

        public Flight()
        {
        }

        public Flight(string planeType, string departureTime, string destination, int numRows, int numSeatsPerRow)
        {
            this.Plane = planeType;
            this.DepartureTime = departureTime;
            this.Destination = destination;
            this.Rows = numRows;
            this.Seats = numSeatsPerRow;

            // create the seat chart array
            mSeatChart = new string[Rows * Seats];

            for (int seat = 0; seat <= mSeatChart.GetUpperBound(0); seat++)
            {
                mSeatChart[seat] = "Open";
            }
        }

        public string Plane
        {
            get { return mPlane; }
            set { mPlane = value; }
        }

        public string DepartureTime
        {
            get { return mDepartureTime; }
            set { mDepartureTime = value; }
        }

        public string Destination
        {
            get { return mDestination; }
            set { mDestination = value; }
        }

        public int Rows
        {
            get { return mRows; }
            set { mRows = value; }
        }

        public int Seats
        {
            get { return mSeats; }
            set { mSeats = value; }
        }

        public string[] SeatChart
        {
            get { return mSeatChart; }
            set { mSeatChart = value; }
        }

        public override string ToString()
        {
            return this.Plane;
        }


        public void MakeReservation(string name, int seat)
        {
            if (IsFull(seat) == false)
            {
                if (seat <= (Rows * Seats) && mSeatChart[seat - 1] == "Open")
                {
                    mSeatChart[seat - 1] = name;
                }
                else
                {
                    MessageBox.Show("Seat is taken.", "Reservation Error");

                }

            }
            else
            {
                MessageBox.Show("Plane is full please choose another flight.", "Reservation Error");
            }
        }

        public void EditReservation(string name, int seat)
        {
            if (seat <= (Rows * Seats) && mSeatChart[seat - 1] != "Open")
            {
                mSeatChart[seat - 1] = name;
            }
            if (seat > mSeatChart.GetUpperBound(0))
            {
                MessageBox.Show("Invalid seat number.", "Reservation Error");
            }
            else
            {
                if (mSeatChart[seat - 1] == "Open")
                {
                    MessageBox.Show("No such reservation exists.", "Reservation Error");
                }
            }

        }

        public bool IsFull(int seat)
        {
            if (seat >= (Rows * Seats) && mSeatChart[seat - 1] != "Open")
            {
                return true;
            }
            return false;
        }
    }
}
4

2 に答える 2

0

答えは簡単ではなく

seatNum = Convert.ToInt16(txtSeatNum.Text);

使用する

seatNum = lst.SeatChart.SelectedIndex + 1;
于 2012-12-02T09:46:09.040 に答える
0

シート チャート コレクション内のアイテムには、最初に識別子が必要です。それを取得したら、その識別子 (または説明、名前など) を取得します。すべての「オープン」シートは、ユーザーが選択できるリスト ボックスに追加する必要があります。予約時には、どのリスト ボックス項目が選択されているかを判断するだけです。これは、データ バインディングの有無に関係なく実行できます。

    List<Seat> seats;

    private void button6_Click(object sender, EventArgs e)
    {
        seats = new List<Seat>
        {
            new Seat { Identifier = "A1", Description = "A1 - Window" },
            new Seat { Identifier = "A2", Description = "A2 - Center" },
            new Seat { Identifier = "A3", Description = "A3 - Aisle" } 
        };

        listBox1.DataSource = seats;
        listBox1.DisplayMember = "Description";
        listBox1.ValueMember = "Identifier"; 
    }

    private void button7_Click(object sender, EventArgs e)
    {
        // get selected seat
        foreach (Seat selectedSeat in listBox1.SelectedItems)
        {
            MessageBox.Show(selectedSeat.Identifier);
        }
    }

もちろん、この例をいじって、空いている席だけを表示することもできます。いくつかの方法がありますが、そのうちの 1 つは、Seat クラスにフラグを追加することです。

于 2012-11-27T04:22:49.593 に答える