Lập trình C# - Quản lý sức khoẻ

Xây dựng chương trình quản lý sức khoẻ

Bạn được yêu cầu thiết kế phần mềm cho chiếc smart watch với các chức năng sau.

- Dự báo thời tiết cho 7 ngày tiết theo

- Thông báo thời tiết hôm nay

- Đếm số bước chân trong ngày: Viết chương trình random sinh ngẫu nhiên các số từ 0-500 => Số sinh ra sẽ được thêm vào số bước chân người dùng đã đi được.

- Thông tin sức khoẻ hiện tại.

- Thiết lập thời gian cho hệ thống -> Nếu chưa thiết lập thời gian sẽ là ngày hiện tại của hệ thống

- Xem thời gian hiện tại

Yêu cầu:

Thông tin dữ liệu thời tiết được quản lý bới class Weather gồm các column: date kiểu Date, status -> string chứa thông tin thời tiết.

Tạo mảng weatherList quản lý dữ liệu thời tiết -> viết chức năng nhập dữ liệu mẫu khoảng 10 objects

Thông tin đếm bước chân: DataFoot: count -> int, level -> string cấp độ đi (INACTIVE: nếu count = 0, SMALL nếu count <= 1000, NORMAL nếu count <= 5000, GOOD nếu count <= 10.000, VERY GOOD nếu count > 10.0000)

Tạo mảng dataFootList quản lý dữ liệu người dùng.


Bước 1Xây dựng Class Weather.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace WeatherSystem
{
    public class DataFoot
    {
        public int Count { get; set; }
        public string Level { get; set; }

        public DataFoot()
        {
            Count = 0;
            Level = GetStringLevel();
        }

        public void AddStep(int steps)
        {
            Count += steps;
            Level = GetStringLevel();
        }

        public void Display()
        {
            Console.WriteLine("Thông tin sức khỏe: Steps = {0}, Level = {1}", Count, Level);
        }

        public string GetStringLevel()
        {
            int[] countList = { 0, 1000, 5000, 10000 };
            string[] levelNames = { "INACTIVE", "SMALL", "NORMAL", "GOOD" };
            for (int i = 0; i < countList.Length; i++)
            {
                if (Count <= countList[i])
                {
                    return levelNames[i];
                }
            }

            return "VERY GOOD";
        }
    }
}

Bước 2Xây dựng Class DataFoot.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace WeatherSystem
{
    public class DataFoot
    {
        public int Count { get; set; }
        public string Level { get; set; }

        public DataFoot()
        {
            Count = 0;
            Level = GetStringLevel();
        }

        public void AddStep(int steps)
        {
            Count += steps;
            Level = GetStringLevel();
        }

        public void Display()
        {
            Console.WriteLine("Thông tin sức khỏe: Steps = {0}, Level = {1}", Count, Level);
        }

        public string GetStringLevel()
        {
            int[] countList = { 0, 1000, 5000, 10000 };
            string[] levelNames = { "INACTIVE", "SMALL", "NORMAL", "GOOD" };
            for (int i = 0; i < countList.Length; i++)
            {
                if (Count <= countList[i])
                {
                    return levelNames[i];
                }
            }

            return "VERY GOOD";
        }
    }
}

Bước 3: Xây dựng hàm Main

using System;
using System.Collections.Generic;
using System.Text;

namespace WeatherSystem
{
    class Program
    {
        static List<Weather> weatherList = new List<Weather>();
        static DataFoot dataFoot = new DataFoot();
        static DateTime CurrentDateTime { get; set; }

        public static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            FakeData();

            int choose;

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

