IMG-LOGO
×

Giáo Trình Học

Khoá học lập trình C# (.NET Core)

Kiến Thức Căn Bản - Khai Báo Biến, Toán Tử, Mệnh Đề Điều Kiện, Lặp

[Video] Giới thiệu về ngôn ngữ lập trình C# & .NET Framework + Khai báo biến trong C# [Video] Hướng dẫn tìm hiểu biến + toán tử trong lập trình C# [Video] Hướng dẫn tìm hiểu mệnh đề điều kiện if - else - switch trong C# [Video] Tìm dãy số nguyên tố - Lập trình C# - Loop trong C# [Video] In dãy số chẵn & lẻ từ mảng số nguyên - Lập trình C# - Loop trong C#




Trang Chủ C Sharp Viết chương trình quản lý tiêm chủng Vacxin COVID-19 - Lập trình C Sharp nâng cao.

Viết chương trình quản lý tiêm chủng Vacxin COVID-19 - Lập trình C Sharp nâng cao.

by GokiSoft.com - 15:35 28/10/2021 1,639 Lượt Xem

Tạo class object đặt tên Record gồm các thuộc tính: Họ tên (string), số CMTND (string), số điện thoại (string), địa chỉ (string), ngày tiêm (DateTime), địa chỉ tiêm (string), tên vacxin (string) -> Sử dụng CMTND làm trường khoá.


Yêu cầu: Viết menu chương trình sau

1.  Thêm N đối tượng Record

2. Hiển thị thông tin chi tiết

3. Tìm kiếm tiêm chủng theo CMTND

4. Lưu vào file db.json

5. Đọc nội dung file db.json

6. Thoát


Chú ý: Option #3 => Viết chức năng tìm kiếm tiêm chủng theo CMTND -> Hiển thị kết quả tiêm chủng của công dân đó theo yêu cầu sau:

+. Thẻ vàng: TH tiêm 1 mũi -> thời gian tiêm tới hiện tại >= 14 ngày

+ Thẻ xanh: TH tiêm 2 mũi -> Mũi gần đây nhất tới hiện tại >= 14 ngày

+ Chưa tiêm chủng: TH chưa tiêm mũi nào hoặc các mũi tiêm < 14 ngày.

Bình luận



Chia sẻ từ lớp học

Đào Mạnh Dũng [C2010L]

Ngày viết: 22:52 12/10/2021


#Program.cs


using System;
using System.Collections.Generic;

namespace _2466
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            List<Record> Recordlist = new List<Record>();
            Dictionary<string, Record> RecordDictionary = new Dictionary<string, Record>();
            while (true)
            {
                switch (menu())
                {
                    case 1:
                        {
                            Console.WriteLine("nhap n : ");
                            int n = Utility.ReadInt();
                            for (int i = 0; i < n; i++)
                            {
                                Console.WriteLine("--------------");
                                Record record = new Record();
                                record.input();
                                Recordlist.Add(record);
                                RecordDictionary.Add(record.id, record);
                            }
                            break;
                        }

                    case 2:
                        {
                            foreach (var item in Recordlist)
                            {
                                Console.WriteLine("--------------");
                                item.display();
                            }
                            break;
                        }

                    case 3:
                        {
                            Console.WriteLine("nhap id : ");
                            string id = Console.ReadLine();
                            TimeSpan time = new TimeSpan(14, 0, 0, 0);
                            string stavaccination = "Chưa tiêm chủng";
                            if (RecordDictionary[id].injection >= DateTime.Now - time)
                            {
                                stavaccination = "Thẻ vàng";
                            }
                            Console.WriteLine(" tiêm chủng la : {0}", stavaccination);
                            break;
                        }

                    case 4:
                        {
                            Utility.SavefileJson(Recordlist, "db.json");
                            break;
                        }

                    case 5:
                        {
                            Recordlist = Utility.ReadfileJson<List<Record>>("db.json");
                            break;
                        }

                    case 6:
                        {
                            Environment.Exit(0);
                            break;
                        }

                    default:
                        {
                            Console.WriteLine("nhap lai !");
                            break;
                        }
                }
            }
        }

        private static int menu()
        {
            Console.WriteLine("1. Thêm N đối tượng Record\n" +
            "\n" +
            "2. Hiển thị thông tin chi tiết\n" +
            "\n" +
            "3. Tìm kiếm tiêm chủng theo CMTND\n" +
            "\n" +
            "4. Lưu vào file db.json\n" +
            "\n" +
            "5. Đọc nội dung file db.json\n" +
            "\n" +
            "6. Thoát");
            return Utility.ReadInt();
        }
    }
}


#Record.cs


