2014年6月4日水曜日

開発環境

Head First C#―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Green (著)、佐藤 嘉一 (監修)、木下 哲也 (翻訳)、オライリージャパン)の5章(継承: オブジェクトの系図)、エクササイズ(p.178)を解いてみる。

エクササイズ(p.178)

コード

Form1.cs

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        BirthdayParty birthdayParty;
        public Form1()
        {
            InitializeComponent();
            birthdayParty = new BirthdayParty((int)numberBirthday.Value, cakeWriting.Text, fancyBirthday.Checked);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void numberBirthday_ValueChanged(object sender, EventArgs e)
        {
            birthdayParty.NumberOfPeople = (int)numberBirthday.Value;
            DisplayBirthdayparty();
        }

        private void fancyBirthday_CheckedChanged(object sender, EventArgs e)
        {
            birthdayParty.CalculateCostOfDecoration(fancyBirthday.Checked);
            DisplayBirthdayparty();
        }

        void DisplayBirthdayparty()
        {
            cakeWriting.Text = birthdayParty.CakeWriging;
            decimal cost = birthdayParty.CalculateCost();
            birthdayCost.Text = cost.ToString("C");
        }

        private void cakeWriting_TextChanged(object sender, EventArgs e)
        {
            birthdayParty.CakeWriging = cakeWriting.Text;
            DisplayBirthdayparty();
        }
    }

    class BirthdayParty
    {
        int numberOfPeople;
        decimal CostOfDecorations;
        int CakeSize;
        string cakeWriging;
        bool decoration;

        public BirthdayParty(int numberOfPeople, string cakeWriging, bool decoration)
        {
            this.numberOfPeople = numberOfPeople;
            CalculateCostOfDecoration(decoration);
            CalculateCakeSize();
            this.cakeWriging = cakeWriging;
            this.decoration = decoration;
        }

        public int NumberOfPeople
        {
            get { return numberOfPeople; }
            set
            {
                numberOfPeople = value;
                CalculateCostOfDecoration(decoration);
                CalculateCakeSize();
            }
        }

        public void CalculateCostOfDecoration(bool decoration)
        {
            if (decoration)
            {
                CostOfDecorations = 1500 * numberOfPeople + 5000;
            }
            else
            {
                CostOfDecorations = 750 * numberOfPeople + 1500;
            }
        }

        void CalculateCakeSize()
        {
            if (NumberOfPeople <= 4)
            {
                CakeSize = 7;
            }
            else
            {
                CakeSize = 15;
            }
        }

        public string CakeWriging
        {
            get { return this.cakeWriging; }
            set
            {
                int maxLength;
                if (CakeSize == 7)
                {
                    maxLength = 16;
                }
                else
                {
                    maxLength = 40;
                }
                if (value.Length > maxLength)
                {
                    MessageBox.Show(CakeSize + "号のケーキには文字数が多すぎます");
                    if (maxLength > this.cakeWriging.Length)
                    {
                        maxLength = this.cakeWriging.Length;
                    }
                    this.cakeWriging = cakeWriging.Substring(0, maxLength);
                }
                else
                {
                    this.cakeWriging = value;
                }
            }
        }

        public decimal CalculateCost()
        {
            return CostOfDecorations + cakeWriging.Length * 25;
        }
    }
}

0 コメント:

コメントを投稿