//==============
//Define a Person class with FirstName, LastName, DateOfBirth, Salary
//Add a default comparer to the Person class to compare by DateOfBirth
//In Form1: Create a list of Person objects
// Add to it at least a dozen people
//Provide ListView to display all the people in the list
//Provide buttons to display all the people to sort using the default comparer
//to sort by salary
//to sort by last name / first name
//======================================================================
//======================================================================
namespace MyClassLibrary
{
public class Person : IComparable<Person>
{
public string FirstName
{
get;
set;
}
public string LastName
{
get;
set;
}
public DateTime DateOfBirth
{
get;
set;
}
public decimal Salary
{
get;
set;
}
public int CompareTo(Person p)
{
if (this.DateOfBirth > p.DateOfBirth)
return -1;
if (this.DateOfBirth == p.DateOfBirth)
return 0;
return 1;
}
}
}
//======================================================================
namespace Zau_IComparableInterface_Lab_11_12_14
{
public partial class Form1 : Form
{
List<Person> personList = new List<Person>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Person p = new Person { FirstName = "Zau", LastName = "Bawk", DateOfBirth = new DateTime(1984, 9, 23), Salary = 5000m };
personList.Add(p);
p = new Person { FirstName = "Sara", LastName = "Phaga", DateOfBirth = new DateTime(1983, 1, 01), Salary = 4000m };
personList.Add(p);
p = new Person { FirstName = "Mike", LastName = "Samuel", DateOfBirth = new DateTime(1981, 3, 10), Salary = 35000m };
personList.Add(p);
p = new Person { FirstName = "Ann", LastName = "Like", DateOfBirth = new DateTime(1970, 2, 20), Salary = 9500m };
personList.Add(p);
p = new Person { FirstName = "David", LastName = "Lama", DateOfBirth = new DateTime(1990, 6, 15), Salary = 4000m };
personList.Add(p);
p = new Person { FirstName = "Sara", LastName = "Cliff", DateOfBirth = new DateTime(1995, 5, 18), Salary = 5000m };
personList.Add(p);
p = new Person { FirstName = "John", LastName = "Doe", DateOfBirth = new DateTime(1989, 8, 30), Salary = 6000m };
personList.Add(p);
p = new Person { FirstName = "Paul", LastName = "Aida", DateOfBirth = new DateTime(1988, 11, 29), Salary = 1000m };
personList.Add(p);
p = new Person { FirstName = "Samuel", LastName = "Richard", DateOfBirth = new DateTime(1965, 12, 12), Salary = 500m };
personList.Add(p);
p = new Person { FirstName = "Adam", LastName = "Brown", DateOfBirth = new DateTime(1991, 7, 06), Salary = 5000m };
personList.Add(p);
p = new Person { FirstName = "Eve", LastName = "Micheal", DateOfBirth = new DateTime(1993, 2, 26), Salary = 563.20m };
personList.Add(p);
p = new Person { FirstName = "Abra", LastName = "Ham", DateOfBirth = new DateTime(1985, 3, 17), Salary = 100056.09m };
personList.Add(p);
}
private void btnDisplay_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
foreach (Person ps in personList)
{
ListViewItem lvi = new ListViewItem(ps.FirstName);
lvi.SubItems.Add(ps.LastName);
lvi.SubItems.Add(ps.DateOfBirth.ToShortDateString());
lvi.SubItems.Add(ps.Salary.ToString("c"));
listView1.Items.Add(lvi);
}
}
private void btnSortByDateTime_Click(object sender, EventArgs e)
{
personList.Sort();
MessageBox.Show("Person List has been sorted by DOB \n" + "You need to re display the person list");
}
private void btnSortBySalary_Click(object sender, EventArgs e)
{
personList.Sort(PersonCompareBySalary);
MessageBox.Show("Person List has been sorted by Salary \n" + "You need to re display the person list");
}
private int PersonCompareBySalary(Person p1, Person p2)
{
if (p1.Salary > p2.Salary)
return 1;
if (p1.Salary == p2.Salary)
return 0;
return -1;
}
private void btnSortByFirstAndLastName_Click(object sender, EventArgs e)
{
personList.Sort(CompareToLastName);
MessageBox.Show("Person List has been sorted by Last Name \n" + "You need to re display the person list");
}
public int CompareToLastName(Person p1, Person p2)
{
return String.Compare(p1.LastName, p2.LastName);
}
}
}

No comments:
Post a Comment