using System;

namespace _2466
{
    internal class Record
    {
        public string fullName { get; set; }
        public string id { get; set; }
        public string phoneNumber { get; set; }
        public string address { get; set; }
        public string injectionAddress { get; set; }
        public string vaccine { get; set; }
        public DateTime injection { get; set; }

        public Record()
        {
        }

        public Record(string fullName, string id, string phoneNumber, string address, string injectionAddress, string vaccine, DateTime injection)
        {
            this.fullName = fullName;
            this.id = id;
            this.phoneNumber = phoneNumber;
            this.address = address;
            this.injectionAddress = injectionAddress;
            this.vaccine = vaccine;
            this.injection = injection;
        }

        public void input()
        {
            System.Console.Write("nhap fullname : ");
            this.fullName = Console.ReadLine();
            System.Console.Write("nhap id : ");
            this.id = Console.ReadLine();
            System.Console.Write("nhap phoneNumber : ");
            this.phoneNumber = Console.ReadLine();
            System.Console.Write("nhap address : ");
            this.address = Console.ReadLine();
            System.Console.Write("nhap injection Address : ");
            this.injectionAddress = Console.ReadLine();
            System.Console.Write("nhap vaccine : ");
            this.vaccine = Console.ReadLine();
            System.Console.Write("nhap injection (dd-MM-yyyy) : ");
            this.injection = Utility.ConvertDateTime(Console.ReadLine());
        }

        public void display()
        {
            System.Console.WriteLine(this);
        }

        public override string ToString()
        {
            return "Record{" + "fullName=" + fullName + ", ID=" + id + ", phoneNumber=" + phoneNumber + ", address=" + address + ", injectionAddress=" + injectionAddress + ", vaccine=" + vaccine + ", injection=" + injection + '}';
        }
    }
}


#Utility.cs


using Newtonsoft.Json;
using System;
using System.IO;

namespace _2466
{
    internal class Utility
    {
        public static int ReadInt()
        {
            int integer = 0;
            while (true)
            {
                try
                {
                    integer = int.Parse(Console.ReadLine());
                    break;
                }
                catch (Exception)
                {
                    Console.Write("du lieu dau vao khong hop le, nhap lai : ");
                }
            }
            return integer;
        }

        public static string ConvertDateTime(DateTime myDate)
        {
            return myDate.ToString("dd-MM-yyyy");
        }

        public static DateTime ConvertDateTime(string str)
        {
            return DateTime.ParseExact(str, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);
        }

        public static T ReadfileJson<T>(string path)
        {
            string json = "";
            using (StreamReader sr = new StreamReader(path))
            {
                string line;

                while ((line = sr.ReadLine()) != null)
                {
                    json += line;
                }
            }
            return JsonConvert.DeserializeObject<T>(json);
        }

        public static void SavefileJson(object obj,string path)
        {
            using (StreamWriter sw = new StreamWriter(path))
            {
                sw.WriteLine(JsonConvert.SerializeObject(obj));
            }
            Console.WriteLine("save file is success!");
        }
    }
}


Hoàng Thiện Thanh [community,AAHN-C2009G]

Ngày viết: 00:15 09/10/2021


#Program.cs


using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;

namespace VaccineCovid
{
    class Program
    {
        public static List<Record> Records = new List<Record>();
        public static void ShowMenu()
        {
            Console.WriteLine(
                "1. Input new Record\n" +
                "2. Display Records\n" +
                "3. Search Records\n" +
                "4. Save Records to JSON\n" +
                "5. Read Records from JSON\n" +
                "6. Exit\n");
        }

        public static void Input()
        {
            Console.WriteLine("Enter number of records to input:");
            int N = Utility.ReadInt();
            for (int i = 0; i < N; i++)
            {
                Record r = new Record();
                r.input();
                Records.Add(r);
            }
        }

