From 48b4b580b8f979e1086019b0c7e96f338d09e8a8 Mon Sep 17 00:00:00 2001 From: Daria Divenkova <47813973+dardiv@users.noreply.github.com> Date: Fri, 29 Mar 2019 15:40:28 +0300 Subject: [PATCH 1/4] Changed Name.cs --- Implementation/Common/Name.cs | 43 +++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/Implementation/Common/Name.cs b/Implementation/Common/Name.cs index 1999220..856ee09 100644 --- a/Implementation/Common/Name.cs +++ b/Implementation/Common/Name.cs @@ -3,26 +3,59 @@ /// /// Скрытая реализация представления об имени человека. /// - internal struct Name + public struct Name : IName { /* * TODO #1: Реализуйте интерфейс IName для структуры Name */ + /// + /// Полная форма имени. + /// + public string FullName { get; } + /// + /// Короткая форма имени. + /// + public string ShortName { get; } /// /// Имя. /// public string FirstName { get; } - + /// /// Фамилия. /// public string Surname { get; } - + /// /// Отчество. /// public string Patronymic { get; } - + + public Name(IName name) + { + FullName = name.FullName; + ShortName = name.ShortName; + + int SurnameLength = FullName.IndexOf(' '); + int FirstnameLength = FullName.Substring(SurnameLength + 1).IndexOf(' '); + + + Surname = FullName.Substring(0, SurnameLength + 1); + FirstName = FullName.Substring(SurnameLength + 1, FirstnameLength); + Patronymic = FullName.Substring(FirstnameLength + 1); + } + + public Name(string surname, string firstname, string patronymic) + { + Surname = surname; + FirstName = firstname; + Patronymic = patronymic; + FullName = surname + ' ' + firstname + ' ' + patronymic; + ShortName = surname + ' ' + firstname[0] + ". " + patronymic[0] + '.'; + } + + + } -} +} \ No newline at end of file From 74e01535644f123482144c6bacfb504e81f6a3d6 Mon Sep 17 00:00:00 2001 From: Daria Divenkova <47813973+dardiv@users.noreply.github.com> Date: Fri, 29 Mar 2019 15:42:06 +0300 Subject: [PATCH 2/4] Changed all Implementation.HR files --- Implementation/HR/AbstractPerson.cs | 50 ++++++++-------- Implementation/HR/Builders.cs | 93 ++++++++++++++++++++++++++++- Implementation/HR/Client.cs | 7 ++- Implementation/HR/Employee.cs | 30 +++++++++- 4 files changed, 147 insertions(+), 33 deletions(-) diff --git a/Implementation/HR/AbstractPerson.cs b/Implementation/HR/AbstractPerson.cs index f104ec0..8668684 100644 --- a/Implementation/HR/AbstractPerson.cs +++ b/Implementation/HR/AbstractPerson.cs @@ -1,39 +1,39 @@ -namespace Practice.HR +using Practice.HR.Events; +using Practice.Common; +using System; + +namespace Practice.HR { /// /// Абстрактная база для описания конкретных реализаций типа "Человек". /// Используется для дальнейшего наследования. /// - internal abstract class AbstractPerson + internal abstract class AbstractPerson : IPerson { - internal Organization.Department Department - { - get - { - throw new System.NotImplementedException(); - } - set - { - throw new System.NotImplementedException(); - } - } + /* + * TODO #3: Реализуйте интерфейс IPerson для класса AbstractPerson + */ - internal Common.Name Name - { - get - { - throw new System.NotImplementedException(); - } + private Name name; + public IName Name + { + get { return name; } set { - throw new System.NotImplementedException(); + ValueChangeEventArgs args = new ValueChangeEventArgs(name); + + name = new Name(value); + + if (NameChange != null) + { + NameChange(this, args); + } } } - - /* - * TODO #3: Реализуйте интерфейс IPerson для класса AbstractPerson - */ + + public event EventHandler> NameChange; + } -} +} \ No newline at end of file diff --git a/Implementation/HR/Builders.cs b/Implementation/HR/Builders.cs index 65650cb..c06265c 100644 --- a/Implementation/HR/Builders.cs +++ b/Implementation/HR/Builders.cs @@ -1,3 +1,6 @@ +using Practice.HR.Events; +using Practice.Common; +using Practice.Organization; using System; namespace Practice.HR @@ -19,7 +22,43 @@ public static IClientBuilder ClientBuilder() /* * TODO #6: Реализовать фабричный метод ClientBuilder класса Builders */ - throw new NotImplementedException(); + return new ClientBuilderClass(); + } + + private class ClientBuilderClass : IClientBuilder + { + private IClient client = new Client(); + + public IClient Build() + { + return client; + } + + public IClientBuilder Name(IName name) + { + client.Name = new Name(name); + client.NameChange += onNameChange; + return this; + } + + public IClientBuilder Name(string name, string surname, string patronymic) + { + client.Name = new Name(name, surname, patronymic); + client.NameChange += onNameChange; + return this; + } + + public IClientBuilder Discount(float discount) + { + client.Discount = discount; + return this; + } + + private void onNameChange(object sender, ValueChangeEventArgs args) + { + Console.WriteLine("Клиент {0} изменил имя на {1}", args.OldValue.FullName, client.Name.FullName); + } + } /// @@ -33,8 +72,56 @@ public static IEmployeeBuilder EmployeeBuilder() /* * TODO #7: Реализовать фабричный метод EmployeeBuilder класса Builders */ - throw new NotImplementedException(); + return new EmployeeBuilderClass(); + } + + private class EmployeeBuilderClass : IEmployeeBuilder + { + private IEmployee employee = new Employee(); + + public IEmployee Build() + { + return employee; + } + + public IEmployeeBuilder Name(IName name) + { + employee.Name = new Name(name); + employee.NameChange += onNameChange; + return this; + } + + public IEmployeeBuilder Name(string surname, string firstname, string patronymic) + { + employee.Name = new Name(surname, firstname, patronymic); + employee.NameChange += onNameChange; + return this; + } + + public IEmployeeBuilder Department(IDepartment department) + { + employee.Department = new Department(department); + return this; + } + + public IEmployeeBuilder Department(string department) + { + employee.Department = new Department(department); + employee.DepartmentChange += onDepartmentChange; + return this; + } + + private void onNameChange(object sender, ValueChangeEventArgs args) + { + Console.WriteLine("Сотрудник {0} изменил имя на {1}", args.OldValue.FullName, employee.Name.FullName); + } + + private void onDepartmentChange(object sender, ValueChangeEventArgs args) + { + Console.WriteLine("Сотрудник {0} был переведен из отдела '{1}' в отдел '{2}'", employee.Name.ShortName, args.OldValue.Name, employee.Department.Name); + } + } } -} +} \ No newline at end of file diff --git a/Implementation/HR/Client.cs b/Implementation/HR/Client.cs index a0c5405..75ed6bf 100644 --- a/Implementation/HR/Client.cs +++ b/Implementation/HR/Client.cs @@ -3,10 +3,13 @@ /// /// Скрытая реализация представления о клиенте. /// - internal class Client : AbstractPerson + internal class Client : AbstractPerson, IClient { /* * TODO #4: Реализуйте интерфейс IClient для класса Client */ + + public float Discount { get; set; } + } -} +} \ No newline at end of file diff --git a/Implementation/HR/Employee.cs b/Implementation/HR/Employee.cs index 5f013e7..ccf8f35 100644 --- a/Implementation/HR/Employee.cs +++ b/Implementation/HR/Employee.cs @@ -1,12 +1,36 @@ -namespace Practice.HR +using Practice.HR.Events; +using Practice.Organization; +using System; + +namespace Practice.HR { /// /// Скрытая реализация представления о сотруднике. /// - internal class Employee : AbstractPerson + internal class Employee : AbstractPerson, IEmployee { /* * TODO #5: Реализуйте интерфейс IEmployee для класса Employee */ + + private Department department; + + public IDepartment Department + { + get { return department; } + set + { + ValueChangeEventArgs args = new ValueChangeEventArgs(Department); + + department = new Department(value); + + if (DepartmentChange != null) + { + DepartmentChange(this, args); + } + } + } + + public event EventHandler> DepartmentChange; } -} +} \ No newline at end of file From 4e7bea06d3bda22b77fc234c5bdda21df2e6720b Mon Sep 17 00:00:00 2001 From: Daria Divenkova <47813973+dardiv@users.noreply.github.com> Date: Fri, 29 Mar 2019 15:42:54 +0300 Subject: [PATCH 3/4] Changed Department.cs --- Implementation/Organization/Department.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Implementation/Organization/Department.cs b/Implementation/Organization/Department.cs index f15d0aa..ef19db0 100644 --- a/Implementation/Organization/Department.cs +++ b/Implementation/Organization/Department.cs @@ -3,10 +3,21 @@ /// /// Скрытая реализация представления об отделе предприятия. /// - internal struct Department + public struct Department : IDepartment { /* * TODO #2: Реализуйте интерфейс IDepartment для структуры Department */ + public string Name { get; } + public Department(IDepartment department) + { + Name = department.Name; + } + + public Department(string name) + { + Name = name; + } + } -} +} \ No newline at end of file From f8376c781400d3967b0aa9e249bac899ab339327 Mon Sep 17 00:00:00 2001 From: Daria Divenkova <47813973+dardiv@users.noreply.github.com> Date: Fri, 29 Mar 2019 15:43:42 +0300 Subject: [PATCH 4/4] Changed Program.cs --- Practice/Program.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Practice/Program.cs b/Practice/Program.cs index 36257df..b9619b4 100644 --- a/Practice/Program.cs +++ b/Practice/Program.cs @@ -1,4 +1,7 @@ -using Practice.HR; +using Practice.Common; +using Practice.HR; +using Practice.Organization; +using System; namespace Practice { @@ -18,7 +21,6 @@ static void Main(string[] args) .Name("Иванов", "Иван", "Иванович") .Discount(.1f) .Build(); - IEmployee employee = Builders.EmployeeBuilder() .Name("Сидоров", "Григорий", "Петрович") .Department("Бухгалтерия") @@ -31,6 +33,10 @@ static void Main(string[] args) /* * TODO #9: При помощи отладчика проверить типы и структуру объектов, адресованных переменными client и employee. */ + + client.Name = new Name("Андреев", "Андрей", "Андреевич"); + employee.Name = new Name("Кузнецов", "Петр", "Игоревич"); + employee.Department = new Department("HR"); } } }