Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C# windows form application Employee and ProductionWorker Classes Create an Empl

ID: 3854965 • Letter: C

Question

C# windows form application Employee and ProductionWorker Classes Create an Employee class that has properties for the following data: Employee name Employee number Next, create a class named Productionworker that is derived from the Employee class. The ProductionWorker class should have properties to hold the following data: Shift number (an integer, such as 1, 2, or 3) Hourly pay rate The workday is divided into two shifts: day and night. The Shift property will hold an integer value representing the shift that the employee works. The day shift is shift 1 and the night shift is shift 2. Create an application that creates an object of the ProductionWorker class and lets the user enter data for each of the object's properties. Retrieve the object's properties and display their values.

Explanation / Answer

Employee.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace act24
{
    class Employee
    {
        //private instance variables
        private string _name;
        private int _number;
      

        //empty constructor
        public Employee()
        {
            _name = null;
            _number = 0;
        }

        //parameterized constructor
        public Employee(string name, int number)
        {
            _name = name;
            _number = number;
        }

        //getters and setters for instance variables
        public string name
        {
            get { return _name; }
            set { _name = value; }
        }
        public int number
        {
            get { return _number; }
            set { _number = value; }
        }

    }
}


Form1.Designer.cs

namespace act24
{
    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.btnSubmit = new System.Windows.Forms.Button();
            this.txtName = new System.Windows.Forms.TextBox();
            this.txtNum = new System.Windows.Forms.TextBox();
            this.txtShiftNum = new System.Windows.Forms.TextBox();
            this.txtHourly = new System.Windows.Forms.TextBox();
            this.lblName = new System.Windows.Forms.Label();
            this.lblNum = new System.Windows.Forms.Label();
            this.lblShiftNum = new System.Windows.Forms.Label();
            this.lblHourly = new System.Windows.Forms.Label();
            this.btnExit = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // btnSubmit
            //
            this.btnSubmit.Location = new System.Drawing.Point(26, 192);
            this.btnSubmit.Name = "btnSubmit";
            this.btnSubmit.Size = new System.Drawing.Size(75, 23);
            this.btnSubmit.TabIndex = 0;
            this.btnSubmit.Text = "Submit";
            this.btnSubmit.UseVisualStyleBackColor = true;
            this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
            //
            // txtName
            //
            this.txtName.Location = new System.Drawing.Point(132, 27);
            this.txtName.Name = "txtName";
            this.txtName.Size = new System.Drawing.Size(100, 20);
            this.txtName.TabIndex = 4;
            //
            // txtNum
            //
            this.txtNum.Location = new System.Drawing.Point(132, 64);
            this.txtNum.Name = "txtNum";
            this.txtNum.Size = new System.Drawing.Size(100, 20);
            this.txtNum.TabIndex = 5;
            //
            // txtShiftNum
            //
            this.txtShiftNum.Location = new System.Drawing.Point(132, 103);
            this.txtShiftNum.Name = "txtShiftNum";
            this.txtShiftNum.Size = new System.Drawing.Size(100, 20);
            this.txtShiftNum.TabIndex = 6;
            //
            // txtHourly
            //
            this.txtHourly.Location = new System.Drawing.Point(132, 140);
            this.txtHourly.Name = "txtHourly";
            this.txtHourly.Size = new System.Drawing.Size(100, 20);
            this.txtHourly.TabIndex = 7;
            //
            // lblName
            //
            this.lblName.AutoSize = true;
            this.lblName.Location = new System.Drawing.Point(291, 34);
            this.lblName.Name = "lblName";
            this.lblName.Size = new System.Drawing.Size(0, 13);
            this.lblName.TabIndex = 8;
            //
            // lblNum
            //
            this.lblNum.AutoSize = true;
            this.lblNum.Location = new System.Drawing.Point(291, 71);
            this.lblNum.Name = "lblNum";
            this.lblNum.Size = new System.Drawing.Size(0, 13);
            this.lblNum.TabIndex = 9;
            //
            // lblShiftNum
            //
            this.lblShiftNum.AutoSize = true;
            this.lblShiftNum.Location = new System.Drawing.Point(291, 110);
            this.lblShiftNum.Name = "lblShiftNum";
            this.lblShiftNum.Size = new System.Drawing.Size(0, 13);
            this.lblShiftNum.TabIndex = 10;
            //
            // lblHourly
            //
            this.lblHourly.AutoSize = true;
            this.lblHourly.Location = new System.Drawing.Point(291, 147);
            this.lblHourly.Name = "lblHourly";
            this.lblHourly.Size = new System.Drawing.Size(0, 13);
            this.lblHourly.TabIndex = 11;
            //
            // btnExit
            //
            this.btnExit.Location = new System.Drawing.Point(157, 192);
            this.btnExit.Name = "btnExit";
            this.btnExit.Size = new System.Drawing.Size(75, 23);
            this.btnExit.TabIndex = 12;
            this.btnExit.Text = "Exit";
            this.btnExit.UseVisualStyleBackColor = true;
            this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(23, 34);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(38, 13);
            this.label1.TabIndex = 13;
            this.label1.Text = "Name:";
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(23, 71);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(47, 13);
            this.label2.TabIndex = 14;
            this.label2.Text = "Number:";
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(23, 110);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(71, 13);
            this.label3.TabIndex = 15;
            this.label3.Text = "Shift Number:";
            //
            // label4
            //
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(23, 147);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(84, 13);
            this.label4.TabIndex = 16;
            this.label4.Text = "Hourly Pay Rate";
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(444, 262);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.btnExit);
            this.Controls.Add(this.lblHourly);
            this.Controls.Add(this.lblShiftNum);
            this.Controls.Add(this.lblNum);
            this.Controls.Add(this.lblName);
            this.Controls.Add(this.txtHourly);
            this.Controls.Add(this.txtShiftNum);
            this.Controls.Add(this.txtNum);
            this.Controls.Add(this.txtName);
            this.Controls.Add(this.btnSubmit);
            this.Name = "Form1";
            this.Text = "Employee Information";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button btnSubmit;
        private System.Windows.Forms.TextBox txtName;
        private System.Windows.Forms.TextBox txtNum;
        private System.Windows.Forms.TextBox txtShiftNum;
        private System.Windows.Forms.TextBox txtHourly;
        private System.Windows.Forms.Label lblName;
        private System.Windows.Forms.Label lblNum;
        private System.Windows.Forms.Label lblShiftNum;
        private System.Windows.Forms.Label lblHourly;
        private System.Windows.Forms.Button btnExit;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
    }
}

