main.cpp
1 /* ************************************************************************** */ 2 /* */ 3 /* ::: :::::::: */ 4 /* main.cpp :+: :+: :+: */ 5 /* +:+ +:+ +:+ */ 6 /* By: gychoi <gychoi@student.42seoul.kr> +#+ +:+ +#+ */ 7 /* +#+#+#+#+#+ +#+ */ 8 /* Created: 2023/11/10 22:20:33 by gychoi #+# #+# */ 9 /* Updated: 2023/12/11 23:05:48 by gychoi ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 #include <sstream> 14 #include "Bureaucrat.hpp" 15 #include "Intern.hpp" 16 17 void setBureaucratInfo(std::string& bureaucratName, int& bureaucratGrade) 18 { 19 std::string bureaucratNameInput; 20 std::string bureaucratGradeInput; 21 22 while (true) 23 { 24 std::cout << "Enter the bureaucrat name: "; 25 std::getline(std::cin, bureaucratNameInput); 26 27 if (bureaucratNameInput.empty()) 28 { 29 std::cout << "Invalid input. Name cannot be empty.\n"; 30 } 31 else 32 { 33 bureaucratName = bureaucratNameInput; 34 break; 35 } 36 } 37 38 while (true) 39 { 40 std::cout << "Enter the bureaucrat grade (1 to 150): "; 41 std::getline(std::cin, bureaucratGradeInput); 42 43 bool isAllDigits = true; 44 45 for (size_t i = 0; i < bureaucratGradeInput.length(); ++i) 46 { 47 if (!std::isdigit(bureaucratGradeInput[i])) { 48 isAllDigits = false; 49 break; 50 } 51 } 52 53 std::istringstream iss(bureaucratGradeInput); 54 55 if (!isAllDigits || bureaucratGradeInput.empty() 56 || (!(iss >> bureaucratGrade))) 57 { 58 std::cout << "Invalid input. Please enter a valid integer.\n"; 59 } 60 else 61 { 62 break; 63 } 64 } 65 } 66 67 void setFormInfo(std::string& formName, std::string const& formType) 68 { 69 std::string formNameInput; 70 71 while (true) 72 { 73 std::cout << "Enter the " << formType << " name: "; 74 std::getline(std::cin, formNameInput); 75 76 if (formNameInput.empty()) 77 { 78 std::cout << "Invalid input. Name cannot be empty.\n"; 79 } 80 else 81 { 82 formName = formNameInput; 83 break; 84 } 85 } 86 } 87 88 bool askConfirm(std::string const& message) 89 { 90 std::string response; 91 92 while (true) 93 { 94 std::cout << message << ": "; 95 std::getline(std::cin, response); 96 97 if (response == "y" || response == "Y" || response == "yes") 98 { 99 return true; 100 } 101 else if (response == "n" || response == "N" || response == "no") 102 { 103 return false; 104 } 105 else 106 { 107 continue; 108 } 109 } 110 return false; 111 } 112 113 int main() 114 { 115 std::string bureaucratName; 116 int bureaucratGrade; 117 std::string formName; 118 119 try 120 { 121 Intern intern; 122 123 std::cout << "INFO: Creating ShrubberyCreationForm..." << std::endl; 124 setFormInfo(formName, "ShrubberyCreationForm"); 125 AForm* sForm = intern.makeForm("shrubbery creation", formName); 126 std::cout << *sForm << '\n' << std::endl; 127 128 std::cout << "INFO: Creating RobotomyRequestForm..." << std::endl; 129 setFormInfo(formName, "RobotomyRequestForm"); 130 AForm* rForm = intern.makeForm("robotomy request", formName); 131 std::cout << *rForm << '\n' << std::endl; 132 133 std::cout << "INFO: Creating PresidentialPardonForm..." << std::endl; 134 setFormInfo(formName, "PresidentialPardonForm"); 135 AForm* pForm = intern.makeForm("presidential pardon", formName); 136 std::cout << *pForm << '\n' << std::endl; 137 138 std::cout << "INFO: Creating Bureaucrat..." << std::endl; 139 setBureaucratInfo(bureaucratName, bureaucratGrade); 140 Bureaucrat bureaucrat(bureaucratName, bureaucratGrade); 141 std::cout << bureaucrat << '\n' << std::endl; 142 143 if (askConfirm("INFO: Do you want to sign ShrubberyCreationForm?")) 144 { 145 bureaucrat.signForm(*sForm); 146 } 147 else 148 { 149 // skipped 150 } 151 std::cout << *sForm << '\n' << std::endl; 152 153 if (askConfirm("INFO: Do you want to sign RobotomyCreationForm?")) 154 { 155 bureaucrat.signForm(*rForm); 156 } 157 else 158 { 159 // skipped 160 } 161 std::cout << *rForm << '\n' << std::endl; 162 163 if (askConfirm("INFO: Do you want to sign PresidentialPardonForm?")) 164 { 165 bureaucrat.signForm(*pForm); 166 } 167 else 168 { 169 // skippled 170 } 171 std::cout << *pForm << '\n' << std::endl; 172 173 std::cout << "INFO: Executing ShrubberyCreationForm..." << std::endl; 174 bureaucrat.executeForm(*sForm); 175 176 std::cout << std::endl; 177 178 std::cout << "INFO: Executing RobotomyCreationForm..." << std::endl; 179 bureaucrat.executeForm(*rForm); 180 181 std::cout << std::endl; 182 183 std::cout << "INFO: Executing PresidentialPardonForm..." << std::endl; 184 bureaucrat.executeForm(*pForm); 185 186 std::cout << std::endl; 187 188 std::cout << "INFO: Creating NuclearLaunchForm..." << std::endl; 189 AForm* nForm = intern.makeForm("nuclear launch", "Big Red Button"); 190 191 delete sForm; 192 delete rForm; 193 delete pForm; 194 delete nForm; 195 } 196 catch (std::exception & e) 197 { 198 std::cout << "Caught exception: " << e.what() << std::endl; 199 } 200 201 return 0; 202 }