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/11/28 17:05:26 by gychoi           ###   ########.fr       */
 10  /*                                                                            */
 11  /* ************************************************************************** */
 12  
 13  #include <sstream>
 14  #include "Bureaucrat.hpp"
 15  #include "Form.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, int& signGrade, int& executeGrade)
 68  {
 69  	std::string	formNameInput;
 70  	std::string	formSignGradeInput;
 71  	std::string formExecuteGradeInput;
 72  
 73  	while (true)
 74  	{
 75  		std::cout << "Enter the form 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  	while (true)
 90  	{
 91  		std::cout << "Enter the form sign grade (1 to 150): ";
 92  		std::getline(std::cin, formSignGradeInput);
 93  
 94  		bool	isAllDigits = true;
 95  
 96  		for (size_t i = 0; i < formSignGradeInput.length(); ++i)
 97  		{
 98  			if (!std::isdigit(formSignGradeInput[i])) {
 99  				isAllDigits = false;
100  				break;
101  			}
102  		}
103  
104  		std::istringstream	iss(formSignGradeInput);
105  
106  		if (!isAllDigits || formSignGradeInput.empty() 
107  			|| (!(iss >> signGrade)))
108  		{
109  			std::cout << "Invalid input. Please enter a valid integer.\n";
110  		}
111  		else
112  		{
113  			break;
114  		}
115  	}
116  
117  	while (true)
118  	{
119  		std::cout << "Enter the form execution grade (1 to 150): ";
120  		std::getline(std::cin, formExecuteGradeInput);
121  
122  		bool	isAllDigits = true;
123  
124  		for (size_t i = 0; i < formExecuteGradeInput.length(); ++i)
125  		{
126  			if (!std::isdigit(formExecuteGradeInput[i])) {
127  				isAllDigits = false;
128  				break;
129  			}
130  		}
131  
132  		std::istringstream	iss(formExecuteGradeInput);
133  
134  		if (!isAllDigits || formExecuteGradeInput.empty() 
135  			|| (!(iss >> executeGrade)))
136  		{
137  			std::cout << "Invalid input. Please enter a valid integer.\n";
138  		}
139  		else
140  		{
141  			break;
142  		}
143  	}
144  }
145  
146  int	main()
147  {
148  	std::string	bureaucratName;
149  	int			bureaucratGrade;
150  	std::string	formName;
151  	int			formSignGrade;
152  	int			formExecuteGrade;
153  
154  	setBureaucratInfo(bureaucratName, bureaucratGrade);
155  	setFormInfo(formName, formSignGrade, formExecuteGrade);
156  
157  	try
158  	{
159  		Bureaucrat	bureaucrat(bureaucratName, bureaucratGrade);
160  		std::cout << bureaucrat << std::endl;
161  
162  		Form	form(formName, formSignGrade, formExecuteGrade);
163  		std::cout << form << std::endl;
164  
165  		bureaucrat.signForm(form);
166  		std::cout << form << std::endl;
167  	}
168  	catch (std::exception & e)
169  	{
170  		std::cout << "Caught exception: " << e.what() << std::endl;
171  	}
172  
173  	return 0;
174  }