0% found this document useful (0 votes)
18 views7 pages

TP Chapter 1 Exercise 1: Defining The Employee Class

The document provides instructions for creating an Employee class with properties and methods like Login(), and testing the class. It involves: 1. Creating the Employee class with private variables and public properties for ID, name, password, security level. 2. Adding a Login() method to set the employee values based on credentials. 3. Overloading the constructor and Update() method to handle different data types. 4. Testing the class using forms that create Employee objects, call methods, and display returned values.

Uploaded by

charbeldaher34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

TP Chapter 1 Exercise 1: Defining The Employee Class

The document provides instructions for creating an Employee class with properties and methods like Login(), and testing the class. It involves: 1. Creating the Employee class with private variables and public properties for ID, name, password, security level. 2. Adding a Login() method to set the employee values based on credentials. 3. Overloading the constructor and Update() method to handle different data types. 4. Testing the class using forms that create Employee objects, call methods, and display returned values.

Uploaded by

charbeldaher34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

TP CHAPTER 1

Exercise 1: Defining the Employee Class


To create the Employee class, follow these steps:
1. Start Visual Studio. Select File ➤ Open ➤ Project.
2. Navigate to the Activity6_1Starter folder, click the Act6 1.sln file, and click Open. When
the project opens, it will contain a login form. You will use this form later to test the
Employee class you create.
3. Select Project ➤ Add Class. In the Add New Item dialog box, rename the class file to
Employee.cs, and then click Open. Visual Studio adds the Employee.cs file to the project and
adds the following class definition code to the file:

class Employee
{
}

4. Enter the following code between the opening and closing brackets to add the private
instance variables to the class body in the definition file:

private int empID;


private string loginName;
private string password;
private int securityLevel;

5. Next, add the following public properties to access the private instance variables defined in
step 4:

public int EmployeeID


{
get { return empID; }
}
public string LoginName
{
get { return loginName; }
set { loginName = value; }
}
public string Password
{
get { return password; }
set { password = value; }
}
public int SecurityLevel
{
get { return securityLevel; }
}
6. After the properties, add the following Login method to the class
definition:

public void Login(string loginName, string password)


{
LoginName = loginName;
Password = password;
//Data nomally retrieved from database.
//Hard coded for demo only.
if (loginName == "Smith" & password == "js")
{
empID = 1;
securityLevel = 2;
}
else if (loginName == "Jones" & password == "mj")
{
empID = 2;
securityLevel = 4;
}
else
{
throw new Exception("Login incorrect.");
}
}

7. Select Build ➤ Build Solution. Make sure there are no build errors in the Error List
window. If there are, fix them, and then rebuild.
Exercise 2: Testing the Employee Class
To test the Employee class, follow these steps:
1. Open frmLogin in the code editor and locate the btnLogin click event code.
■Tip Double-clicking the Login button in the form designer will also bring up the event code
in the code editor.
2. In the body of the btnLogin click event, declare and instantiate a variable of type Employee
called oEmployee:
Employee oEmployee = new Employee();
3. Next, call the Login method of the oEmployee object, passing in the values of the login
name and the password from the text boxes on the form:
oEmployee.Login(txtName.Text,txtPassword.Text);
4. After calling the Login method, show a message box stating the user’s security level,
which is retrieved by reading the SecurityLevel property of the oEmployee object:
MessageBox.Show("Your security level is " + oEmployee.SecurityLevel);
5. Select Build ➤ Build Solution. Make sure there are no build errors in the Error List
window. If there are, fix them, and then rebuild.
6. Select Debug ➤ Start to run the project. Test the login form by entering a login name of
Smith and a password of is. You should get a message indicating a security level of 2. Try
entering your name and a password of pass. You should get a message indicating the login
failed.
After testing the login procedure, close the form; this will stop the debugger.
Exercise 3: Overloading Constructor
7. Add the following code to the overloaded constructor, which simulates extracting the
employee data from a database and assigns the data to the instance properties of the class:

//Simulates retrieval from database


if (empID == 1)
{
empID = empID;
LoginName = "smith";
PassWord = "js";
SSN = 123456789;
Department = "IS";
}
else if (empID == 2)
{
empID = empID;
LoginName = "jones";
PassWord = "mj";
SSN = 987654321;
Department = "HR";
}
else
{
throw new Exception("Invalid Employee ID");
}

