1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace StoreEmployeeUsingStruct
7{
8 class Program
9 {
10 /// <summary>
11 /// Cấu trúc Nhân viên bao gồm
12 /// - Họ tên
13 /// - Ngày sinh
14 /// - Giới tính
15 /// </summary>
16 struct NhanVien
17 {
18 public string HoTen;
19 public NgaySinhDataType NgaySinh;
20 public int GioiTinh;
21 public bool LaNhanVienChinhThuc;
22 public int Tuoi;
23
24 public void InThongTin()
25 {
26 Console.WriteLine("Ho ten: {0}", this.HoTen);
27 Console.WriteLine("Ngay sinh: {0}", this.HoTen);
28 Console.WriteLine("Tuoi: {0}", this.Tuoi);
29 Console.WriteLine("Gioi tinh: {0}", (this.GioiTinh == 0) ? "Nam" : "Nu");
30 Console.WriteLine("Loai hop dong: {0}", (this.LaNhanVienChinhThuc == true) ? "Nhan vien chinh thuc" : "Nhan vien tap su");
31 }
32 }
33
34 /// <summary>
35 /// Cấu trúc Ngày sinh bao gồm
36 /// - Ngày
37 /// - Tháng
38 /// - Năm
39 /// </summary>
40 struct NgaySinhDataType
41 {
42 public int Day;
43 public int Month;
44 public int Year;
45 }
46
47 /// <summary>
48 /// Dem tong so nu
49 /// </summary>
50 /// <param name="danhsachNhanVien"></param>
51 /// <returns></returns>
52 static int DemTongSoNu(NhanVien[] danhsachNhanVien)
53 {
54 int result = 0;
55 for (int i = 0; i < danhsachNhanVien.Length; i++)
56 {
57 if(danhsachNhanVien[i].GioiTinh == 1)
58 {
59 result++;
60 }
61 }
62 return result;
63 }
64
65 static NhanVien TimNhanVienTuoiCaoNhat(NhanVien[] danhsachNhanVien)
66 {
67 // Giả sử nhân viên đầu tiên tuổi cao nhất
68 NhanVien temp = danhsachNhanVien[0];
69
70 // Duyệt danh sách các nhân viên đang có trong mảng
71 // Nếu nhân viên nào cao tuổi hơn thì gán lại
72 for (int i = 0; i < danhsachNhanVien.Length; i++)
73 {
74 if(danhsachNhanVien[i].Tuoi > temp.Tuoi)
75 {
76 temp = danhsachNhanVien[i];
77 }
78 }
79
80 return temp;
81 }
82
83 static void Main(string[] args)
84 {
85 int total = 3;
86 Console.Write("\nNhap thong tin Nhan vien su dung Struct trong C#:\n");
87 Console.Write("--------------------------\n");
88
89 // Nhập thông tin danh sách [total] Nhân viên
90 NhanVien[] emp = new NhanVien[total];
91 for (int i = 0; i < total; i++)
92 {
93 Console.WriteLine("--- NHAP THONG TIN NHAN VIEN THU {0} ---", (i+1));
94 // Họ tên
95 Console.Write("Ho ten nhan vien: ");
96 string hotenInput = Console.ReadLine();
97 emp[i].HoTen = hotenInput;
98
99 // Nhập tuổi
100 Console.Write("Nhap ngay sinh: ");
101 int tuoiInput = Convert.ToInt32(Console.ReadLine());
102 emp[i].Tuoi = tuoiInput;
103
104 // Giới tính
105 Console.Write("Nhap gioi tinh (0: Nam; 1: Nu): ");
106 int gioiTinhInput = Convert.ToInt32(Console.ReadLine());
107 emp[i].GioiTinh = gioiTinhInput;
108
109 // Ngày tháng năm sinh
110 Console.Write("Nhap ngay sinh: ");
111 int ddInput = Convert.ToInt32(Console.ReadLine());
112 emp[i].NgaySinh.Day = ddInput;
113
114 Console.Write("Nhap thang sinh: ");
115 int mmInput = Convert.ToInt32(Console.ReadLine());
116 emp[i].NgaySinh.Month = mmInput;
117
118 Console.Write("Nhap nam sinh: ");
119 int yyInput = Convert.ToInt32(Console.ReadLine());
120 emp[i].NgaySinh.Year = yyInput;
121
122 // Loại hợp đồng
123 Console.Write("Nhap loai hop dong (Y: chinh thuc; N: tap su): ");
124 string loaiHopDongInput = Console.ReadLine();
125 emp[i].LaNhanVienChinhThuc = (loaiHopDongInput == "Y") ? true : false;
126
127 Console.WriteLine("--------------------------------------");
128 }
129
130 // In thông tin danh sách [total] Nhân viên vừa nhập
131 Console.WriteLine("=== DANH SACH NHAN VIEN ===");
132 for (int i = 0; i < total; i++)
133 {
134 Console.WriteLine("---Nhan vien thu {0}", (i+1));
135 emp[i].InThongTin();
136 Console.WriteLine("--------------------");
137 }
138 Console.WriteLine("===========================");
139
140 // Dừng màn hình để kiểm tra
141 Console.ReadKey();
142 }
143 }
144}