Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace test
- {
- public class Person
- {
- public string Name { get; set;}
- public int Age { get; set;}
- public string StreetAddress { get; set;}
- public int PostCode { get; set;}
- public string Country { get; set;}
- public string CompanyName { get; set;}
- public string Position { get; set;}
- public int YearsOfWork { get; set;}
- public static PersonBuilder Create()
- {
- return new PersonBuilder (new Person ());
- }
- public void ShowPerson()
- {
- Console.WriteLine ("Name: {0} \nAge: {1}, \nAddress: {2}, {3}, {4} city\nWork/Study at: {5}, as a {6}, {7} years",
- this.Name, this.Age, this.StreetAddress, this.PostCode, this.Country, this.CompanyName, this.Position, this.YearsOfWork);
- }
- }
- public class PersonAddressBuilder
- {
- private Person person;
- public PersonAddressBuilder (Person person)
- {
- this.person = person;
- }
- public PersonAddressBuilder At (string streetAddress)
- {
- person.StreetAddress = streetAddress;
- return this;
- }
- public PersonAddressBuilder WithPostCode (int postCode)
- {
- person.PostCode = postCode;
- return this;
- }
- public PersonAddressBuilder In (string country)
- {
- person.Country = country;
- return this;
- }
- public PersonAddressBuilder Lives
- {
- get { return new PersonAddressBuilder(person); }
- }
- public static implicit operator Person (PersonAddressBuilder builder)
- {
- return builder.person;
- }
- public PersonJobBuilder Works
- {
- get { return new PersonJobBuilder (person);}
- }
- }
- public class PersonJobBuilder
- {
- private Person person;
- public PersonJobBuilder(Person person)
- {
- this.person = person;
- }
- public PersonJobBuilder At (string companyName)
- {
- person.CompanyName = companyName;
- return this;
- }
- public PersonJobBuilder AsA (string position)
- {
- person.Position = position;
- return this;
- }
- public PersonJobBuilder Years(int yearsOfWork)
- {
- person.YearsOfWork = yearsOfWork;
- return this;
- }
- public static implicit operator Person (PersonJobBuilder builder)
- {
- return builder.person;
- }
- }
- public class PersonBuilder
- {
- private Person person;
- public PersonBuilder(Person person)
- {
- this.person = person;
- }
- public PersonBuilder Called (string name)
- {
- person.Name = name;
- return this;
- }
- public PersonBuilder Age (int age)
- {
- person.Age = age;
- return this;
- }
- public static implicit operator Person (PersonBuilder pb)
- {
- return pb.person;
- }
- public PersonAddressBuilder Lives
- {
- get { return new PersonAddressBuilder(person); }
- }
- public PersonJobBuilder Works
- {
- get { return new PersonJobBuilder (person);}
- }
- }
- public class MainClass
- {
- public static void Main (string[] args)
- {
- Person p_1 = Person.Create ().Called ("Ma").Age (20)
- .Lives.At ("V, flat 2").WithPostCode (8).In ("S")
- .Works.At("S").AsA("student").Years(3);
- Person p_2 = Person.Create ().Called ("Ir").Age (41)
- .Lives.At ("Vo, flat 2").WithPostCode (2).In ("S")
- .Works.At("T").AsA("Manager").Years(20);
- p_1.ShowPerson ();
- Console.WriteLine ("_____________________");
- p_2.ShowPerson ();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment