|
1 | 1 | #include <iostream> |
2 | | - |
3 | | -#include "./Bureaucrat.hpp" |
| 2 | +#include "Bureaucrat.hpp" |
4 | 3 |
|
5 | 4 | int main() { |
6 | | - try { |
7 | | - Bureaucrat b("Mark", 2); |
8 | | - std::cout << b.getName() << "\'s grade test:" << std::endl; |
9 | | - std::cout << "Incrementing grade..." << std::endl; |
10 | | - |
11 | | - b.incrementGrade(); |
12 | | - std::cout << b.getGrade() << std::endl; |
13 | | - |
14 | | - std::cout << "Decrementing grade..." << std::endl; |
15 | | - for (int i = 0; i < 149; i++) { |
16 | | - std::cout << b.getGrade() << " -> "; |
17 | | - b.decrementGrade(); |
18 | | - } |
19 | | - std::cout << std::endl; |
20 | | - |
21 | | - Bureaucrat c("Geert", 145); |
22 | | - std::cout << c.getName() << "\'s grade test:" << std::endl; |
23 | | - std::cout << "Decrementing grade..." << std::endl; |
24 | | - for (int i = 0; i < 10; i++) { |
25 | | - std::cout << c.getGrade() << " -> "; |
26 | | - c.decrementGrade(); |
27 | | - } |
28 | | - std::cout << std::endl; |
29 | | - } catch (std::exception& e) { |
30 | | - std::cout << e.what() << std::endl; |
31 | | - } |
| 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; |
32 | 62 | } |
0 commit comments