By GokiSoft.com|
18:34 12/10/2021|
C Sharp
[Video] Tìm hiểu namespace C Sharp | Exception C Sharp | Delegate C Sharp | Event C Sharp | Collections C Sharp - C2010L
Nội dung chính trong buổi học:
- delegate + event + collections (List, ArrayList, Dictionary (Hashmap), Stack, Queue)
Bài toán:
Viết 1 chương trình phần mềm quản lý bán hàng cho siêu thì VinMart
Một số chức năng trong dự án
- Quản lý KH
- Quản lý danh mục
- Quản lý thương hiệu (Brand)
- Quản lý sản phẩm
- Quản lý promotion
- Quản lý đơn hàng
- Quản lý phản hồi
...
Senior & SA: Lên cấu trúc của dự án.
Modules -> Ứng dụng khá nhiều trong projects
- Database
- Hàm sẽ đc sử dụng lại cho cả project, .v.v
Projects:
Entities (Mapping tables <-> class objects) -> GEN tự động
Database -> Có sẵn trọng Framework
Config -> database
DBHelper
Utils
Utility -> rất nhiều loại khác nhau -> Sử dụng trong cả project
Modules
Customer
-
-
-
Category
Brand
Product
Order
Promotion
Feedback
Exception:
Thiết kế lớp đối tượng Student -> Fullname, Age, Email -> Hàm tạo đẩy đủ các tham số đầu vào
Age >= 0
Email chữa ký tự @
Age < 0 -> AgeException
Email @ -> EmailException
Test -> Chương trình hàm tạo -> Bắt lỗi cho 2 exception trên -> Hiển thị message phù hợp cho từng TH
Interface & Abtract class -> Ko khoi tao dc object truc tiep tu interface & abstract
Anonymous class -> interface & abstract
Ví dụ tạo khung dự án:
Exception
#Student.cs
using System;
namespace Lesson05.Exceptions
{
public class Student
{
public string Fullname { get; set; }
public int Age { get; set; }
public string Email { get; set; }
public Student()
{
}
public Student(string fullname, int age, string email)
{
if(age < 0)
{
throw new AgeException();
//throw new Exception("Age exception");
}
if(!email.Contains("@")) {
throw new EmailException();
//throw new Exception("Email exception");
}
Fullname = fullname;
Age = age;
Email = email;
}
}
}
#EmailException.cs
using System;
namespace Lesson05.Exceptions
{
public class EmailException : Exception
{
public EmailException()
{
}
}
}
#AgeException.cs
using System;
namespace Lesson05.Exceptions
{
public class AgeException : Exception
{
public AgeException()
{
}
}
}
Test exception và tìm hiểu Delegate C# | Event C# | Collection C#
#Program.cs
using System;
using Lesson05.Exceptions;
using System.Collections;
using System.Collections.Generic;
namespace Lesson05
{
interface IRunning
{
void OnRunning();
}
class People : IRunning
{
public void OnRunning()
{
Console.WriteLine("Test running...");
}
}
delegate void OnRunning();
delegate double Calculator(double x, double y);
class Program
{
static event Calculator tinhtongEvent;
static void Main(string[] args)
{
//TestException();
//Test01();
Test02();
}
static void Test02()
{
//List
List<int> list1 = new List<int>();
list1.Add(123);
list1.Add(34);
//ArrayList -> Quan ly nhieu kieu du lieu khac nhau.
ArrayList list2 = new ArrayList();
list2.Add(123);
list2.Add("test sdfdsf");
list2.Add(true);
list2.Add('C');
List<Object> list3 = new List<object>();
list3.Add(123);
list3.Add("test sdfdsf");
list3.Add(true);
list3.Add('C');
//Dictionary -> Hashmap
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("fullname", "Tran Van Diep");
dic.Add("email", "abc@gmail.com");
if(dic.ContainsKey("fullname"))
{
dic["fullname"] = "asdsadsad";
} else
{
dic.Add("fullname", "ABCBABCS");
}
string v = dic["fullname"];
Console.WriteLine(v);
Testing(dic);
Console.WriteLine("okok: " + dic["okok"]);
//Stack
Stack<string> stack = new Stack<string>();
stack.Push("1");
foreach(string s in stack)
{
Console.WriteLine(s);
}
stack.Push("2");
foreach (string s in stack)
{
Console.WriteLine(s);
}
stack.Push("3");
foreach (string s in stack)
{
Console.WriteLine(s);
}
string v1 = stack.Pop();
Console.WriteLine("---" + v1);
foreach (string s in stack)
{
Console.WriteLine(s);
}
//Queue
Queue<string> queue = new Queue<string>();
queue.Enqueue("1");
foreach (string s in queue)
{
Console.WriteLine(s);
}
queue.Enqueue("2");
foreach (string s in queue)
{
Console.WriteLine(s);
}
queue.Enqueue("3");
foreach (string s in queue)
{
Console.WriteLine(s);
}
string v2 = queue.Dequeue();
Console.WriteLine("---" + v2);
foreach (string s in queue)
{
Console.WriteLine(s);
}
}
static void Testing(Dictionary<string, string> dic)
{
dic.Add("okok", "ABC");
}
static void Test01()
{
People p = new People();
p.OnRunning();
//C1: Khoi tao delegate C# -> method anonymous
OnRunning running1 = delegate {
Console.WriteLine("Testing ...");
};
running1();
//C2:
OnRunning running2 = delegate() {
Console.WriteLine("Testing 2 ...");
};
running2();
//C3:
OnRunning running3 = () => {
Console.WriteLine("Testing 2 ...");
};
running3();
//C4:
OnRunning running4 = () => Console.WriteLine("Testing 2 ...");
running4();
Calculator tong = delegate(double x, double y) {
return x + y;
};
double r = tong(3, 6);
Console.WriteLine("Tong: " + r);
Calculator hieu = (x, y) => {
return x + y;
};
r = hieu(3, 6);
Console.WriteLine("Hieu: " + r);
hieu = (double x, double y) => {
return x + y;
};
r = hieu(3, 6);
Console.WriteLine("Hieu: " + r);
hieu = (x, y) => x + y;
r = hieu(3, 6);
Console.WriteLine("Hieu: " + r);
Calculator tich = new Calculator((x, y) => x * y);
Console.WriteLine("Tich: " + tich(2, 6));
Calculator cal = TinhTong;
cal = new Calculator(TinhTong);
r = cal(5, 6);
Console.WriteLine("Tich: " + r);
//Delegate -> Noi nhieu function lai -> chay list
Calculator calculator = TinhTong;
//T1
calculator += (x, y) => {
Console.WriteLine("Tinh tong test........");
return x + y;
};
//T2
calculator += (x, y) => {
Console.WriteLine("Tinh hieu ----- test........");
return x - y;
};
r = calculator(1, 9);
//TinhTong(1,9), T1(1,9), T2(1,9)
Console.WriteLine("r: " + r);
//Event
tinhtongEvent = TinhTong;
tinhtongEvent = new Calculator(TinhTong);
tinhtongEvent(2, 8);
//Truyen param la function | methods
TestDelegate(TinhTong);
}
static double TinhTong(double x, double y)
{
Console.WriteLine("Test tinh tong ...");
return x + y;
}
static void TestDelegate(Calculator cal)
{
cal(2, 8);
}
static void TestException()
{
//Exception -> Logic -> 3 Crash
int x = 0, y = 0;
try
{
Console.WriteLine("Nhap X = ");
x = int.Parse(Console.ReadLine());
} catch (Exception ex)
{
Console.WriteLine("Nhap sai!!!");
}
try
{
Console.WriteLine("Nhap Y = ");
y = int.Parse(Console.ReadLine());
} catch(Exception e)
{
Console.WriteLine("Nhap sai!!!");
}
if(y == 0)
{
Console.WriteLine("Khong dc chia cho Zero");
} else
{
int z = x / y;
Console.WriteLine("z = " + z);
}
//Next custom exception
Student std;
try
{
std = new Student("ABC", -10, "abc@gmail.com");
Console.WriteLine("Khoi tao thanh cong");
} catch(AgeException ex)
{
Console.WriteLine("Age exception");
} catch(EmailException ex)
{
Console.WriteLine("Email exception");
} catch(Exception e)
{
Console.WriteLine(e.Message);
}
try
{
std = new Student("ABC", 10, "abcgmail.com");
Console.WriteLine("Khoi tao thanh cong");
}
catch (AgeException ex)
{
Console.WriteLine("Age exception");
}
catch (EmailException ex)
{
Console.WriteLine("Email exception");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
try
{
std = new Student("ABC", 10, "abc@gmail.com");
Console.WriteLine("Khoi tao thanh cong");
}
catch (AgeException ex)
{
Console.WriteLine("Age exception");
}
catch (EmailException ex)
{
Console.WriteLine("Email exception");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)