Form1.cs


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 act24
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //create an object for production worker and assign entered values to the class
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            string name = txtName.Text;
            int number = Convert.ToInt32(txtNum.Text);
            int shiftNum = Convert.ToInt32(txtShiftNum.Text);
            double hourlyRate = Convert.ToDouble(txtHourly.Text);
            if (name != null && number > 0 && (shiftNum == 1 || shiftNum == 2) && hourlyRate > 0.0)
            {
                ProductionWorker prodWorker = new ProductionWorker();
                prodWorker.name = name;
                prodWorker.number = number;
                prodWorker.shiftNum = shiftNum;
                prodWorker.hourlyPayRate = hourlyRate;

                //print the info from ProductionWorker class in labels
                if (prodWorker.shiftNum == 1)
                {
                    lblShiftNum.Text = "Day Shift";
                }
                else if (prodWorker.shiftNum == 2)
                {
                    lblShiftNum.Text = "Night Shift";
                }
                else
                {
                    lblShiftNum.Text = "Invalid!";
                }

                lblName.Text = prodWorker.name;
                lblHourly.Text = Convert.ToString(prodWorker.hourlyPayRate);
                lblNum.Text = Convert.ToString(prodWorker.number);
            }


        }
    }
}


ProductionWorker.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace act24
{
    //productionworker extends employee
    class ProductionWorker : Employee
    {
        //instance variables
        private int _shiftNum;
        private double _hourlyPayRate;

        //Empty Constructor
        public ProductionWorker()
        {
            _shiftNum = 0;
            _hourlyPayRate = 0.0;
        }
      
        //parameterized constructor
        public ProductionWorker(int shiftNum, double hourlyPayRate, string name, int number):base(name, number)
        {
            _shiftNum = shiftNum;
            _hourlyPayRate = hourlyPayRate;
        }
      
        //getters and setters
        public int shiftNum
        {
            get { return _shiftNum; }
            set { _shiftNum = value; }
        }
        public double hourlyPayRate
        {
            get { return _hourlyPayRate; }
            set { _hourlyPayRate = value; }
        }
    }
}

Program.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace act24
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}