Skip to content

Commit 0d4bff7

Browse files
committed
Add Bureaucrat and Form classes with basic functionality and error handling
1 parent 3519110 commit 0d4bff7

File tree

13 files changed

+391
-31
lines changed

13 files changed

+391
-31
lines changed

03/ex02/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
claptrap

03/ex02/claptrap

-45.4 KB
Binary file not shown.

05/ex00/Bureaucrat.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "./Bureaucrat.hpp"
22

3+
Bureaucrat::Bureaucrat() : _name("default"), _grade(150) {}
4+
35
Bureaucrat::Bureaucrat(std::string name, int grade) : _name(name) {
46
if (grade < 1) throw Bureaucrat::GradeTooHighException();
57
if (grade > 150) throw Bureaucrat::GradeTooLowException();
@@ -8,10 +10,12 @@ Bureaucrat::Bureaucrat(std::string name, int grade) : _name(name) {
810

911
Bureaucrat::~Bureaucrat() {}
1012

11-
Bureaucrat::Bureaucrat(const Bureaucrat& other) : _name(other._name) { *this = other; }
13+
Bureaucrat::Bureaucrat(const Bureaucrat& other) : _name(other._name), _grade(other._grade) {}
1214

1315
Bureaucrat& Bureaucrat::operator=(const Bureaucrat& other) {
14-
_grade = other._grade;
16+
if (this != &other) {
17+
_grade = other._grade;
18+
}
1519
return *this;
1620
}
1721

@@ -32,3 +36,8 @@ void Bureaucrat::decrementGrade() {
3236
const char* Bureaucrat::GradeTooHighException::what() const throw() { return "Grade is too high!"; }
3337

3438
const char* Bureaucrat::GradeTooLowException::what() const throw() { return "Grade is too low!"; }
39+
40+
std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat) {
41+
os << bureaucrat.getName() << ", bureaucrat grade " << bureaucrat.getGrade();
42+
return os;
43+
}

05/ex00/Bureaucrat.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
#include <iostream>
44
#include <string>
5-
// !TODO: Add overload!
65

76
class Bureaucrat {
87
private:
98
std::string const _name;
109
int _grade;
1110

1211
public:
12+
Bureaucrat();
1313
Bureaucrat(std::string name, int grade);
1414
~Bureaucrat();
1515
Bureaucrat(const Bureaucrat& other);
@@ -29,3 +29,5 @@ class Bureaucrat {
2929
virtual const char* what() const throw();
3030
};
3131
};
32+
33+
std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat);

05/ex00/bureaucrat

-42.7 KB
Binary file not shown.

05/ex00/main.cpp

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,62 @@
11
#include <iostream>
2-
3-
#include "./Bureaucrat.hpp"
2+
#include "Bureaucrat.hpp"
43

54
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;
3262
}

05/ex01/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bureaucrat

05/ex01/Bureaucrat.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "./Bureaucrat.hpp"
2+
3+
#include "Form.hpp"
4+
5+
Bureaucrat::Bureaucrat() : _name("default"), _grade(150) {}
6+
7+
Bureaucrat::Bureaucrat(std::string name, int grade) : _name(name) {
8+
if (grade < 1) throw Bureaucrat::GradeTooHighException();
9+
if (grade > 150) throw Bureaucrat::GradeTooLowException();
10+
_grade = grade;
11+
}
12+
13+
Bureaucrat::~Bureaucrat() {}
14+
15+
Bureaucrat::Bureaucrat(const Bureaucrat& other) : _name(other._name), _grade(other._grade) {}
16+
17+
Bureaucrat& Bureaucrat::operator=(const Bureaucrat& other) {
18+
if (this != &other) {
19+
_grade = other._grade;
20+
}
21+
return *this;
22+
}
23+
24+
std::string Bureaucrat::getName() const { return _name; }
25+
26+
int Bureaucrat::getGrade() const { return _grade; }
27+
28+
void Bureaucrat::incrementGrade() {
29+
if (_grade == 1) throw Bureaucrat::GradeTooHighException();
30+
_grade--;
31+
}
32+
33+
void Bureaucrat::decrementGrade() {
34+
if (_grade == 150) throw Bureaucrat::GradeTooLowException();
35+
_grade++;
36+
}
37+
38+
const char* Bureaucrat::GradeTooHighException::what() const throw() { return "Grade is too high!"; }
39+
40+
const char* Bureaucrat::GradeTooLowException::what() const throw() { return "Grade is too low!"; }
41+
42+
void Bureaucrat::signForm(Form& form) {
43+
try {
44+
form.beSigned(*this);
45+
std::cout << _name << " signed " << form.getName() << std::endl;
46+
} catch (const std::exception& e) {
47+
std::cout << _name << " couldn't sign " << form.getName() << " because " << e.what() << std::endl;
48+
}
49+
}
50+
51+
std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat) {
52+
os << bureaucrat.getName() << ", bureaucrat grade " << bureaucrat.getGrade();
53+
return os;
54+
}

05/ex01/Bureaucrat.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
3+
#include <iostream>
4+
#include <string>
5+
6+
#include "Form.hpp"
7+
8+
class Bureaucrat {
9+
private:
10+
std::string const _name;
11+
int _grade;
12+
13+
public:
14+
Bureaucrat();
15+
Bureaucrat(std::string name, int grade);
16+
~Bureaucrat();
17+
Bureaucrat(const Bureaucrat& other);
18+
Bureaucrat& operator=(const Bureaucrat& other);
19+
20+
std::string getName() const;
21+
int getGrade() const;
22+
void incrementGrade();
23+
void decrementGrade();
24+
void signForm(Form& form);
25+
26+
class GradeTooHighException : public std::exception {
27+
public:
28+
virtual const char* what() const throw();
29+
};
30+
class GradeTooLowException : public std::exception {
31+
public:
32+
virtual const char* what() const throw();
33+
};
34+
};
35+
36+
std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat);

05/ex01/Form.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "Form.hpp"
2+
3+
#include "Bureaucrat.hpp"
4+
5+
Form::Form() : _name("default"), _signed(false), _gradeToSign(150), _gradeToExecute(150) {}
6+
7+
Form::Form(std::string name, int gradeToSign, int gradeToExecute)
8+
: _name(name), _signed(false), _gradeToSign(gradeToSign), _gradeToExecute(gradeToExecute) {
9+
if (gradeToSign < 1 || gradeToExecute < 1) throw Form::GradeTooHighException();
10+
if (gradeToSign > 150 || gradeToExecute > 150) throw Form::GradeTooLowException();
11+
}
12+
13+
Form::~Form() {}
14+
15+
Form::Form(const Form& other)
16+
: _name(other._name),
17+
_signed(other._signed),
18+
_gradeToSign(other._gradeToSign),
19+
_gradeToExecute(other._gradeToExecute) {}
20+
21+
Form& Form::operator=(const Form& other) {
22+
if (this != &other) _signed = other._signed;
23+
return *this;
24+
}
25+
26+
std::string Form::getName() const { return _name; }
27+
bool Form::isSigned() const { return _signed; }
28+
int Form::getGradeToSign() const { return _gradeToSign; }
29+
int Form::getGradeToExecute() const { return _gradeToExecute; }
30+
31+
void Form::beSigned(const Bureaucrat& bureaucrat) {
32+
if (bureaucrat.getGrade() > _gradeToSign) throw Form::GradeTooLowException();
33+
_signed = true;
34+
}
35+
36+
const char* Form::GradeTooHighException::what() const throw() { return "Grade too high"; }
37+
38+
const char* Form::GradeTooLowException::what() const throw() { return "Grade too low"; }
39+
40+
std::ostream& operator<<(std::ostream& os, const Form& form) {
41+
os << form.getName() << " form is " << (form.isSigned() ? "signed" : "not signed")
42+
<< ", grade to sign: " << form.getGradeToSign() << ", grade to execute: " << form.getGradeToExecute();
43+
return os;
44+
}

0 commit comments

Comments
 (0)