-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory.cpp
143 lines (131 loc) · 3.42 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
143
#include"inventory.h"
#include<iostream>
#include<fstream>
#include<sstream>
Inventory::Inventory()
{
std::ifstream file("inventory.csv");
// 检查文件是否成功打开
if (!file.is_open())
{
std::cout << "书籍目录出错,请及时修复!" << std::endl;
return;
}
// 读取文件
std::string line;
while (getline(file, line))
{
std::stringstream ss(line);
std::string title;
std::string author;
std::string ISBN;
std::string press;
std::string type;
std::string borrowimgStatus;
std::string borrower;
std::string borrowDate;
std::vector<std::string> status;
getline(ss, title, ',');
getline(ss, author, ',');
getline(ss, ISBN, ',');
getline(ss, press, ',');
getline(ss, type, ',');
getline(ss, borrowimgStatus, ','); //是否出借
getline(ss, borrower, ','); //借阅人
getline(ss, borrowDate, '\n'); //借阅时间
status = {borrowimgStatus, borrower, borrowDate};
Book book(title, author, ISBN, press, type, status);
books.push_back(book);
}
// 关闭文件
file.close();
}
void Inventory::addBook(Book &book)
{
books.push_back(book);
}
void Inventory::deleteBook(Book &book)
{
for (int index = 0; index < books.size(); index++)
{
if (books[index].getISBN() == book.getISBN())
{
books.erase(books.begin() + index);
break;
}
}
}
void Inventory::modifyBook(Book &book)
{
for (int i = 0; i < books.size(); i++)
{
if (books[i].getISBN() == book.getISBN())
{
books[i] = book;
break;
}
}
}
void Inventory::printBookList()
{
bool flag = 1;
for (int i = 0; i < books.size(); i++)
{
std::cout << "书名:" << books[i].getTitle() << '\t';
std::cout << "作者:" << books[i].getAuthor() << '\t';
std::cout << "ISBN:" << books[i].getISBN() << '\t';
std::cout << "出版社:" << books[i].getPress() << '\t';
std::cout << "类型:" << books[i].getType() << '\t';
std::cout << "借阅状态:" << books[i].getStatus()[0] << '\t';
if(books[i].getStatus()[0] == "已借出")
{
std::cout << "借阅人:" << books[i].getStatus()[1] << '\t';
std::cout << "借阅时间:" << books[i].getStatus()[2] << '\t';
}
flag = 0;
std::cout << std::endl;
}
if(flag)
{
std::cout << "当前没有书籍" << std::endl;
}
}
bool Inventory::isExist(Book &book)
{
for (int i = 0; i < books.size(); i++)
{
if (books[i].getTitle() == book.getTitle())
{
return true;
}
}
return false;
}
Book Inventory::findBook(std::string strToFind, std::string findType)
{
if (findType == "title")
{
for (int i = 0; i < books.size(); i++)
{
if (books[i].getTitle() == strToFind)
{
return books[i];
}
}
}
else if (findType == "ISBN")
{
for (int i = 0; i < books.size(); i++)
{
if (books[i].getISBN() == strToFind)
{
return books[i];
}
}
}
return Book("NULL", "NULL", "NULL", "NULL", "NULL", {"NULL", "NULL", "NULL"});
}
std::vector<Book> Inventory::getBooks()
{
return books;
}