Module 6: Course Project – Encapsulating Data And Writing To A File

Background

This course project is designed to help you practice using your skills. Each week, you will learn important concepts. Then, you will apply these concepts to build an amazing course project over the next seven weeks. What’s more, this project will have a Graphical User Interface (GUI) instead of using the console. This course project has two parts. For the first couple of weeks, you will be create a basic GUI and learn how to work with GUI controls while practicing basic programming concepts. After that, you will create an object oriented project.

Don't use plagiarized sources. Get Your Custom Essay on
Module 6: Course Project – Encapsulating Data And Writing To A File
Just from $13/Page
Order Essay

Part 2: Scenario

You have been hired as a consultant to create a basic Payroll System for a small business. The business wants to keep track of basic employee information and print paychecks for the employees every two weeks. Your client has some basic functionality in mind, such as the ability to add and remove employees, display basic employee information, and print paychecks. Maintenance is key! You realize that Object-Oriented programming will provide the most efficient application that is easy to maintain and update as needed. Let’s get started!

 

 

 

Week 6: Encapsulating Data and Writing to a File

Objectives

1. Integrate Employee class

2. Data hiding

Introduction

This week, you will use your Employee class in your project to encapsulate the employee information and make your application more maintainable. When a user clicks the AddButton, you will get the information for the employee, create an Employee object, and add the Employee object to the employees listbox. Finally, you will save all of the employee information to the file.

Steps

Picking up from last week, we will include the Employee class in our forms application.

1. When a user clicks the Add button, we need to get the employee’s information, create an Employee object, and add the Employee object to the employees listbox. How can we get the employee’s information? We need to create a second form and then call it when the Add button is clicked. Create an input form to get the employee’s information. Add a Submit button and a Cancel button to the form. When you finish, the form should look similar to this one: 

 

The InputForm has a property called CancelButton. As a result, we will need to rename our CancelButton to ExitButton. We can keep the text as Cancel. Change the button name now! The textboxes on the InputForm are set to Private. This means that the second form will not be able to see the textboxes. Change the accessibility to Public so the second form will be able to see the textboxes. Click in the firstName text box and in the properties the Modifiers should be Public.  When the user clicks the Add button, the code should create an InputForm and show it as a Dialog. The user will enter the employee’s information and then click the Submit button. Otherwise, the user can click the Cancel button. If the user clicks the Submit button, set the DialogResult property of the InputForm to DialogResult.OK. If the user clicks the Cancel button, set the DialogResult property to DialogResult.Cancel. When you are finished, your code should look like this: private void SubmitButton_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.OK; this.Hide(); } private void ExitButton_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; } Let’s change the code for the Add button on the Main form.

a. Create an InputForm object and show it as a Dialog.

b. Get the information off the InputForm and create an Employee object.

c. Dispose of the InputForm

d. Add the Employee object to the employees listbox

When you are finished, your AddButton code should look like this: private void AddButton_Click(object sender, EventArgs e)

{

// add item to the employee listbox

InputForm frmInput = new InputForm();

 

using (frmInput)

{

DialogResult result = frmInput.ShowDialog();

 

// see if input form was cancelled

if (result == DialogResult.Cancel)

return; // end the method since the user cancelled the input

 

// get user’s input and create an Employee object

string fName = frmInput.FirstNameTextBox.Text;

string lName = frmInput.LastNameTextBox.Text;

string ssn = frmInput.SSNTextBox.Text;

string date = frmInput.HireDateTextBox.Text;

DateTime hireDate = DateTime.Parse(date);

 

Employee emp = new Employee(fName, lName, ssn, hireDate);

 

// add the Employee object to the employees listbox

EmployeesListBox.Items.Add(emp);

}

}

2. We set up the Remove button last week based on the index of the object. This code will continue to work even though we changed the type of object that we are using! Test the functionality of the Remove button and confirm that it works.

3. Every time we start our application, we have to type in new Employee information. It is important to have persistent data. In the business world, you will be reading and writing to databases. In this course project, we will read and write to files. When the user adds an Employee object, we need to write the updated Employee data to the file. When a user removes an Employee object, we also need to write the updated Employee data to the file. On the other hand, when a user clicks the Display button, we need to read the Employee data from the file. Since we want to keep the code as maintainable as possible, let’s do the reading and writing to files using methods Add a WriteEmpsToFile( ) method call to the end of your Add button code and then create the method.

e. Create a string variable for the filename

f. Open a pipe to the file using the StreamWriter object

i. Add “using System.IO” to the top of your code so you can read and write to files

g. Write all of the Employee object data to the file using Comma Separated Values (CSV) format

h. Remember to close your StreamWriter object!

 

4. Add this same method call to the end of your Remove button code so it updates the file when you remove an Employee object. Add a ReadEmpsFromFile( ) method call to the end of your Display button code and then create the method.

i. Create a string variable for the filename

j. Open a pipe from the file using the StreamReader object

k. Read all of the Employee object data from the file. Remember that the data is in Comma Separated Values (CSV) format.

i. Read a line

ii. Split the line into a string array called “parts” or something similar

iii. Use the items in the string array to build an Employee object

iv. Add the Employee object to the employees listbox

l. Remember to close your StreamReader object!

 

If you want the Employee data to be automatically loaded when you start your application, add the ReadEmpsFromFile( ) method to the MainForm_Load event method

 

Deliverables Week 6

· Use the Word document template and put your information at the top. Take a screenshot while your application is running. YOU MUST ENTER YOUR NAME AS ONE OF THE EMPLOYEES. Paste the screenshot to a Word document below your information. Finally, copy-paste your Form code (only the code for the form, not program.cs) to the Word document below the screenshot. Save and close your Word document. Submit the Word document for your first Course Project deliverable.

Homework On Time
Calculate the Price of your PAPER Now
Pages (550 words)
Approximate price: -

Why Choose Us

Top quality papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

Professional academic writers

We have hired a team of professional writers experienced in academic and business writing. Most of them are native speakers and PhD holders able to take care of any assignment you need help with.

Free revisions

If you feel that we missed something, send the order for a free revision. You will have 10 days to send the order for revision after you receive the final paper. You can either do it on your own after signing in to your personal account or by contacting our support.

On-time delivery

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & confidential

We use several checkers to make sure that all papers you receive are plagiarism-free. Our editors carefully go through all in-text citations. We also promise full confidentiality in all our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

You are welcome to choose your academic level and the type of your paper. Our academic experts will gladly help you with essays, case studies, research papers and other assignments.

Admissions

Admission help & business writing

You can be positive that we will be here 24/7 to help you get accepted to the Master’s program at the TOP-universities or help you get a well-paid position.

Reviews

Editing your paper

Our academic writers and editors will help you submit a well-structured and organized paper just on time. We will ensure that your final paper is of the highest quality and absolutely free of mistakes.

Reviews

Revising your paper

Our academic writers and editors will help you with unlimited number of revisions in case you need any customization of your academic papers