-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook.cpp
More file actions
87 lines (69 loc) · 1.98 KB
/
Copy pathbook.cpp
File metadata and controls
87 lines (69 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "book.hpp"
#include <stdexcept> // invalid_argument
Book::Book(const std::string &title,
const std::string &content,
Genre genre,
Publisher publisher,
const std::vector<Author> &authors) {
// валидация аргументов
if (title.empty()) {
throw std::invalid_argument("Book::title cannot be empty");
}
if (content.empty()) {
throw std::invalid_argument(
"Book::content cannot be empty");
}
if (authors.empty()) {
throw std::invalid_argument("Book::authors cannot be empty");
}
// Tip 1: остались слезы на щеках, осталось лишь инициализировать поля ...
title_ = title;
content_ = content;
genre_ = genre;
publisher_ = publisher;
authors_ = authors;
}
// 2. реализуйте метод ...
bool Book::AddAuthor(const Author &author) {
// здесь мог бы быть ваш сногсшибающий код ...
// Tip 1: для поиска дубликатов можно использовать цикл for-each
for(int i : authors_){
if(i == author) return false;
}
authors_.push_back(author);
return true;
}
// РЕАЛИЗОВАНО
const std::string &Book::GetTitle() const {
return title_;
}
const std::string &Book::GetContent() const {
return content_;
}
Genre Book::GetGenre() const {
return genre_;
}
Publisher Book::GetPublisher() const {
return publisher_;
}
const std::vector<Author> &Book::GetAuthors() const {
return authors_;
}
void Book::SetTitle(const std::string &title) {
if (title.empty()) {
throw std::invalid_argument("Book::title cannot be empty");
}
title_ = title;
}
void Book::SetContent(const std::string &content) {
if (content.empty()) {
throw std::invalid_argument("Book::content cannot be empty");
}
content_ = content;
}
void Book::SetGenre(Genre genre) {
genre_ = genre;
}
void Book::SetPublisher(Publisher publisher) {
publisher_ = publisher;
}