-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookList.cpp
More file actions
115 lines (105 loc) · 2.04 KB
/
BookList.cpp
File metadata and controls
115 lines (105 loc) · 2.04 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
#include "BookList.h"
BookList::BookList()
{
capacity = 0;
booksCount = 0;
}
BookList::BookList(int capacity)
{
this->capacity = capacity;
books = new Book[capacity];
booksCount = 0;
Book::count = Book::count - capacity;
}
void BookList::addBook(const Book &book)
{
if (capacity >= booksCount)
{
books[booksCount] = book;
booksCount++;
}
}
Book *BookList::searchBook(string name)
{
for (int i = 0; i < booksCount; i++)
{
if (books[i].getTitle() == name)
{
return &books[i];
}
}
return nullptr;
}
Book *BookList::searchBook(int id)
{
for (int i = 0; i < booksCount; i++)
{
if (books[i].getId() == id)
{
return &books[i];
}
}
return nullptr;
}
void BookList::deleteBook(int id)
{
for (int i = 0; i < booksCount; i++)
{
if (books[i].getId() == id)
{
while (i < booksCount - 1)
{
books[i] = books[i + 1];
// i won't change the id
i++;
}
booksCount--;
break;
}
}
}
ostream &operator<<(ostream &output, const BookList &bookList)
{
for (int i = 0; i < bookList.booksCount; i++)
{
output << bookList.books[i];
}
return output;
}
BookList::~BookList()
{
delete[] books;
}
Book BookList::getTheHighestRatedBook()
{
int index = 0;
double highest = -1;
for (int i = 0; i < booksCount; i++)
{
if (books[i].getAverageRating() < highest)
{
highest = books[i].getAverageRating();
index = i;
}
}
return books[index];
}
void BookList::getBooksForUser(const User &user)
{
for (int i = 0; i < booksCount; i++)
{
if (books[i].getAuthor() == user)
{
cout << books[i];
}
}
}
Book &BookList::operator[](int index)
{
if (index < 0 || index >= capacity)
{
cout << "wrong index" << endl;
exit(1);
}
return books[index];
}