Reputation: 13
I have a pretty silly question but I am kind of stumped. I am still learning C# and I am not entirely sure how to wrap my head around this one part of the program. Anyway, the goal is to keep track of the total amount owed to each employee in a float. Then when the program finishes, print this total amount out. It should look like this:
The total amount of money owed by the company to Employees is: $2108.25
This is what the code looks like so far.
Program
using System;
using System.Collections.Generic;
using System.Linq;
namespace Assignment5
{
abstract class Employee
{
private string firstName = "";
private string lastName = "";
private int employeeID;
public Employee(string firstname, string lastname, int employeeid)
{
firstName = firstname;
lastName = lastname;
employeeID = employeeid;
}
public abstract float CalculatePay();
}
public class Program
{
public static void Main(string[] args)
{
List<Employee> employees = new List<Employee>();
employees.Add(new HourlyEmployee("Joe", "Smith", 1234, 12.50f, 40));
employees.Add(new CommissionEmployee("Alice", "Mason", 4321, 20, 1000));
employees.Add(new HourlyEmployee("Richard", "Lionheart", 1212, 11.75f, 43));
employees.Add(new CommissionEmployee("Mark", "Wells", 9876, 21, 4300));
foreach(Employee e in employees)
{
e.CalculatePay();
Console.WriteLine(e.ToString());
}
Console.WriteLine("");
Console.WriteLine("The total amount of money owed by the company to Employees is: ${0)", totalOwed;
}
}
}
HourlyEmployee file
using System;
using System.Collections.Generic;
using System.Linq;
namespace Assignment5
{
class HourlyEmployee : Employee
{
public float hourlyRate;
public int hoursWorked;
public float employeePay;
public string firstname;
public string lastname;
public int employeeid;
public HourlyEmployee(string firstname, string lastname, int employeeid,
float hourlyRate, int hoursWorked) : base(firstname, lastname, employeeid)
{
this.hourlyRate = hourlyRate;
this.hoursWorked = hoursWorked;
this.firstname = firstname;
this.lastname = lastname;
this.employeeid = employeeid;
}
public override string ToString()
{
return string.Format("Employee: " + firstname + " " + lastname + " with ID of " + employeeid + " makes $" + hourlyRate.ToString("0.00")
+ " per hour and worked for " + hoursWorked + " hours. We owe them $" + employeePay.ToString("0.00") + ".");
}
public override float CalculatePay()
{
employeePay = hourlyRate * hoursWorked;
return employeePay;
}
}
}
CommissionEmployee file
using System;
using System.Collections.Generic;
using System.Linq;
namespace Assignment5
{
class CommissionEmployee : Employee
{
private int commission;
private int totalSales;
private float employeePay;
private string firstname;
private string lastname;
private int employeeid;
public CommissionEmployee(string firstname, string lastname, int employeeid,
int commission, int totalSales) : base(firstname, lastname, employeeid)
{
this.commission = commission;
this.totalSales = totalSales;
this.firstname = firstname;
this.lastname = lastname;
this.employeeid = employeeid;
}
public override float CalculatePay()
{
employeePay = (totalSales * commission) / 100;
return employeePay;
}
public override string ToString()
{
return "Employee: " + firstname + " " + lastname + " with ID of " + employeeid + " makes a " + commission
+ " percent commission on $" + totalSales.ToString("0.00") + ". We owe them $" + employeePay.ToString("0.00") + ".";
}
}
}
I'm pretty close to done here but I don't really know how to add up all the employeePay variables together. Here is what the output looks like so far:
Employee: Joe Smith with ID of 1234 makes $12.50 per hour and worked for 40 hours. We owe them $500.00.
Employee: Alice Mason with ID of 4321 makes a 20 percent commission on $1000.00. We owe them $200.00.
Employee: Richard Lionheart with ID of 1212 makes $11.75 per hour and worked for 43 hours. We owe them $505.25.
Employee: Mark Wells with ID of 9876 makes a 21 percent commission on $4300.00. We owe them $903.00.
The total amount of money owed by the company to Employees is: ${0}
So basically I'm trying to add up all the employeePay fields together to get the total I described at the beginning. Any ideas?
Upvotes: 0
Views: 352
Reputation: 9509
Try
float totalOwed = 0;
foreach(Employee e in employees)
{
totalOwed += e.CalculatePay();
Console.WriteLine(e.ToString());
}
Console.WriteLine("");
Console.WriteLine($"The total amount of money owed by the company to Employees is: {totalOwed})";
As a side note, storing money in float
or double
is a bad idea. Use decimal
for that.
Upvotes: 1