By GokiSoft.com| 14:58 05/10/2021|
C Sharp

[Share Code] Bài tập - Xây dựng chương trình quản lý sức khoẻ - Lập trình C# - AAHN-C2009G

Bài tập - Xây dựng chương trình quản lý sức khoẻ - Lập trình C#


#Weather.cs


using System;
namespace BT2311
{
    public class Weather
    {
        public DateTime MyDate { get; set; }
        public string Status { get; set; }

        public Weather()
        {
        }

        public Weather(DateTime dateTime, string status)
        {
            MyDate = dateTime;
            Status = status;
        }

        public Weather(string dateTimeStr, string status)
        {
            Status = status;
            MyDate = Utility.ConvertStrignToDateTime(dateTimeStr);
        }

        public void Display()
        {
            Console.WriteLine("Ngay: {0}, thoi tiet: {1}", Utility.ConvertDateTimeToString(MyDate), Status);
        }
    }
}


#Utility.cs


using System;
namespace BT2311
{
    public class Utility
    {
        public static DateTime ConvertStrignToDateTime(string str)
        {
            DateTime myDate = DateTime.ParseExact(str, "yyyy-MM-dd",
                                       System.Globalization.CultureInfo.InvariantCulture);
            return myDate;
        }

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

        public static int ReadInt()
        {
            int value;
            while(true)
            {
                try
                {
                    value = int.Parse(Console.ReadLine());
                    return value;
                } catch(Exception e)
                {
                    Console.WriteLine("Nhap lai!!!");
                }
            }
        }
    }
}


#Program.cs


using System;
using System.Collections.Generic;

namespace BT2311
{
    class Program
    {
        static List<Weather> weatherList;
        static DataFood dataFood;
        static DateTime currentDateTime;

        static void Main(string[] args)
        {
            weatherList = new List<Weather>();
            dataFood = new DataFood();
            currentDateTime = DateTime.Now;

            FakeWeatherData();

            int choose;

            do
            {
                ShowMenu();
                choose = Utility.ReadInt();

                switch(choose)
                {
                    case 1:
                        DuBaoThoiTiet();
                        break;
                    case 2:
                        ThoiTietHomNay();
                        break;
                    case 3:
                        DemSoBuocChan();
                        break;
                    case 4:
                        ThongTinSucKhoe();
                        break;
                    case 5:
                        CaiDatThoiGian();
                        break;
                    case 6:
                        XemThoiGianHienTai();
                        break;
                    case 7:
                        Console.WriteLine("Thoat!!!");
                        break;
                    default:
                        Console.WriteLine("Nhap sai!!!");
                        break;
                }
            } while (choose != 7);
        }

        private static void FakeWeatherData()
        {
            Weather weather;

            weather = new Weather("2021-10-01", "Troi mat");
            weatherList.Add(weather);

            weather = new Weather("2021-10-02", "Troi nong");
            weatherList.Add(weather);

            weather = new Weather("2021-10-03", "Troi rat nong");
            weatherList.Add(weather);

            weather = new Weather("2021-10-04", "Troi mua");
            weatherList.Add(weather);

            weather = new Weather("2021-10-05", "Troi nong");
            weatherList.Add(weather);

            weather = new Weather("2021-10-06", "Troi mua");
            weatherList.Add(weather);

            weather = new Weather("2021-10-07", "Troi mat");
            weatherList.Add(weather);

            weather = new Weather("2021-10-08", "Troi mua");
            weatherList.Add(weather);

            weather = new Weather("2021-10-09", "Troi nang nong");
            weatherList.Add(weather);

            weather = new Weather("2021-10-10", "Troi okok");
            weatherList.Add(weather);
        }

        private static void XemThoiGianHienTai()
        {
            Console.WriteLine("Thoi gian: " + Utility.ConvertDateTimeToString(currentDateTime));
        }

        private static void CaiDatThoiGian()
        {
            Console.WriteLine("Cap nhat thoi gian (yyyyy-MM-dd): ");
            string myDate = Console.ReadLine();

            currentDateTime = Utility.ConvertStrignToDateTime(myDate);
        }

        private static void ThongTinSucKhoe()
        {
            dataFood.Display();
        }

        private static void DemSoBuocChan()
        {
            Random random = new Random();

            int count = random.Next(500);
            Console.WriteLine("So buoc chan di them: " + count);
            dataFood.IncreaseStep(count);
        }

        private static void ThoiTietHomNay()
        {
            for (int i = 0; i < weatherList.Count; i++)
            {
                Weather weather = weatherList[i];
                if (Utility.ConvertDateTimeToString(weather.MyDate) == Utility.ConvertDateTimeToString(currentDateTime))
                {
                    weather.Display();
                }
            }
        }

        private static void DuBaoThoiTiet()
        {
            int count = 0;//Dem so ngay da hien thi ra
            for(int i=0;i<weatherList.Count && count < 7;i++)
            {
                Weather weather = weatherList[i];
                if(weather.MyDate > currentDateTime)
                {
                    count++;
                    weather.Display();
                }
            }
        }

        static void ShowMenu()
        {
            Console.WriteLine("1. Du bao thoi tiet");
            Console.WriteLine("2. Thong bao thoi tiet");
            Console.WriteLine("3. Dem so buoc chan");
            Console.WriteLine("4. Thong tin suc khoe");
            Console.WriteLine("5. Thiet lap thoi gian he thong");
            Console.WriteLine("6. Xem thoi gian hien tai");
            Console.WriteLine("7. Thoat");
            Console.WriteLine("Chon: ");
        }
    }
}


#DataFood.cs


using System;
namespace BT2311
{
    public class DataFood
    {
        public int Count { get; set; }
        public string Level { get; set; }

        public DataFood()
        {
        }

        public void Display()
        {
            Console.WriteLine("So buoc chan: {0}, trang thai: {1}", Count, Level);
        }

        public void IncreaseStep(int count)
        {
            Count += count;

            if (Count == 0)
            {
                Level = "Inactive";
            }
            else if (Count <= 1000)
            {
                Level = "Small";
            }
            else if (Count <= 5000)
            {
                Level = "Normal";
            }
            else if (Count <= 10000)
            {
                Level = "Good";
            }
            else
            {
                Level = "Very Good";
            }
        }
    }
}


Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)