У меня довольно глупый вопрос, но я немного озадачен. Я все еще изучаю C #, и я не совсем уверен, как обернуть голову вокруг этой одной части программы. В любом случае, цель состоит в том, чтобы отследить общую сумму, причитающуюся с каждого сотрудника на поплавке. Затем, когда программа завершится, выведите эту общую сумму. Это должно выглядеть следующим образом:
Общая сумма денег, которую компания должна Сотрудникам, составляет: $ 2108,25
Так выглядит код до сих пор.
Программа
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
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
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") + ".";
}
}
}
Я довольно близок к завершению, но я не знаю, как сложить всепеременные employeePay вместе. Вот как выглядит результат на данный момент:
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}
Итак, в основном я пытаюсь сложить все поля employeePay вместе, чтобы получить общее значение, которое я описал в начале. Есть идеи?