        public static void Display()
        {
            int i = 0;
            foreach(Record r in Records)
            {
                i++;
                Console.WriteLine("Record[" + i + "]");
                r.display();
            }
        }
        public static void Search()
        {
            Console.WriteLine("Enter CMTND:");
            string str = Utility.ReadNum();
            string card = "";
            int count = 0;
            DateTime minDate = new DateTime();
            foreach (Record r in Records)
            {
                if (r.CMTND.Equals(str))
                {
                    if (minDate < r.VaccinatedDate)
                    {
                        minDate = r.VaccinatedDate;
                    }
                    count++;
                    r.display();
                    
                }
                int days = (DateTime.Now.Date - r.VaccinatedDate).Days;
                int daysMin = (DateTime.Now.Date - minDate).Days;
                if (count >= 1 && (days >= 14))
                {
                    card = "Yellow Card";
                }
                if (count >= 2 && (daysMin >= 14))
                {
                    card = "Green Card";
                } 
                if (count == 0 || (daysMin < 14 && days < 14) )
                {
                    card = "Not Vaccinated";
                }
            }
            
            Console.WriteLine(
                "Vaccine Card: {0}\n", card);
        }
        public static void SaveFile()
        {
            try
            {
                var data = JsonConvert.SerializeObject(Records);
                File.WriteAllText(@"Records.json", data);
            } catch (Exception e)
            {
                Console.WriteLine(e.Message);
            } finally
            {
                Console.WriteLine("Save Success");
            }
        }
        public static void ReadFile()
        {
            try
            {
                var data = File.ReadAllText(@"Records.json");
                Records = JsonConvert.DeserializeObject<List<Record>>(data);
            } catch (FileNotFoundException)
            {
                Console.WriteLine("No Record file exists!");
            } catch (Exception e)
            {
                Console.WriteLine(e.Message);
            } finally
            {
                Console.WriteLine("Read Complete!");
            }
            
        }
        static void Main(string[] args)
        {
            int choose;

            do
            {
                ShowMenu();
                choose = Utility.ReadInt();
                switch (choose)
                {
                    case 1:
                        Input();
                        break;
                    case 2:
                        Display();
                        break;
                    case 3:
                        Search();
                        break;
                    case 4:
                        SaveFile();
                        break;
                    case 5:
                        ReadFile();
                        break;
                    default:
                        Console.WriteLine("Wrong input");
                        break;
                }
            } while (choose != 6);
        }
    }
}


#Record.cs


using Newtonsoft.Json;
using System;

namespace VaccineCovid
{
    [Serializable]
    public class Record
    {
        [JsonProperty("fullname")]
        public string FullName { get; set; }
        [JsonProperty("cmtnd")]
        public string CMTND { get; set; }
        [JsonProperty("phone")]
        public string Phone { get; set; }
        [JsonProperty("address")]
        public string Address { get; set; }
        [JsonProperty("vaccinateddate")]
        public DateTime VaccinatedDate { get; set; }
        [JsonProperty("vaccineaddress")]
        public string VaccineAddress { get; set; }
        [JsonProperty("vaccinename")]
        public string VaccineName { get; set; }
        public Record()
        {

        }
        public Record(string FullName, string CMTND, string Phone, string Address, string VaccinatedDate, string VaccineAddress, string VaccineName)
        {
            this.FullName = FullName;
            this.CMTND = CMTND;
            this.Phone = Phone;
            this.Address = Address;
            this.VaccinatedDate = DateTime.Parse(VaccinatedDate);
            this.VaccineAddress = VaccineAddress;
            this.VaccineName = VaccineName;
        }
        public Record(string FullName, string CMTND, string Phone, string Address, DateTime VaccinatedDate, string VaccineAddress, string VaccineName)
        {
            this.FullName = FullName;
            this.CMTND = CMTND;
            this.Phone = Phone;
            this.Address = Address;
            this.VaccinatedDate = VaccinatedDate;
            this.VaccineAddress = VaccineAddress;
            this.VaccineName = VaccineName;
        }
        public void input()
        {
            Console.WriteLine("Input full name:");
            FullName = Utility.ReadAlp();
            Console.WriteLine("Input CMTND:");
            CMTND = Utility.ReadNum();
            while (CMTND.Length != 9)
            {
                Console.WriteLine("They must have at least 9 characters");
                CMTND = Utility.ReadNum();
            }
            Console.WriteLine("Input Phone Num:");
            Phone = Utility.ReadNum();
            while (Phone.Length != 10)
            {
                Console.WriteLine("They must have at least 10 characters");
                Phone = Utility.ReadNum();
            }
            Console.WriteLine("Input Address:");
            Address = Console.ReadLine();
            Console.WriteLine("Input Vaccinated Date:");
            VaccinatedDate = Utility.ReadDate();
            Console.WriteLine("Input Vaccine Address:");
            VaccineAddress = Console.ReadLine();
            Console.WriteLine("Input Vaccine Name:");
            VaccineName = Console.ReadLine();
        }

        public void display()
        {
            Console.WriteLine("Full Name: {0}\n" +
                "CMTND: {1}\n" +
                "Phone: {2}\n" +
                "Address: {3}\n" +
                "Vaccinated Date: {4}\n" +
                "Vaccine Location: {5}\n" +
                "Vaccine Name: {6}\n", FullName, CMTND, Phone, Address, VaccinatedDate, VaccineAddress, VaccineName);
        }
    }
}


#Utility.cs


