Untitled
unknown
plain_text
2 years ago
3.8 kB
4
Indexable
namespace Homework { public class Person { public string FirstName { get { return FirstName; } set { bool isLatin = FirstName.All(c => (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); if (FirstName.Length < 1 || FirstName.Length > 30 || !isLatin) { throw new Exception("Name is invalid"); } else FirstName = value; } } public string LastName { get { return LastName; } set { bool isLatin = LastName.All(c => (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); if (LastName.Length < 1 || LastName.Length > 30 || !isLatin) { throw new Exception("Name is invalid"); } else LastName = value; } } public DateTime DateOfBirth { get { return DateOfBirth; } set { if (DateOfBirth.Date < DateTime.Today) { throw new Exception("Invalid Date of Birth."); } else DateOfBirth = value; } } public string ID { get { return ID; } set { int digits = 0; for (int i = 2; i < ID.Length; i++) { if (Char.IsDigit(ID[i])) { digits++; } } if (!Char.IsUpper(ID[0]) || !Char.IsUpper(ID[1]) || digits != 10) { throw new Exception("Invalid ID number"); } ID = value; } } public Person(string firstName, string lastName, DateTime dateOfBirth, string iD) { FirstName = firstName; LastName = lastName; DateOfBirth = dateOfBirth; ID = iD; } } public interface IPersonRepository { Person GetByFirstName(string firstName); Person GetByLastName(string lastName); Person GetById(string id); void Add(Person person); } public class PersonRepository : IPersonRepository { public List<Person> People; public PersonRepository(List<Person> people) { this.People = people; } public Person GetByFirstName(string firstName) { foreach (Person person in People) { if (person.FirstName == firstName) { return person; } } return null; } public Person GetByLastName(string lastName) { foreach (Person person in People) { if (person.LastName == lastName) { return person; } } return null; } public Person GetById(string id) { foreach (Person person in People) { if (person.ID == id) { return person; } } return null; } public void Add(Person person) { if (GetByFirstName(person.FirstName) == null || GetByLastName(person.LastName) == null || GetById(person.ID) == null) { People.Add(person); } } } }
Editor is loading...