CPP05 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
main.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* main.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/06/30 12:43:18 by kamitsui #+# #+# */
9/* Updated: 2025/06/30 12:50:30 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
21#include "AForm.hpp"
22#include "Bureaucrat.hpp"
23#include "Intern.hpp"
27#include <cstdlib>
28#include <ctime>
29#include <iostream>
30
31void print_header(const std::string &title) { std::cout << "\n\033[1;33m--- " << title << " ---\033[0m" << std::endl; }
32
33int main() {
34 // Seed the random number generator
35 srand(time(NULL));
36
37 // Create an Intern and a high-level Bureaucrat to test the forms
38 Intern someRandomIntern;
39 Bureaucrat boss("Zaphod", 1);
40 AForm *form;
41
42 // --- Test 1: Create a Robotomy Request Form ---
43 print_header("Test 1: Create Robotomy Request");
44 try {
45 form = someRandomIntern.makeForm("robotomy request", "Bender");
46 if (form) {
47 std::cout << boss << std::endl;
48 std::cout << *form << std::endl;
49 boss.signForm(*form);
50 boss.executeForm(*form);
51 delete form; // Clean up the allocated memory
52 }
53 } catch (const std::exception &e) {
54 std::cerr << e.what() << std::endl;
55 }
56
57 // --- Test 2: Create a Shrubbery Creation Form ---
58 print_header("Test 2: Create Shrubbery Creation");
59 try {
60 form = someRandomIntern.makeForm("shrubbery creation", "home");
61 if (form) {
62 std::cout << boss << std::endl;
63 std::cout << *form << std::endl;
64 boss.signForm(*form);
65 boss.executeForm(*form);
66 delete form;
67 }
68 } catch (const std::exception &e) {
69 std::cerr << e.what() << std::endl;
70 }
71
72 // --- Test 3: Create a Presidential Pardon Form ---
73 print_header("Test 3: Create Presidential Pardon");
74 try {
75 form = someRandomIntern.makeForm("presidential pardon", "Ford Prefect");
76 if (form) {
77 std::cout << boss << std::endl;
78 std::cout << *form << std::endl;
79 boss.signForm(*form);
80 boss.executeForm(*form);
81 delete form;
82 }
83 } catch (const std::exception &e) {
84 std::cerr << e.what() << std::endl;
85 }
86
87 // --- Test 4: Attempt to create an unknown form ---
88 print_header("Test 4: Create Unknown Form");
89 try {
90 form = someRandomIntern.makeForm("lunch request", "the kitchen");
91 if (form) { // This block should not be reached
92 std::cout << "This should not print." << std::endl;
93 delete form;
94 }
95 } catch (const std::exception &e) {
96 std::cerr << "\033[1;31mCaught expected exception: " << e.what() << "\033[0m" << std::endl;
97 }
98
99 print_header("All tests complete.");
100 return 0;
101}
Defines the Intern class, which can create forms.
An abstract base class for forms.
Definition AForm.hpp:34
Represents a bureaucrat with a name and a grade.
void signForm(Form &form)
Attempts to sign a form. Calls the form's beSigned method and prints the result.
void executeForm(AForm const &form)
Attempts to execute a form. Calls the form's execute method and prints the result.
AForm * makeForm(const std::string &formName, const std::string &formTarget)
Definition Intern.cpp:45
T endl(T... args)
void print_header(const std::string &title)
Prints a formatted header to the console.
Definition main.cpp:28
int main()
Main entry point of the program.
Definition main.cpp:34
Defines the abstract base class AForm for all specific forms.
Defines the Bureaucrat class, updated to execute AForms.
Defines the PresidentialPardonForm class.
Defines the RobotomyRequestForm class.
Defines the ShrubberyCreationForm class.
T what(T... args)