In: Computer Science
USING VISUAL STUDIO 2017, LANGUAGE VISUAL C#
I have struggled on this program for quite some time and still can't quite figure it out.
I'm creating an app that has 2 textboxes, 1 for inputting customer name, and the second for entering the number of tickets the customer wants to purchase. There are 3 listboxes, the first with the days of the week, the second with 4 different theaters, and the third listbox is to display the customer name, number of tickets, and total price. The ticketbase = 50, weekend upcharge = 30, for Imax & Upac theaters = 35 and for Capital and Premier theaters = 25. There are 4 buttons: Process customer, Display daily report, clear all, and exit. For the process customer button, it is clicked after customer has inputted their info in textbox1 and 2 and selected from listbox 1 and 2. it is supposed to compute the cost of tickets and enter that data into an array, not more than 5 customers can be processed without clearing all with the clear all button. Once process customer button is pressed it displays a message box displaying "customer processed", and all selected inputs should be cleared after pressing OK. In the display daily report button it is supposed to take array elements using a loop, one at a time and add them to the listbox3. There are supposed to be 2-3 arrays for the input one for name of customer, another for number of tickets, and another for the cost. And for the output it is supposed to be stored in internal arrays. I am not sure of how to set this up.
Short Summary:
Source Code:
Form1.cs File:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BookingApp
{
public partial class Form1 : Form
{
// Arrays to hold the input values
string[] name;
int[] numTickets;
//string[] days;
//string[] theaters;
int[] costs;
const int MAX_CUSTOMERS = 5;
int currentCapacity;
// Ticket costs
const int TICKET_BASE = 50;
const int WEEKEND_UPCHARGE = 30;
const int IMAX_COST = 35;
const int UPAC_COST = 35;
const int CAPTIAL_COST = 25;
const int PREMIER_COST = 25;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Initialize the array
name = new string[MAX_CUSTOMERS];
numTickets = new int[MAX_CUSTOMERS];
//days = new string[MAX_CUSTOMERS];
//theaters = new string[MAX_CUSTOMERS];
costs = new int[MAX_CUSTOMERS];
currentCapacity = 0;
}
/// <summary>
/// Calculates total costs for the given input
/// </summary>
private int GetCost(int numTicket, string day, string theater)
{
int totalcost = TICKET_BASE;
//weekend upcharge
if(day == "Saturday" || day == "Sunday")
{
totalcost += WEEKEND_UPCHARGE;
}
// Add theater cost
if(theater == "Imax")
{
totalcost += IMAX_COST;
}
else if(theater == "Upac")
{
totalcost += UPAC_COST;
}
else if (theater == "Capital")
{
totalcost += CAPTIAL_COST;
}
else if (theater == "Premier")
{
totalcost += PREMIER_COST;
}
//return the total
return totalcost;
}
/// <summary>
/// Reset the input boxes
/// </summary>
private void ResetInputs()
{
txtCustName.Text = "";
txtNumOfTickets.Text = "";
lbDays.SelectedIndex = -1;
lbTheater.SelectedIndex = -1;
}
private void btnProcess_Click(object sender, EventArgs e)
{
// Validate if it has enough capacity
if(MAX_CUSTOMERS == currentCapacity)
{
MessageBox.Show("Reached maximum capacity, clear all before proceed to add new customer",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// Validate if customer entered
if (string.IsNullOrEmpty(txtCustName.Text.Trim()))
{
MessageBox.Show("Enter customer name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// validate if number of tickcets
int numTicket = 0;
if (int.TryParse(txtNumOfTickets.Text.Trim(), out numTicket) == false || numTicket <= 0)
{
MessageBox.Show("Enter valid number of tickets", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// Validate if day is selected
if(lbDays.SelectedIndex == -1)
{
MessageBox.Show("Select a day of the week", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// Validate if theater is selected
if (lbTheater.SelectedIndex == -1)
{
MessageBox.Show("Select a theater", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// Add input values to the array
name[currentCapacity] = txtCustName.Text.Trim();
numTickets[currentCapacity] = numTicket;
//days[currentCapacity] = lbDays.SelectedItem.ToString();
//theaters[currentCapacity] = lbTheater.SelectedItem.ToString();
costs[currentCapacity] = GetCost(numTicket, lbDays.SelectedItem.ToString(), lbTheater.SelectedItem.ToString());
// Increase the current index
currentCapacity++;
// Display the meesage
MessageBox.Show("Customer processed", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
// clear the inputs
ResetInputs();
txtCustName.Focus();
}
private void btnDailyReport_Click(object sender, EventArgs e)
{
// clear any previous values
lbResults.Items.Clear();
lbResults.Items.Add(string.Format("{0, -10} {1, 10} {2, 10}", "Name", "NumTickets", "TotalCost"));
for (int index = 0; index < currentCapacity; index++)
{
lbResults.Items.Add(string.Format("{0, -10} {1, 10} {2, 10}", name[index], numTickets[index], costs[index]));
}
}
private void btnClear_Click(object sender, EventArgs e)
{
ResetInputs();
lbResults.Items.Clear();
Array.Clear(name, 0, MAX_CUSTOMERS);
Array.Clear(numTickets, 0, MAX_CUSTOMERS);
Array.Clear(costs, 0, MAX_CUSTOMERS);
currentCapacity = 0;
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
Form1.designer.cs File:
namespace BookingApp
{
partial class Form1
{
/// <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.txtCustName = new System.Windows.Forms.TextBox();
this.txtNumOfTickets = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.lbDays = new System.Windows.Forms.ListBox();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.lbTheater = new System.Windows.Forms.ListBox();
this.btnProcess = new System.Windows.Forms.Button();
this.btnDailyReport = new System.Windows.Forms.Button();
this.btnClear = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.lbResults = new System.Windows.Forms.ListBox();
this.label5 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(89, 57);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(132, 20);
this.label1.TabIndex = 0;
this.label1.Text = "Customer Name: ";
//
// txtCustName
//
this.txtCustName.Location = new System.Drawing.Point(265, 54);
this.txtCustName.Name = "txtCustName";
this.txtCustName.Size = new System.Drawing.Size(257, 26);
this.txtCustName.TabIndex = 1;
//
// txtNumOfTickets
//
this.txtNumOfTickets.Location = new System.Drawing.Point(265, 115);
this.txtNumOfTickets.Name = "txtNumOfTickets";
this.txtNumOfTickets.Size = new System.Drawing.Size(257, 26);
this.txtNumOfTickets.TabIndex = 3;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(80, 118);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(141, 20);
this.label2.TabIndex = 2;
this.label2.Text = "Number of Tickets:";
//
// lbDays
//
this.lbDays.FormattingEnabled = true;
this.lbDays.ItemHeight = 20;
this.lbDays.Items.AddRange(new object[] {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"});
this.lbDays.Location = new System.Drawing.Point(265, 177);
this.lbDays.Name = "lbDays";
this.lbDays.Size = new System.Drawing.Size(160, 164);
this.lbDays.TabIndex = 4;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(117, 177);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(104, 20);
this.label3.TabIndex = 5;
this.label3.Text = "Day of Week:";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(91, 376);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(130, 20);
this.label4.TabIndex = 6;
this.label4.Text = "Select a Theater:";
//
// lbTheater
//
this.lbTheater.FormattingEnabled = true;
this.lbTheater.ItemHeight = 20;
this.lbTheater.Items.AddRange(new object[] {
"Imax",
"Upac",
"Capital",
"Premier"});
this.lbTheater.Location = new System.Drawing.Point(265, 376);
this.lbTheater.Name = "lbTheater";
this.lbTheater.Size = new System.Drawing.Size(160, 144);
this.lbTheater.TabIndex = 7;
//
// btnProcess
//
this.btnProcess.Location = new System.Drawing.Point(65, 564);
this.btnProcess.Name = "btnProcess";
this.btnProcess.Size = new System.Drawing.Size(182, 38);
this.btnProcess.TabIndex = 8;
this.btnProcess.Text = "Process Customer";
this.btnProcess.UseVisualStyleBackColor = true;
this.btnProcess.Click += new System.EventHandler(this.btnProcess_Click);
//
// btnDailyReport
//
this.btnDailyReport.Location = new System.Drawing.Point(340, 564);
this.btnDailyReport.Name = "btnDailyReport";
this.btnDailyReport.Size = new System.Drawing.Size(182, 38);
this.btnDailyReport.TabIndex = 9;
this.btnDailyReport.Text = "Display Daily Report";
this.btnDailyReport.UseVisualStyleBackColor = true;
this.btnDailyReport.Click += new System.EventHandler(this.btnDailyReport_Click);
//
// btnClear
//
this.btnClear.Location = new System.Drawing.Point(65, 649);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(182, 38);
this.btnClear.TabIndex = 10;
this.btnClear.Text = "Clear All";
this.btnClear.UseVisualStyleBackColor = true;
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(340, 649);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(182, 38);
this.btnExit.TabIndex = 11;
this.btnExit.Text = "Exit";
this.btnExit.UseVisualStyleBackColor = true;
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// lbResults
//
this.lbResults.Enabled = false;
this.lbResults.FormattingEnabled = true;
this.lbResults.ItemHeight = 20;
this.lbResults.Location = new System.Drawing.Point(265, 778);
this.lbResults.Name = "lbResults";
this.lbResults.Size = new System.Drawing.Size(325, 204);
this.lbResults.TabIndex = 12;
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(93, 778);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(114, 20);
this.label5.TabIndex = 13;
this.label5.Text = "Display Result:";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(649, 1028);
this.Controls.Add(this.label5);
this.Controls.Add(this.lbResults);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.btnDailyReport);
t