Tuesday, November 25, 2014

IComparable


namespace MyClassLab
{
    public class Account : IComparable<Account>  //Option1: This is added to compare accounts
    {

        //define an account class without
        //any fields or constructor.
        //This is a quick way of defining a class

        //see tutrial on msdn:
        //http://msdn.microsoft.com/en-us/library/bb384062.aspx
        //http://msdn.microsoft.com/en-us/library/bb397680.aspx
        public int AccountNumber
        {
            get;
            set;
        }
        public decimal Balance
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }

        public int CompareTo(Account other)
        {
            //compare this object to other
            //compare by balance
            if (this.Balance > other.Balance)
                //return a positive value
                return 1;

            //if (this.Balance == other.Balance)
            //    return 0;

            if (this.Balance == other.Balance)
            {
                //this is to sort alphabeticl order if two balance have the same amount.
                return (String.Compare(this.Name, other.Name, true));
            }
            return -1;
        }
    }
}
//====================================================================
namespace IComparable_Interface
{
    public partial class Form1 : Form
    {
        List<Account> accountList = new List<Account>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //populate acountlist with few accounts
            //create an Account object using initializers (syntax style)
            Account a = new Account { Name = "John", AccountNumber = 12345, Balance = 2500 };
            accountList.Add(a);
            a = new Account { Name = "Zau", AccountNumber = 23456, Balance = 1500 };
            accountList.Add(a);
            a = new Account { Name = "Lisa", AccountNumber = 34567, Balance = 25000 };
            accountList.Add(a);
            a = new Account { Name = "Mary", AccountNumber = 45678, Balance = 2000 };
            accountList.Add(a);
            a = new Account { Name = "Ibrahim", AccountNumber = 8542, Balance = 1790 };
            accountList.Add(a);
            a = new Account { Name = "Yeti", AccountNumber = 56789, Balance = 11500 };
            accountList.Add(a);
            a = new Account { Name = "Sape", AccountNumber = 67890, Balance = 14500 };
            accountList.Add(a);
            a = new Account { Name = "Mike", AccountNumber = 78901, Balance = 7900 };
            accountList.Add(a);
            a = new Account { Name = "Dack", AccountNumber = 89012, Balance = 1500 };
            accountList.Add(a);
            a = new Account { Name = "Nau", AccountNumber = 90123, Balance = 25000 };
            accountList.Add(a);
        }

        private void btnDisplayAccount_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            foreach (Account ac in accountList)
            {
                ListViewItem lvi = new ListViewItem(ac.Name);
                lvi.SubItems.Add(ac.AccountNumber.ToString());
                lvi.SubItems.Add(ac.Balance.ToString("c"));
                listView1.Items.Add(lvi);
            }
        }

        private void btnSortAccountByBalance_Click(object sender, EventArgs e)
        {
            //use the Sort method defined in .Net to sort lists
            //See: http://msdn.microsoft.com/en-us/library/b0zbh7b6(v=vs.110).aspx

            accountList.Sort();
            MessageBox.Show("Account List has been sorted by balance \n" + "need to re-display the account list");
        }

        private void btnSortAcByIDWithExternalMethod_Click(object sender, EventArgs e)
        {
            //sort by account number
            accountList.Sort(AccountComparer);
            MessageBox.Show("Account list has been sorted by account number \n" + "display the account to the outcome");
        }

            // public delegate int Comparison <in T> (T x, T y)
            //
            //Define a method with same signature as the delegate above
            //to compare two accounts By accountNumber
        private int AccountComparer(Account a1, Account a2)
        {
            if (a1.AccountNumber > a2.AccountNumber)
                return 1;
            if (a1.AccountNumber < a2.AccountNumber)
                return -1;
            return 0;
        }
    }
}
//Lab assignment (Due Tomorrow_ 11_13_2014)
//==============
//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

No comments:

Post a Comment