Assigning decimal value to individual C # employee
I am currently creating a payroll system. I have 5 or 6 employees and they each have a different hourly rate, how would I assign the hourly rate to each person?
At the moment I have something line by line (which is not correct) ...
string[] employeeID = {"Brian", "Richard", etc....};
decimal hourlyPay = 0.0M;
if (employeeID == "Brian")
{
hourlyPay = 8.00M;
}
Thank!
source to share
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 loop through all the elements in the array and access them by index. When filling, changing elements, always use a loop.
source to share
You can 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, this is also the most efficient way to get the later hourly rate of each employee using EmployeeId. This is an O (1) operation.
If you don't like this approach, you can declare a class called Employee as shown 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 an array with employee ids, you can define an array of objects Employee
.
var employees = new []
{
new Employee("Brian", 8.00M),
new Employee("Richard", 10.50M)
}
Thus, every time you create an employee, you must provide the Id and hourly rate.
How can I show the results?
Since you have chosen a class solution, I would prefer to overload the method ToString
.
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 the instance employees[0].ToString()
, you get the following line:
"Staff Name: Brian, Hourly Rate: 8.00"
source to share
I guess it is very likely that you will need to assign other things to your employees (ex: Hours done per week, address, etc.), so instead of matching everything in a dictionary or other array, do Class
to store 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 store this information somewhere, it's easy to serialize it or map to a DB table.
You can keep all your employees on the 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 information at a later stage, simply get the employee with the appropriate ID and set their hourly wages:
employees.FirstOrDefault(e => e.EmployeeId == "Brian").HourlyPay = 8.0M;
source to share