「健康管理」Form1.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace HealthManagement
{
public partial class Form1 : Form
{
private const string filePath = @"HealManagement.txt";
private const string Format1 = "00.0";
private const string Format2 = "0.0";
private const string legend1 = "体重";
private List<Bmi> HealthList = new List<Bmi> { };
private const int ind = 0;
private const char jugChar = ',';
private const double MinVal = 20.0;
private const double MaxVal = 200.0;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
Clear(); if (File.Exists(filePath))
{
var lines = File.ReadAllLines(filePath, Encoding.UTF8);
var jug = false;
foreach (var line in lines)
{
if (line != null)
{
jug = true;
string[] words = line.Split(',');
var bmi = new Bmi(double.Parse(words[ind]), double.Parse(words[ind + 1]), DateTime.Parse(words[ind + 2]));
HealthList.Add(bmi);
ChartPointAndItemAdd();
}
}
if (jug == true)
{
var dateMax = HealthList.Max(x => x.Date);
var index = HealthList.FindIndex(n => n.Date == dateMax);
textBoxHeight.Text = HealthList[index].Height.ToString();
} }
} private void ButtonJudge_Click(object sender, EventArgs e)
{
double weight, height;
DateTime date; if (textBoxWeight.Text.Contains(jugChar) || textBoxHeight.Text.Contains(jugChar))
{
MessageBox.Show(" , は使用できません", "注意");
return;
}
bool weightValueFlg, heightValueFlg; weightValueFlg = TextToValue.textToValueMinMax(textBoxWeight.Text, out weight, MinVal, MaxVal);
heightValueFlg = TextToValue.textToValueMinMax(textBoxHeight.Text, out height, MinVal, MaxVal); if (weightValueFlg && heightValueFlg)
{
date = dateTimePicker1.Value;
var bmi = new Bmi(weight, height, date);
labelBMI.Text = bmi.BMI.ToString(Format1);
labelStandardWeight.Text = bmi.StandardWeight.ToString(Format1) + "Kg";
labelDifference.Text = (weight - bmi.StandardWeight).ToString(Format2) + "㎏";
labelJudge.Text = bmi.Jug;
labelDate.Text = bmi.Date.ToString("D") + "現在"; if (HealthList.Any(x => x.Date.Date == bmi.Date.Date))//日付のみを比較する
{
var dateJag = MessageBox.Show(
"同じ日付の記録が存在します。書き換えますか?"
, "確認"
, MessageBoxButtons.YesNo
, MessageBoxIcon.Question
, MessageBoxDefaultButton.Button2);
if (dateJag == DialogResult.Yes)
{
var index = HealthList.FindIndex(x => x.Date.Date == bmi.Date.Date);
HealthList.RemoveAt(index);
HealthList.Add(bmi);
ChartPointAndItemAdd();
MessageBox.Show(
"変更されました"
, "報告"
);
return;
}
else
return;
}
var jag = MessageBox.Show(
"記録しますか?"
, "確認"
, MessageBoxButtons.YesNo
, MessageBoxIcon.Question
, MessageBoxDefaultButton.Button1);
if (jag == DialogResult.Yes)
{
HealthList.Add(bmi);
ChartPointAndItemAdd(); MessageBox.Show("記録されました", "報告");
return;
}
else
return; }
else if (!weightValueFlg)
{
MessageBox.Show("体重の数値が適切ではありません", "判定不能"); }
else if (!heightValueFlg)
{
MessageBox.Show("身長のの数値が適切ではありません", "判定不能"); }
else
return;
}
public void ChartPointAndItemAdd()
{
var SortHealthList = HealthList.OrderBy(d => d.Date).ToList();
chart1.Series[legend1].Points.Clear();
listView1.Items.Clear();
foreach (var list in SortHealthList)
{
chart1.Series[legend1].Points.AddXY(list.Date, list.Weight);
string[] item = { list.Date.ToString("D"), list.Height.ToString(), list.Weight.ToString(Format1), list.BMI.ToString(Format1) };
listView1.Items.Add(new ListViewItem(item));
} }
public void Clear()
{
labelBMI.Text = "";
labelJudge.Text = "";
labelStandardWeight.Text = "";
labelDifference.Text = "";
labelDate.Text = "";
} private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{ } private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
using (var writer = new StreamWriter(filePath))
{
foreach (var list in HealthList)
{
writer.WriteLine(list.Weight + "," + list.Height + "," + list.Date.ToString("D"));
}
}
} private void TextBoxHeight_TextChanged(object sender, EventArgs e)
{ } private void TextBoxHeight_Leave(object sender, EventArgs e)
{ }
}
}

無料でホームページを作成しよう! このサイトはWebnodeで作成されました。 あなたも無料で自分で作成してみませんか? さあ、はじめよう