Created
April 18, 2012 05:24
-
-
Save masaru-b-cl/2411260 to your computer and use it in GitHub Desktop.
古典MVCなBMI計算アプリ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.ComponentModel; | |
| using System.Data; | |
| using System.Drawing; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Windows.Forms; | |
| namespace BMICalculator | |
| { | |
| public partial class TraditionalMVCForm : BaseForm | |
| { | |
| class Model | |
| { | |
| public double Height { get; set; } | |
| public double Weight { get; set; } | |
| public event EventHandler<EventArgs> BmiChanged; | |
| private double bmi; | |
| public double Bmi | |
| { | |
| get | |
| { | |
| return bmi; | |
| } | |
| set | |
| { | |
| if (bmi != value) | |
| { | |
| bmi = value; | |
| if (BmiChanged != null) | |
| { | |
| BmiChanged(this, EventArgs.Empty); | |
| } | |
| } | |
| } | |
| } | |
| public void Calculate() | |
| { | |
| Bmi = Weight / Math.Pow(Height * 0.01, 2); | |
| } | |
| } | |
| class Controller | |
| { | |
| private Model model; | |
| public Controller(Model model) | |
| { | |
| this.model = model; | |
| } | |
| public void Calculate(string height, string weight) | |
| { | |
| double oHeight; | |
| if (!double.TryParse(height, out oHeight)) | |
| { | |
| return; | |
| } | |
| double oWeight; | |
| if (!double.TryParse(weight, out oWeight)) | |
| { | |
| return; | |
| } | |
| model.Height = oHeight; | |
| model.Weight = oWeight; | |
| model.Calculate(); | |
| } | |
| } | |
| private Model model; | |
| private Controller controller; | |
| public TraditionalMVCForm() | |
| { | |
| InitializeComponent(); | |
| model = new Model(); | |
| model.BmiChanged += (sender, e) => | |
| { | |
| bmiTextBox.Text = model.Bmi.ToString("f2"); | |
| if (model.Bmi < 18d) | |
| { | |
| bmiTextBox.BackColor = Color.White; | |
| } | |
| else if (model.Bmi < 22d) | |
| { | |
| bmiTextBox.BackColor = Color.Yellow; | |
| } | |
| else if (model.Bmi < 24d) | |
| { | |
| bmiTextBox.BackColor = Color.Orange; | |
| } | |
| else | |
| { | |
| bmiTextBox.BackColor = Color.Red; | |
| } | |
| }; | |
| controller = new Controller(model); | |
| } | |
| private void calculateButton_Click(object sender, EventArgs e) | |
| { | |
| controller.Calculate(heightTextBox.Text, weightTextBox.Text); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment