【问题标题】:how to get employee using optimal way by id,name ,exp如何通过 id,name,exp 使用最佳方式获得员工
【发布时间】:2020-03-30 17:00:19
【问题描述】:

我在没有应用任何设计模式的情况下编写了下面的代码,这段代码有什么问题。

class Employee {
    public int EmployeeID
    { get; set; }
    public int YearOExperience
    { get; set; }
    public int Salary
    { get; set; }
    public string EmploeyeeType
    { get; set; }
}

interface IEmployee {
    List<Employee> getInformation(int id, int exp, int sal);

}

class EmployeeData1 {

    public List<Employee> GetData(int id,int exp , int sal)
        {
            List<Employee> empInfo = new List<Employee> {

    new Employee { EmploeyeeType = "P", EmployeeID = 1, Salary = 20000, YearOExperience= 2  },
     new Employee { EmploeyeeType = "P", EmployeeID = 2, Salary = 20000, YearOExperience= 2  },
      new Employee { EmploeyeeType = "C", EmployeeID = 3, Salary = 20000, YearOExperience= 2  }

        };
            return empInfo;
        }

    }

    static void Main(string[] args) {EmployeeData1 emp = new EmployeeData1();
        emp.getInformation(1, 2, 2000);
    };
}

这是作业:

【问题讨论】:

  • 您要解决什么问题?这会做你不想要的事情,还是没有做你想做的事情?
  • 这段代码遇到了什么问题?您创建的界面不能在代码中的任何地方使用。这是我注意到的一个问题。
  • 我想用设计模式,你可以吗

标签: c# oop design-patterns solid-principles


【解决方案1】:

由于您粘贴的是图片而不是文字,因此我们必须通过重新输入来支付费用。

不要这样交接。与其尝试使用设计模式(这不是你所期望的),不如移交正确的工作。

您的任务要求您输入员工姓名、员工类型和工作年限。

您的控制台应用应该通过Console.ReadLine() 命令获取这三个值。

请确保是这样。但它可能是,因为所有的代码竞赛都使用标准输入 (Console.ReadLine() reads from stdin) 将输入提供给应用程序。

那么,你的老师对你的要求是:

  1. 生成一个连续的员工 ID,
  2. 按工作年限计算工资
  3. 打印结果(使用Console.WriteLine(),写入标准输出(标准输出))
  4. 转到下一位员工

这里是一个有限的例子来帮助你。请自行研究并填写所需的部分,否则,如果不是您的,那么获得好分数意味着什么?

请注意,我并不是说您可以直接移交。

但至少你可以以此为起点。

祝你好运。

    static void Main()
    {
        // this will be used to create sequential employee Ids
        int employeeId = 0;

        while(true) // Just press enter without entering anything to exit.
        {
            string employeeName = Console.ReadLine();

            if(employeeName == "")
            {
                return;
            }

            // Get the other two input parameters like the one above.
            // Please be careful with the last one (years of experience)
            // It should be parsed to an integer (a little research effort)

            // string employeeType = ..
            // int yearsOfExperience = ..

            // Now, assign this employee the next Id.
            employeeId++;

            // And now, calculate the employee's salary
            // You should use the years of experience and employee type
            // to match an amount in the salary table.
            int salary;
            if(employeeType == "Permanent")
            {
                salary = ..;
            }
            else
            {
                salary = ..;
            }

            // And finally, print-out using stdout
            Console.WriteLine("Employee ID: {0}", employeeId);
            // Print the other two outputs like the one above 

        }
    }

【讨论】:

    猜你喜欢
    • 2020-06-03
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多