                switch (choose)
                {
                    case 1:
                        ShowWeatherInWeek();
                        break;
                    case 2:
                        ShowWeatherToDay();
                        break;
                    case 3:
                        CountSteps();
                        break;
                    case 4:
                        dataFoot.Display();
                        break;
                    case 5:
                        Console.WriteLine("Thiết lập thời gian hệ thống (dd/MM/yyyy HH:mm:ss): ");
                        string dateStr = Console.ReadLine();

                        CurrentDateTime = DateTime.ParseExact(dateStr, "dd/MM/yyyy HH:mm:ss",
                                       System.Globalization.CultureInfo.InvariantCulture);
                        break;
                    case 6:
                        Console.WriteLine("Thời gian hiện tại: {0}", CurrentDateTime.ToString("dd/MM/yyyy HH:mm:ss"));
                        break;
                    case 7:
                        Console.WriteLine("Thoát!!!");
                        break;
                    default:
                        Console.WriteLine("Nhập sai!!!");
                        break;
                }
            } while (choose != 7);
        }

        static void CountSteps()
        {
            Random random = new Random();
            int count = random.Next(500);

            dataFoot.AddStep(count);

            Console.WriteLine("Steps: {0}", count);
        }

        static void ShowWeatherToDay()
        {
            string currentDateTimeStr = CurrentDateTime.ToString("dd/MM/yyyy");
            for (int i = 0; i < weatherList.Count; i++)
            {
                string myDateTimeStr = weatherList[i].MyDateTime.ToString("dd/MM/yyyy");
                if (currentDateTimeStr.Equals(myDateTimeStr))
                {
                    weatherList[i].Display();
                    break;
                }
            }
        }

        static void ShowWeatherInWeek()
        {
            int count = 0;//Xac dinh so ngay da duoc hien thi ra => check count <= 7
            for (int i = 0; i < weatherList.Count && count <= 7; i++)
            {
                if (weatherList[i].MyDateTime.CompareTo(CurrentDateTime) >= 0)
                {
                    count++;
                    weatherList[i].Display();
                }
            }
        }

        static void FakeData()
        {
            //Fake tu ngay 1/6/2021 -> 9/6/2021
            for (int i = 1; i <= 9; i++)
            {
                string dateStr = "0" + i + "/06/2021 14:30:00";
                string status = "Ngày " + i;
                Weather weather = new Weather(dateStr, status);

                weatherList.Add(weather);
            }
            //Thoi Gian Hien Tai
            CurrentDateTime = DateTime.Now;
        }

        static void ShowMenu()
        {
            Console.WriteLine("\nCHƯƠNG TRÌNH QUẢN LÝ SỨC KHỎE C#");
            Console.WriteLine("*********************************MENU************************************");
            Console.WriteLine("**                   1. Thời tiết 7 ngày sau                           **");
            Console.WriteLine("**                   2. Thời tiết hôm nay                              **");
            Console.WriteLine("**                   3. Đếm số bước chân                               **");
            Console.WriteLine("**                   4. Thông tin sức khỏe                             **");
            Console.WriteLine("**                   5. Thiết lập thời gian hệ thống                   **");
            Console.WriteLine("**                   6. Xem thời gian hiện tại                         **");
            Console.WriteLine("**                   7. Thoát.                                         **");
            Console.WriteLine("**                   Chọn:                                             **");
            Console.WriteLine("*************************************************************************");
        }
    }
}

Xem ví dụ

Bước 4: Nhấn Ctrl+F5 để chạy và xem kết quả

CHƯƠNG TRÌNH QUẢN LÝ SỨC KHỎE C#
*********************************MENU************************************
**                   1. Thời tiết 7 ngày sau                           **
**                   2. Thời tiết hôm nay                              **
**                   3. Đếm số bước chân                               **
**                   4. Thông tin sức khỏe                             **
**                   5. Thiết lập thời gian hệ thống                   **
**                   6. Xem thời gian hiện tại                         **
**                   7. Thoát.                                         **
**                   Chọn:                                             **
*************************************************************************

Chọn 1:  "1. Thời tiết 7 ngày sau "

1

Chọn 3:  "3. Đếm số bước chân "

3
Steps: 374

Chọn 4:  "4. Thông tin sức khỏe "

4
Thông tin sức khỏe: Steps = 374, Level = SMALL

Chọn 5: "5. Thiết lập thời gian hệ thống "

5
Thiết lập thời gian hệ thống (dd/MM/yyyy HH:mm:ss):
10/12/2021 08:33:22

Chọn 6: " 6. Xem thời gian hiện tại "

6
Thời gian hiện tại: 10/12/2021 08:33:22