Skip to content

Instantly share code, notes, and snippets.

@j2jensen
Forked from jeremDev/Program.cs
Last active July 21, 2016 15:35
Show Gist options
  • Save j2jensen/65e362ab09c37bf133404d39b80cd65a to your computer and use it in GitHub Desktop.
Save j2jensen/65e362ab09c37bf133404d39b80cd65a to your computer and use it in GitHub Desktop.
Bedtime Routine
using System;
using static System.Console;
using static System.Threading.Thread;
using Throwable = System.Exception;
public class Parent
{
public string Name => "Parent";
public void Say(string message, Child child)
{
WriteLine(this.Name + ": " + message);
child.Hear(message);
}
public void PutToBed(Child child)
{
while (!child.IsInBed)
{
try
{
this.Say("Go to bed.", child);
}
catch (Excuse excuse)
{
WriteLine($"{child.Name}: " + excuse.Message);
this.Say("OK", child);
}
catch (Tantrum tantrum)
{
WriteLine($"{child.Name}: " + tantrum.Message);
this.Say("It's bed time.", child);
}
}
}
}
public class Child
{
public string Name => "Child";
const bool Smoochable = true;
private int timesDelayed;
public bool IsInBed { get; private set; }
public void Say(string message)
{
WriteLine(this.Name + ": " + message);
}
void Delay()
{
this.timesDelayed++;
if (this.timesDelayed == 1)
{
throw new Excuse("I want water");
}
if (this.timesDelayed == 2)
{
throw new Tantrum("I want applesauce!");
}
}
public void Rest()
{
if (this.IsInBed)
{
Sleep(TimeSpan.FromHours(10));
this.IsInBed = false;
}
}
public void Hear(string message)
{
if (message == "Go to bed.")
{
this.Delay();
this.Say("Okay");
this.IsInBed = true;
}
}
}
public class Excuse : Exception
{
public Excuse(string message) : base("(Pouting) " + message)
{
}
}
public class Tantrum : Throwable
{
public Tantrum(string message) : base("(Fussing) " + message)
{
}
}
public class BedTimeRoutine
{
public void ReadStory()
{
WriteLine("Reads Stories");
}
public void Sing()
{
WriteLine("Sings song");
}
public void Run()
{
this.ReadStory();
this.Sing();
}
}
class Program
{
static void Main()
{
Parent parent = new Parent();
Child child = new Child();
BedTimeRoutine routine = new BedTimeRoutine();
routine.Run();
parent.PutToBed(child);
parent.Say("I love you!", child);
child.Say("I love you too!");
child.Rest();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment