【问题标题】:C# How to return the sum of payments calculated in another methodC#如何返回以另一种方法计算的付款总和
【发布时间】:2019-11-04 05:01:53
【问题描述】:

我有一个非常愚蠢的问题,但我有点难过。我仍在学习 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 字段加在一起以获得我在开始时描述的总数。有什么想法吗?

【问题讨论】:

    标签: c# methods sum add


    【解决方案1】:

    试试

    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

    【讨论】:

    • 谢谢,这行得通!我不反对您所说的将钱存储在浮动或双倍中是不好的,但任务告诉我们以这种方式存储它,所以我只是接受任务。谢谢!
    • 用于防止下一个潜在的floatincurracy question +1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    相关资源
    最近更新 更多