計算機科学のブログ

interfaces, casting, and "is" - Making classes keep their promises - object references

Head First C#: A Learner’s Guide to Real-World Programming with C# and .NET Core (Andrew Stellman(著)、Jennifer Greene(著)、O’Reilly Media)のChapter 7(interfaces, casting, and “is” - Making classes keep their promises)、p.371(Exercise)の解答を求めてみる。

コード

IWorker.cs

using System;
namespace BeehiveManagementSystem
{
    public interface IWorker
    {
        public string Job { get; }
        public void WorkTheNextShift();
    }
}

コード

Bee.cs

using System;
namespace BeehiveManagementSystem
{
    public class Bee: IWorker
    {
        public string Job { get; private set; }
        public virtual float CostPerShift { get; }

        public Bee(string job)
        {
            Job = job;
        }
        public void WorkTheNextShift()
        {
            if (HoneyValut.ConsumeHoney(CostPerShift))
            {
                DoJob();
            }
        }
        protected virtual void DoJob() { }
    }
}

コード

Queen.cs

using System;
namespace BeehiveManagementSystem
{
    public class Queen : Bee
    {
        private const float EGGS_PER_SHIFT = 0.45f;
        private const float HONEY_PER_UNASSIGNED_WORKER = 0.5f;
        private IWorker[] workers = Array.Empty<IWorker>();
        private float eggs = 0;
        private float unassignedWorkers = 3;

        public override float CostPerShift
        {
            get
            {
                return 2.15f;
            }
        }
        public string StatusReport { get; private set; }
        public Queen() : base("Queen")
        {
            AssignBee("Egg Care");
            AssignBee("Honey Manufacturer");
            AssignBee("Nectar Collector");
        }
        protected override void DoJob()
        {
            eggs += EGGS_PER_SHIFT;
            foreach (IWorker worker in workers)
            {
                worker.WorkTheNextShift();
            }
            HoneyValut.ConsumeHoney(HONEY_PER_UNASSIGNED_WORKER * workers.Length);
            updateStatusReport();
        }
        public void AddWorker(IWorker worker)
        {
            if (unassignedWorkers > 0)
            {
                unassignedWorkers--;
                Array.Resize(ref workers, workers.Length + 1);
                workers[workers.Length - 1] = worker;
            }
        }
        public void AssignBee(string job)
        {
            switch (job)
            {
                case "Egg Care":
                    AddWorker(new EggCare(this));
                    break;
                case "Honey Manufacturer":
                    AddWorker(new HoneyManufacturer());
                    break;
                case "Nectar Collector":
                    AddWorker(new NectarCollector());
                    break;
                default:
                    break;
            }
            updateStatusReport();
        }
        public void CareForEggs(float eggsToConvert)
        {
            if (eggs >= eggsToConvert)
            {
                eggs -= eggsToConvert;
                unassignedWorkers += eggsToConvert;
            }
        }
        private void updateStatusReport()
        {
            int nectarCollectorBeeCount = 0;
            int honeyManufacturerBeeCount = 0;
            int eggCareBeeCount = 0;
            foreach (IWorker worker in workers)
            {
                switch (worker.Job)
                {
                    case "Nectar Collector":
                        nectarCollectorBeeCount++;
                        break;
                    case "Honey Manufacturer":
                        honeyManufacturerBeeCount++;
                        break;
                    case "Egg Care":
                        eggCareBeeCount++;
                        break;
                    default:
                        break;
                }
            }
            string nectors = "bee";
            string honeys = "bee";
            string eggCares = "bee";
            if (nectarCollectorBeeCount > 1)
            {
                nectors += "s";
            }
            if (honeyManufacturerBeeCount > 1)
            {
                honeys += "s";
            }
            if (eggCareBeeCount > 1)
            {
                eggCares += "s";
            }
            StatusReport = $"Vault report:\n{HoneyValut.StatusReport}\n\n" +
                $"Egg count: {eggs:0.0}\n" +
                $"Unassigned workers: {unassignedWorkers:0.0}\n" +
                $"{nectarCollectorBeeCount} NectarCollector {nectors}\n" +
                $"{honeyManufacturerBeeCount} Honey Manufacturer {honeys}\n" +
                $"{eggCareBeeCount} Egg Care {eggCares}\n" +
                $"TOTAL WORKERS: {workers.Length}";
        }
    }
}