简体   繁体   中英

Assigning a decimal value to an individual employee c#

Im currently making a payroll system. I have like 5 or 6 employee's and each of them has a different hourly rate, how would I assign an hourly rate to each individual?!

At the moment I have something along the lines of(Which is incorrect)...

string[] employeeID = {"Brian", "Richard", etc....};
decimal hourlyPay = 0.0M;

if (employeeID == "Brian")
{
  hourlyPay = 8.00M;
}

Thanks!

You could create a dictionary,

var rates = new Dictionary<string, decimal>();

rates.Add("Brian",8.00M);
rates.Add("Richard",10.50M);

Or in a more compact edition , using a collection initializer:

var rate = new Dictionary<string, decimal>
{
    {"Brian", 8.00M},
    {"Richard",10.50M}
};

Using a dictionary, it's also the most efficient way to get later the hourly rate of each employee, by using the employeId. It's an O(1) operation.

If you don't like this approach, you could declare a class called Employee, like below:

public class Employee
{
    public string Id { get; set; }
    public decimal HourlyRate { get; set; }

    public Employee(string id, decimal hourlyRate)
    {
        Id = id;
        HourlyRate = hourlyRate;
    }
}

Then instead of having an array with the employees ids, you could define an array of Employee objects.

var employees = new []
{
    new Employee("Brian", 8.00M),
    new Employee("Richard", 10.50M)
}

This way each time you create an employee you have to provide an Id and a hourly rate.

How would I display my results?

Since you picked the solution of the class, I would opt to overload the ToString method.

public class Employee
{
    // The previous declarations, please see above.

    public string override ToString()
    {
        return String.Format("Staff Name: {0}, Hourly Rate: {1}", Id, HourlyRate);
    } 
}

So, when you call for instance employees[0].ToString() , you get the following string:

"Staff Name: Brian, Hourly Rate: 8.00"

Try this code:

string[] employeeID = {"Brian", "Richard"};
decimal hourlyPay = 0.0M;
for (int i = 0; i < employeeID.Length; i++)
{
    if (employeeID[i] == "Brian")
    {
        hourlyPay = 8.00M;
    }

}
Console.WriteLine(hourlyPay);

Always use a loop to go through all elements in array, and access them by index. When populating, modifying elements, always use a loop.

I guess it's very likely you will need to assign other things to your employees (Ex: Hours done in the week, address, etc.) so instead of mapping everything in a dictionary or an other array, make a Class to hold your employees information:

public class Employee 
{
    public string EmployeeId { get; set; }
    public decimal HourlyPay { get; set; }
}

With a class you can keep your model extensible and if you need to save that information somewhere, it's easy to serialize it or map it to a DB table.

You can keep all your employees in a list. To initialize them with a value, you do this:

List<Employee> employees = new List<Employee>();
employees.Add(new Employee { EmployeeId = "Brian", HourlyPay = 8.0M });

To set the info at a later stage, simply get the employee with the corresponding ID and set its hourly pay:

employees.FirstOrDefault(e => e.EmployeeId == "Brian").HourlyPay = 8.0M;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM