0% found this document useful (0 votes)
15 views10 pages

Lab 5 6 7 8 C#1

The third document defines interfaces and classes for books, including an IBook interface, Book class, and BookList class. The BookList class can add Book objects, display book information, and sort the books by different
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views10 pages

Lab 5 6 7 8 C#1

The third document defines interfaces and classes for books, including an IBook interface, Book class, and BookList class. The BookList class can add Book objects, display book information, and sort the books by different
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Sex

class ChuNhat
{
public double rong;
public double dai;

public ChuNhat(double rong, double dai)


{
this.rong = rong;
this.dai = dai;
}

public double GetChuVi()


{
return 2 * rong + dai;
}

public double GetDienTich()


{
return rong * dai;
}

public virtual void Xuat()


{
Console.WriteLine("Chieu rong: {0}", rong);
Console.WriteLine("Chieu dai: {0}", dai);
Console.WriteLine("Chu vi: {0}", GetChuVi());
Console.WriteLine("Dien tich: {0}", GetDienTich());
}
}

class Vuong : ChuNhat


{
public Vuong(double canh) : base(canh, canh) { }
public override void Xuat()
{
Console.WriteLine("Canh: {0}", rong);
Console.WriteLine("Chu vi: {0}", GetChuVi());
Console.WriteLine("Dien tich: {0}", GetDienTich());
}
}
internal class Program
{
static void Main(string[] args)
{
Console.Write("Nhap chieu rong hinh chu nhat 1:");
double rong1 = double.Parse(Console.ReadLine());
Console.Write("Nhap chieu dai hinh chu nhat 1:");
double dai1 = double.Parse(Console.ReadLine());
ChuNhat hinhChuNhat1 = new ChuNhat(rong1, dai1);

Console.Write("Nhap chieu rong hinh chu nhat 2:");


double rong2 = double.Parse(Console.ReadLine());
Console.Write("Nhap chieu dai hinh chu nhat 2:");
double dai2 = double.Parse(Console.ReadLine());
ChuNhat hinhChuNhat2 = new ChuNhat(rong2, dai2);

Console.Write("Nhap canh hinh vuong:");


double canh = double.Parse(Console.ReadLine());
Vuong hinhVuong = new Vuong(canh);

Console.WriteLine("Thong tin hinh chu nhat 1:");


hinhChuNhat1.Xuat();
Console.WriteLine("Thong tin hinh chu nhat 2:");
hinhChuNhat2.Xuat();
Console.WriteLine("Thong tin hinh vuong:");
hinhVuong.Xuat();
}
}\

abstract class SinhVienPoly


{
public string hoTen;
public string tenNganh;
public SinhVienPoly(string hoTen, string tenNganh)
{
this.hoTen = hoTen;
this.tenNganh = tenNganh;
}
public abstract double GetDiem();
public string GetHocLuc()
{
double diem = GetDiem();
if (diem >= 8.5)
return "Xuat sac";
else if (diem >= 7)
return "Gioi";
else if (diem >= 5.5)
return "Kha";
else if (diem >= 4)
return "Trung Binh";
else return "Yeu";

}
public void Xuat()
{
Console.WriteLine("Ho ten: {0}", hoTen);
Console.WriteLine("Nganh: {0}", tenNganh);
Console.WriteLine("Diem: {0}", GetDiem());
Console.WriteLine("Hoc Luc: {0}", GetHocLuc());
}
class SinhVienPolyEx : SinhVienPoly
{
private double diem;
public SinhVienPolyEx(string hoTen, string tenNganh, double diem) : base(hoTen, tenNganh)
{
this.diem = diem;
}
public override double GetDiem()
{
return diem;
}
}
class Prongram
{
static void Main(string[] args)
{
SinhVienPoly sv = new SinhVienPolyEx("BEo", "CNTT", 4);
sv.Xuat();
}
}
}

abstract class SinhVienPoly


