Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iomanip>
- #include <iostream>
- #include <string>
- #include <unordered_map>
- enum class Sex { MALE, FEMALE, UNKNOWN };
- enum class Education {
- ELEMENTARY,
- JUNIORHIGH,
- HIGHSCHOOL,
- TRADESCHOOL,
- ASSOCIATES,
- BACHELORS,
- MASTERS,
- DOCTORATE,
- UNKNOWN
- };
- static std::unordered_map<Sex, std::string> sex{
- {Sex::FEMALE, "Female"}, {Sex::MALE, "Male"}, {Sex::UNKNOWN, "Unknown"}};
- static std::unordered_map<Education, std::string> eduLevel{
- {Education::ELEMENTARY, "Elementary"}, {Education::JUNIORHIGH, "Junior High"},
- {Education::HIGHSCHOOL, "High School"}, {Education::TRADESCHOOL, "Trade School"},
- {Education::ASSOCIATES, "Associates Degree"}, {Education::BACHELORS, "Bachelors Degree"},
- {Education::MASTERS, "Masters Degree"}, {Education::UNKNOWN, "Unknown"}};
- namespace svh {
- std::string convert_enum(Sex gender) { return sex[gender]; }
- std::string convert_enum(Education education) { return eduLevel[education]; }
- } // namespace svh
- class PersonBuilder;
- class Person {
- friend PersonBuilder;
- private:
- std::string name = "Unknown";
- Sex sex = Sex::UNKNOWN;
- int age = 0;
- std::string location = "Unknown";
- Education education = Education::UNKNOWN;
- std::string profession = "Unknown";
- public:
- Person() = default;
- ~Person() = default;
- void printPersonProfile() {
- std::cout << std::setw(12) << "Name: " << name << "\n"
- << std::setw(12) << "Sex: " << svh::convert_enum(sex) << "\n"
- << std::setw(12) << "Age: " << (age ? std::to_string(age) : "Unknown") << "\n"
- << std::setw(12) << "Profession: " << profession << "\n"
- << std::setw(12) << "Location: " << location << "\n"
- << std::setw(12) << "Education: " << svh::convert_enum(education) << std::endl;
- }
- };
- class PersonBuilder {
- Person person;
- public:
- PersonBuilder() = default;
- ~PersonBuilder() = default;
- PersonBuilder& addName(std::string name) {
- person.name = name;
- return *this;
- }
- PersonBuilder& addLocation(std::string location) {
- person.location = location;
- return *this;
- }
- PersonBuilder& addProfession(std::string profession) {
- person.profession = profession;
- return *this;
- }
- PersonBuilder& addSex(Sex sex) {
- person.sex = sex;
- return *this;
- }
- PersonBuilder& addAge(int age) {
- person.age = age;
- return *this;
- }
- PersonBuilder& addEducation(Education education) {
- person.education = education;
- return *this;
- }
- PersonBuilder& create(std::string name = "Unknown") {
- person.name = name;
- return *this;
- }
- Person build() {
- auto temp = person;
- person = Person();
- return temp;
- }
- };
- int main() {
- PersonBuilder builder;
- Person person = builder.create("Jenny")
- .addSex(Sex::FEMALE)
- .addLocation("San Francisco")
- .addAge(42)
- .addProfession("Software Engineer")
- .build();
- person.printPersonProfile();
- puts("");
- auto person1 = builder.create().addName("Bob").addLocation("El Quinto Pino Tio!").build();
- person1.printPersonProfile();
- auto person2 = person;
- puts("");
- person2.printPersonProfile();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment