Skip to content

Instantly share code, notes, and snippets.

@hagbarddenstore
Last active December 11, 2015 01:09
Show Gist options
  • Save hagbarddenstore/4521553 to your computer and use it in GitHub Desktop.
Save hagbarddenstore/4521553 to your computer and use it in GitHub Desktop.
A simple domain that can book rooms if a lesson matches the given requirements.
class UnableToBookRoomException : Exception
{
public UnableToBookRoomException()
: base("Unable to book the room")
{
}
}
class Room
{
public IEnumerable<Requirement> Requirements
{
get { return _requirements; }
}
public void Book(Lesson lesson, DateTime startsOn, DateTime endsOn)
{
var canBook = _requirements.All(x => x.Valid(lesson));
if (!canBook)
{
throw new UnableToBookRoomException();
}
// Do more stuff.
}
}
abstract class Requirement
{
public bool Valid(Lesson lesson);
}
class NationalityRequirement : Requirement
{
public int Amount { get; private set; }
public string Nationality { get; private set; }
public override bool Valid(Lesson lesson)
{
var valid = lesson.Students.Count(x => x.Nationality == Nationality) >= Amount;
return valid;
}
}
class Lesson
{
private readonly HashSet<Student> _students = new HashSet<Student>();
public Lecturer Lecturer { get; private set; }
public IEnumerable<Student> Students
{
get { return _students; }
}
public void Enlist(Student student)
{
if (_students.Contains(student))
{
throw new ArgumentException("Student is already enlisted.");
}
}
}
class Lecturer
{
}
class Student
{
public string Nationality { get; private set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment