2014年6月9日月曜日

開発環境

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

エクササイズ(p.211)

コード

Form1.cs

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Queen queen;
        public Form1()
        {
            InitializeComponent();
            Worker[] workers = new Worker[4];
            workers[0] = new Worker(new string[] { "蜂蜜の収集", "蜂蜜の製造" });
            workers[1] = new Worker(new string[] { "卵の世話", "幼虫の世話" });
            workers[2] = new Worker(new string[] { "巣のメンテナンス", "パトロール" });
            workers[3] = new Worker(new string[] {"蜂蜜の収集", "蜂蜜の製造", "卵の世話", "幼虫の世話", "巣のメンテナンス",
                "パトロール"});
            queen = new Queen(workers);
        }

        private void shifts_ValueChanged(object sender, EventArgs e)
        {

        }

        private void assignWork_Click(object sender, EventArgs e)
        {
            string job = workerBeeJob.Text;
            int n = (int)shifts.Value;
            if (queen.AssignWork(job, n))
            {
                MessageBox.Show("'" + job + "'は、あと" + n + "シフトで終わる。", "女王蜂が言った");
            }
            else
            {
                MessageBox.Show("'" + job + "'を実行できる蜂がいない。", "女王蜂が言った。");
            }
        }

        private void nextShift_Click(object sender, EventArgs e)
        {
            report.Text = queen.WorkTheNextShift();
        }
    }
    class Queen
    {
        private Worker[] workers;
        private int shiftNumber = 1;

        public Queen(Worker[] workers)
        {
            this.workers = workers;
        }

        public bool AssignWork(string job, int shiftsToWork)
        {
            for (int i = 0; i < workers.Length; ++i)
            {
                if (workers[i].DoThisJob(job, shiftsToWork))
                {
                    return true;
                }
            }
            return false;
        }
        public string WorkTheNextShift()
        {
            string report = "シフト#" + shiftNumber + "のレポート\r\n";
            for (int i = 0; i < workers.Length; ++i)
            {
                report += "働き蜂#" + (i + 1) + "は";
                string job = workers[i].CurrentJob;
                if (String.IsNullOrEmpty(job))
                {
                    report += "は作業をしていません。";
                }
                else if (workers[i].WorkOneShift())
                {
                    report += "このシフトで'" + job + "'の作業を完了します。";
                }
                else
                {
                    report += "'" + job + "'の作業中で、あと" + workers[i].ShiftLeft + "つのシフトがあります。";
                }
                report += "\r\n";
            }
            ++shiftNumber;
            return report;
        }
    }
    class Worker
    {
        private string currentJob = "";
        private string[] jobsICanDo;
        private int shiftsToWork;
        private int shiftsWorked;

        public Worker(string[] jobsICanDo)
        {
            this.jobsICanDo = jobsICanDo;
        }

        public string CurrentJob
        {
            get { return currentJob; }
        }
        public int ShiftLeft
        {
            get { return shiftsToWork - shiftsWorked; }
        }
        public bool DoThisJob(string job, int shiftsToWork)
        {
            if (!String.IsNullOrEmpty(currentJob))
            {
                return false;
            }
            for (int i = 0; i < jobsICanDo.Length; ++i)
            {
                if (jobsICanDo[i] == job)
                {
                    currentJob = job;
                    this.shiftsToWork = shiftsToWork;
                    return true;
                }
            }
            return false;
        }
        public bool WorkOneShift()
        {
            ++shiftsWorked;
            if (shiftsWorked == shiftsToWork)
            {
                shiftsWorked = 0;
                shiftsToWork = 0;
                currentJob = "";
                return true;
            }
            return false;
        }
    }
}

0 コメント:

コメントを投稿