{
public string hoTen;
public string tenNganh;
public SinhVienPoly(string hoTen, string tenNganh)
{
this.hoTen = hoTen;
this.tenNganh = tenNganh;
}
public abstract double GetDiem();
public string GetHocLuc()
{
double diem = GetDiem();
if (diem >= 8.5)
return "Xuat sac";
else if (diem >= 7)
return "Gioi";
else if (diem >= 5.5)
return "Kha";
else if (diem >= 4)
return "Trung Binh";
else return "Yeu";

}
public void Xuat()
{
Console.WriteLine("Ho ten: {0}", hoTen);
Console.WriteLine("Nganh: {0}", tenNganh);
Console.WriteLine("Diem: {0}", GetDiem());
Console.WriteLine("Hoc Luc: {0}", GetHocLuc());
}
class SinhVienIT : SinhVienPoly
{
private double diemJava;
private double diemHTML;
private double diemCSS;
public SinhVienIT(string hoTen, string tenNganh, double diemJava, double diemHTML, double diemCSS) :
base(hoTen, tenNganh)
{
this.diemJava = diemJava;
this.diemHTML = diemHTML;
this.diemCSS = diemCSS;

}
public override double GetDiem()
{
return (2 * diemJava + diemHTML + diemCSS) / 4 ;
}

}
class SinhVienBiz : SinhVienPoly
{
private double diemMarketing;
private double diemSales;
public SinhVienBiz(string hoTen, string tenNganh, double diemMarketing, double diemSales) : base(hoTen,
tenNganh)
{
this.diemMarketing = diemMarketing;
this.diemSales = diemSales;

}
public override double GetDiem()
{
return (2 * diemMarketing + diemSales) / 3;
}
}
class Prongram
{
static void Main(string[] args)
{
SinhVienPoly svit = new SinhVienIT("BEo", "CNTT", 4, 8 ,7 );
Console.WriteLine("Thong tin sinh vien IT: ");
svit.Xuat();
SinhVienPoly svbiz = new SinhVienBiz("gay", "Kinh daonh", 6, 8);
Console.WriteLine("Thong tin sinh vien Biz: ");
svbiz.Xuat();
}
}
}

public interface IBook


{
string Title { get; set; }
string Author { get; set; }
string Publisher { get; set; }
int Year { get; set; }
string ISBN { get; set; }
List<string> Chapters { get; set; }

void DisplayInfo();
}
public class Book : IBook
{
public string Title { get; set; }
public string Author { get; set; }
public string Publisher { get; set; }
public int Year { get; set; }
public string ISBN { get; set; }
public List<string> Chapters { get; set; }
public Book(string title, string author, string publisher, int year, string iSBN, List<string> chapters)
{
Title = title;
Author = author;
Publisher = publisher;
Year = year;
ISBN = iSBN;
Chapters = chapters;
}
public void DisplayInfo()
{
Console.WriteLine("Ten sach: {0}", Title);
Console.WriteLine("Ten tac gia: {0}", Author);
Console.WriteLine("Nha xuat ban: {0}", Publisher);
Console.WriteLine("ISNB: {0}", ISBN);
Console.WriteLine("Chuong: ");
foreach (var chap in Chapters)
{
Console.WriteLine("{0}", chap);
}
}
}
public class BookList
{
private List<Book> books;

public BookList()
{
books = new List<Book>();
}
public void AddBook(IBook book)
{
books.Add((Book)book);
}
public void DisplayBooks()
{
foreach (var book in books)
{
book.DisplayInfo();
}
}
public void SortByAuthor()
{
books.Sort((x,y) => x.Author.CompareTo(y.Author));
}
public void SortByTitle()
{
books.Sort((x, y) => x.Title.CompareTo(y.Title));
}
public void SortByYear()
{
books.Sort((x, y) => x.Year.CompareTo(y.Year));
}
}
internal class Program
{
static void Main(string[] args)
{
List<string> chapters1 = new List<string> { "Chapter 1", "Chapter 2", "Chapter 3" };
IBook book1 = new Book("Book 1", "Author B", "Publisher X", 2020, "ISBN123456", chapters1);

List<string> chapters2 = new List<string> { "Introduction", "Body", "Conclusion" };


IBook book2 = new Book("Book 2", "Author C", "Publisher Y", 2019, "ISBN654321", chapters2);

List<string> chapters3 = new List<string> { "Part 1", "Part 2" };


IBook book3 = new Book("Book 3", "Author A", "Publisher Z", 2021, "ISBN987654", chapters3);

BookList bookList = new BookList();


bookList.AddBook(book1);
bookList.AddBook(book2);
bookList.AddBook(book3);

Console.WriteLine("Danh sach sach: ");


bookList.DisplayBooks();
Console.WriteLine("Danh sach Sach sap theo tac gia: ");
bookList.SortByAuthor();
Console.WriteLine("Danh sach sach sap theo ten sach: ");
bookList.SortByTitle();
Console.WriteLine("Danh sach sach sap theo nam: ");
bookList.SortByYear();
}
}

public delegate void CalculationDelegate(double a, double b);


