By GokiSoft.com| 09:38 21/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# - C2009G


LINK VIDEO BAI GIANG

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 System.Collections.Generic;

namespace BT2311
{
    delegate void SwitchCase();

    class Program
    {
        static List<Weather> weatherList = new List<Weather>();
        static DataFoot dataFoot = new DataFoot();
        static DateTime currentDateTime;

        static void Main(string[] args)
        {
            int choose;

            currentDateTime = DateTime.Now;
            SwitchCase[] cases = {NextWeather, TodayWeather, CountStep,
                ShowHealth, SetupTime, DisplayTime, ExitProgram};

            FakeWeathers();
            SortWeather();

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

                if(choose <= cases.Length && choose > 0)
                {
                    cases[choose - 1]();
                } else
                {
                    Console.WriteLine("Nhap sai!!!");
                }
            } while (choose != 7);
        }

        private static void FakeWeathers()
        {
            Weather weather;

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

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

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

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

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

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

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

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

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

        private static void ExitProgram()
        {
            Console.WriteLine("Thoat chuong trinh!!!");
        }

        private static void DisplayTime()
        {
            Console.WriteLine("Thoi gian: {0}",
                Utility.ConvertDateTimeToString(currentDateTime));
        }

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

        private static void ShowHealth()
        {
            dataFoot.Display();
        }

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

            dataFoot.IncreaseStep(step);

            Console.WriteLine("So buoc chan them moi: " + step);
        }

        private static void TodayWeather()
        {
            string todayStr = Utility.ConvertDateTimeToString(currentDateTime);

            foreach(Weather weather in weatherList)
            {
                string dateStr = Utility.ConvertDateTimeToString(weather.MyDate);
                if(todayStr == dateStr)
                {
                    weather.Display();
                    break;
                }
            }
        }

        private static void NextWeather()
        {
            //C1: Du lieu thoi tiet dang co day du thong tin
            Console.WriteLine("=== Du lieu thoi tiet dang co day du thong tin");
            int count = 0;
            for(int i=0;i<weatherList.Count && count < 7; i++)
            {
                if(weatherList[i].MyDate > currentDateTime)
                {
                    weatherList[i].Display();
                    count++;
                }
            }


            //C2: Lam vs bai toan tong quat
            Console.WriteLine("=== Lam vs bai toan tong quat");
            DateTime maxDate = currentDateTime.AddDays(7);
            foreach(Weather weather in weatherList)
            {
                if(weather.MyDate > currentDateTime && weather.MyDate <= maxDate)
                {
                    weather.Display();
                }
            }
        }

        static void SortWeather()
        {
            weatherList.Sort((w1, w2) => {
                if(w1.MyDate >= w2.MyDate)
                {
                    return 1;
                }
                return -1;
            });
        }

        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. Thong tin suc khoe");
            Console.WriteLine("5. Thiet lap thoi gian");
            Console.WriteLine("6. Xem thoi gian");
            Console.WriteLine("7. Thoat");
            Console.WriteLine("Chon: ");
        }
    }
}


#Utility.cs


using System;
namespace BT2311
{
    public class Utility
    {
        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 dateTime)
        {
            return dateTime.ToString("yyyy-MM-dd");
        }
    }
}


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

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


#DataFoot.cs


using System;
namespace BT2311
{
    public class DataFoot
    {
        public enum LEVEL {INACTIVE, SMALL, NORMALL, GOOD, VERY_GOOD};

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

        public DataFoot()
        {
            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.NORMALL;
            } else if(Count <= 10000)
            {
                Level = LEVEL.GOOD;
            } else
            {
                Level = LEVEL.VERY_GOOD;
            }
        }

        public void Display()
        {
            Console.WriteLine("So buoc chan: {0}, cap do: {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)