-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.h
More file actions
141 lines (126 loc) · 2.56 KB
/
Library.h
File metadata and controls
141 lines (126 loc) · 2.56 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
#pragma once
#include <vector>
#include "Student.h"
#include "Book.h"
#include <iostream>
using namespace std;
class Library
{
private:
vector<Student> students;
vector<Book> books;
vector<pair<string, string>> bookAssignment;
int getIndex(pair<string, string> obj);
public:
void addBook(Book book);
void addStudent(Student student);
bool searchBook(string title);
bool searchBook(Book book);
bool searchStudent(string name);
bool assignBookToStudent(string stdName, string bookName);
bool returnBook(string title);
int getBookIdByTitle(string title);
void occupyBook(string title);
void printStudentNames();
void printBooksNames();
};
int Library::getIndex(pair<string, string> obj)
{
for (int i = 0; i < bookAssignment.size(); i++) {
if (bookAssignment.at(i).first == obj.first && bookAssignment.at(i).second == obj.second) {
return i;
}
}
return -1;
}
void Library::addBook(Book book)
{
books.push_back(book);
}
void Library::addStudent(Student student)
{
students.push_back(student);
}
bool Library::searchBook(string title)
{
for (Book& book : books) {
if (book.getTitle() == title && book.getIsAvailable()) {
return true;
}
}
return false;
}
bool Library::searchBook(Book book)
{
for (Book& b : books) {
if (book == b) {
return true;
}
return false;
}
return false;
}
bool Library::searchStudent(string name)
{
for (Student s : students) {
if (s.getName() == name) {
return true;
}
}
return false;
}
bool Library::assignBookToStudent(string stdName, string bookName)
{
if (searchBook(bookName) && searchStudent(stdName) && getBookIdByTitle(bookName) ){
occupyBook(bookName);
bookAssignment.push_back(make_pair(stdName, bookName));
return true;
}
return false;
}
bool Library::returnBook(string title)
{
for (auto pair : bookAssignment) {
if (pair.second == title) {
for (Book& book : books) {
if (title == book.getTitle()) {
book.setIsAvailable(true);
bookAssignment.erase(bookAssignment.begin() + getIndex(pair));
return true;
}
}
}
}
return false;
}
int Library::getBookIdByTitle(string title)
{
for (Book& b : books) {
if (b.getTitle() == title) {
return b.getBookId();
}
}
return -1;
}
void Library::occupyBook(string title)
{
for (Book& book : books) {
if (book.getTitle() == title) {
book.setIsAvailable(false);
}
}
}
void Library::printStudentNames()
{
cout << "test" << endl;
for (auto s : students) {
cout << s.getName() << endl;
}
}
void Library::printBooksNames()
{
cout << "test2" << endl;
for (auto book : books) {
cout << book.getTitle() << endl;
}
}