C#:如何显示薪水最低的员工姓名

时间:2016-04-15 18:32:20

标签: c#-4.0

我必须创建一个适当的GUI来输入至少10名员工的信息。对于每个员工,我必须输入以下信息。员工ID,员工名字,员工姓氏和年薪。此外,我必须检查输入数据的正确性。此外,我需要创建一个单独的类 EMPLOYEE ,其中包含员工信息:员工ID,名字,姓氏和年薪。该类应具有构造函数属性和方法。所有员工信息必须存储在类型员工的数组中。在阅读表单GUI后,有关特定员工的信息,还会使用相关的构造函数创建类employee(对象的元素)的对象。尽管有一个以上年薪最低的员工,但用户希望能够找到年薪最低的员工。并显示有关它们的信息。应为用户提供适当的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(例如消息框)以显示所需信息,包括处理异常的适当代码,并在适当情况下使用方法?

3 个答案:

答案 0 :(得分:0)

您需要让您的类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];
}

答案 1 :(得分:0)

这将列出最低工资的员工

        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();

答案 2 :(得分:0)

稍微修改了你的代码

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);
相关问题