【问题标题】:C#: how to display the employee names with lowest salaryC#:如何显示工资最低的员工姓名
【发布时间】:2016-04-15 18:32:20
【问题描述】:

我必须创建一个适当的 GUI 来输入至少 10 名员工的信息。对于每个员工,我必须输入以下信息。员工 ID、员工名字、员工姓氏和年薪。此外我必须检查输入数据的正确性。此外,我需要创建一个单独的类 EMPLOYEE,其中包含员工信息:员工 ID、名字、姓氏和年薪。该类应该具有构造函数属性和方法。所有员工信息都必须存储在员工类型的数组中。在阅读表单 GUI 有关特定员工的信息后,还使用相关的构造函数创建类员工(数组的元素)的对象。尽管有不止一名员工的年薪最低,用户仍希望能够找到年薪最低的员工。并显示有关它们的信息。应为用户提供适当的 GUI 以显示所需的信息。 我需要确保在我的程序中包含适当的代码来处理异常以及适当的方法。

这是班级员工:

  using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project_employee
{
    class Employee
    {
        private int employeeID;
        private string fullName;
        private string lastName;
        private double salary;
        public Employee()
        {
            employeeID = 0;
            fullName = "";
            lastName = "";
            salary = 0.0;
        }
        public Employee(int empIDValue, string fullNameVal, string lastNameVal)
        {
            employeeID = empIDValue;
            fullName = fullNameVal;
            lastName = lastNameVal;
            salary = 0.0;
        }
        public Employee(int empIDValue, string fullNameVal, string lastNameVal, double salaryValue)
        {
            employeeID = empIDValue;
            fullName = fullNameVal;
            lastName = lastNameVal;
            salary = salaryValue;
        }
        public int EmployeeIDNum
        {
            get
            {
                return employeeID;
            }
            set
            {
                employeeID = value;
            }
        }
        public string FullName
        {
            get
            {
                return fullName;
            }
            set
            {
                fullName = value;

            }
        }
        public int Getinfo
        {
            get
            {
                return employeeID;
            }
            set
            {
                employeeID = value;
            }

        }
        public string employeeInformationToString()
        {
           // employeeID = Convert.ToInt32(this.textBox1.Text);
            return (Convert.ToString(employeeID) + " " + fullName + " " + lastName + " " + Convert.ToString(salary));
        }
    }
}




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Project_employee
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void Searchbtn_Click(object sender, EventArgs e)
        {
            employee[0] = new Employee();
            employee[1] = new Employee(17433, "Adrian", "Smith", 8000.00);
            employee[2] = new Employee(17434, "Stephen", "Rad", 9000.00);
            employee[3] = new Employee(17435, "Jesse", "Harris", 800.00);
            employee[4] = new Employee(17436, "jonatan", "Morris", 9500.00);
            employee[5] = new Employee(17437, "Morgen", "Freeman", 12000.00);
            employee[6] = new Employee(17438, "Leory", "Gomez", 10200.00);
            employee[7] = new Employee(17439, "Michael", "Brown", 9000.00);
            employee[8] = new Employee(17440, "Andrew", "White", 3500.00);
            employee[9] = new Employee(17441, "Maria", "Carson", 12000.00);
            //employee[10] = new Employee(17442, "Mark", "Jonson", 17000.00);

            for(int i = 0; i < 10; i++)
            {
                string employeeString = employee[i].employeeInformationToString() + "\r\n";

                richTextBox1.AppendText(employeeString);

            }

        }
        Employee[] employee = new Employee[10];

        private void getinfibtn_Click(object sender, EventArgs e)
        {
            Find();
        }
        private void Find()
        {

        }
    }
}

我的问题是:

用户如何找到年薪最低的员工。我必须确保可以有不止一名员工的年薪最低,并显示有关他们的信息。为用户提供适当的 GUI(例如消息框)以显示所需的信息,包括用于处理异常的适当代码并在适当的情况下使用方法?

【问题讨论】:

  • 您想要一份最低薪员工名单吗?如果你想要这个,请看我的回答

标签: c#-4.0


【解决方案1】:

您需要让您的 Employee 类实现 IComparable 接口,然后将对象与薪水进行比较,并在另一个类中对数组进行排序...

示例:

public class Employee :IComparable<Employee>
{
    private int employeeID;
    private string fullName;
    private string lastName;
    private double salary;

  public int CompareTo(Employee other)
  {
     return salary.CompareTo(other.salary);
  }
}

private void Find()
{
    Array.Sort(employee); // after this Employee is sorted
    employee[0];  
    or 
    employee[9];
}

【讨论】:

  • 感谢您的帮助。如何获取特定员工 ID 的全名、姓氏和年薪?
  • 错误名称employee在当前上下文中不存在,但存在Employee[] employee = new Employee[10];
  • 是的,它在 find 方法中
  • 员工[]员工=新员工[10];必须是类变量...我在find方法中写了employees,请检查你是否也有这个????
  • 现在它在员工类中,但形式为员工[0] = new Employee();员工[1] = 新员工(17433,“阿德里安”,“史密斯”,8000.00);员工[2] = 新员工(17434,“斯蒂芬”,“拉德”,9000.00);并且其显示错误员工在当前上下文中不存在
【解决方案2】:

这将给出最低工资员工的列表

        employee.Add(new Employee(17434, "Stephen", "Rad", 9000.00));
        employee.Add(new Employee(17435, "Jesse", "Harris", 800.00));
        employee.Add(new Employee(17436, "jonatan", "Morris", 9500.00));
        var c = employee.OrderBy(i => i.salary).ToList();
        var e = employee.Where(i => Math.Abs(i.salary - c[0].salary) < 1).ToList();

【讨论】:

    【解决方案3】:

    稍微修改了你的代码

    class Employee
        {
            private int employeeID;
            private string fullName;
            private string lastName;
            private double salary;
            public double Salary
            {
                get
                {
                    return salary;
                }
                set
                {
                    salary = value;
                }
            }
            //public Employee()
            //{
            //    employeeID = 0;
            //    fullName = "";
            //    lastName = "";
            //    salary = 0.0;
            //}
            //public Employee(int empIDValue, string fullNameVal, string lastNameVal)
            //{
            //    employeeID = empIDValue;
            //    fullName = fullNameVal;
            //    lastName = lastNameVal;
            //    salary = 0.0;
            //}
            public Employee(int empIDValue, string fullNameVal, string lastNameVal, double salaryValue)
            {
                employeeID = empIDValue;
                fullName = fullNameVal;
                lastName = lastNameVal;
                salary = salaryValue;
            }
            public int EmployeeIDNum
            {
                get
                {
                    return employeeID;
                }
                set
                {
                    employeeID = value;
                }
            }
            public string FullName
            {
                get
                {
                    return fullName;
                }
                set
                {
                    fullName = value;
    
                }
            }
            public int Getinfo
            {
                get
                {
                    return employeeID;
                }
                set
                {
                    employeeID = value;
                }
    
            }
            public string employeeInformationToString()
            {
                // employeeID = Convert.ToInt32(this.textBox1.Text);
                return (Convert.ToString(employeeID) + " " + fullName + " " + lastName + " " + Convert.ToString(salary));
            }
        }
    

    并获取列表中的最小值

     var minEmpSalarylist = employee.Where(x => x.Salary == employee.Min(y => y.Salary)).ToList();
    

    如果存在默认构造函数,则所有minEmpSalarylist 都将使用默认构造函数进行初始化。

    employee[0] = new Employee();
    

    改成

    employee[0] = new Employee(17433, "XXX", "YYY", 8000.00);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      • 1970-01-01
      • 2016-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多