-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBook.cpp
More file actions
162 lines (146 loc) · 3.69 KB
/
Book.cpp
File metadata and controls
162 lines (146 loc) · 3.69 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <iostream>
#include <cstring>
#include <fstream>
#include <string>
#include <vector>
#include <stdexcept>
#include "Book.hpp"
#include "ISBN.hpp"
#include "Person.hpp"
Book::Book(ISBN* isbn_num, std::string title_string, std::string author_string)
{
isbn = isbn_num;
title = title_string;
author = author_string;
reader = nullptr;
}
Book::~Book()
{
delete isbn;
delete reader;
for(Book* book : booksRegistry)
{
delete book;
}
}
ISBN* Book::getIsbn() const
{
return isbn;
}
std::string Book::getTitle() const
{
return title;
}
std::string Book::getAuthor() const
{
return author;
}
Person* Book::getReader() const
{
return reader;
}
Book* Book::getBooksRegistry()
{
for(Book* book : booksRegistry)
{
return book;
}
}
void Book::setReader(Person* person)
{
reader = person;
}
void Book::removeReader()
{
reader = nullptr;
}
Book* Book::createBook(ISBN* isbn, std::string title, std::string author)
{
std::string str = (*isbn).getIsbn();
if(ISBN::isValid(str))
{
for(Book* book : booksRegistry)
{
if (book->getIsbn() == isbn)
{
std::cout << "A book with this ISBN already exists" << std::endl;
}
}
Book* newBook = new Book(isbn, title, author);
booksRegistry.push_back(newBook);
return newBook;
}
else
{
std::cout << "The ISBN is not valid! A book can not be created!" << std::endl;
}
}
void Book::removeBook(Book* book)
{
if (!book)
{
std::cout << "Invalid book!" << std::endl;
return;
}
for(Book* bookToBeFound : booksRegistry)
{
if ((*bookToBeFound).getIsbn() == (*book).getIsbn() && (*bookToBeFound).getTitle() == (*book).getTitle() && (*bookToBeFound).getAuthor() == (*book).getAuthor())
{
if ((*bookToBeFound).isBorrowed())
{
(*bookToBeFound).setReader(nullptr);
booksRegistry.erase(std::remove(booksRegistry.begin(), booksRegistry.end(), book), booksRegistry.end());
delete book;
book = nullptr;
break;
}
else
{
booksRegistry.erase(std::remove(booksRegistry.begin(), booksRegistry.end(), book), booksRegistry.end());
delete book;
book = nullptr;
break;
}
}
}
}
void Book::showAllBooks()
{
for(Book* book : booksRegistry)
{
std::cout << book << std::endl;
}
}
Book* Book::findBook(ISBN* isbn)
{
std::string str = (*isbn).getIsbn();
if (ISBN::isValid(str))
{
for(Book* bookToBeFound : booksRegistry)
{
if (bookToBeFound->getIsbn() == isbn)
{
return bookToBeFound;
}
else
{
return nullptr;
}
}
}
else
{
std::cout << "The certain ISBN is not valid!" << std::endl;
}
}
bool Book::isBorrowed() const
{
if (reader != nullptr)
{
return true;
}
else
{
return false;
}
}