By GokiSoft.com| 19:59 14/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# - C2010L

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#

#Program.cs


using System;
using BT2311.Models;
using BT2311.Utils;
using System.Collections.Generic;

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

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

            //Fake du lieu
            FakeWeatherData();

            int choose;

            do
            {
                ShowMenu();
                choose = int.Parse(Console.ReadLine());

                switch(choose)
                {
                    case 1:
                        ShowWeather7DaysNext();
                        break;
                    case 2:
                        ShowWeatherToday();
                        break;
                    case 3:
                        CountStep();
                        break;
                    case 4:
                        ShowDataFoodInfo();
                        break;
                    case 5:
                        SettingSystemTime();
                        break;
                    case 6:
                        ShowSystemTime();
                        break;
                    case 7:
                        Console.WriteLine("Thoat!!!");
                        break;
                    default:
                        Console.WriteLine("Nhap sai!!!");
                        break;
                }
            } while (choose != 7);
        }

        private static void ShowSystemTime()
        {
            Console.WriteLine("Current Time: " + Utility.ConvertDateTimeToString(currentDateTime));
        }

        private static void SettingSystemTime()
        {
            Console.WriteLine("Thiet lap thoi gian hien tai cua he thong (yyyy-MM-dd): ");
            currentDateTime = Utility.ConvertStringToDateTime(Console.ReadLine());
        }

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

        private static void CountStep()
        {
            Random random = new Random();
            int step = random.Next(500);

            dataFood.IncreaseStep(step);

            Console.WriteLine("So buoc chan di dc: " + step);
        }

        private static void ShowWeatherToday()
        {
            string currentDateTimeStr = Utility.ConvertDateTimeToString(currentDateTime);

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

        private static void ShowWeather7DaysNext()
        {
            //weatherList -> sort -> Date -> asc
            //weatherList -> chua du lieu lien tiep nhau
            Console.WriteLine("=== C1: Giai thuat hien thi 7 ngay tiep theo");
            int count = 0;
            for(int i=0;i<weatherList.Count && count < 7;i++)
            {
                if(weatherList[i].MyDate > currentDateTime)
                {
                    weatherList[i].Display();
                    count++;
                }
            }

            //C1: Code vs TH weatherList -> sort lung tung
            //Co the ko ton tai thoi tiet 1 so ngay.
            Console.WriteLine("=== C2: Giai thuat hien thi 7 ngay tiep theo");
            DateTime nextWeekFirst = currentDateTime.AddDays(7);
            foreach(Weather weather in weatherList)
            {
                if(weather.MyDate > currentDateTime && weather.MyDate < nextWeekFirst)
                {
                    weather.Display();
                }
            }
        }

        private static void FakeWeatherData()
        {
            Weather weather;

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

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

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

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

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

            weather = new Weather("2021-10-17", "Kho Hanh");
            weatherList.Add(weather);

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

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

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

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

        static void ShowMenu()
        {
            Console.WriteLine("1. Du bao thoi tiet");
            Console.WriteLine("2. Thoi tiet hom nay");
            Console.WriteLine("3. Dem so buoc chan");
            Console.WriteLine("4. Xem thong tin skhoe");
            Console.WriteLine("5. Thiet lap tgian he thong");
            Console.WriteLine("6. Xem tgian");
            Console.WriteLine("7. Thoat");
            Console.WriteLine("Chon: ");
        }
    }
}


#Utility.cs


using System;
namespace BT2311.Utils
{
    public class Utility
    {
        public static string ConvertDateTimeToString(DateTime myDate)
        {
            return myDate.ToString("yyyy-MM-dd");
        }

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


#Weather.cs


using System;
using BT2311.Utils;

namespace BT2311.Models
{
    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;
        }

        public void Input()
        {
            Console.WriteLine("Nhap ngay (yyyy-MM-dd): ");
            MyDate = Utility.ConvertStringToDateTime(Console.ReadLine());

            Console.WriteLine("Nhap thong tin thoi tiet: ");
            Status = Console.ReadLine();
        }

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


#DataFood.cs


using System;
namespace BT2311.Models
{
    public class DataFood
    {
        public enum LEVEL
        {
            INACTIVE,
            SMALL,
            NORMAL,
            GOOD,
            VERY_GOOD
        }

        public int Count { get; set; }
        public LEVEL Level { get; set; }

        public DataFood()
        {
            Count = 0;
            Level = LEVEL.INACTIVE;
        }

        public void IncreaseStep(int step)
        {
            Count += step;
            if(Count == 0)
            {
                Level = LEVEL.INACTIVE;
            } else if(Count <= 1000)
            {
                Level = LEVEL.SMALL;
            } else if(Count <= 5000)
            {
                Level = LEVEL.NORMAL;
            } else if(Count <= 10000)
            {
                Level = LEVEL.GOOD;
            } else
            {
                Level = LEVEL.VERY_GOOD;
            }
        }

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


Tags:

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

5

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