using System;
using System.Text.RegularExpressions;

namespace VaccineCovid
{
    public class Utility
    {
        public static int ReadInt()
        {
            string str = Console.ReadLine();
            bool check()
            {
                try { int.Parse(str); } catch (Exception) { return false; }
                return true;
            }
            while (!check())
            {
                Console.WriteLine("Input is not numerical value");
                str = Console.ReadLine();
            }
            return int.Parse(str);
        }

        public static string ReadNum()
        {
            string str = Console.ReadLine();
            while (!Regex.Match(str, "^[0-9]*$").Success)
            {
                Console.WriteLine("Input can only accept numbers");
                str = Console.ReadLine();
            }
            return str;
        }

        public static string ReadAlp()
        {
            string str = Console.ReadLine();
            while (!Regex.Match(str, "^([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)*$").Success)
            {
                Console.WriteLine("Input only accepts alphabet letter");
                str = Console.ReadLine();
            }
            return str;
        }
        
        
        public static DateTime ReadDate()
        {
            DateTime date = new DateTime();
            string str = Console.ReadLine();
            bool checkParse()
            {
                try { DateTime.Parse(str); } catch (Exception) { return false; }
                return true;
            }
            bool checkConvert()
            {
                try { Convert.ToDateTime(str); } catch (Exception) { return false; }
                return true;
            }
            while (!checkParse() && !checkConvert())
            {
                str = Console.ReadLine();
            }
            if (checkParse())
            {
                date = DateTime.Parse(str);
            } else if (checkConvert())
            {
                date = Convert.ToDateTime(str);
            }
            return date;
        }
    }
}


Nguyễn Việt Hoàng [community,AAHN-C2009G]

Ngày viết: 00:10 09/10/2021


#Program.cs


using Newtonsoft.Json;
using Ss8.Ultitis;
using System;
using System.Collections.Generic;
using System.IO;

namespace Ss8
{
    class Program
    {
        public static List<Record> RecordList = new List<Record>();
        static void ShowMenu()
        {
            Console.WriteLine("1.Add N class Record");
            Console.WriteLine("2.Display information");
            Console.WriteLine("3.Find Vaccine by CMTND");
            Console.WriteLine("4.Save to file db.json");
            Console.WriteLine("5.Read contents file db.json");
            Console.WriteLine("6.Exits");
        }
        static void Input()
        {
            Console.WriteLine("Enter Sum Of Record :");
            int sum = int.Parse(Console.ReadLine());
            for (int i = 0; i < sum; i++)
            {
                Console.WriteLine("Record[" + (i + 1) + "]");
                Record r = new Record();
                r.Input();
                RecordList.Add(r);
            }
        }
        static void Display()
        {
            foreach (Record r in RecordList)
            {
                r.Display();
            }
        }
        static void FindByCMTND()
        {
            int count = 0;
            Console.WriteLine("Enter CMTND you need to find :");
            string find = Console.ReadLine();
            string status = "";
            for (int i = 0; i < RecordList.Count; i++)
            {
                
                int days = (DateTime.Now.Date - RecordList[i].VaccinatedDate.Date).Days;
                if (RecordList[i].CMTND.Equals(find))
                {
                    FindDayNear(find);
                    count++;
                    int mins = (DateTime.Now.Date - FindDayNear(find).Date).Days;
                    if (days >= 14 && count >= 1)
                    {
                        status = "Yellow Card";
                    }
                    if (mins >= 14 && count >= 2)
                    {
                        status = "Blue Card";
                    }
                    if (days < 14 && mins < 14 || count == 0)
                    {
                        status = "UnVaccinated";
                    }
                }
               
            }
            Console.WriteLine(status);
        }
        static DateTime FindDayNear(string find)
        {
            DateTime mins = new DateTime();
            foreach(Record r in RecordList)
            {
                if (r.CMTND.Equals(find))
                {
                    if (mins.Date < r.VaccinatedDate.Date)
                    {
                        mins = r.VaccinatedDate;
                    }
                }
               
            }
            return mins;
        }
        static void SaveFileJson()
        {
            string content = JsonConvert.SerializeObject(RecordList);

            System.IO.File.WriteAllText(@"db.json", content);
            Console.WriteLine("Save file success");
        }
        static void ReadFileJson()
        {
            string content = System.IO.File.ReadAllText(@"db.json");

            //Chuyen content -> List<ClassRoom>
            RecordList = JsonConvert.DeserializeObject<List<Record>>(content);
            Console.WriteLine("Read file success");
        }
        static void ListMenu()
        {
            int choose;
            do
            {
                ShowMenu();
                Console.WriteLine("Enter choose value :");
                choose = int.Parse(Console.ReadLine());
                switch (choose)
                {
                    case 1:
                        Input();
                        break;
                    case 2:
                        Display();
                        break;
                    case 3:
                        FindByCMTND();
                        break;
                    case 4:
                        SaveFileJson();
                        break;
                    case 5:
                        ReadFileJson();
                        break;
                    case 6:
                        Console.WriteLine("Exits");
                        return;
                    default:
                        Console.WriteLine("Value must be 1 -> 6");
                        break;
                }

            } while (choose != 6);
        }
        static void Main(string[] args)
        {
            ListMenu();
        }
    }
}


#Record.cs


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

namespace Ss8
{
    class Record
    {
        public string FullName { get; set; }
        public string CMTND { get; set; }
        public string Phone { get; set; }
        public string Address { get; set; }
        public DateTime VaccinatedDate { get; set; }
        public string VaccineName { get; set; }
        public Record()
        {

        }
        public Record(string fullname, string cmtnd, string phone, string address, DateTime dayvacc, string namevacc )
        {
            this.FullName = fullname;
            this.CMTND = cmtnd;
            this.Phone = phone;
            this.Address = address;
            this.VaccinatedDate = dayvacc;
            this.VaccineName = namevacc;
        }
        public void Input()
        {
            Console.WriteLine("Enter FullName :");
            FullName = Console.ReadLine();
            Console.WriteLine("Enter CMTND :");
            CMTND = Console.ReadLine();
            Console.WriteLine("Enter Phone :");
            Phone = Console.ReadLine();
            Console.WriteLine("Enter Address :");
            Address = Console.ReadLine();
            Console.WriteLine("Enter VaccinatedDate :");
            VaccinatedDate = Ultility.ConvertStringToDateTime(Console.ReadLine());
            Console.WriteLine("Enter Vaccine Name :");
            VaccineName = Console.ReadLine();

        }
        public void Display()
        {
            Console.WriteLine("FullName :{0}, CMTND :{1}, Phone :{2}, Address :{3}, VaccinatedDate :{4}, VaccineName :{5}"
                ,FullName,CMTND,Phone,Address,VaccinatedDate,VaccineName);
        }
    }
}


Nam20021608 [community,AAHN-C2009G]

Ngày viết: 22:30 08/10/2021


#Program.cs



using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;

namespace lap8
{
    class Program
    {
        public static List<Record> Records = new List<Record>();
        public static void ShowMenu()
        {
            Console.WriteLine(
                "1. Input new Record\n" +
                "2. Display Records\n" +
                "3. Search Records\n" +
                "4. Save Records to JSON\n" +
                "5. Read Records from JSON\n" +
                "6. Exit\n");
        }

        public static void Input()
        {
            Console.WriteLine("Enter number of records to input:");
            int N = Utility.ReadInt();
            for (int i = 0; i < N; i++)
            {
                Record r = new Record();
                r.input();
                Records.Add(r);
            }
        }

        public static void Display()
        {
            int i = 0;
            foreach(Record r in Records)
            {
                i++;
                Console.WriteLine("Record[" + i + "]");
                r.display();
            }
        }
        public static void Search()
        {
            Console.WriteLine("Enter CMTND:");
            string str = Utility.ReadNum();
            string card = "";
            int count = 0;
            DateTime minDate = new DateTime();
            Record found = new Record();
            foreach (Record r in Records)
            {
                if (minDate < r.VaccinatedDate)
                {
                    minDate = r.VaccinatedDate;
                }

                int days = (DateTime.Now.Date - r.VaccinatedDate).Days;
                int daysMin = (DateTime.Now.Date - minDate).Days;

                if (r.CMTND.Equals(str))
                {
                    count++;
                    found = r;
                }
                if (count == 1 && days >= 14)
                {
                    card = "Yellow Card";
                } else if (count == 2 && daysMin >= 14)
                {
                    card = "Green Card";
                } else if (count == 0 || days < 14)
                {
                    card = "Not Vaccinated";
                }
            }
          
            Console.WriteLine("Full Name: {0}\n" +
                "CMTND: {1}\n" +
                "Phone: {2}\n" +
                "Address: {3}\n" +
                "Vaccine Card: {4}\n", found.FullName, found.CMTND, found.Phone, found.Address, card);
        }
        public static void SaveFile()
        {
            try
            {
                var data = JsonConvert.SerializeObject(Records);
                File.WriteAllText(@"Records.json", data);
            } catch (Exception e)
            {
                Console.WriteLine(e.Message);
            } finally
            {
                Console.WriteLine("Save Success");
            }
        }
        public static void ReadFile()
        {
            try
            {
                var data = File.ReadAllText(@"Records.json");
                Records = JsonConvert.DeserializeObject<List<Record>>(data);
            } catch (FileNotFoundException)
            {
                Console.WriteLine("No Record file exists!");
            } catch (Exception e)
            {
                Console.WriteLine(e.Message);
            } finally
            {
                Console.WriteLine("Read Complete!");
            }
            
        }
        static void Main(string[] args)
        {
            int choose;

            do
            {
                ShowMenu();
                choose = Utility.ReadInt();
                switch (choose)
                {
                    case 1:
                        Input();
                        break;
                    case 2:
                        Display();
                        break;
                    case 3:
                        Search();
                        break;
                    case 4:
                        SaveFile();
                        break;
                    case 5:
                        ReadFile();
                        break;
                    default:
                        Console.WriteLine("Wrong input");
                        break;
                }
            } while (choose != 6);
        }
    }
}


