Answer to Question #201248 in C# for saqi

Question #201248

Star Children’s Academy has opened a movie theater in its own capacity to host movie afternoon once a week for every class (including all sections e.g. Class 1 has three sections Class1-A, Class1-B, Class1-C divided because of large strength in one class, so here one class means 3 sections’ students will be watching a movie together one week and then next week another class and its sections) covering the topics such as patriotism, kindness, love, self-awareness, animal care and protection, etc.   As a Visual Programmer, you are required to design and develop a Windows Form Application that facilitates the class teachers to reserve tickets on behalf of students for the movie. No student can reserve directly for himself.


1
Expert's answer
2021-06-01T11:33:10-0400
namespace StarChildrenAcademy
{
    partial class frmMain
    {
        /// <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.label1 = new System.Windows.Forms.Label();
            this.cbSections = new System.Windows.Forms.ComboBox();
            this.label2 = new System.Windows.Forms.Label();
            this.cbGenre = new System.Windows.Forms.ComboBox();
            this.label3 = new System.Windows.Forms.Label();
            this.cbSeat = new System.Windows.Forms.ComboBox();
            this.txtStudentName = new System.Windows.Forms.TextBox();
            this.btnReserveTicket = new System.Windows.Forms.Button();
            this.btnClose = new System.Windows.Forms.Button();
            this.label4 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(72, 34);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(77, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Select section:";
            // 
            // cbSections
            // 
            this.cbSections.FormattingEnabled = true;
            this.cbSections.Items.AddRange(new object[] {
            "Class1-A",
            "Class1-B",
            "Class1-C"});
            this.cbSections.Location = new System.Drawing.Point(169, 31);
            this.cbSections.Name = "cbSections";
            this.cbSections.Size = new System.Drawing.Size(121, 21);
            this.cbSections.TabIndex = 1;
            this.cbSections.SelectedIndexChanged += new System.EventHandler(this.cbSections_SelectedIndexChanged);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(79, 93);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(70, 13);
            this.label2.TabIndex = 0;
            this.label2.Text = "Select genre:";
            // 
            // cbGenre
            // 
            this.cbGenre.FormattingEnabled = true;
            this.cbGenre.Items.AddRange(new object[] {
            "Patriotism",
            "Kindness",
            "Love",
            "Self-awareness",
            "Animal care and protection"});
            this.cbGenre.Location = new System.Drawing.Point(169, 90);
            this.cbGenre.Name = "cbGenre";
            this.cbGenre.Size = new System.Drawing.Size(121, 21);
            this.cbGenre.TabIndex = 3;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(86, 63);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(63, 13);
            this.label3.TabIndex = 0;
            this.label3.Text = "Select seat:";
            // 
            // cbSeat
            // 
            this.cbSeat.FormattingEnabled = true;
            this.cbSeat.Location = new System.Drawing.Point(169, 60);
            this.cbSeat.Name = "cbSeat";
            this.cbSeat.Size = new System.Drawing.Size(121, 21);
            this.cbSeat.TabIndex = 2;
            // 
            // txtStudentName
            // 
            this.txtStudentName.Location = new System.Drawing.Point(169, 122);
            this.txtStudentName.Name = "txtStudentName";
            this.txtStudentName.Size = new System.Drawing.Size(121, 20);
            this.txtStudentName.TabIndex = 2;
            // 
            // btnReserveTicket
            // 
            this.btnReserveTicket.Location = new System.Drawing.Point(40, 166);
            this.btnReserveTicket.Name = "btnReserveTicket";
            this.btnReserveTicket.Size = new System.Drawing.Size(123, 23);
            this.btnReserveTicket.TabIndex = 3;
            this.btnReserveTicket.Text = "Reserve ticket";
            this.btnReserveTicket.UseVisualStyleBackColor = true;
            this.btnReserveTicket.Click += new System.EventHandler(this.btnReserveTicket_Click);
            // 
            // btnClose
            // 
            this.btnClose.Location = new System.Drawing.Point(169, 166);
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(123, 23);
            this.btnClose.TabIndex = 3;
            this.btnClose.Text = "Close";
            this.btnClose.UseVisualStyleBackColor = true;
            this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(79, 125);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(76, 13);
            this.label4.TabIndex = 0;
            this.label4.Text = "Student name:";
            // 
            // frmMain
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(333, 215);
            this.Controls.Add(this.btnClose);
            this.Controls.Add(this.btnReserveTicket);
            this.Controls.Add(this.txtStudentName);
            this.Controls.Add(this.cbGenre);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.cbSeat);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.cbSections);
            this.Controls.Add(this.label1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmMain";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Star Children’s Academy";
            this.ResumeLayout(false);
            this.PerformLayout();


        }


