-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook.cpp
154 lines (115 loc) · 3.08 KB
/
book.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
#include "book.hpp"
#include <stdio.h>
Book::Book()
{
}
Book::Book( std::map<std::string, std::string> m )
{
this->author = m["author"];
this->title = m["title"];
this->subject = m["subject"];
}
bool Book::operator++ (int)
{
this->count++;
return this->save();
}
bool Book::operator-- (int)
{
if(!(--this->count))
{
return this->removeSelf();
} else
{
return this->save();
}
}
void Book::getInfo(bool b)
{
std::cout << "\n\tauthor: " << this->author << std::endl;
std::cout << "\ttitle: " << this->title << std::endl;
std::cout << "\tsubject: " << this->subject << std::endl;
if(b)
std::cout << "\tcount: " << this->count << std::endl << std::endl;
}
bool Book::setAllInfo()
{
bool b = false;
std::cout << "\nInput new book info\n\n";
std::cout << "Input new book autor: ";
std::cin >> this->author;
b = this->input( this->author, "0", &this->validateNameAndSubject );
if(!b) return false;
std::cout << "Input new book title: ";
std::cin >> this->title;
b = this->input( this->title, "0", &this->validateTitle );
if(!b) return false;
std::cout << "Input new book subject: ";
std::cin >> this->subject;
b = this->input( this->subject, "0", &this->validateNameAndSubject );
if(!b) return false;
std::string temp;
std::cout << "Input new book count: ";
std::cin >> temp;
b = this->input( temp, "stp", &this->validateNumb, this->count );
if(!b) return false;
this->isSet = true;
return this->save();
}
bool Book::setAllInfo(std::string fileName, bool b)
{
if(!this->isFile(fileName))
{
if(b) std::cout << "No such file";
return false;
}
std::ifstream f;
f.open(fileName);
f >> this->author;
if(!this->validateNameAndSubject(this->author)) return false;
f >> this->subject;
if(!this->validateNameAndSubject(this->subject)) return false;
f >> this->title;
if(!this->validateTitle(this->title)) return false;
std::string temp;
f >> temp;
if(!this->validateNumb(temp)) return false;
this->count = std::stoi(temp, nullptr, 10);
if(this-count == 0) return false;
this->isSet = true;
return this->save();
}
bool Book::save()
{
std::string path = this->pathToFile();
std::ofstream bookFile;
bookFile.open(path);
bookFile << this->author + "\n";
bookFile << this->subject + "\n";
bookFile << this->title + "\n";
bookFile << this->count;
bookFile.close();
return true;
}
bool Book::removeSelf()
{
std::string path = this->pathToFile();
return !remove(path.c_str());
}
bool Book::write(std::ostream & _stream)
{
_stream << "\n" + this->author + "\n";
_stream << this->subject + "\n";
_stream << this->title;
return true;
}
bool Book::setCount(int c)
{
if(!(c > 0)) return false;
this->count = c;
return true;
}
std::string Book::pathToFile()
{
return this->relPath + "books/" + this->strToLower(this->author) + "_" + this->strToLower(this->subject) + "_" + this->strToLower(this->title) + ".txt";
}