#Record.cs


using Newtonsoft.Json;
using System;

namespace lap8
{
    [Serializable]
    public class Record
    {
        [JsonProperty("fullname")]
        public string FullName { get; set; }
        [JsonProperty("cmtnd")]
        public string CMTND { get; set; }
        [JsonProperty("phone")]
        public string Phone { get; set; }
        [JsonProperty("address")]
        public string Address { get; set; }
        [JsonProperty("vaccinateddate")]
        public DateTime VaccinatedDate { get; set; }
        [JsonProperty("vaccineaddress")]
        public string VaccineAddress { get; set; }
        [JsonProperty("vaccinename")]
        public string VaccineName { get; set; }
        public Record()
        {

        }
        public Record(string FullName, string CMTND, string Phone, string Address, string VaccinatedDate, string VaccineAddress, string VaccineName)
        {
            this.FullName = FullName;
            this.CMTND = CMTND;
            this.Phone = Phone;
            this.Address = Address;
            this.VaccinatedDate = DateTime.Parse(VaccinatedDate);
            this.VaccineAddress = VaccineAddress;
            this.VaccineName = VaccineName;
        }
        public Record(string FullName, string CMTND, string Phone, string Address, DateTime VaccinatedDate, string VaccineAddress, string VaccineName)
        {
            this.FullName = FullName;
            this.CMTND = CMTND;
            this.Phone = Phone;
            this.Address = Address;
            this.VaccinatedDate = VaccinatedDate;
            this.VaccineAddress = VaccineAddress;
            this.VaccineName = VaccineName;
        }
        public void input()
        {
            Console.WriteLine("Input full name:");
            FullName = Utility.ReadAlp();
            Console.WriteLine("Input CMTND:");
            CMTND = Utility.ReadNum();
            while (CMTND.Length != 9)
            {
                Console.WriteLine("They must have at least 9 characters");
                CMTND = Utility.ReadNum();
            }
            Console.WriteLine("Input Phone Num:");
            Phone = Utility.ReadNum();
            while (Phone.Length != 10)
            {
                Console.WriteLine("They must have at least 10 characters");
                Phone = Utility.ReadNum();
            }
            Console.WriteLine("Input Address:");
            Address = Console.ReadLine();
            Console.WriteLine("Input Vaccinated Date:");
            VaccinatedDate = Utility.ReadDate();
            Console.WriteLine("Input Vaccine Address:");
            VaccineAddress = Console.ReadLine();
            Console.WriteLine("Input Vaccine Name:");
            VaccineName = Console.ReadLine();
        }

        public void display()
        {
            Console.WriteLine("Full Name: {0}\n" +
                "CMTND: {1}\n" +
                "Phone: {2}\n" +
                "Address: {3}\n" +
                "Vaccinated Date: {4}\n" +
                "Vaccine Location: {5}\n" +
                "Vaccine Name: {6}\n", FullName, CMTND, Phone, Address, VaccinatedDate, VaccineAddress, VaccineName);
        }
    }
}


#Records.json


[{"fullname":"Nam","cmtnd":"012345678","phone":"0123456789","address":"Ha noi","vaccinateddate":"2012-04-20T00:00:00","vaccineaddress":"Ha noi","vaccinename":"n"}]


#Utility.cs


using System;
using System.Text.RegularExpressions;

namespace lap8
{
    public class Utility
    {
        public static int ReadInt()
        {
            string str = Console.ReadLine();
            bool check()
            {
                try { int.Parse(str); } catch (Exception) { return false; }
                return true;
            }
            while (!check())
            {
                Console.WriteLine("Input is not numerical value");
                str = Console.ReadLine();
            }
            return int.Parse(str);
        }

