-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inventory.cpp
142 lines (113 loc) · 2.96 KB
/
Inventory.cpp
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
#include "Inventory.h"
#include <vector>
#include <fstream>
#include <string>
int Inventory::NumberOfBooks()
{
return Inventory::Books.size();
}
Book Inventory::GetBookByIndex(int index)
{
return Inventory::Books[index];
}
void Inventory::AddBook(Book book)
{
int nextBookId = 0;
if (Books.size() > 0)
nextBookId = Books.back().Id + 1;
book.Id = nextBookId;
Inventory::Books.push_back(book);
std::ofstream oFile("books.txt", std::ios_base::app);
oFile << book.ObtainBookFileData() << std::endl;
oFile.close();
}
void Inventory::LoadBooks()
{
Books.clear();
std::ifstream inFile("books.txt");
std::string bookData[4];
std::string bookLine;
while (std::getline(inFile, bookLine))
{
size_t nextIndex = bookLine.find('|');
bookData[0] = bookLine.substr(0, nextIndex);
size_t prevIndex = nextIndex;
nextIndex = bookLine.find('|', prevIndex + 1);
bookData[1] = bookLine.substr(prevIndex+1, nextIndex - prevIndex - 1);
prevIndex = nextIndex;
nextIndex = bookLine.find('|', prevIndex + 1);
bookData[2] = bookLine.substr(prevIndex + 1, nextIndex - prevIndex - 1);
prevIndex = nextIndex;
nextIndex = bookLine.find('|', prevIndex + 1);
bookData[3] = bookLine.substr(prevIndex + 1, nextIndex - prevIndex - 1);
Book loadedBook(bookData[1], bookData[2]);
loadedBook.Id = stoi(bookData[0]);
loadedBook.InOrOutCheck(stoi(bookData[3]));
Books.push_back(loadedBook);
}
}
void Inventory::RemoveBook(std::string booktitle)
{
std::vector<Book>::iterator it = std::find(Inventory::Books.begin(), Inventory::Books.end(), Book(booktitle, ""));
if (it != Inventory::Books.end())
{
Inventory::Books.erase(it);
}
}
int Inventory::FindBookByTitle(std::string booktitle)
{
std::vector<Book>::iterator it = std::find(Inventory::Books.begin(), Inventory::Books.end(), Book(booktitle, ""));
if (it == Inventory::Books.end())
{
return -1;
}
int index = it - Inventory::Books.begin();
return index;
}
CheckInOrOutResult Inventory::CheckInOrOutBook(std::string booktitle, bool checkOut)
{
int foundBookIndex = FindBookByTitle(booktitle);
if (foundBookIndex < 0)
{
return CheckInOrOutResult::Book_Not_Found;
}
else if (checkOut == Books[foundBookIndex].IsCheckedOut())
{
if (checkOut)
{
return CheckInOrOutResult::Already_Checked_Out;
}
else
{
return CheckInOrOutResult::Already_Checked_In;
}
}
Books[foundBookIndex].InOrOutCheck(checkOut);
std::ofstream oFile("books.txt");
for (int i = 0; i < Books.size(); i++)
{
oFile << Books[i].ObtainBookFileData() << std::endl;
}
return CheckInOrOutResult::Successful;
}
void Inventory::ShowAllBooks()
{
std::cout << "\nID\tTitle\tAuthor" << std::endl;
for (int i = 0; i < NumberOfBooks(); i++)
{
Books[i].ShowBook();
}
std::cout << std::endl;
}
void Inventory::ShowCheckedOutBooks()
{
std::cout << "\nID\tBook Title\tAuthor Name " << std::endl;
for (int i = 0; i < NumberOfBooks(); i++)
{
if (GetBookByIndex(i).IsCheckedOut())
{
Books[i].ShowBook();
}
}
std::cout << std::endl;
}