Tuesday, June 9, 2015

Day 5: Collections

Lab 7:

Code: 




Class Employee


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

namespace FPT_Day05
{
    class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public double Salary { get; set; }

        public Employee(int pId, string pName, int pAge, double pSalary)
        {
            Id = pId;
            Name = pName;
            Age = pAge;
            Salary = pSalary;
        }

        public override string ToString()
        {
            return "+ID: " + Id + " +Name: " + Name + " +Age: " + Age + " +Salary: " + Salary;
        }
    }

}

Class EmployeeManager


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections; // Gọi cái này thì mới gọi đc ArrayList
namespace FPT_Day05
{
    class EmployeeManager
    {
        ArrayList listEmployee;

        public EmployeeManager()
        {
            listEmployee = new ArrayList();
        }

        public void Display()
        {
            foreach(object student in listEmployee)
            {
                Console.WriteLine(student.ToString());
            }
        }

        public void AddStudent (Employee obj)
        {
            listEmployee.Add(obj);
        }

        public Employee SearchById(int id)
        {
            foreach(Employee employee in listEmployee)
            {
                if(employee.Id == id)
                {
                    return employee;
                }
            }
            Console.WriteLine("ID cannot be found! Please try again.");
            return null;
        }

        public void DeleteById(int id)
        {
            Employee s = SearchById(id);
            if(s != null)
            {
                listEmployee.Remove(s);
                Console.WriteLine("Delete successful !\n");
            }
        }

        public void UpdateById(int id)
        {
            Employee s = SearchById(id);
            if(s != null)
            {
                Console.Write("Name change to: ");
                s.Name = Console.ReadLine();
                Console.Write("Age change to: ");
                s.Age= Int32.Parse(Console.ReadLine());
                Console.Write("Salary change to ");
                s.Salary = Int32.Parse(Console.ReadLine());
            }
        }
    }
}

Class Program


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

namespace FPT_Day05
{
    class Program
    {
        static void Main(string[] args)
        {
            int choice;
            EmployeeManager em = new EmployeeManager();
            do
            {
                Console.WriteLine("1 - Display");
                Console.WriteLine("2 - Add Student");
                Console.WriteLine("3 - Search ");
                Console.WriteLine("4 - Delete");
                Console.WriteLine("5 - Update");
                Console.Write("Your choice: ");
                choice = Int32.Parse(Console.ReadLine());
                Console.WriteLine("");

                switch (choice)
                {
                    case 1:
                        em.Display();
                        Console.WriteLine("");
                        break;
                    case 2:
                        Console.Write("Enter employee ID: ");
                        int id = Int32.Parse(Console.ReadLine());
                        Console.Write("Enter name: ");
                        string name = Console.ReadLine();
                        Console.Write("Enter age: ");
                        int age = Int32.Parse(Console.ReadLine());
                        Console.Write("Enter salary: ");
                        double salary = double.Parse(Console.ReadLine());
                        Console.WriteLine("");
                        Employee s = new Employee(id, name, age, salary);
                        em.AddStudent(s);
                        break;
                    case 3:
                        Console.Write("ID want to search: ");
                        int findId = Int32.Parse(Console.ReadLine());
                        Console.WriteLine(em.SearchById(findId));
                        Console.WriteLine("");
                        break;
                    case 4:
                        Console.Write("ID want to delete: ");
                        int deleteId = Int32.Parse(Console.ReadLine());
                        em.DeleteById(deleteId);
                        Console.WriteLine("");
                        break;
                    case 5:
                        Console.Write("ID want to update: ");
                        int updateId = Int32.Parse(Console.ReadLine());
                        em.UpdateById(updateId);
                        break;
                }
            } while (choice != 0);
        }
    }
}

Result:


Summary:


  • Collection

    • Là tập hợp các dữ liệu có liên quan nhưng không nhất thiết phải cùng kiểu dữ liệu.
    • Dùng namespace System.Collections
    • Một số kiểu collections:
      • ArrayList : Sắp xếp mảng thường như array, kích thước 16. Datatype có thể khác nhau và có thể thay đổi.
      • Hashtable: tạo tập hợp dưới dạng key và value 
      • SortedList: Các giá trị trong mảng sẽ được sắp xếp theo giá trị, tuy nhiên tốc độ sẽ bị chậm.

Arrays
Collections
-Không thể thực hiện lúc run-time
- Có thể thực hiện lúc run-time
-Các phần tử riêng lẻ phải có liên quan đến nhau.có cùng kiể dữ liệu.
- Các phần tử có thể khác kiểu dữ liệu
-Không hề có bất kỳ phương thức hay tóan tử cho các phần tử.
- Bao gồm các phương thức , các toán tử trên các thành phần.

  • Generic
    • Các collection dạng generic ta sẽ phải thêm khai báo data type vào trong dấu "< >" trước tên mỗi collection.
    • Tác dụng của generic là đảm bảo chuẩn của nó.

No comments:

Post a Comment