        public static string ReadNum()
        {
            string str = Console.ReadLine();
            while (!Regex.Match(str, "^[0-9]*$").Success)
            {
                Console.WriteLine("Input can only accept numbers");
                str = Console.ReadLine();
            }
            return str;
        }

        public static string ReadAlp()
        {
            string str = Console.ReadLine();
            while (!Regex.Match(str, "^([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)*$").Success)
            {
                Console.WriteLine("Input only accepts alphabet letter");
                str = Console.ReadLine();
            }
            return str;
        }
        
        
        public static DateTime ReadDate()
        {
            DateTime date = new DateTime();
            string str = Console.ReadLine();
            bool checkParse()
            {
                try { DateTime.Parse(str); } catch (Exception) { return false; }
                return true;
            }
            bool checkConvert()
            {
                try { Convert.ToDateTime(str); } catch (Exception) { return false; }
                return true;
            }
            while (!checkParse() && !checkConvert())
            {
                str = Console.ReadLine();
            }
            if (checkParse())
            {
                date = DateTime.Parse(str);
            } else if (checkConvert())
            {
                date = Convert.ToDateTime(str);
            }
            return date;
        }
    }
}


Hoàng Thiện Thanh [community,AAHN-C2009G]

Ngày viết: 11:38 08/10/2021


#Program.cs


using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;

namespace VaccineCovid
{
    class Program
    {
        public static List<Record> Records = new List<Record>();
        public static void ShowMenu()
        {
            Console.WriteLine(
                "1. Input new Record\n" +
                "2. Display Records\n" +
                "3. Search Records\n" +
                "4. Save Records to JSON\n" +
                "5. Read Records from JSON\n" +
                "6. Exit\n");
        }

        public static void Input()
        {
            Console.WriteLine("Enter number of records to input:");
            int N = Utility.ReadInt();
            for (int i = 0; i < N; i++)
            {
                Record r = new Record();
                r.input();
                Records.Add(r);
            }
        }

        public static void Display()
        {
            int i = 0;
            foreach(Record r in Records)
            {
                i++;
                Console.WriteLine("Record[" + i + "]");
                r.display();
            }
        }
        public static void Search()
        {
            Console.WriteLine("Enter CMTND:");
            string str = Utility.ReadNum();
            string card = "";
            int count = 0;
            DateTime minDate = new DateTime();
            Record found = new Record();
            foreach (Record r in Records)
            {
                if (minDate < r.VaccinatedDate)
                {
                    minDate = r.VaccinatedDate;
                }

                int days = (DateTime.Now.Date - r.VaccinatedDate).Days;
                int daysMin = (DateTime.Now.Date - minDate).Days;

                if (r.CMTND.Equals(str))
                {
                    count++;
                    found = r;
                }
                if (count == 1 && days >= 14)
                {
                    card = "Yellow Card";
                } else if (count == 2 && daysMin >= 14)
                {
                    card = "Green Card";
                } else if (count == 0 || days < 14)
                {
                    card = "Not Vaccinated";
                }
            }
          
            Console.WriteLine("Full Name: {0}\n" +
                "CMTND: {1}\n" +
                "Phone: {2}\n" +
                "Address: {3}\n" +
                "Vaccine Card: {4}\n", found.FullName, found.CMTND, found.Phone, found.Address, card);
        }
        public static void SaveFile()
        {
            try
            {
                var data = JsonConvert.SerializeObject(Records);
                File.WriteAllText(@"Records.json", data);
            } catch (Exception e)
            {
                Console.WriteLine(e.Message);
            } finally
            {
                Console.WriteLine("Save Success");
            }
        }
        public static void ReadFile()
        {
            try
            {
                var data = File.ReadAllText(@"Records.json");
                Records = JsonConvert.DeserializeObject<List<Record>>(data);
            } catch (FileNotFoundException)
            {
                Console.WriteLine("No Record file exists!");
            } catch (Exception e)
            {
                Console.WriteLine(e.Message);
            } finally
            {
                Console.WriteLine("Read Complete!");
            }
            
        }
        static void Main(string[] args)
        {
            int choose;

            do
            {
                ShowMenu();
                choose = Utility.ReadInt();
                switch (choose)
                {
                    case 1:
                        Input();
                        break;
                    case 2:
                        Display();
                        break;
                    case 3:
                        Search();
                        break;
                    case 4:
                        SaveFile();
                        break;
                    case 5:
                        ReadFile();
                        break;
                    default:
                        Console.WriteLine("Wrong input");
                        break;
                }
            } while (choose != 6);
        }
    }
}


#Record.cs


using Newtonsoft.Json;
using System;

