-
Notifications
You must be signed in to change notification settings - Fork 0
/
studentUser.cpp
193 lines (176 loc) · 6.34 KB
/
studentUser.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include"studentUser.h"
#include<chrono>
#include<fstream>
#include<sstream>
#include<iostream>
StudentUser::StudentUser():User()
{
m_reading = {"NULL", "NULL", "NULL"};
}
StudentUser::StudentUser(std::string name, std::string userName, std::string password):User(name, userName, password)
{
m_reading = {"NULL", "NULL", "NULL"};
}
StudentUser::StudentUser(std::string name, std::string userName, std::string password, std::vector<std::string> reading) : User(name, userName, password)
{
m_reading = reading;
}
void StudentUser::reading()
{
bool flag = 1;
for (int index = 0; index < m_reading.size(); index++)
{
if(m_reading[index] != "NULL")
{
std::cout << m_reading[index] << std::endl;
flag = 0;
}
}
if(flag)
{
std::cout << "当前未借阅任何书籍!" << std::endl;
}
}
std::vector<std::string> StudentUser::getReading()
{
return m_reading;
}
void StudentUser::borrowBook(StudentAccount &studentAccount, Inventory &inventory)
{
if(m_reading[0] != "NULL" && m_reading[1] != "NULL" && m_reading[2] != "NULL")
{
std::cout << "当前已借阅三本书籍,无法借阅更多书籍!" << std::endl;
return;
}
std::string bookName;
std::cout<< "请输入要借阅的书籍名称:";
std::cin >> bookName;
Book bookToBorrow = inventory.findBook(bookName, "title");
if (bookToBorrow.getTitle() != "NULL")
{
// 获取当前时间点
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
// 将当前时间点转换为时间戳
std::time_t timestamp = std::chrono::system_clock::to_time_t(now);
// 将时间戳转换为本地时间
std::tm *local_time = std::localtime(×tamp);
inventory.deleteBook(bookToBorrow);
std::vector<std::string> status = {"已借阅", m_userName, std::asctime(local_time)};
bookToBorrow = Book(bookToBorrow.getTitle(), bookToBorrow.getAuthor(), bookToBorrow.getISBN(), bookToBorrow.getPress(), bookToBorrow.getType(), status);
// 将借阅信息写入库房文件
inventory.addBook(bookToBorrow);
std::ofstream outInventoryFile("inventory.csv");
for (Book &book : inventory.getBooks())
{
outInventoryFile << book.getTitle() << ','
<< book.getAuthor() << ','
<< book.getISBN() << ','
<< book.getPress() << ','
<< book.getType() << ','
<< book.getStatus()[0] << ','
<< book.getStatus()[1] << ','
<< book.getStatus()[2] << '\n';
}
std::ofstream outStudentUserFile("studentUser.csv");
// 将借阅信息写入学生账户文件
for (int index = 0; index < 3; index++)
{
if (m_reading[index] == "NULL")
{
m_reading[index] = bookToBorrow.getTitle();
break;
}
}
studentAccount.setStudentReading(m_userName, bookToBorrow.getTitle());
for (auto student : studentAccount.getUsers())
{
outStudentUserFile << student.getUserName() << ","
<< student.getPassword() << ","
<< student.getName() << ','
<< student.getReading()[0] << ','
<< student.getReading()[1] << ','
<< student.getReading()[2] << ','
<< '\n';
}
std::cout << "借阅成功!" << std::endl;
}
else
{
std::cout << "没有找到该书籍" << std::endl;
}
}
void StudentUser::returnBook(StudentAccount &account, Inventory &inventory)
{
if(m_reading[0] == "NULL" && m_reading[1] == "NULL" && m_reading[2] == "NULL" && m_reading[3] == "NULL")
{
std::cout << "当前未借阅任何书籍!" << std::endl;
return;
}
std::cout << "请选择要归还的书籍名称:";
int choice;
for (int index = 0; index < 3; index++)
{
std::cout << index + 1 << '.' << m_reading[index] << std::endl;
}
std::cin >> choice;
m_reading.erase(m_reading.begin() + choice - 1);
Book bookToReturn = inventory.findBook(m_reading[choice - 1], "title");
inventory.deleteBook(bookToReturn);
bookToReturn.setStatus({"未借阅", "NULL", "NULL"});
// 将归还信息写入库房文件
inventory.addBook(bookToReturn);
std::ofstream outFile("inventory.csv");
for (Book &book : inventory.getBooks())
{
outFile << book.getTitle() << ','
<< book.getAuthor() << ','
<< book.getISBN() << ','
<< book.getPress() << ','
<< book.getType() << ','
<< "未借阅" << ','
<< "NULL" << ','
<< "NULL" << '\n';
}
// 将归还信息写入学生账户文件
for (int index = 0; index < 3; index++)
{
if (m_reading[index] == bookToReturn.getTitle())
{
m_reading.erase(m_reading.begin() + index);
m_reading.push_back("NULL");
break;
}
}
account.deleteStudentReading(m_userName, bookToReturn.getTitle());
std::ofstream outStudentUserFile("studentUser.csv");
for (auto student : account.getUsers())
{
outStudentUserFile << student.getUserName() << ","
<< student.getPassword() << ","
<< student.getName() << ','
<< student.getReading()[0] << ','
<< student.getReading()[1] << ','
<< student.getReading()[2] << '\n';
}
std::cout << "归还成功!" << std::endl;
}
void StudentUser::storeStudentUser(StudentAccount& account)
{
std::ofstream file("studentUser.csv", std::ios::app);
// 检查文件是否成功打开
if (!file.is_open())
{
std::cout << "用户目录出错,请联系管理员" << std::endl;
return;
}
// 写入用户信息到CSV文件
file << m_userName << ","
<< m_password << ","
<< m_name << ','
<< m_reading[0] << ','
<< m_reading[1] << ','
<< m_reading[2] << '\n';
account.addAccount(*this);
// 关闭文件
file.close();
}