2018年4月8日日曜日

開発環境

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

コード

Form1.cs

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 __DinnerParty
{
    public partial class Form1 : Form
    {
        DinnerParty dinnerParty;
        BirthdayParty birthdayParty;

        public Form1()
        {
            InitializeComponent();
            dinnerParty = new DinnerParty((int)numericUpDown1.Value, healthyBox.Checked, fancyBox.Checked);
            DisplayDinnerPartyCost();

            birthdayParty = new BirthdayParty((int)numberBirthday.Value, fancyBirthday.Checked);
            DisplayBirthdayPartyCost();
        }

        private void fancyBox_CheckedChanged(object sender, EventArgs e)
        {
            dinnerParty.CalculateCostOfDecorations(fancyBox.Checked);
            DisplayDinnerPartyCost();
        }

        private void healthyBox_CheckedChanged(object sender, EventArgs e)
        {
            dinnerParty.SetHealthyOption(healthyBox.Checked);
            DisplayDinnerPartyCost();
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            dinnerParty.NumberOfPeople = (int)numericUpDown1.Value;
            DisplayDinnerPartyCost();
        }

        private void DisplayDinnerPartyCost()
        {
            decimal Cost = dinnerParty.CalculateCost(healthyBox.Checked);
            costLabel.Text = Cost.ToString("c");
        }

        private void DisplayBirthdayPartyCost()
        {
            decimal Cost = birthdayParty.CalculateCost();
            birthdayCost.Text = Cost.ToString("c");
        }

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

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

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

Party.cs

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

namespace __DinnerParty
{
    public class Party
    {
        const int CostOfFoodPerPerson = 2500;
        private bool fancyDecorations;
        private int numberOfPeople;
        public decimal CostOfDecorations = 0;

        public Party(int numberOfPeople, bool fancyDecorations)
        {
            this.numberOfPeople = numberOfPeople;
            this.fancyDecorations = fancyDecorations;
            CalculateCostOfDecorations(fancyDecorations);
        }

        public int NumberOfPeople
        {
            get { return numberOfPeople; }
            set
            {
                numberOfPeople = value;
                CalculateCostOfDecorations(fancyDecorations);
            }
        }

        public void CalculateCostOfDecorations(bool fancy)
        {
            fancyDecorations = fancy;
            if (fancy)
            {
                CostOfDecorations = (NumberOfPeople * 1500M) + 5000M;
            }
            else
            {
                CostOfDecorations = (NumberOfPeople * 750M) + 3000M;
            }
        }

        public virtual decimal CalculateCost()
        {
            decimal totalCost = CostOfDecorations + CostOfFoodPerPerson * NumberOfPeople;
            if (numberOfPeople > 12)
            {
                totalCost += 10000M;
            }
            return totalCost;
        }
    }
}

DinnerParty.cs

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

namespace __DinnerParty
{
    public class DinnerParty:Party
    {

        private decimal CostOfBeveragesPerPerson;

        public DinnerParty(int numberOfPeople, bool healthyOption, bool fancyDecorations):
            base(numberOfPeople, fancyDecorations)
        {
            SetHealthyOption(healthyOption);
            CalculateCostOfDecorations(fancyDecorations);
        }

        public void SetHealthyOption(bool healthyOption)
        {
            if (healthyOption)
            {
                CostOfBeveragesPerPerson = 500M;
            }
            else
            {
                CostOfBeveragesPerPerson = 2000M;
            }
        }

        public decimal CalculateCost(bool healthyOption)
        {
            decimal totalCost = base.CalculateCost() + CostOfBeveragesPerPerson * NumberOfPeople;

            if (healthyOption)
            {
                totalCost *= .95M;
            }
            return totalCost;
        }
    }
}

BirthdayParty.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace __DinnerParty
{
    class BirthdayParty: Party
    {
        private int cakeSize;
        private string cakeWriting = "";

        public BirthdayParty(int numberOfPeople, bool fancyDecorations):
            base(numberOfPeople, fancyDecorations)
        {
            calculateCakeSize();
        }

        private void calculateCakeSize()
        {
            if (NumberOfPeople <= 4)
            {
                cakeSize = 7;
            }
            else
            {
                cakeSize = 15;
            }
        }

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

        public override decimal CalculateCost()
        {
            decimal cakeCost;

            if (cakeSize == 7)
            {
                cakeCost = 4000M + cakeWriting.Length * 25M;
            }
            else
            {
                cakeCost = 7500M + cakeWriting.Length * 25M;
            }

            return base.CalculateCost() + cakeCost;
        }
    }
}

0 コメント:

コメントを投稿