namespace VaccineCovid
{
    [Serializable]
    public class Record
    {
        [JsonProperty("fullname")]
        public string FullName { get; set; }
        [JsonProperty("cmtnd")]
        public string CMTND { get; set; }
        [JsonProperty("phone")]
        public string Phone { get; set; }
        [JsonProperty("address")]
        public string Address { get; set; }
        [JsonProperty("vaccinateddate")]
        public DateTime VaccinatedDate { get; set; }
        [JsonProperty("vaccineaddress")]
        public string VaccineAddress { get; set; }
        [JsonProperty("vaccinename")]
        public string VaccineName { get; set; }
        public Record()
        {

        }
        public Record(string FullName, string CMTND, string Phone, string Address, string VaccinatedDate, string VaccineAddress, string VaccineName)
        {
            this.FullName = FullName;
            this.CMTND = CMTND;
            this.Phone = Phone;
            this.Address = Address;
            this.VaccinatedDate = DateTime.Parse(VaccinatedDate);
            this.VaccineAddress = VaccineAddress;
            this.VaccineName = VaccineName;
        }
        public Record(string FullName, string CMTND, string Phone, string Address, DateTime VaccinatedDate, string VaccineAddress, string VaccineName)
        {
            this.FullName = FullName;
            this.CMTND = CMTND;
            this.Phone = Phone;
            this.Address = Address;
            this.VaccinatedDate = VaccinatedDate;
            this.VaccineAddress = VaccineAddress;
            this.VaccineName = VaccineName;
        }
        public void input()
        {
            Console.WriteLine("Input full name:");
            FullName = Utility.ReadAlp();
            Console.WriteLine("Input CMTND:");
            CMTND = Utility.ReadNum();
            while (CMTND.Length != 9)
            {
                Console.WriteLine("They must have at least 9 characters");
                CMTND = Utility.ReadNum();
            }
            Console.WriteLine("Input Phone Num:");
            Phone = Utility.ReadNum();
            while (Phone.Length != 10)
            {
                Console.WriteLine("They must have at least 10 characters");
                Phone = Utility.ReadNum();
            }
            Console.WriteLine("Input Address:");
            Address = Console.ReadLine();
            Console.WriteLine("Input Vaccinated Date:");
            VaccinatedDate = Utility.ReadDate();
            Console.WriteLine("Input Vaccine Address:");
            VaccineAddress = Console.ReadLine();
            Console.WriteLine("Input Vaccine Name:");
            VaccineName = Console.ReadLine();
        }

        public void display()
        {
            Console.WriteLine("Full Name: {0}\n" +
                "CMTND: {1}\n" +
                "Phone: {2}\n" +
                "Address: {3}\n" +
                "Vaccinated Date: {4}\n" +
                "Vaccine Location: {5}\n" +
                "Vaccine Name: {6}\n", FullName, CMTND, Phone, Address, VaccinatedDate, VaccineAddress, VaccineName);
        }
    }
}


#Utility.cs


using System;
using System.Text.RegularExpressions;

namespace VaccineCovid
{
    public class Utility
    {
        public static int ReadInt()
        {
            string str = Console.ReadLine();
            bool check()
            {
                try { int.Parse(str); } catch (Exception) { return false; }
                return true;
            }
            while (!check())
            {
                Console.WriteLine("Input is not numerical value");
                str = Console.ReadLine();
            }
            return int.Parse(str);
        }

        public static string ReadNum()
        {
            string str = Console.ReadLine();
            while (!Regex.Match(str, "^[0-9]*$").Success)
            {
                Console.WriteLine("Input can only accept numbers");
                str = Console.ReadLine();
            }
            return str;
        }

        public static string ReadAlp()
        {
            string str = Console.ReadLine();
            while (!Regex.Match(str, "^([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)*$").Success)
            {
                Console.WriteLine("Input only accepts alphabet letter");
                str = Console.ReadLine();
            }
            return str;
        }
        
        
        public static DateTime ReadDate()
        {
            DateTime date = new DateTime();
            string str = Console.ReadLine();
            bool checkParse()
            {
                try { DateTime.Parse(str); } catch (Exception) { return false; }
                return true;
            }
            bool checkConvert()
            {
                try { Convert.ToDateTime(str); } catch (Exception) { return false; }
                return true;
            }
            while (!checkParse() && !checkConvert())
            {
                str = Console.ReadLine();
            }
            if (checkParse())
            {
                date = DateTime.Parse(str);
            } else if (checkConvert())
            {
                date = Convert.ToDateTime(str);
            }
            return date;
        }
    }
}


Phân Loại Bài Viết

Tài Liệu Tham Khảo

Đã sao chép!!!