開発環境
- OS: Windows 10 Pro
- IDE(統合開発環境): Visual Studio Community 2017
Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の5章(継承 - オブジェクトの系図)、エクササイズ(p. 211)を取り組んでみる。
コード
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[] {"花蜜の収集", "蜂蜜の製造" }),
new Worker(new string[] {"卵の世話", "幼虫の世話" }),
new Worker(new string[] {"巣のメンテナンス", "パトロール" }),
new Worker(
new string[] {
"花蜜の収集", "蜂蜜の製造", "卵の世話",
"幼虫の世話", "巣のメンテナンス", "パトロール"})
};
queen = new Queen(workers);
}
private void button1_Click(object sender, EventArgs e)
{
queen.assignWork(workerBeeJob.Text, (int)shifts.Value);
}
private void button2_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 Queen
{
private Worker[] workers;
private int shiftNumber = 0;
public Queen(Worker[] workers)
{
this.workers = workers;
}
public bool assignWork(string s, int n)
{
foreach (Worker worker in workers)
{
if (worker.doThisJob(s, n))
{
return true;
}
}
return false;
}
public string workTheNextShift()
{
shiftNumber++;
string s = "シフト#" + shiftNumber + "のレポート\r\n";
for (int i = 0; i < workers.Length; i++)
{
Worker w = workers[i];
s += "働き蜂#" + (i + 1);
if (w.workOneShift())
{
s += "は作業を完了しました。";
}
else if (String.IsNullOrEmpty(w.CurrentJob))
{
s += "は作業はありません。";
}
else
{
s += "'" + w.CurrentJob + "'の作業中で、あと" +
w.ShiftsLeft + "つシフトがあります。";
}
s += "\r\n";
}
return s;
}
}
class Worker
{
private string currentJob = "";
private int shiftsLeft;
private string[] jobsICanDo;
private int shiftsToWork;
private int shiftsWorked;
public Worker(string[] jobsICanDo)
{
this.jobsICanDo = jobsICanDo;
}
public string CurrentJob
{
get { return currentJob; }
}
public int ShiftsLeft
{
get
{
shiftsLeft = shiftsToWork - shiftsWorked;
return shiftsLeft;
}
}
public bool doThisJob(string job, int n)
{
if (!String.IsNullOrEmpty(currentJob))
{
return false;
}
foreach (string jobICanDo in jobsICanDo)
{
if (jobICanDo == job)
{
currentJob = job;
shiftsToWork = n;
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 コメント:
コメントを投稿