enums and collections - Organizing your data - Stack, Queue
Head First C Sharp: A Learner’s Guide to Real-World Programming with C Sharp and .NET (Andrew Stellman(著)、Jennifer Greene(著)、O’Reilly Media)の Chapter 8(enums and collections - Organizing your data)、p.449(Exercise)の解答を求めてみる。
コード
Program.cs
using ConsoleApp6;
Console.Write("First lumberjack's name: ");
Queue<Lumberjack> lumberjacks = new Queue<Lumberjack>();
string? nameOrNull;
Random random = new Random();
while ((nameOrNull = Console.ReadLine()) != "")
{
if (nameOrNull == null)
{
continue;
}
Console.Write("Number of flapjacks: ");
if (int.TryParse(Console.ReadLine(), out int numberOfFlapjacks))
{
string name = nameOrNull.ToString();
Lumberjack lumberjack = new Lumberjack(name);
for (int i = 0; i < numberOfFlapjacks; i++)
{
lumberjack.TakeFlapjack((Flapjack)random.Next(4));
}
lumberjacks.Enqueue(lumberjack);
}
Console.Write("Next lumberjack's name (blank to end): ");
}
while (lumberjacks.Count > 0)
{
lumberjacks.Dequeue().EatFlapjacks();
}
Flapjack.cs
namespace ConsoleApp6;
public enum Flapjack
{
Crispy,
Soggy,
Browned,
Banan,
}
Lumberjack.cs
namespace ConsoleApp6;
public class Lumberjack
{
public string Name { get; }
private Stack<Flapjack> flapjackStack = new Stack<Flapjack>();
public void TakeFlapjack(Flapjack flapjack)
{
flapjackStack.Push(flapjack);
}
public void EatFlapjacks()
{
Console.WriteLine($"{Name} is eating flapjacks");
while (flapjackStack.Count > 0)
{
Console.WriteLine($"{Name} ate a {flapjackStack.Pop().ToString().ToLower()}");
}
}
public Lumberjack(string name)
{
Name = name;
}
}
入出力結果(Terminal, Zsh)
% dotnet run
First lumberjack's name: Number of flapjacks: Erik
Next lumberjack's name (blank to end):
kamimura@kamimuras-MacBook ConsoleApp6 % dotnet run
First lumberjack's name: Erik
Number of flapjacks: 4
Next lumberjack's name (blank to end): Hidur
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 banan
Erik ate a browned
Erik ate a crispy
Erik ate a banan
Hidur is eating flapjacks
Hidur ate a banan
Hidur ate a browned
Hidur ate a browned
Hidur ate a soggy
Hidur ate a crispy
Hidur ate a crispy
Jan is eating flapjacks
Jan ate a browned
Jan ate a banan
Jan ate a browned
Betty is eating flapjacks
Betty ate a soggy
Betty ate a banan
Betty ate a banan
Betty ate a crispy
% dotnet run
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 crispy
Erik ate a crispy
Erik ate a banan
Erik ate a banan
Hildur is eating flapjacks
Hildur ate a soggy
Hildur ate a browned
Hildur ate a soggy
Hildur ate a crispy
Hildur ate a banan
Hildur ate a banan
Jan is eating flapjacks
Jan ate a banan
Jan ate a crispy
Jan ate a crispy
Betty is eating flapjacks
Betty ate a soggy
Betty ate a soggy
Betty ate a soggy
Betty ate a soggy
%