計算機科学のブログ

enums and collections - Organizing your data - Stack, Queue, Count, Pop, Enqueue, Dequeue

Head First C#: A Learner’s Guide to Real-World Programming with C# and .NET Core (Andrew Stellman(著)、Jennifer Greene(著)、O’Reilly Media)のChapter 8(enums and collections - Organizing your data)、p.449(Exercise)の解答を求めてみる。

コード

Flapjack.cs

using System;
namespace MyFirstConsoleApp
{
    public enum Flapjack
    {
        Crispy,
        Soggy,
        Browned,
        Banana,
    }
}

コード

Lumberjack.cs

using System;
using System.Collections.Generic;

namespace MyFirstConsoleApp
{
    public class Lumberjack
    {
        public string Name { get; private set; }
        private Stack<Flapjack> flapjacks = new Stack<Flapjack>();

        public Lumberjack(string name)
        {
            Name = name;
        }
        public void TakeFlapjack(Flapjack flapjack)
        {
            flapjacks.Push(flapjack);
        }
        public void EatFlapjack()
        {
            Console.WriteLine($"{Name} is eating flapjacks");
            while (flapjacks.Count > 0)
            {
                Console.WriteLine(
                    $"{Name} ate a {flapjacks.Pop().ToString().ToLower()} flapjack");
            }
        }
    }
}

コード

Program.cs

using System;
using System.Collections.Generic;

namespace MyFirstConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Random random = new Random();
            Queue<Lumberjack> lumberjacks = new Queue<Lumberjack>();
            Console.Write("First lumberjack's name: ");
            string name = Console.ReadLine();
            while (name != "")
            {
                Console.Write("Number of flapjacks: ");
                string s = Console.ReadLine();
                if (int.TryParse(s, out int result))
                {
                    Lumberjack lumberjack = new Lumberjack(name);
                    for (int i = 0; i < result; i++)
                    {
                        lumberjack.TakeFlapjack((Flapjack)random.Next(4));
                    }
                    lumberjacks.Enqueue(lumberjack);
                    Console.Write("Next lumberjack's name (blank to end): ");
                    name = Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("Invalid input ${s}");
                }
            }
            while (lumberjacks.Count > 0)
            {
                lumberjacks.Dequeue().EatFlapjack();
            }
        }
    }
}

入出力結果(Terminal, Zsh)

First lumberjack's name: Erik
Number of flapjacks: 4
Next lumberjack's name (blank to end): Hildur
Number of flapjacks: 6
Next lumberjack's name (blank to end): Jan
Number of flapjacks: 3
Next lumberjack's name (blank to end): Betty
Number of flapjacks: 4
Next lumberjack's name (blank to end): 
Erik is eating flapjacks
Erik ate a browned flapjack
Erik ate a browned flapjack
Erik ate a banana flapjack
Erik ate a browned flapjack
Hildur is eating flapjacks
Hildur ate a crispy flapjack
Hildur ate a soggy flapjack
Hildur ate a soggy flapjack
Hildur ate a browned flapjack
Hildur ate a crispy flapjack
Hildur ate a browned flapjack
Jan is eating flapjacks
Jan ate a banana flapjack
Jan ate a banana flapjack
Jan ate a crispy flapjack
Betty is eating flapjacks
Betty ate a browned flapjack
Betty ate a crispy flapjack
Betty ate a crispy flapjack
Betty ate a crispy flapjack