開発環境
- OS: Windows 10 Pro
- IDE(統合開発環境): Visual Studio Community 2017
Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の5章(継承 - オブジェクトの系図)、エクササイズ(p. 216)を取り組んでみる。
コード
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.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp16 { public partial class Form1 : Form { Queen queen; public Form1() { InitializeComponent(); Worker[] workers = new Worker[] { new Worker(new string[] {"花蜜の収集", "蜂蜜の製造" }, 175), new Worker(new string[] {"卵の世話", "幼虫の世話" }, 114), new Worker(new string[] {"巣のメンテナンス", "パトロール" }, 149), new Worker( new string[] { "花蜜の収集", "蜂蜜の製造", "卵の世話", "幼虫の世話", "巣のメンテナンス", "パトロール"}, 155) }; queen = new Queen(workers, 275); } private void assignJob_Click(object sender, EventArgs e) { if (queen.assignWork(workerBeeJob.Text, (int)shifts.Value)) { MessageBox.Show("'" + workerBeeJob.Text + "'は、あと" + shifts.Value + "シフトで終わる。", "女王蜂が言った..."); } else { MessageBox.Show("'" + workerBeeJob.Text + "'の仕事をさせるための蜂がいない。", "女王蜂が言った..."); } } private void nextShift_Click(object sender, EventArgs e) { report.Text = queen.workTheNextShift(); } } }
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp16 { static class Program { /// <summary> /// アプリケーションのメイン エントリ ポイントです。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } class Bee { private int shiftsLeft = 0; private double weight; public Bee(double weight) { this.weight = weight; } public virtual int ShiftsLeft { get { return shiftsLeft; } } public virtual double getHoneyConsumption() { double consumption = 0; if (shiftsLeft > 0) { consumption = shiftsLeft + 9; } else if (shiftsLeft == 0) { consumption = 7.5; } if (weight > 150) { consumption *= 1.35; } return consumption; } } class Queen: Bee { private Worker[] workers; private int shiftNumber = 0; public Queen(Worker[] workers, double weight):base(weight) { this.workers = workers; } public override double getHoneyConsumption() { double consumption = 0; int shifts = 0; int working = 0; foreach (Worker worker in workers) { if (worker.ShiftsLeft > shifts) { shifts = worker.ShiftsLeft; } if (worker.ShiftsLeft > 0) { working++; } } consumption += shifts + 9; if (working <= 2) { consumption += 20; } else { consumption += 30; } return consumption; } public bool assignWork(string job, int numberOfShifts) { foreach (Worker worker in workers) { if (worker.doThisJob(job, numberOfShifts)) { return true; } } return false; } public string workTheNextShift() { double consumption = getHoneyConsumption(); foreach (Worker worker in workers) { consumption += worker.getHoneyConsumption(); } shiftNumber++; string report = "シフト#" + shiftNumber + "のレポート\r\n"; for (int i = 0; i < workers.Length; i++) { Worker worker = workers[i]; if (worker.workOneShift()) { report += "働き蜂#" + (i + 1) + "の作業は完了しました。\r\n"; } if (String.IsNullOrEmpty(worker.CurrentJob)) { report += "働き蜂#" + (i + 1) + "は作業がありません。\r\n"; } else { if (worker.ShiftsLeft > 0) { report += "働き蜂#" + (i + 1) + "'" + worker.CurrentJob + "'の作業中で、あと" + worker.ShiftsLeft + "つのシフトがあります。\r\n"; } else { report += "働き蜂#" + (i + 1) + "はこのシフトで'" + worker.CurrentJob + "'の作業を完了します。\r\n"; } } } report += "消費したすべての蜂蜜の量: " + consumption + "単位\r\n"; return report; } } class Worker : Bee { private string[] jobsICanDo; private string currentJob = ""; private int shiftsToWork; private int shiftsWorked; public Worker(string[] jobsICanDo, double weight):base(weight) { this.jobsICanDo = jobsICanDo; } public string CurrentJob { get { return currentJob; } } public override int ShiftsLeft { get { return shiftsToWork - shiftsWorked; } } public bool doThisJob(string job, int numberOfShifts) { if (!String.IsNullOrEmpty(currentJob)) { return false; } foreach (string jobICanDo in jobsICanDo) { if (jobICanDo == job) { currentJob = job; shiftsToWork = numberOfShifts; shiftsWorked = 0; return true; } } return false; } public bool workOneShift() { if (String.IsNullOrEmpty(currentJob)) { return false; } shiftsWorked++; if (shiftsWorked > shiftsToWork) { shiftsWorked = 0; shiftsToWork = 0; currentJob = ""; return true; } return false; } } }
0 コメント:
コメントを投稿