Trang

14/01/2014

Demo lập trình hướng đối tượng với C#


using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;

namespace OOP
{
    public class Huyen : IComparable<Huyen>
    {
        #region Properties
        public int ID { get; set; }
        public string Name { get; set; }
        #endregion

        #region Methods
        public Huyen() { ID = 0; Name = ""; }
        public Huyen(int _id, string _name) { ID = _id; Name = _name; }

        public override string ToString()
        {
            return "ID = " + ID + ", Name = " + Name;
        }

        public override bool Equals(object obj)
        {
            Huyen h = (Huyen)obj;
            return h.Name.Equals(Name);
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public int CompareTo(Huyen h)
        {
            return this.Name.CompareTo(h.Name);
        }
        #endregion
    }

    /// <summary>
    /// Lớp ListHuyen
    /// </summary>
    public class ListHuyen
    {
        List<Huyen> listHuyen;
        public ListHuyen()
        {
            listHuyen = new List<Huyen>();
        }
        public void Add(Huyen h)
        {
            listHuyen.Add(h);
            return;
        }
        public void Add(int _id, string _name)
        {
            listHuyen.Add(new Huyen(_id, _name));
            return;
        }
        public bool DeleteByID(int _id)
        {
            foreach (Huyen h in listHuyen)
                if (h.ID == _id)
                {
                    listHuyen.Remove(h);
                    return true;
                }
            return false;
        }
        public bool DeleteByID(Huyen _h)
        {
            foreach (Huyen h in listHuyen)
                if (h.ID == _h.ID)
                {
                    listHuyen.Remove(h);
                    return true;
                }
            return false;
        }
        public bool Update(Huyen _h)
        {
            for (int i = 0; i < listHuyen.Count; i++)
                if (listHuyen[i].ID == _h.ID)
                {
                    listHuyen[i].Name = _h.Name;
                    return true;
                }
            return false;
        }
        public DataTable GetHuyenList()
        {
            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("Name", typeof(string));
            foreach (Huyen h in listHuyen)
                table.Rows.Add(h.ID, h.Name);
            return table;
        }
        public void Sort()
        {
            this.listHuyen.Sort();
        }
    }
}



//.........................................................................................................................
using System;
using System.Collections.Generic;

namespace OOP
{
    /// <summary>
    /// Class CollectionHuyen được thừ kế từ lớp Huyện
    /// </summary>
    class CollectionHuyen : List<Huyen>
    {
        public CollectionHuyen()
        {

        }
       
        public override string ToString()
        {
            string s = "";
            foreach (Huyen h in this)
            {
                s += h.ToString() + "\n";
            }
            return s;
        }
    }
}

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

namespace OOP
{
    public class Person
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        /// <summary>
        /// Hàm tạo không đối
        /// </summary>
        public Person()
        {
            ID = 0; Name = ""; Age = 0;
        }
        /// <summary>
        /// Hàm tạo có hai đối số
        /// </summary>
        /// <param name="_name">Full Name</param>
        /// <param name="_Age">Tuổi >0, <100</param>
        public Person(int _ID, string _name, int _Age)
        {
            ID = _ID; Name = _name; Age = _Age;
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public override bool Equals(Object ob)
        {
            if (ob == null)
                return false;
            Person p = (Person)ob;
            if (ID == p.ID && Name.Equals(p.Name) && Age == p.Age)
                return true;
            return false;
        }
    }
    /// <summary>
    /// Sắp xếp theo tên và
    /// </summary>
    public class PersonNameComparer : IComparer<Person>
    {
        public int Compare(Person x, Person y)
        {
            return x.Name.CompareTo(y.Name);
        }
    }
    public class PersonAgeComparer : IComparer<Person>
    {
        public int Compare(Person x, Person y)
        {
            return x.Age.CompareTo(y.Age);
        }
    }
    public class PersonName_AgeComparer : IComparer<Person>
    {
        public int Compare(Person x, Person y)
        {
            return x.Age.CompareTo(y.Age);
        }
    }
}


//.........................................................................................................................
//file frmmain.cs
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;

namespace Combobox_DB
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }
        ListHuyen ListHuyen = new ListHuyen();
        CollectionHuyen CollectionHuyen = new CollectionHuyen();
        List<Person> listPerson = new List<Person>();


        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int id;
                int.TryParse(listBoxHuyen.SelectedValue.ToString(), out id);

                ListHuyen.DeleteByID(id);
                DisplayDataHuyen();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }



        private void btnPerson_Click(object sender, EventArgs e)
        {
            Person h1 = new Person(1, "Nguyễn Văn A", 20);
            Person h2 = new Person(2, "Nguyễn Văn C", 21);
            Person h3 = new Person(3, "Nguyễn Văn B", 22);
            Person h4 = new Person(4, "Nguyễn Văn D", 23);
            Person h5 = new Person(1, "Nguyễn Văn A", 24);

            listPerson.Add(h1);
            listPerson.Add(h2);
            listPerson.Add(h3);
            listPerson.Add(h4);
            listPerson.Sort(new PersonAgeComparer());

            DisplayDataPerson();
        }
        void DisplayDataPerson()
        {
            listBoxPerson.DataSource = null;
            listBoxPerson.Items.Clear();

            listBoxPerson.DisplayMember = "Name";
            listBoxPerson.ValueMember = "ID";
            listBoxPerson.DataSource = this.listPerson;
        }
        void DisplayDataHuyenCol()
        {
            listBoxHuyenColection.DataSource = null;
            listBoxHuyenColection.Items.Clear();

            listBoxHuyenColection.DisplayMember = "Name";
            listBoxHuyenColection.ValueMember = "ID";
            this.listBoxHuyenColection.DataSource = this.CollectionHuyen;
        }
        void DisplayDataHuyen()
        {
            listBoxHuyen.DataSource = null;
            listBoxHuyen.Items.Clear();

            listBoxHuyen.DisplayMember = "Name";
            listBoxHuyen.ValueMember = "ID";
            this.listBoxHuyen.DataSource = ListHuyen.GetHuyenList();
        }
        private void btnHuyenCol_Click(object sender, EventArgs e)
        {
            Huyen h1 = new Huyen(1, "Định hóa");
            Huyen h2 = new Huyen(1, "Phổ Yên");
            Huyen h3 = new Huyen(1, "Phú Bình");
            Huyen h4 = new Huyen(1, "Bạch Thông");

            CollectionHuyen.Add(h1);
            CollectionHuyen.Add(h2);
            CollectionHuyen.Add(h3);
            CollectionHuyen.Add(h4);
            DisplayDataHuyenCol();
        }
        private void btnLoadData_Click(object sender, EventArgs e)
        {
            Huyen h1 = new Huyen(1, "Định hóa");
            Huyen h2 = new Huyen(1, "Phổ Yên");
            Huyen h3 = new Huyen(1, "Phú Bình");
            Huyen h4 = new Huyen(1, "Bạch Thông");

            ListHuyen.Add(h1);
            ListHuyen.Add(h2);
            ListHuyen.Add(h3);
            ListHuyen.Add(h4);

            DisplayDataHuyen();
        }

        private void btnSort_Click(object sender, EventArgs e)
        {
            ListHuyen.Sort();
            DisplayDataHuyen();
        }

        private void btnSortPerson_Click(object sender, EventArgs e)
        {
            listPerson.Sort(new PersonNameComparer());
            DisplayDataPerson();
        }

    }
}

No comments:

Post a Comment