By GokiSoft.com| 10:09 11/10/2021|
C Sharp

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

Link Video Bài Giảng




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 myDate, string status)
        {
            MyDate = myDate;
            Status = status;
        }

        public Weather(string myDate, string status)
        {
            MyDate = Utility.ConvertStringToDateTime(myDate);
            Status = status;
        }

        internal void Display()
        {
            Console.WriteLine("Ngay: {0}, trang thai: {1}",
                Utility.ConvertDateTimeToString(MyDate), Status);
        }
    }
}


#Utility.cs


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

        public static string ConvertDateTimeToString(DateTime myDate)
        {
            return myDate.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 DateTime currentDateTime;
        static DataFood myDataFood;

        static void Main(string[] args)
        {
            //Khoi tao doi tuong
            weatherList = new List<Weather>();
            myDataFood = new DataFood();
            currentDateTime = DateTime.Now;

            //Fake du lieu thoi tiet
            FakeWeather();

            //trien khai menu chuong trinh
            int choose;
            do
            {
                ShowMenu();
                choose = Utility.ReadInt();

                switch(choose)
                {
                    case 1:
                        NextWeathers();
                        break;
                    case 2:
                        TodayWeather();
                        break;
                    case 3:
                        CountStep();
                        break;
                    case 4:
                        ShowMyHealth();
                        break;
                    case 5:
                        SettingClock();
                        break;
                    case 6:
                        ShowClock();
                        break;
                    case 7:
                        Console.WriteLine("Thoat!!!");
                        break;
                    default:
                        Console.WriteLine("Nhap sai!!!");
                        break;
                }
            } while (choose != 7);
        }

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

        private static void SettingClock()
        {
            Console.WriteLine("Thiet lap ngay he thong: (yyyy-MM-dd): ");
            string str = Console.ReadLine();

            currentDateTime = Utility.ConvertStringToDateTime(str);
        }

        private static void ShowMyHealth()
        {
            myDataFood.Display();
        }

        private static void CountStep()
        {
            Random random = new Random();
            int count = random.Next(5000);

            Console.WriteLine("So buoc chan di them: " + count);

            myDataFood.CountStep(count);
        }

        private static void TodayWeather()
        {
            for (int i = 0; i < weatherList.Count; i++)
            {
                string myDateStr = Utility.ConvertDateTimeToString(weatherList[i].MyDate);
                string currentDateTimeStr = Utility.ConvertDateTimeToString(currentDateTime);
                if (myDateStr == currentDateTimeStr)
                {
                    weatherList[i].Display();
                    break;
                }
            }
        }

        private static void NextWeathers()
        {
            //Step => sort weather: Tang theo tgian & giam theo gian
            int count = 0;

            for(int i=0;i<weatherList.Count && count < 7;i++)
            {
                if(weatherList[i].MyDate > currentDateTime)
                {
                    weatherList[i].Display();
                    count++;
                }
            }
        }

        private static void FakeWeather()
        {
            Weather weather;
            weather = new Weather("2021-10-06", "Nong");
            weatherList.Add(weather);

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

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

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

            weather = new Weather("2021-10-10", "Mua");
            weatherList.Add(weather);

            weather = new Weather("2021-10-11", "Gio");
            weatherList.Add(weather);

            weather = new Weather("2021-10-12", "Bao");
            weatherList.Add(weather);

            weather = new Weather("2021-10-13", "Nong");
            weatherList.Add(weather);

            weather = new Weather("2021-10-14", "Mat");
            weatherList.Add(weather);

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

        static void ShowMenu()
        {
            Console.WriteLine("1. Du bao");
            Console.WriteLine("2. Thoi tiet hom nay");
            Console.WriteLine("3. Count step");
            Console.WriteLine("4. Thong tin skhoe");
            Console.WriteLine("5. Thiet lap tgian");
            Console.WriteLine("6. Xem tgian");
            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()
        {
            Level = "INACTIVE";
        }

        public DataFood(int count, string level)
        {
            Count = count;
            Level = level;
        }

        public void CountStep(int step)
        {
            Count += step;

            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";
            }
        }

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




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

5

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

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó