简体   繁体   English

试图使用c#中的抽象类来计算工资,计算扣减和净工资

[英]trying to calculate compute salary , compute deduction and compute net pay using abstract class in c#

abstract class Salary is calculated by multiplying the daily rate by the numbers of days worked in a month. 抽象类的工资是通过将每日工资乘以一个月的工作天数得出的。 im trying to calculate the salary which computes the salary of the employee and deduction which computes the deduction of the salary and net pay salary-deduction for example: doctor=10000, teacher=20000, engineer=30000 我正在尝试计算用于计算员工工资的工资和用于计算工资扣除额和净工资扣除额的扣除额,例如:医生= 10000,教师= 20000,工程师= 30000

trying to get the output // 试图获取输出//

  ID 001 Name J Salary 15750.00 Deduction 3386.25 Net Pay 12363.75 
     namespace Practice
    {

////////////////////////////////////////////
////////////////////////////////////////////
///////////// class employee ///////////////
////////////////////////////////////////////
//////////////////////////////////////////// 

abstract class Employee
{
    int employeeid;
    string name;
    double salary, deduction, daysworked;
    public Employee()
    {

    }

    public int Employeeid
    {
        get { return employeeid; }
        set { employeeid = value; }
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public double Daysworked
    {
        get { return daysworked; }
        set { daysworked = value; }
    }

    public double Deduction
    {
        get { return deduction; }
        set { deduction = value; }
    }

    public double Salary
    {
        get { return salary; }
        set { salary = value; }
    }
    public virtual double calculatesalary()
    {
        return Salary * Daysworked;
    }
    public virtual double calulatededuction()
    {
        return calculatesalary() * Deduction;
    }
}


////////////////////////////////////////////
////////////////////////////////////////////
///////////// class sample1 ///////////////
////////////////////////////////////////////
//////////////////////////////////////////// 

class sample1 : Employee
{
    public sample1 (string name,int employeeid, double daysworked,double salary)
    {
        Name = name;
        Employeeid = employeeid;
        Daysworked = daysworked;
        Salary = salary;
    }
    public override double calculatesalary()
    {
        if (calculatesalary() <= 10000)
        {
            Deduction = 0.11;
        }
        if (calculatesalary() >= 10001 && calculatesalary() <= 20000)
        {
            Deduction = 0.22;
        }
        if (calculatesalary() >= 20001 && calculatesalary() <= 30000)
        {
            Deduction = 0.34;
        }
        if (calculatesalary() >= 30001)
        {
            Deduction = 0.58;
        }
        return calculatesalary() * Deduction;
    }

    }
}

my question is where did i get it wrong ? 我的问题是我在哪里弄错了? :/ :/

As John Wu pointed out, it ends up in an infinite loop since you invoke the calculatesalary() method in the sample1 class recursively. 正如John Wu指出的那样,由于您递归调用sample1类中的calculatesalary()方法,所以它最终陷入无限循环。 Using base.calculatesalary() will invoke the base method and then perform additional logic in the derived class. 使用base.calculatesalary()将调用base方法,然后在派生类中执行其他逻辑。 Modify your your method as follows: 修改您的方法,如下所示:

public override double calculatesalary()
    {
        var salary = base.calculatesalary();

        if (salary <= 10000)
        {
            Deduction = 0.11;
        }
        else if (salary >= 10001 && salary <= 20000)
        {
            Deduction = 0.22;
        }
        else if (salary >= 20001 && salary <= 30000)
        {
            Deduction = 0.34;
        }
        else if (salary >= 30001)
        {
            Deduction = 0.58;
        }

        return salary * Deduction;
    }

Please consider refactoring this class since it requires some fixes as Dan Chase mentioned. 请考虑重构该类,因为它需要一些修复,如Dan Chase所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM