My problem pointing continuously at the code if (m_nameList [index] == null) in public bool reserved seat (String name, double price, int index) method, in the seat the manager class as soon as i try to put in a value in the interface. That is why I copy all the classes to you here and hope for help. Thanks
Program class
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace Assignment4 { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } } }
MainForm class
using System; using System.Windows.Forms; namespace Assignment4 { public partial class MainForm : Form { private const int m_numOfSeats = 60; private SeatManager m_seatMngr; public MainForm() { InitializeComponent(); m_seatMngr = new SeatManager(m_numOfSeats); InitializeGUI(); } private void InitializeGUI() { cmbDisplayOption.Items.Add("All Seats"); cmbDisplayOption.Items.Add("Vacant Seat"); cmbDisplayOption.Items.Add("Reserved Seat"); cmbDisplayOption.SelectedIndex = 0; UpdateGUI(); rbtnReserve.Checked = true; lstReservations.Items.Clear(); txtName.Text = string.Empty; } private bool CheckSelectedIndex() { for (int i = 0; i < lstReservations.Items.Count; i++) lstReservations.SelectedIndex = i; if (lstReservations.SelectedIndex < 0) { MessageBox.Show("Item must be selected"); lstReservations.SelectedIndex = -1; return false; } else { return true; } } private bool ReadAndValidateName(out string name) { name = txtName.Text; if (string.IsNullOrEmpty(name)) { txtName.Focus(); MessageBox.Show("There is no name value"); return false; } else { return true; } } private bool ReadAndValidatePrice(out double price) { int minValue = 0; const int maxValue = 1000; price = Double.Parse(txtPrice.Text); if(price < minValue) { MessageBox.Show("The value in the price box is under 0"); txtPrice.Focus(); return false; } else if (price > maxValue) { MessageBox.Show("The value in the price box is over max limit"); txtPrice.Focus(); return false; } else return true; } private bool ReadAndValidateInput(out string name, out double price) { ReadAndValidateName(out name); ReadAndValidatePrice(out price); //name = txtName.Text; //price = +Double.Parse(txtPrice.Text); if (ReadAndValidateName(out name) && ReadAndValidatePrice(out price)) { ReadAndValidateName(out name); ReadAndValidatePrice(out price); return true; } else return false; } private void ReserveOrCancelSeat(SeatManager.DisplayOptions choice) { if (CheckSelectedIndex() == false) { MessageBox.Show("a seat must be selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (rbtnReserve.Checked == true) { // Validate data string name = null; double price = 0.0; if (ReadAndValidateInput(out name, out price) == true) { // Data is ok, reserve seat if (!m_seatMngr.ReserveSeat(name, price, lstReservations.SelectedIndex)) { lstReservations.Items.Add(!m_seatMngr.ReserveSeat(name, price, lstReservations.SelectedIndex)); // The seat was already reserved, ask user if we should continue with reservation if (MessageBox.Show("The seat is already reserved, continue with reservation?", "Seat already reserved", MessageBoxButtons.YesNo) == DialogResult.Yes) { // We continue. First cancel seat. if (!m_seatMngr.CancelSeat(lstReservations.SelectedIndex)) { // This should not happen, throw exception throw new Exception("The seat could not be canceled."); } // Then we reserve it if (!m_seatMngr.ReserveSeat(name, price, lstReservations.SelectedIndex)) { // This should not happen, throw exception throw new Exception("The seat could not be reserved."); } } } } } } private void UpdateGUI() { string[] strSeatInfoStrings = new String[20]; lstReservations.Items.Clear(); m_seatMngr.GetSeatInfoStrings((SeatManager.DisplayOptions)cmbDisplayOption.SelectedIndex, out strSeatInfoStrings); //Get array of seats to show //Denna ToString är bara för test ska egentligen inte finnas // lstReservations.Items.Add(strSeatInfoStrings.ToString()); lblNumOfReservedSeats.Text = "" + m_seatMngr.GetNumReserved(); lblNumberOfVacant.Text = "" + m_seatMngr.GetNumVacant(); } private void cmbDisplayOption_SelectedIndexChanged(object sender, EventArgs e) { UpdateGUI(); } private void lbSeats_SelectedIndexChanged(object sender, EventArgs e) { } private void MainForm_Load(object sender, EventArgs e) { } private void btnOK_Click(object sender, EventArgs e) { ReserveOrCancelSeat(SeatManager.DisplayOptions.ReservedSeats); } } }
SeatManager class
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Assignment4 { class SeatManager { private readonly int m_totNumOfSeats; private double[] m_priceList = new double[0]; private string[] m_nameList = new string[0]; public enum DisplayOptions { AllSeats, VacantSeats, ReservedSeats } public SeatManager(int maxNumberofSeats) { m_totNumOfSeats = maxNumberofSeats; m_nameList = new string[m_totNumOfSeats]; m_priceList = new double[m_totNumOfSeats]; } private bool CheckIndex(int index) { if (index >= 0 && index < m_totNumOfSeats) { return true; } else { return false; } } public int GetNumReserved() { int numOfResv = 0; foreach (string seat in m_nameList) { if (seat != null) numOfResv++; } return numOfResv; } public int GetNumVacant() { int notReserved = 60; foreach (string seat in m_nameList) { if (seat != null) notReserved--; } return notReserved; } public int GetNumOfSeats(DisplayOptions choice) { if (choice == DisplayOptions.ReservedSeats) { return GetNumReserved(); } if (choice == DisplayOptions.VacantSeats) { return GetNumVacant(); } else { return m_totNumOfSeats; } } public bool ReserveSeat(String name, double price, int index) { if (m_nameList[index] == null) { m_nameList[index] = name; m_priceList[index] = price; return true; } else { return false; } } public bool CancelSeat(int index) { if (m_nameList[index] == null) { return false; } else { m_nameList[index] = string.Empty; m_priceList[index] = 0.0; return true; } } public string GetSeatInfoAt(int index) { for (index = 0; index < m_nameList.Length; index++) { return m_nameList[index]; } return m_nameList[index]; } public int GetSeatInfoStrings(DisplayOptions choice, out string[] strSeatInfoStrings) { strSeatInfoStrings = null; int count = GetNumOfSeats(choice); if ((count <= 0)) return 0; string strOut = "Vacant"; strSeatInfoStrings = new string[count]; string priset = ""; string namnet; int i = 0; //counter for return array // is the element corresponding with the index empty for (int index = 0; index <= m_totNumOfSeats - 1; index++) { if (m_nameList[index] != null) { strOut = "Reserverad"; } else { strOut = "Vacans"; } if (choice == DisplayOptions.VacantSeats) { priset = m_priceList[index].ToString(); namnet = m_nameList[index]; } else if (choice == DisplayOptions.ReservedSeats) { priset = m_nameList[index].ToString(); namnet = m_nameList[index]; } else { priset = m_priceList[index].ToString(); namnet = m_nameList[index]; } if (i < strSeatInfoStrings.Length) strSeatInfoStrings[index++] = string.Format("{0,5}{1, 35}{2, 35}{3,40}", index, strOut, namnet, priset); } return i; } } }
And finally the MainForm designer class
namespace Assignment4 { partial class MainForm { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.btnOK = new System.Windows.Forms.Button(); this.lab1 = new System.Windows.Forms.Label(); this.lab2 = new System.Windows.Forms.Label(); this.lab4 = new System.Windows.Forms.Label(); this.rbtnReserve = new System.Windows.Forms.RadioButton(); this.rbtnCancel = new System.Windows.Forms.RadioButton(); this.grpInput = new System.Windows.Forms.GroupBox(); this.lblPrice = new System.Windows.Forms.Label(); this.lblName = new System.Windows.Forms.Label(); this.txtName = new System.Windows.Forms.TextBox(); this.grpOutput = new System.Windows.Forms.GroupBox(); this.lblNumberOfVacant = new System.Windows.Forms.Label(); this.lblNumOfReservedSeats = new System.Windows.Forms.Label(); this.lblNumOfSeats = new System.Windows.Forms.Label(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.cmbDisplayOption = new System.Windows.Forms.ComboBox(); this.lstReservations = new System.Windows.Forms.ListBox(); this.txtPrice = new System.Windows.Forms.NumericUpDown(); this.grpInput.SuspendLayout(); this.grpOutput.SuspendLayout(); this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.txtPrice)).BeginInit(); this.SuspendLayout(); // // btnOK // this.btnOK.Location = new System.Drawing.Point(27, 91); this.btnOK.Name = "btnOK"; this.btnOK.Size = new System.Drawing.Size(122, 26); this.btnOK.TabIndex = 0; this.btnOK.Text = "Reserv / Cancel"; this.btnOK.UseVisualStyleBackColor = true; this.btnOK.Click += new System.EventHandler(this.btnOK_Click); // // lab1 // this.lab1.AutoSize = true; this.lab1.Cursor = System.Windows.Forms.Cursors.No; this.lab1.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.lab1.Location = new System.Drawing.Point(24, 170); this.lab1.Name = "lab1"; this.lab1.Size = new System.Drawing.Size(89, 13); this.lab1.TabIndex = 1; this.lab1.Text = "Number of Seats:"; // // lab2 // this.lab2.AutoSize = true; this.lab2.Location = new System.Drawing.Point(24, 196); this.lab2.Name = "lab2"; this.lab2.Size = new System.Drawing.Size(108, 13); this.lab2.TabIndex = 1; this.lab2.Text = "Number of Reserved:"; // // lab4 // this.lab4.AutoSize = true; this.lab4.Location = new System.Drawing.Point(24, 223); this.lab4.Name = "lab4"; this.lab4.Size = new System.Drawing.Size(96, 13); this.lab4.TabIndex = 1; this.lab4.Text = "Number of Vacant:"; // // rbtnReserve // this.rbtnReserve.AutoSize = true; this.rbtnReserve.Location = new System.Drawing.Point(15, 14); this.rbtnReserve.Name = "rbtnReserve"; this.rbtnReserve.Size = new System.Drawing.Size(65, 17); this.rbtnReserve.TabIndex = 2; this.rbtnReserve.TabStop = true; this.rbtnReserve.Text = "Reserve"; this.rbtnReserve.UseVisualStyleBackColor = true; // // rbtnCancel // this.rbtnCancel.AutoSize = true; this.rbtnCancel.Location = new System.Drawing.Point(108, 14); this.rbtnCancel.Name = "rbtnCancel"; this.rbtnCancel.Size = new System.Drawing.Size(98, 17); this.rbtnCancel.TabIndex = 2; this.rbtnCancel.TabStop = true; this.rbtnCancel.Text = "Cancel Reserv."; this.rbtnCancel.UseVisualStyleBackColor = true; // // grpInput // this.grpInput.Controls.Add(this.txtPrice); this.grpInput.Controls.Add(this.lblPrice); this.grpInput.Controls.Add(this.lblName); this.grpInput.Controls.Add(this.rbtnReserve); this.grpInput.Controls.Add(this.txtName); this.grpInput.Controls.Add(this.rbtnCancel); this.grpInput.Location = new System.Drawing.Point(12, 7); this.grpInput.Name = "grpInput"; this.grpInput.Size = new System.Drawing.Size(212, 121); this.grpInput.TabIndex = 4; this.grpInput.TabStop = false; this.grpInput.Text = "Booking Input"; // // lblPrice // this.lblPrice.AutoSize = true; this.lblPrice.Location = new System.Drawing.Point(159, 42); this.lblPrice.Name = "lblPrice"; this.lblPrice.Size = new System.Drawing.Size(31, 13); this.lblPrice.TabIndex = 5; this.lblPrice.Text = "Price"; // // lblName // this.lblName.AutoSize = true; this.lblName.Location = new System.Drawing.Point(55, 42); this.lblName.Name = "lblName"; this.lblName.Size = new System.Drawing.Size(35, 13); this.lblName.TabIndex = 4; this.lblName.Text = "Name"; // // txtName // this.txtName.Location = new System.Drawing.Point(15, 58); this.txtName.Name = "txtName"; this.txtName.Size = new System.Drawing.Size(122, 20); this.txtName.TabIndex = 3; // // grpOutput // this.grpOutput.Controls.Add(this.lblNumberOfVacant); this.grpOutput.Controls.Add(this.lblNumOfReservedSeats); this.grpOutput.Controls.Add(this.lblNumOfSeats); this.grpOutput.Location = new System.Drawing.Point(12, 149); this.grpOutput.Name = "grpOutput"; this.grpOutput.Size = new System.Drawing.Size(212, 151); this.grpOutput.TabIndex = 4; this.grpOutput.TabStop = false; this.grpOutput.Text = "Output Data"; // // lblNumberOfVacant // this.lblNumberOfVacant.AutoSize = true; this.lblNumberOfVacant.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.lblNumberOfVacant.Location = new System.Drawing.Point(150, 74); this.lblNumberOfVacant.Name = "lblNumberOfVacant"; this.lblNumberOfVacant.Size = new System.Drawing.Size(27, 15); this.lblNumberOfVacant.TabIndex = 2; this.lblNumberOfVacant.Text = "240"; // // lblNumOfReservedSeats // this.lblNumOfReservedSeats.AutoSize = true; this.lblNumOfReservedSeats.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.lblNumOfReservedSeats.Location = new System.Drawing.Point(150, 47); this.lblNumOfReservedSeats.Name = "lblNumOfReservedSeats"; this.lblNumOfReservedSeats.Size = new System.Drawing.Size(27, 15); this.lblNumOfReservedSeats.TabIndex = 1; this.lblNumOfReservedSeats.Text = " "; // // lblNumOfSeats // this.lblNumOfSeats.AutoSize = true; this.lblNumOfSeats.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.lblNumOfSeats.Location = new System.Drawing.Point(150, 20); this.lblNumOfSeats.Name = "lblNumOfSeats"; this.lblNumOfSeats.Size = new System.Drawing.Size(27, 15); this.lblNumOfSeats.TabIndex = 0; this.lblNumOfSeats.Text = "240"; // // groupBox1 // this.groupBox1.Controls.Add(this.cmbDisplayOption); this.groupBox1.Controls.Add(this.lstReservations); this.groupBox1.Location = new System.Drawing.Point(246, 22); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(360, 270); this.groupBox1.TabIndex = 4; this.groupBox1.TabStop = false; this.groupBox1.Text = "Booking Input"; // // cmbDisplayOption // this.cmbDisplayOption.AccessibleDescription = ""; this.cmbDisplayOption.AccessibleName = ""; this.cmbDisplayOption.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cmbDisplayOption.FormattingEnabled = true; this.cmbDisplayOption.Location = new System.Drawing.Point(233, 19); this.cmbDisplayOption.Name = "cmbDisplayOption"; this.cmbDisplayOption.Size = new System.Drawing.Size(121, 21); this.cmbDisplayOption.TabIndex = 1; this.cmbDisplayOption.Tag = ""; this.cmbDisplayOption.SelectedIndexChanged += new System.EventHandler(this.cmbDisplayOption_SelectedIndexChanged); // // lstReservations // this.lstReservations.FormattingEnabled = true; this.lstReservations.Items.AddRange(new object[] { "lstReservations"}); this.lstReservations.Location = new System.Drawing.Point(6, 53); this.lstReservations.Name = "lstReservations"; this.lstReservations.Size = new System.Drawing.Size(354, 212); this.lstReservations.TabIndex = 0; this.lstReservations.SelectedIndexChanged += new System.EventHandler(this.lbSeats_SelectedIndexChanged); // // txtPrice // this.txtPrice.Location = new System.Drawing.Point(143, 59); this.txtPrice.Name = "txtPrice"; this.txtPrice.Size = new System.Drawing.Size(63, 20); this.txtPrice.TabIndex = 6; // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(618, 304); this.Controls.Add(this.lab4); this.Controls.Add(this.lab2); this.Controls.Add(this.lab1); this.Controls.Add(this.btnOK); this.Controls.Add(this.grpOutput); this.Controls.Add(this.groupBox1); this.Controls.Add(this.grpInput); this.Name = "MainForm"; this.Text = "CBC Cinema Booking System"; this.Load += new System.EventHandler(this.MainForm_Load); this.grpInput.ResumeLayout(false); this.grpInput.PerformLayout(); this.grpOutput.ResumeLayout(false); this.grpOutput.PerformLayout(); this.groupBox1.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.txtPrice)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } private System.Windows.Forms.Button btnOK; private System.Windows.Forms.Label lab2; private System.Windows.Forms.Label lab4; private System.Windows.Forms.RadioButton rbtnReserve; private System.Windows.Forms.RadioButton rbtnCancel; private System.Windows.Forms.GroupBox grpInput; private System.Windows.Forms.GroupBox grpOutput; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.TextBox txtName; private System.Windows.Forms.Label lblPrice; private System.Windows.Forms.Label lblName; private System.Windows.Forms.Label lab1; private System.Windows.Forms.Label lblNumOfSeats; private System.Windows.Forms.Label lblNumberOfVacant; private System.Windows.Forms.Label lblNumOfReservedSeats; private System.Windows.Forms.ListBox lstReservations; private System.Windows.Forms.ComboBox cmbDisplayOption; private System.Windows.Forms.NumericUpDown txtPrice; } }
This post has been edited by donderma: 28 September 2011 - 03:14 PM