Bài tập - Xây dựng chương trình quản lý sức khoẻ - Lập trình C#
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 DateTime, 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)
Khai báo 1 đối tượng dataFood -> Lưu thông tin bước chân hiện tại.
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Đào Mạnh Dũng [C2010L]](https://www.gravatar.com/avatar/6a111fa53fd75dc87034660a8857df16.jpg?s=80&d=mm&r=g)
Đào Mạnh Dũng
2021-10-10 12:50:49
#DataFoot.cs
namespace _2311
{
internal class DataFoot
{
public int count;
public string level
{
get
{
if (count == 0)
{
return "INACTIVE";
}
else
{
if (count <= 1000)
{
return "SMALL";
}
else
{
if (count <= 5000)
{
return "NORMAL";
}
else
{
if (count <= 10000)
{
return "GOOD";
}
else
{
if (count > 10.0000)
{
return "VERY GOOD ";
}
}
}
}
}
return null;
}
private set
{
}
}
public DataFoot()
{
}
public DataFoot(int count)
{
this.count = count;
}
}
}
#Program.cs
using System;
namespace _2311
{
internal class Program
{
private static DataFoot dataFoot = new DataFoot();
private static Weather[] weatherList = new Weather[10];
private static Random rd = new Random();
private static DateTime isnow = default(DateTime);
private static void Main(string[] args)
{
while (true)
{
Console.WriteLine("nhập dữ liệu mẫu khoảng 10 Weather : ");
for (int i = 0; i < 10; i++)
{
weatherList[i] = new Weather();
weatherList[i].input();
}
switch (menu())
{
case 1:
{
for (int i = 0; i < 7; i++)
{
Console.WriteLine(weatherList[i].date.ToString("dd/MM/yyyy")+" troi " + weatherList[i].status);
}
break;
}
case 2:
{
Console.WriteLine("hom nay troi "+weatherList[0].status);
break;
}
case 3:
{
int data = rd.Next(0, 501);
Console.WriteLine("hom nay di dc {0} buoc", data);
dataFoot.count += data;
break;
}
case 4:
{
Console.WriteLine(dataFoot.level);
break;
}
case 5:
{
Console.WriteLine("nhap ngay gio hien tai : 'dd/MM/yyyy HH:mm:ss'");
isnow = DateTime.ParseExact(Console.ReadLine(), "dd/MM/yyyy HH:mm:ss",
System.Globalization.CultureInfo.InvariantCulture);
break;
}
case 6:
{
if (!isnow.Equals(default(DateTime)))
{
Console.WriteLine(isnow.ToString("dd/MM/yyyy HH:mm:ss"));
}
else
{
Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
}
break;
}
case 7:
{
Environment.Exit(0);
break;
}
default:
{
Console.WriteLine("nhap sai ");
break;
}
}
}
}
private static int menu()
{
System.Console.WriteLine("1- Dự báo thời tiết cho 7 ngày tiết theo\n" +
"\n" +
"2- Thông báo thời tiết hôm nay\n" +
"\n" +
"3- Đếm số bước chân trong ngày\n" +
"\n" +
"4- Thông tin sức khoẻ hiện tại.\n" +
"\n" +
"5- Thiết lập thời gian cho hệ thống\n" +
"\n" +
"6- Xem thời gian hiện tại.\n" +
"\n" +
"7- exit."
);
return Utility.ReadInt();
}
}
}
#Utility.cs
using System;
namespace _2311
{
internal class Utility
{
public static int ReadInt()
{
int integer = 0;
while (true)
{
try
{
integer = int.Parse(Console.ReadLine());
break;
}
catch (Exception)
{
Console.Write("du lieu dau vao khong hop le, nhap lai : ");
}
}
return integer;
}
}
}
#Weather.cs
using System;
namespace _2311
{
internal class Weather
{
public DateTime date;
public string status;
public Weather()
{
}
public Weather(DateTime date, string status)
{
this.date = date;
this.status = status;
}
internal void input()
{
Console.WriteLine("nhap ngay gio : 'dd/MM/yyyy'");
date = DateTime.ParseExact(Console.ReadLine(), "dd/MM/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine("nhap thoi tiet : ");
status = Console.ReadLine();
}
}
}
![Nguyễn Việt Hoàng [community,AAHN-C2009G]](https://www.gravatar.com/avatar/bdbde8074d82148dcda6927ccb016466.jpg?s=80&d=mm&r=g)
Nguyễn Việt Hoàng
2021-10-04 15:01:11
#Datafoot.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ss6.B2
{
class Datafoot
{
public int Count { get; set; }
public string Level { get; set; }
public Datafoot()
{
this.Count = 0;
Level = GetStringLevel();
}
public void AddStep(int steps)
{
Count += steps;
Level = GetStringLevel();
}
public void display()
{
Console.WriteLine("Health Information :\n Steps : {0}, Level : {1}", Count, Level);
}
public string GetStringLevel()
{
int[] CountList = { 0, 1000, 5000, 7000, 10000 };
string[] LevelList = { "INACTIVE", "SMALL", "NORMAL", "TRY", "GOOD" };
for (int i = 0; i < CountList.Length; i++){
if (Count <= CountList[i])
{
return LevelList[i];
}
}
return "VERY GOOD";
}
}
}
#Main2.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ss3_2.Ultitis;
namespace Ss6.B2
{
class Main2
{
static List<Weather> WeatherList = new List<Weather>();
static Datafoot DTFoot = new Datafoot();
static DateTime CurrentTime { get; set; }
public static void ShowMenu()
{
Console.WriteLine("1.Weather 7 Day Soon");
Console.WriteLine("2.Weather ToDay");
Console.WriteLine("3.Count Steps");
Console.WriteLine("4.Information Hearth");
Console.WriteLine("5.Set System Time");
Console.WriteLine("6.See CurrentTime");
Console.WriteLine("7.Exits");
}
static void FakeData()
{
for(int i = 1; i <= 7; i++)
{
string fake = Ultility.ConvertDateTimeToString(CurrentTime.AddDays(i - 1).AddMonths(10).AddYears(2020));
string day = "Ngay " + i;
Weather weather = new Weather(fake, day);
WeatherList.Add(weather);
}
CurrentTime = DateTime.Now;
}
static void Weather7daysoon()
{
int count = 0;
for (int i = 0; i < WeatherList.Count && count <= 7; i++){
if(WeatherList[i].MyDateTime.CompareTo(CurrentTime) >= 0)
{
count++;
WeatherList[i].display();
}
}
}
static void Weathertoday()
{
string current = Ultility.ConvertDateTimeToString(CurrentTime);
for (int i = 0; i < WeatherList.Count; i++)
{
string dt = Ultility.ConvertDateTimeToString(WeatherList[i].MyDateTime);
if (current.Equals(dt))
{
WeatherList[i].display();
break;
}
}
}
static void Countsteps()
{
Random r = new Random();
int count = r.Next(200);
DTFoot.AddStep(count);
Console.WriteLine("Steps :{0}", count);
}
static void Inf_hearth()
{
DTFoot.display();
}
static void System_time()
{
Console.WriteLine("Set System Time (dd/MM/yyyy) :");
string dt = Console.ReadLine();
CurrentTime = Ultility.ConvertStringToDateTime(dt);
}
static void Current_time()
{
Console.WriteLine("CurrentTime :{0}", CurrentTime);
}
static void ListMenu()
{
FakeData();
int choose;
do
{
ShowMenu();
Console.WriteLine("Enter choose value :");
choose = int.Parse(Console.ReadLine());
switch (choose)
{
case 1:
Weather7daysoon();
break;
case 2:
Weathertoday();
break;
case 3:
Countsteps();
break;
case 4:
Inf_hearth();
break;
case 5:
System_time();
break;
case 6:
Current_time();
break;
case 7:
Console.WriteLine("Exits");
return;
default:
Console.WriteLine("Value must be 1 -> 7");
break;
}
} while (choose != 7);
}
public static void Main(string[] args)
{
ListMenu();
}
}
}
#Weather.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ss3_2.Ultitis;
namespace Ss6.B2
{
class Weather
{
public DateTime MyDateTime { get; set; }
public string Status { get; set; }
public Weather()
{
}
public Weather(DateTime myDateTime, string status)
{
this.MyDateTime = myDateTime;
this.Status = status;
}
public Weather(string myDateTime, string status)
{
this.MyDateTime = Ultility.ConvertStringToDateTime(myDateTime);
this.Status = status;
}
public void display()
{
Console.WriteLine("DateTime :{0}, Status :{1}", MyDateTime, Status);
}
}
}
![GokiSoft.com [Teacher]](https://www.gravatar.com/avatar/fc6ba9324e017d540af3613b3a77dd21.jpg?s=80&d=mm&r=g)
GokiSoft.com
2021-06-02 08:01:24
using System;
namespace Lesson09.Modals
{
public class Weather
{
public DateTime MyDateTime { get; set; }
public string Status { get; set; }
public Weather()
{
}
public Weather(DateTime dateTime, string status)
{
this.MyDateTime = dateTime;
this.Status = status;
}
public Weather(string dateStr, string status)
{
this.MyDateTime = ToDateTimeFromString(dateStr);
this.Status = status;
}
public void Display()
{
Console.WriteLine("DateTime: {0}, Status: {1}", ToStringFromDateTime(), Status);
}
public string ToStringFromDateTime()
{
string date_str = MyDateTime.ToString("dd/MM/yyyy HH:mm:ss");
return date_str;
}
public DateTime ToDateTimeFromString(string dateStr)
{
DateTime myDate = DateTime.ParseExact(dateStr, "dd/MM/yyyy HH:mm:ss",
System.Globalization.CultureInfo.InvariantCulture);
return myDate;
}
}
}
using System;
namespace Lesson09.Modals
{
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("Thong tin suc khoe: 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";
}
}
}
using System;
using System.Collections.Generic;
using Lesson09.Modals;
namespace Lesson09
{
class MainClass
{
static List<Weather> weatherList = new List<Weather>();
static DataFoot dataFoot = new DataFoot();
static DateTime CurrentDateTime { get; set; }
public static void Main(string[] args)
{
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("Thiet lap Tgian he thong (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("Current Date Time: {0}", CurrentDateTime.ToString("dd/MM/yyyy HH:mm:ss"));
break;
case 7:
Console.WriteLine("Thoat!!!");
break;
default:
Console.WriteLine("Nhap 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 = "Ngay " + i;
Weather weather = new Weather(dateStr, status);
weatherList.Add(weather);
}
//Thoi Gian Hien Tai
CurrentDateTime = DateTime.Now;
}
static void ShowMenu()
{
Console.WriteLine("1. Thoi Tiet 7 Ngay Sau");
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 TGian He Thong");
Console.WriteLine("6. Xem TGian Hien Tai");
Console.WriteLine("7. Thoat.");
Console.WriteLine("Lua Chon: ");
}
}
}
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2021-05-31 10:41:40
using System;
using System.Collections.Generic;
using System.Text;
namespace suckhoe
{
class Weather
{
public DateTime date { set; get; }
public String status { set; get; }
public Weather() {
}
public Weather(DateTime date, String status)
{
this.date = date;
this.status = status;
}
public void hienthi() {
String date1 = date.ToString("d");
Console.WriteLine("Ngay : " + date1 + " Tinh hinh thoi tiet : " + status ) ;
}
}
}
using System;
using System.Collections.Generic;
namespace suckhoe
{
class Program
{
public static List<Weather> listwe = new List<Weather>();
static void Main(string[] args)
{
String[] st = { "nang", "Nhieu may", "Lanh", "Mat me", "Mua", "Lanh", "Nang", "Nang", "Nhieu may", "Nhieu may" };
DateTime date1 = DateTime.Today;
for (int i = 0; i < 10; i++) {
Weather w = new Weather();
w.date = date1;
w.status = st[i];
listwe.Add(w);
date1 = w.date.AddDays(1);
}
int lc;
do {
menu();
Console.WriteLine("Nhap lua chon: ");
lc = Int32.Parse(Console.ReadLine());
Console.WriteLine();
switch (lc) {
case 1:
Console.WriteLine("Du bao thoi tiet trong 7 ngay toi :");
for (int i = 0; i < 7; i++) {
listwe[i].hienthi();
}
break;
case 2:
Console.WriteLine("Du bao thoi tiet hom nay");
foreach (Weather w in listwe) {
if (w.date == DateTime.Today) {
w.hienthi();
}
}
break;
case 3:
Random random = new Random();
int count = random.Next(maxValue: 100000);
Console.WriteLine("SO BUOC CHAN = " + count + " buoc ;");
if (count == 0)
{
Console.WriteLine("INACTIVE");
}
else if (count <= 1000)
{
Console.WriteLine("SMALL");
}
else if (count <= 5000)
{
Console.WriteLine("NORMAL");
}
else if (count <= 10000)
{
Console.WriteLine("GOOD ");
}
else {
Console.WriteLine("VERY GOOD");
}
break;
default:
Console.WriteLine("Chon lai !!!");
break;
}
} while (lc>0);
}
public static void menu(){
Console.WriteLine();
Console.WriteLine( " 1 Du bao thoi tiet trong 7 ngay");
Console.WriteLine( " 2 Du bao thoi tiet hom nay " );
Console.WriteLine( " 3 Dem so buoc chan trong ngay:" );
Console.WriteLine();
}
}
}
![Nguyễn Tiến Đạt [T2008A]](https://www.gravatar.com/avatar/b5819cd0adc95c727c7ad0c2bcf6098b.jpg?s=80&d=mm&r=g)
Nguyễn Tiến Đạt
2021-05-31 08:34:53
#Program.cs
using System;
using System.Collections.Generic;
namespace QuanLySucKhoe
{
class Program
{
static void Main(string[] args)
{
List<Weather> weatherList = new List<Weather>();
for (int i = 0; i < 10; i++)
{
if (i == 0)
{
Console.WriteLine("Nhap du lieu cho ngay hom nay:");
}
else Console.WriteLine("Nhap du lieu cho ngay thu " + (i + 1) + ":");
DateTime date = DateTime.Now.AddDays(i);
Console.WriteLine("Nhap trang thai cho ngay:");
string status = Console.ReadLine();
Weather weather = new Weather(date, status);
weatherList.Add(weather);
}
foreach (Weather w in weatherList)
{
Console.WriteLine("{0} : {1}", w.date, w.status);
}
Random random = new Random();
int countFoot = random.Next(100000);
Console.WriteLine(countFoot);
if (countFoot == 0) Console.WriteLine("INACTIVE");
else if (countFoot <= 1000) Console.WriteLine("SMALL");
else if (countFoot <= 5000) Console.WriteLine("NORMAL");
else if (countFoot <= 10000) Console.WriteLine("GOOD");
else Console.WriteLine("VERY GOOD");
}
}
}
#Weather.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace QuanLySucKhoe
{
class Weather
{
public DateTime date { get; set; }
public string status { get; set; }
public Weather(DateTime date, string status)
{
this.date = date;
this.status = status;
}
}
}