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/04 00:02:01 by gychoi ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 #include <sstream> 14 #include "Bureaucrat.hpp" 15 #include "PresidentialPardonForm.hpp" 16 #include "RobotomyRequestForm.hpp" 17 #include "ShrubberyCreationForm.hpp" 18 19 void setBureaucratInfo(std::string& bureaucratName, int& bureaucratGrade) 20 { 21 std::string bureaucratNameInput; 22 std::string bureaucratGradeInput; 23 24 while (true) 25 { 26 std::cout << "Enter the bureaucrat name: "; 27 std::getline(std::cin, bureaucratNameInput); 28 29 if (bureaucratNameInput.empty()) 30 { 31 std::cout << "Invalid input. Name cannot be empty.\n"; 32 } 33 else 34 { 35 bureaucratName = bureaucratNameInput; 36 break; 37 } 38 } 39 40 while (true) 41 { 42 std::cout << "Enter the bureaucrat grade (1 to 150): "; 43 std::getline(std::cin, bureaucratGradeInput); 44 45 bool isAllDigits = true; 46 47 for (size_t i = 0; i < bureaucratGradeInput.length(); ++i) 48 { 49 if (!std::isdigit(bureaucratGradeInput[i])) { 50 isAllDigits = false; 51 break; 52 } 53 } 54 55 std::istringstream iss(bureaucratGradeInput); 56 57 if (!isAllDigits || bureaucratGradeInput.empty() 58 || (!(iss >> bureaucratGrade))) 59 { 60 std::cout << "Invalid input. Please enter a valid integer.\n"; 61 } 62 else 63 { 64 break; 65 } 66 } 67 } 68 69 void setFormInfo(std::string& formName, std::string const& formType) 70 { 71 std::string formNameInput; 72 73 while (true) 74 { 75 std::cout << "Enter the " << formType << " name: "; 76 std::getline(std::cin, formNameInput); 77 78 if (formNameInput.empty()) 79 { 80 std::cout << "Invalid input. Name cannot be empty.\n"; 81 } 82 else 83 { 84 formName = formNameInput; 85 break; 86 } 87 } 88 } 89 90 bool askConfirm(std::string const& message) 91 { 92 std::string response; 93 94 while (true) 95 { 96 std::cout << message << ": "; 97 std::getline(std::cin, response); 98 99 if (response == "y" || response == "Y" || response == "yes") 100 { 101 return true; 102 } 103 else if (response == "n" || response == "N" || response == "no") 104 { 105 return false; 106 } 107 else 108 { 109 continue; 110 } 111 } 112 return false; 113 } 114 115 int main() 116 { 117 std::string bureaucratName; 118 int bureaucratGrade; 119 std::string formName; 120 121 try 122 { 123 std::cout << "INFO: Creating ShrubberyCreationForm..." << std::endl; 124 setFormInfo(formName, "ShrubberyCreationForm"); 125 ShrubberyCreationForm sForm(formName); 126 std::cout << sForm << '\n' << std::endl; 127 128 std::cout << "INFO: Creating RobotomyRequestForm..." << std::endl; 129 setFormInfo(formName, "RobotomyRequestForm"); 130 RobotomyRequestForm rForm(formName); 131 std::cout << rForm << '\n' << std::endl; 132 133 std::cout << "INFO: Creating PresidentialPardonForm..." << std::endl; 134 setFormInfo(formName, "PresidentialPardonForm"); 135 PresidentialPardonForm pForm(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 catch (std::exception & e) 187 { 188 std::cout << "Caught exception: " << e.what() << std::endl; 189 } 190 191 return 0; 192 }