public class Calculation
{
public static void Add(double a, double b)
{
Console.WriteLine("Tong: {0} ", a + b);
}
public static void Subtract(double a, double b)
{
Console.WriteLine("Hieu: {0} ", a - b);
}
public static void Mutilply(double a, double b)
{
Console.WriteLine("Tich: {0} ", a * b);
}
public static void Divide(double a, double b)
{
if (b != 0)
{
Console.WriteLine("Thuong: {0} ", a / b);
}else { Console.WriteLine("Khong chia duoc cho 0"); }
}
}
public class Program
{
static void Main(string[] args)
{
CalculationDelegate calDelegate;
Console.WriteLine("Nhap so a");
double a = double.Parse(Console.ReadLine());
Console.WriteLine("Nhap so b");
double b = double.Parse(Console.ReadLine());
calDelegate = Calculation.Add;
calDelegate(a, b);
calDelegate = Calculation.Subtract;
calDelegate(a, b);
calDelegate = Calculation.Mutilply;
calDelegate(a, b);
calDelegate = Calculation.Divide;
calDelegate(a,b);
}
}

public class NumberEventArgs


{
public int Number { get; set; }
public NumberEventArgs(int number)
{
Number = number;
}
}
public delegate void NumberAdd(object sender, NumberEventArgs e);
public class NumberManager
{
public event NumberAdd NumberAdded;
private ArrayList nums = new ArrayList();
public void AddNumber(int number)
{
if (number > 0)
{
nums.Add(number);
}
else { Console.WriteLine("Invalid"); }
}
protected virtual void OnNumberAdded(int num)
{
NumberAdded?.Invoke(this, new NumberEventArgs(num));
}
public void DisplayNumber()
{
Console.WriteLine("Number List");
foreach (int num in nums)
{
Console.WriteLine(num);
}
}
}
class Program
{
static void Main(string[] args)
{
NumberManager manager = new NumberManager();

while (true)
{
Console.WriteLine("Menu");
Console.WriteLine("Add");
Console.WriteLine("Display");
Console.WriteLine("Exit");
Console.WriteLine("Choice: ");
string choice = Console.ReadLine();
switch (choice)
{
case "add":
Console.WriteLine("Enter integer number:");
if (int.TryParse(Console.ReadLine(),out int num))
{
manager.AddNumber(num);
}
else
{
Console.WriteLine("Invalid");
}
break;
case "display":
manager.DisplayNumber();
break;
case "exit":
Console.WriteLine("Exit");
return;
default : Console.WriteLine("Ivnvalid");
break;
}
}
}
}

class Product
{
public string Name { get; set; }
public double Cost { get; set; }
public int OnHand { get; set; }
public override string ToString()
{
return $"Name: {Name}, Cost: {Cost}, On Hand {OnHand}";
}
}
internal class Program
{
static void Main(string[] args)
{
ArrayList pro = new ArrayList();

pro.Add(new Product { Name = "Product 1", Cost = 30, OnHand = 20});


pro.Add(new Product { Name = "Product 2", Cost = 14.5, OnHand = 70 });
pro.Add(new Product { Name = "Product 3", Cost = 23, OnHand = 10 });
pro.Add(new Product { Name = "Product 4", Cost = 7.1, OnHand = 90 });
pro.Add(new Product { Name = "Product 5", Cost = 20, OnHand = 2 });

Console.WriteLine("Product Info: ");


foreach (Product p in pro)
{
Console.WriteLine(p);
}
}
}

class Program
{
static void Main(string[] args)
{
Hashtable weekDay = new Hashtable();
weekDay.Add(1, "Monday");
weekDay.Add(2, "Tuesday");
weekDay.Add(3, "Wednesday");
weekDay.Add(4, "Thursday");
weekDay.Add(5, "Friday");
weekDay.Add(6, "Saturday");
weekDay.Add(7, "Sunday");

string tuesday = (string)weekDay[2];


if (tuesday != null)
{
Console.WriteLine("Tuesday: "+ tuesday);
}
else
{
Console.WriteLine("Khong tim thay ngay tuesday trong Hashtable");
}
Console.WriteLine("day on week: ");
foreach (DictionaryEntry entry in weekDay)
{
Console.WriteLine("Key: "+ entry.Key);
Console.WriteLine("Value: "+ entry.Value);
}
}
}

class Program
{
static void Main(string[] args) {
int a = 1;
int b = 2;
Console.WriteLine($"Truoc khi hoan vi: a = {a}, b = {b} ");
Swap( ref a, ref b );
Console.WriteLine($"Sau khi hoan vi: a = {a}, b = {b} ");

string x = "Beo";
string y = "Vai";

Console.WriteLine($"Truoc khi hoan vi: x = {x}, y = {y} ");


Swap(ref x, ref y);
Console.WriteLine($"Sau khi hoan vi: x = {x}, y = {y} ");
}
static void Swap<T> (ref T fisrt, ref T secord)
{
T temp = fisrt;
fisrt = secord;
secord = temp;
}
}

You might also like