7
Answers

Please need HELP! to solve the following. c# newbie

Jesse Pazzi

Jesse Pazzi

7y
280
1
You have to create two classes. The first class “Employee” which included: The private variables:
 
• first name
• last name
• id number (Format: A#####)
• Wage
• hoursWorked
 
The hoursWorked variable holds how many total hours the person worked. When the hoursWorked is over 40 hours, it will be considered as over time. Wage holds how much the person makes per hour. After passing the 40 hours, they get 1.5 x the wage for each additional hour worked.
 
The functions:
 
• Constructors
• Properties
• GetGrossPay: calculate the gross pay for the employee (Be careful with the over time)
 
The second class will be “EmployeeDemo” class with the main function. In the main function, Read the data from the “employeeinfo.txt” file and save the data in the array of (Employee) objects. In the file, the first number is the total number of employees and each employee’s information.
After import the data, display a menu for user to choose:
 
1. Display all employees
2. Add New employee
3. Quit the program (save the information to “employeeinfo.txt” file)
(employeeinfo.txt)
5
John
Wu
A00001
14.50
55
Bob
Ho
A00002
10.50
40
Jenny
Pena
A00003
7.25
65
Sam
Sosa
A00004
12
35
Linda
Jodan
A00005
20.5
46.5
Thanks.
 
I have the following but for some reason is not working. any help will be appreciated...
 
namespace ConsoleApplication7
{
class Employee
{
//private variables
private string firstName;
private string lastName;
private string idNumber;
private double empWage;
private double hoursWorked;
//constroctor for employee
public Employee()
{}
public void setData(string afirstName, string bsecondName, string cidNumber, double dempWage, double ehoursWorked)
{
firstName = afirstName;
lastName = bsecondName;
idNumber = cidNumber;
empWage = dempWage;
hoursWorked = ehoursWorked;
}
//calculating the gross payment for the employee
public double GetGrossPay()
{
double perHour = empWage / hoursWorked;
if (hoursWorked > 40)
return empWage + (1.50 * perHour * (hoursWorked - 40));
else
return empWage;
}
public void showEmployee()
{
double getPay = GetGrossPay();
Console.WriteLine(firstName + "," + lastName + "," + idNumber + "," + empWage + "," + hoursWorked + "," + getPay);
}
}
}
_____________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication7
{
class EmployeeDemo
{
static void Main()
{
int counter = 0, emp_c = 0;
Employee[] empdata = new Employee[0];
string line, emp = "";
// Open the file
using (StreamReader file = new StreamReader(@"C:\Users\jesse.cantu\Documents\Visual Studio 2012\Projects\Employee\ConsoleApplication7\bin\Debug\employeeinfo.txt"))
// Read the file and display it line by line.
while ((line = file.ReadLine()) != null)
{
while (!file.EndOfStream)
{
emp = emp + "," + line;
if (counter == 0)
{
Array.Resize(ref empdata, empdata.Length + Int32.Parse(line));
emp = "";
}
if (counter % 5 == 0 && counter > 0)
{
string[] strArr = null;
strArr = emp.Split(',');
empdata[emp_c].setData(strArr[0], strArr[1], strArr[2], double.Parse(strArr[3]), double.Parse(strArr[4]));
emp_c++;
emp = "";
}
counter++;
}
file.Close();
for (int i = 0; i < empdata.Length; i++)
{
empdata[i].showEmployee();
}
}
}
}
}
Answers (7)