Last active
December 11, 2015 01:09
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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