-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.cpp
114 lines (108 loc) · 2.73 KB
/
store.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
/**
* Store.cpp
* Store class read files for movies, customers, and commands.
* This class stores the list of commands and process them.
*
* @author Olga Kuriatnyk
*/
#include "store.h"
// destructor
Store::~Store() {
auto itr = commands.begin();
while (!commands.empty()) {
delete *itr;
commands.erase(itr);
itr = commands.begin();
}
}
// reading list of customers from the file
void Store::readCustomersFromFile(const string &filename) {
fstream fs;
fs.open(filename);
if (!fs.is_open()) {
cerr << "Could not open file: " << filename << endl;
return;
}
string readInput;
while (getline(fs, readInput)) {
if (readInput.empty()) {
break;
}
stringstream lineInput(readInput);
getline(lineInput, readInput, ' ');
int id = stoi(readInput);
bool validID = CustomerContainer::getInstance().validID(id);
if (validID) {
Customer *customer = CustomerFactory::create(id);
customer->read(lineInput);
CustomerContainer::getInstance().insert(customer);
} else {
cerr << "ERROR: invalid customer ID " << readInput << endl;
}
}
fs.close();
}
// read list of movies from the file
void Store::readMoviesFromFile(const string &filename) {
fstream fs;
fs.open(filename);
if (!fs.is_open()) {
cerr << "Could not open file: " << filename << endl;
return;
}
string readInput;
while (getline(fs, readInput)) {
if (readInput.empty()) {
break;
}
stringstream lineInput(readInput);
// read the character
getline(lineInput, readInput, ',');
char type = readInput[0];
// create a movie using the item factory
Movie *tempMovie = Movie::create(type);
if (tempMovie != nullptr) {
bool validMovie = tempMovie->read(lineInput);
if (validMovie) {
tempMovie->printMovie();
Inventory::getInstance().insert(tempMovie);
} else {
delete tempMovie;
}
}
}
fs.close();
Inventory::getInstance().sortItems();
}
// reading commands from the file
void Store::readCommandsFromFile(const string &filename) {
fstream fs;
fs.open(filename);
if (!fs.is_open()) {
cerr << "Could not open file: " << filename << endl;
return;
}
string readInput;
while (getline(fs, readInput)) {
if (readInput.empty()) {
break;
}
stringstream lineInput(readInput);
// read the character
getline(lineInput, readInput, ' ');
char type = readInput[0];
Command *tempCommand = Command::create(type);
if (tempCommand != nullptr) {
tempCommand->read(lineInput);
commands.push_back(tempCommand);
}
}
fs.close();
}
// processing all comands
void Store::processCommands() {
cout << "\n";
for (auto &command : commands) {
command->process();
}
}