8. Select Build ➤ Build Solution. Make sure there are no build errors in the Error List
window. If there are, fix them, and then rebuild.
Exercise 4: Testing the Employee Class Constructors
To test the Employee class constructors, follow these steps:
1. Open the EmployeeInfoForm in the form editor and double click the New Employee
button to bring up the click event code in the code editor.
2. In the Click Event method body, declare and instantiate a variable of type Employee called
oEmployee:
Employee
oEmployee = new Employee();
3. Next, update the EmployeeID text box with the employee ID, disable the EmployeeID text
box, and clear the remaining textboxes:

Employee oEmployee = new Employee();


txtEmpID.Text = oEmployee.EmpID.ToString();
txtEmpID.Enabled = false;
txtLoginName.Text = "";
txtPassword.Text = "";
txtSSN.Text = "";
txtDepartment.Text = "";

4. Select Build ➤ Build Solution. Make sure there are no build errors in the Error List
window. If there are, fix them, and then rebuild.
5. Open the EmployeeInfoForm in the form editor and double click the Existing Employee
button to bring up the click event code in the code editor.
6. In the Click Event method body, declare and instantiate a variable of type Employee called
oEmployee. Retrieve the employee ID from the txtEmpID text box and pass it as an argument
in the constructor. The int.Parse method converts the text to an integer data type:
Employee oEmployee = new Employee(int.Parse(txtEmpID.Text));
7. Next, disable the Employee ID textbox and fill in the remaining text boxes with the values
of the Employee object’s properties:

txtEmpID.Enabled = false;
txtLoginName.Text = oEmployee.LoginName;
txtPassword.Text = oEmployee.PassWord;
txtSSN.Text = oEmployee.SSN.ToString();
txtDepartment.Text = oEmployee.Department;

8. Select Build ➤ Build Solution. Make sure there are no build errors in the Error List
window. If there are, fix them, and then rebuild.
9. Select Debug ➤ Start to run the project and test the code.
10. When the EmployeeInfo form is displayed, click the New Employee button. You should
see that a new employee ID has been generated in the Employee ID textbox.
11. Click the Reset button to clear and enable the Employee ID text box.
12. Enter a value of 1 for the employee ID and click the Get Existing Employee button. The
information for the employee is displayed on the form.
13. After testing the constructors, close the form, which will stop the debugger.
Exercise 5: Overloading a Class Method
To overload a class method, follow these steps:
1. Open the Employee.cs code in the code editor.
2. Add the following Update method to the Employee class. This method simulates the
updating of the employee security information to a database:

public string Update(string loginName, string password)


{
LoginName = loginName;
PassWord = password;
return "Security info updated.";
}

3. Add a second Update method to simulate the updating of the employee human resources
data to a database:

public string Update(int ssNumber, string department)


{
SSN = ssNumber;
Department = department;
return "HR info updated.";
}

4. Select Build ➤ Build Solution. Make sure there are no build errors in the Error List
window. If there are, fix them, and then rebuild.
Exercise 6: Testing the Overloaded Update Method
To test the overloaded Update method, follow these steps:
1. Open the EmployeeInfo Form in the Form editor and double click the Update SI button.
You are presented with the click event code in the Code Editor window.
2. In the Click Event method, declare and instantiate a variable of type Employee called
oEmployee. Retrieve the employee ID from the txtEmpID text box and pass it as an argument
in the constructor:
Employee oEmployee = new Employee(int.Parse(txtEmpID.Text));
3. Next, call the Update method, passing the values of the login name and password from the
text boxes. Show the method return message to the user in a message box:
MessageBox.Show(oEmployee.Update(txtLoginName.Text, txtPassword.Text));
4. Update the login name and password text boxes with the property values of the Employee
object:
txtLoginName.Text = oEmployee.LoginName;
txtPassword.Text = oEmployee.PassWord;
5. Repeat this process to add similar code to the Update HR button Click Event method to
simulate updating the human resources information. Add the following code to the Click
Event method:

Employee oEmployee = new Employee(int.Parse(txtEmpID.Text));


MessageBox.Show(oEmployee.Update(int.Parse(txtSSN.Text), txtDepartment.Text));
txtSSN.Text = oEmployee.SSN.ToString();
txtDepartment.Text = oEmployee.Department;
6. Select Build ➤ Build Solution. Make sure there are no build errors in the Error List
window. If there are, fix them, and then rebuild.
7. Select Debug ➤ Start to run the project and test the code.
8. Enter a value of 1 for the employee ID and click the Get Existing Employee button.
9. Change the values for the security information and click the Update button.
10. Change the values for the human resources information and click the Update button.
11. You should see that the correct Update method is called in accordance with the
parameters passed in to it. After testing the Update method, close the form.

You might also like