using System; using System.Collections.Generic; using System.Linq; namespace Bai2 { class PERSON { protected string hoten; protected string gioitinh; protected int namsinh; public string HoTen => hoten; // thuộc tính chỉ đọc để kiểm tra trùng tên public PERSON() { } public PERSON(string hoten, string gioitinh, int namsinh) { this.hoten = hoten; this.gioitinh = gioitinh; this.namsinh = namsinh; } public virtual void Nhap() { Console.Write("Nhap ho ten: "); hoten = Console.ReadLine(); Console.Write("Nhap gioi tinh: "); gioitinh = Console.ReadLine(); Console.Write("Nhap nam sinh: "); namsinh = int.Parse(Console.ReadLine()); } public virtual void Xuat() { Console.WriteLine($"{hoten,-25} {gioitinh,-10} {namsinh,-10}"); } } class CONGNHAN : PERSON { private string tenCongTy; private double heSoLuong; private double thuNhap; public double HeSoLuong => heSoLuong; public CONGNHAN() { } public CONGNHAN(string hoten, string gioitinh, int namsinh, string tenCongTy, double heSoLuong) : base(hoten, gioitinh, namsinh) { this.tenCongTy = tenCongTy; this.heSoLuong = heSoLuong; this.thuNhap = heSoLuong * 850000; } public override void Nhap() { base.Nhap(); Console.Write("Nhap ten cong ty: "); tenCongTy = Console.ReadLine(); Console.Write("Nhap he so luong: "); heSoLuong = double.Parse(Console.ReadLine()); thuNhap = heSoLuong * 850000; } public override void Xuat() { Console.WriteLine($"{hoten,-25} {gioitinh,-10} {namsinh,-10} {tenCongTy,-20} {heSoLuong,-10:F2} {thuNhap,-15:N0}"); } } class Program { static void Main(string[] args) { List ds = new List(); Console.Write("Nhap so luong cong nhan: "); int n = int.Parse(Console.ReadLine()); for (int i = 0; i < n; i++) { Console.WriteLine($"\nNhap thong tin cong nhan thu {i + 1}:"); CONGNHAN cn = new CONGNHAN(); // Kiểm tra trùng họ tên while (true) { cn.Nhap(); bool trungTen = ds.Any(x => x.HoTen.Equals(cn.HoTen, StringComparison.OrdinalIgnoreCase)); if (trungTen) { Console.WriteLine("❌ Ho ten da ton tai, vui long nhap lai!\n"); } else { break; } } ds.Add(cn); } Console.WriteLine("\n=== DANH SACH CONG NHAN ==="); Console.WriteLine($"{"Ho Ten",-25} {"Gioi Tinh",-10} {"Nam Sinh",-10} {"Cong Ty",-20} {"He So",-10} {"Thu Nhap",-15}"); Console.WriteLine(new string('-', 90)); foreach (var cn in ds) { cn.Xuat(); } // Tìm công nhân có hệ số lương cao nhất double maxHeSo = ds.Max(c => c.HeSoLuong); Console.WriteLine("\n=== CONG NHAN CO HE SO LUONG CAO NHAT ==="); Console.WriteLine($"{"Ho Ten",-25} {"Gioi Tinh",-10} {"Nam Sinh",-10} {"Cong Ty",-20} {"He So",-10} {"Thu Nhap",-15}"); Console.WriteLine(new string('-', 90)); foreach (var cn in ds) { if (cn.HeSoLuong == maxHeSo) { cn.Xuat(); } } Console.ReadLine(); } } }