C# 如何return 另一种方法计算的付款总和

C# How to return the sum of payments calculated in another method

我有一个很愚蠢的问题,但我有点难过。我仍在学习 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;
        }
    }
}

每小时员工文件

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;
        }
    }
}

佣金员工档案

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 .50 per hour and worked for 40 hours. We owe them 0.00.
Employee: Alice Mason with ID of 4321 makes a 20 percent commission on 00.00. We owe them 0.00.
Employee: Richard Lionheart with ID of 1212 makes .75 per hour and worked for 43 hours. We owe them 5.25.
Employee: Mark Wells with ID of 9876 makes a 21 percent commission on 00.00. We owe them 3.00.

The total amount of money owed by the company to Employees is: [=14=]

所以基本上我试图将所有 employeePay 字段加在一起以获得我在开头描述的总数。有什么想法吗?

尝试

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})";

附带说明一下,将钱存入 floatdouble 是一个 糟糕的 想法。为此使用 decimal