|
1 | 1 | #include <iostream> |
| 2 | + |
2 | 3 | #include "Bureaucrat.hpp" |
3 | 4 |
|
4 | 5 | int main() { |
5 | | - try { |
6 | | - // Testing the constructor within valid range |
7 | | - Bureaucrat bureaucratA("Alice", 2); |
8 | | - std::cout << "Created: " << bureaucratA << std::endl; |
9 | | - |
10 | | - // Use incrementGrade() - should go from grade 2 to grade 1, which is valid |
11 | | - std::cout << "Incrementing " << bureaucratA.getName() << "'s grade..." << std::endl; |
12 | | - bureaucratA.incrementGrade(); |
13 | | - std::cout << bureaucratA << std::endl; |
14 | | - |
15 | | - // This increment attempt should throw GradeTooHighException (grade 1 -> would become 0) |
16 | | - std::cout << "Incrementing " << bureaucratA.getName() << "'s grade again..." << std::endl; |
17 | | - bureaucratA.incrementGrade(); // This will throw |
18 | | - std::cout << bureaucratA << std::endl; // Not reached |
19 | | - } |
20 | | - catch (std::exception &e) { |
21 | | - std::cerr << "Exception caught: " << e.what() << std::endl; |
22 | | - } |
23 | | - |
24 | | - std::cout << "-------------------------------------------" << std::endl; |
25 | | - |
26 | | - try { |
27 | | - // Testing bureaucrat creation with invalid grade |
28 | | - std::cout << "Attempting to create bureaucrat with invalid grade..." << std::endl; |
29 | | - Bureaucrat bureaucratB("Bob", 151); // This should throw GradeTooLowException |
30 | | - std::cout << bureaucratB << std::endl; // Not reached |
31 | | - } |
32 | | - catch (std::exception &e) { |
33 | | - std::cerr << "Exception caught: " << e.what() << std::endl; |
34 | | - } |
35 | | - |
36 | | - std::cout << "-------------------------------------------" << std::endl; |
37 | | - |
38 | | - try { |
39 | | - // Valid grade |
40 | | - Bureaucrat bureaucratC("Charlie", 149); |
41 | | - std::cout << "Created: " << bureaucratC << std::endl; |
42 | | - |
43 | | - // Decrement multiple times |
44 | | - std::cout << "Decrementing " << bureaucratC.getName() << "'s grade multiple times..." << std::endl; |
45 | | - for (int i = 0; i < 3; ++i) { |
46 | | - bureaucratC.decrementGrade(); |
47 | | - std::cout << bureaucratC << std::endl; |
48 | | - } |
49 | | - |
50 | | - // Attempting to decrement below grade 150 --> next will be 151 |
51 | | - std::cout << "Trying to decrement " << bureaucratC.getName() << "'s grade beyond the limit..." << std::endl; |
52 | | - while (true) { |
53 | | - bureaucratC.decrementGrade(); // Will throw when it goes beyond 150 |
54 | | - std::cout << bureaucratC << std::endl; |
55 | | - } |
56 | | - } |
57 | | - catch (std::exception &e) { |
58 | | - std::cerr << "Exception caught: " << e.what() << std::endl; |
59 | | - } |
60 | | - |
61 | | - return 0; |
| 6 | + try { |
| 7 | + // Testing the constructor within valid range |
| 8 | + Bureaucrat bureaucratA("Alice", 2); |
| 9 | + std::cout << "Created: " << bureaucratA << std::endl; |
| 10 | + |
| 11 | + // Use incrementGrade() - should go from grade 2 to grade 1, which is valid |
| 12 | + std::cout << "Incrementing " << bureaucratA.getName() << "'s grade..." << std::endl; |
| 13 | + bureaucratA.incrementGrade(); |
| 14 | + std::cout << bureaucratA << std::endl; |
| 15 | + |
| 16 | + // This increment attempt should throw GradeTooHighException (grade 1 -> would become 0) |
| 17 | + std::cout << "Incrementing " << bureaucratA.getName() << "'s grade again..." << std::endl; |
| 18 | + bureaucratA.incrementGrade(); // This will throw |
| 19 | + std::cout << bureaucratA << std::endl; // Not reached |
| 20 | + } catch (std::exception &e) { |
| 21 | + std::cerr << "Exception caught: " << e.what() << std::endl; |
| 22 | + } |
| 23 | + |
| 24 | + std::cout << "-------------------------------------------" << std::endl; |
| 25 | + |
| 26 | + try { |
| 27 | + // Testing bureaucrat creation with invalid grade |
| 28 | + std::cout << "Attempting to create bureaucrat with invalid grade..." << std::endl; |
| 29 | + Bureaucrat bureaucratB("Bob", 151); // This should throw GradeTooLowException |
| 30 | + std::cout << bureaucratB << std::endl; // Not reached |
| 31 | + } catch (std::exception &e) { |
| 32 | + std::cerr << "Exception caught: " << e.what() << std::endl; |
| 33 | + } |
| 34 | + |
| 35 | + std::cout << "-------------------------------------------" << std::endl; |
| 36 | + |
| 37 | + try { |
| 38 | + // Valid grade |
| 39 | + Bureaucrat bureaucratC("Charlie", 149); |
| 40 | + std::cout << "Created: " << bureaucratC << std::endl; |
| 41 | + |
| 42 | + // Decrement multiple times |
| 43 | + std::cout << "Decrementing " << bureaucratC.getName() << "'s grade multiple times..." << std::endl; |
| 44 | + for (int i = 0; i < 3; ++i) { |
| 45 | + bureaucratC.decrementGrade(); |
| 46 | + std::cout << bureaucratC << std::endl; |
| 47 | + } |
| 48 | + |
| 49 | + // Attempting to decrement below grade 150 --> next will be 151 |
| 50 | + std::cout << "Trying to decrement " << bureaucratC.getName() << "'s grade beyond the limit..." << std::endl; |
| 51 | + while (true) { |
| 52 | + bureaucratC.decrementGrade(); // Will throw when it goes beyond 150 |
| 53 | + std::cout << bureaucratC << std::endl; |
| 54 | + } |
| 55 | + } catch (std::exception &e) { |
| 56 | + std::cerr << "Exception caught: " << e.what() << std::endl; |
| 57 | + } |
| 58 | + |
| 59 | + return 0; |
62 | 60 | } |
0 commit comments