SethVan

BuilderDoodle

Aug 27th, 2022 (edited)
1,296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. #include <iomanip>
  2. #include <iostream>
  3. #include <string>
  4. #include <unordered_map>
  5.  
  6. enum class Sex { MALE, FEMALE, UNKNOWN };
  7. enum class Education {
  8.     ELEMENTARY,
  9.     JUNIORHIGH,
  10.     HIGHSCHOOL,
  11.     TRADESCHOOL,
  12.     ASSOCIATES,
  13.     BACHELORS,
  14.     MASTERS,
  15.     DOCTORATE,
  16.     UNKNOWN
  17. };
  18.  
  19. static std::unordered_map<Sex, std::string> sex{
  20.     {Sex::FEMALE, "Female"}, {Sex::MALE, "Male"}, {Sex::UNKNOWN, "Unknown"}};
  21.  
  22. static std::unordered_map<Education, std::string> eduLevel{
  23.     {Education::ELEMENTARY, "Elementary"},        {Education::JUNIORHIGH, "Junior High"},
  24.     {Education::HIGHSCHOOL, "High School"},       {Education::TRADESCHOOL, "Trade School"},
  25.     {Education::ASSOCIATES, "Associates Degree"}, {Education::BACHELORS, "Bachelors Degree"},
  26.     {Education::MASTERS, "Masters Degree"},       {Education::UNKNOWN, "Unknown"}};
  27.  
  28. namespace svh {
  29. std::string convert_enum(Sex gender) { return sex[gender]; }
  30. std::string convert_enum(Education education) { return eduLevel[education]; }
  31. }  // namespace svh
  32.  
  33. class PersonBuilder;
  34.  
  35. class Person {
  36.     friend PersonBuilder;
  37.  
  38.    private:
  39.     std::string name = "Unknown";
  40.     Sex sex = Sex::UNKNOWN;
  41.     int age = 0;
  42.     std::string location = "Unknown";
  43.     Education education = Education::UNKNOWN;
  44.     std::string profession = "Unknown";
  45.  
  46.    public:
  47.     Person() = default;
  48.     ~Person() = default;
  49.  
  50.     void printPersonProfile() {
  51.         std::cout << std::setw(12) << "Name: " << name << "\n"
  52.                   << std::setw(12) << "Sex: " << svh::convert_enum(sex) << "\n"
  53.                   << std::setw(12) << "Age: " << (age ? std::to_string(age) : "Unknown") << "\n"
  54.                   << std::setw(12) << "Profession: " << profession << "\n"
  55.                   << std::setw(12) << "Location: " << location << "\n"
  56.                   << std::setw(12) << "Education: " << svh::convert_enum(education) << std::endl;
  57.     }
  58. };
  59.  
  60. class PersonBuilder {
  61.     Person person;
  62.  
  63.    public:
  64.     PersonBuilder() = default;
  65.     ~PersonBuilder() = default;
  66.  
  67.     PersonBuilder& addName(std::string name) {
  68.         person.name = name;
  69.         return *this;
  70.     }
  71.     PersonBuilder& addLocation(std::string location) {
  72.         person.location = location;
  73.         return *this;
  74.     }
  75.     PersonBuilder& addProfession(std::string profession) {
  76.         person.profession = profession;
  77.         return *this;
  78.     }
  79.     PersonBuilder& addSex(Sex sex) {
  80.         person.sex = sex;
  81.         return *this;
  82.     }
  83.     PersonBuilder& addAge(int age) {
  84.         person.age = age;
  85.         return *this;
  86.     }
  87.     PersonBuilder& addEducation(Education education) {
  88.         person.education = education;
  89.         return *this;
  90.     }
  91.     PersonBuilder& create(std::string name = "Unknown") {
  92.         person.name = name;
  93.         return *this;
  94.     }
  95.     Person build() {
  96.         auto temp = person;
  97.         person = Person();
  98.         return temp;
  99.     }
  100. };
  101.  
  102. int main() {
  103.     PersonBuilder builder;
  104.     Person person = builder.create("Jenny")
  105.                         .addSex(Sex::FEMALE)
  106.                         .addLocation("San Francisco")
  107.                         .addAge(42)
  108.                         .addProfession("Software Engineer")
  109.                         .build();
  110.     person.printPersonProfile();
  111.     puts("");
  112.  
  113.     auto person1 = builder.create().addName("Bob").addLocation("El Quinto Pino Tio!").build();
  114.     person1.printPersonProfile();
  115.  
  116.     auto person2 = person;
  117.     puts("");
  118.     person2.printPersonProfile();
  119.  
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment