user1336868
user1336868

Reputation: 109

find the minimum of a set of results stored in an array C#

My program has textboxes and a listView and info is typed into the textboxes and then displayed into the listview at the click of a button. the info is ID, first name, last name and yearly salary. The info is stored in array.

I want to find the person with the lowest salary. How do I go about doing this? (in C#)

this is my Form1:

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


    namespace Employee_Program
    {
        public partial class Form1 : Form
        {


            public Form1()
            {
        em = new ArrayList();
        InitializeComponent();
    }

    public ArrayList em = new ArrayList();


    private void show_employee()
    {
        listView1.Items.Clear();
        foreach(Employee a in em)
        {
            int i = listView1.Items.Count;
            listView1.Items.Add(a.EmployeeId.ToString());
            listView1.Items[i].SubItems.Add(a.FirstName);
            listView1.Items[i].SubItems.Add(a.LastName);
            listView1.Items[i].SubItems.Add(a.YearSalary.ToString());


        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Employee a = new Employee();
        a.EmployeeId = float.Parse(employeeId.Text);
        a.FirstName = firstName.Text;
        a.LastName = lastName.Text;
        a.YearSalary = float.Parse(yearSalary.Text);
        em.Add(a);
        show_employee();


    }

    private void button2_Click(object sender, EventArgs e)
    {

    // this is the button that will return the lowest salary value. Preferably in a                        
    //message box? Any idea?

        }
    }}

this is my class, Employee:

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

       namespace Employee_Program
       {
class Employee
{
    protected float employeeId;
    protected string firstName;
    protected string lastName;
    protected float yearSalary;


    // first constructor
    public Employee()
    {
        employeeId = 0;
        firstName = "";
        lastName = "";
        yearSalary = 0;
    }

    // second constructor
    public Employee(float EmployeeId, string FirstName,
                           string LastName, float YearSalary) 
    {
        employeeId = EmployeeId;
        firstName = FirstName;
        lastName = LastName;
        yearSalary = YearSalary;
    }

    public float EmployeeId
    {
        get
        {
            return employeeId;
        }

        set
        {
            employeeId = value;
        }
    }

    public string FirstName
    {
        get
        {
            return firstName;
        }

        set
        {
            firstName = value;
        }
    }
    public string LastName
    {
        get
        {
            return lastName;
        }

        set
        {
            lastName = value;
        }
    }

    public float YearSalary
    {
        get
        {
            return yearSalary;
        }

        set
        {
            yearSalary = value;
        }
    }


           }

       }

Upvotes: 1

Views: 458

Answers (4)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236208

Consider about refactoring your code.

  • you can use chain constructors to avoid initialization duplication
  • you can use auto-properties
  • its very strange that you use float as id. consider using something like int
  • usually camelCase used for parameters naming
  • consider using decimal type for salary

Your Employee class is much cleaner now:

public class Employee
{   
    public Employee()
        : this(0, "", "", 0)
    {
    }

    public Employee(int employeeId, string firstName,
                           string lastName, decimal yearSalary) 
    {
        EmployeeId = employeeId;
        FirstName = firstName;
        LastName = lastName;
        YearSalary = yearSalary;
    }

    public int EmployeeId  { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set;}
    public decimal YearSalary { get; set; }
}
  • consider using NumericUpDown control for numeric values input
  • consider using descriptive names for controls
  • consider adding new employee to the end of listView, instead of reloading all employees
  • consider using generic list for employees collection
  • usually PascalCase used for methods naming

Here is Form1 code:

private List<Employee> employees = new List<Employee>();

private void ShowEmployee(Employee employee)
{
    var item = employeeListView.Items.Add(employee.EmployeeId.ToString());
    item.SubItems.Add(employee.FirstName);
    item.SubItems.Add(employee.LastName);
    item.SubItems.Add(employee.YearSalary.ToString());
}

private void AddEmployeeButton_Click(object sender, EventArgs e)
{
    Employee employee = new Employee();
    employee.EmployeeId = (int)idNumericUpDown.Value;
    employee.FirstName = firstNameTextBox.Text;
    employee.LastName = lastNameTextBox.Text;
    employee.YearSalary = salaryNumericUpDown.Value;
    employees.Add(employee);
    ShowEmployee(employee);
}

private void LowestSalaryButton_Click(object sender, EventArgs e)
{
    decimal minSalary = employees.Min(em => em.YearSalary);
    MessageBox.Show(minSalary.ToString("C"), "Min salary");
} 

Upvotes: 1

Kendall Frey
Kendall Frey

Reputation: 44316

MoreLINQ has a MinBy method. If you don't want to use MinBy, there are several ways to do it. I recommend this approach:

// Don't use an ArrayList, use a List<Employee>
Employee minEmp = employees.Aggregate(float.MinValue, (min, e) => (e.YearSalary < min.YearSalary) ? e : min);

If you need a list of all the employees with matching minimum salary, you could do something like this:

float min = employees.Min(e => e.YearSalary);
var minEmps = employees.Where(e => e.YearSalary == min);

Upvotes: 2

spender
spender

Reputation: 120430

Look into using a MinBy extension method. It's notably lacking in Linq. An implementation can be found here.

Then you'd simply:

Employee aCheapEmployee = employees.MinBy(e => e.Salary);

If you need to find all lowest paid employees:

var minSalary = employees.Min(e => e.Salary); 
IEnumerable<Employee> slaveLabourers = employees.Where(e => e.Salary==minSalary);

Upvotes: 1

Mike Christensen
Mike Christensen

Reputation: 91598

Note: Be sure to include:

using System.Linq;

You can use a LINQ expression such as:

Employee[] employees;
//Populate employees   
var min = (from e in employees select e.YearSalary).Min();

Upvotes: 2

Related Questions