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

我有一个很愚蠢的问题,但我有些困惑。我仍在学习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,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,int commission,int totalSales) : base(firstname,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字段加起来,以获得我在开始时描述的总数。有什么想法吗?

TCTCZKF 回答:C#如何返回用另一种方法计算的付款总和

尝试

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

本文链接:https://www.f2er.com/3169008.html

大家都在问