Head First C#
頭とからだで覚えるC#の基本
(オライリージャパン)
Andrew Stellman (著), Jennifer Green (著)
佐藤 嘉一 (監修), 木下 哲也 (翻訳)
開発環境
- Microsoft Windows 8.1 Pro (VMware Fusion 6, OS X Mavericks - Apple) (OS)
- C# (プログラミング言語)
- Microsoft Visual Studio Express 2013 for Windows Desktop (統合開発環境, IDE)
Head First C#―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Green (著)、佐藤 嘉一 (監修)、木下 哲也 (翻訳)、オライリージャパン)の5章(継承: オブジェクトの系図)、エクササイズ(p.216)を解いてみる。
エクササイズ(p.216)
コード
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(175, new string[] { "蜂蜜の収集", "蜂蜜の製造" }); workers[1] = new Worker(114, new string[] { "卵の世話", "幼虫の世話" }); workers[2] = new Worker(149, new string[] { "巣のメンテナンス", "パトロール" }); workers[3] = new Worker(155, new string[] {"蜂蜜の収集", "蜂蜜の製造", "卵の世話", "幼虫の世話", "巣のメンテナンス", "パトロール"}); queen = new Queen(275, 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 Bee { private double weight; public Bee(double weight) { this.weight = weight; } public virtual int ShiftLeft { get { return 0; } } public virtual double GetHoneyConsumption() { double consumption; if (ShiftLeft > 0) { consumption = ShiftLeft + 9; } else { consumption = 7.5; } if (this.weight > 150) { consumption *= 1.35; } return consumption; } } class Queen : Bee { private Worker[] workers; private int shiftNumber = 1; public Queen(double weight, Worker[] workers):base(weight) { 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"; double consumption = 0; for (int i = 0; i < workers.Length; ++i) { consumption += workers[i].GetHoneyConsumption(); } consumption += this.GetHoneyConsumption(); 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"; } report += "消費したすべての蜂蜜の量: " + consumption + "単位"; report += "\r\n"; ++shiftNumber; return report; } public override double GetHoneyConsumption() { double consumption = 0; double max = 0; int working = 0; for (int i = 0; i < workers.Length; ++i) { if(workers[i].ShiftLeft > max) { consumption += workers[i].GetHoneyConsumption(); max = workers[i].ShiftLeft; } if (String.IsNullOrEmpty(workers[i].CurrentJob)) { ++working; } } if (working > 3) { consumption += 30; } else { consumption += 20; } return consumption; } } class Worker : Bee { private string currentJob = ""; private string[] jobsICanDo; private int shiftsToWork; private int shiftsWorked; public Worker(double weight, string[] jobsICanDo):base(weight) { this.jobsICanDo = jobsICanDo; } public string CurrentJob { get { return currentJob; } } public override 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; } public override double GetHoneyConsumption() { return base.GetHoneyConsumption(); } } }
0 コメント:
コメントを投稿