        #endregion


        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.ComboBox cbSections;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.ComboBox cbGenre;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.ComboBox cbSeat;
        private System.Windows.Forms.TextBox txtStudentName;
        private System.Windows.Forms.Button btnReserveTicket;
        private System.Windows.Forms.Button btnClose;
        private System.Windows.Forms.Label label4;
    }
}



The file frmMain.cs


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 StarChildrenAcademy
{
    public partial class frmMain : Form
    {
        private string[] class1_A_Seats;
        private string[] class1_B_Seats;
        private string[] class1_C_Seats;


        public frmMain()
        {
            InitializeComponent();
            this.class1_A_Seats = new string[15];
            this.class1_B_Seats = new string[12];
            this.class1_C_Seats = new string[10];


            for (int i = 0; i < this.class1_A_Seats.Length; i++) {
                this.class1_A_Seats[i] = "";    
            }
            for (int i = 0; i < this.class1_B_Seats.Length; i++)
            {
                this.class1_B_Seats[i] = "";
            }
            for (int i = 0; i < this.class1_C_Seats.Length; i++)
            {
                this.class1_C_Seats[i] = "";
            }
            
            displaySeats(this.class1_A_Seats);


            cbSections.SelectedIndex = 0;
            cbSeat.SelectedIndex = 0;
            cbGenre.SelectedIndex = 0;
        }
        /// <summary>
        /// Displays seats
        /// </summary>
        /// <param name="class1"></param>
        private void displaySeats(string[] class1)
        {
            cbSeat.Items.Clear();
            for (int i = 0; i < class1.Length; i++)
            {
                if (class1[i] == "")
                {
                    cbSeat.Items.Add((i+1).ToString());
                }
            }
        }
        /// <summary>
        /// This function checks if seat is free
        /// </summary>
        /// <param name="class1"></param>
        /// <param name="selectedSeat"></param>
        /// <returns></returns>
        private bool seatIsFree(string[] class1, int selectedSeat)
        {
            for (int i = 0; i < class1.Length; i++)
            {
                if (class1[selectedSeat] == "")
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// Reserve Ticket button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReserveTicket_Click(object sender, EventArgs e)
        {
            bool isReserved = false;
            if (cbSections.SelectedIndex == 0)
            {
                if (seatIsFree(this.class1_A_Seats,cbSeat.SelectedIndex)) {
                    this.class1_A_Seats[cbSeat.SelectedIndex] = txtStudentName.Text;
                    isReserved = true;    
                }
            }
            if (cbSections.SelectedIndex == 1)
            {
                if (seatIsFree(this.class1_B_Seats, cbSeat.SelectedIndex))
                {
                    this.class1_B_Seats[cbSeat.SelectedIndex] = txtStudentName.Text;
                    isReserved = true;
                }
            }
            if (cbSections.SelectedIndex == 2)
            {
                if (seatIsFree(this.class1_C_Seats, cbSeat.SelectedIndex))
                {
                    this.class1_C_Seats[cbSeat.SelectedIndex] = txtStudentName.Text;
                    isReserved = true;
                }
            }
            if (isReserved)
            {
                MessageBox.Show("The ticket has been reserved.");
            }
            else {
                MessageBox.Show("The seat is not free.");
            }
        }
        /// <summary>
        /// Close the program
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// Select section
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbSections_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbSections.SelectedIndex == 0) {
                displaySeats(this.class1_A_Seats);
            }
            if (cbSections.SelectedIndex == 1)
            {
                displaySeats(this.class1_B_Seats);
            }
            if (cbSections.SelectedIndex == 2)
            {
                displaySeats(this.class1_C_Seats);
            }


        }
    }
}






Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS