-
Notifications
You must be signed in to change notification settings - Fork 0
/
magazin.cpp
177 lines (145 loc) · 4.02 KB
/
magazin.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
#include "magazin.h"
#include <algorithm>
#include <cstring>
vector<Magazin*> Magazin::instances;
// Constructors
Magazin::Magazin() {
cout << "Am construit un magazin default\n";
this->name[0] = '\0';
this->items_in_store = 0;
this->sold_items = 0;
this->max_items = 0;
this->closing_time = 0.0;
this->opening_time = 0.0;
this->profit = 0;
this->max_categories = 0;
this->products = nullptr;
instances.push_back(this);
}
Magazin::Magazin(char* name) : Magazin() {
strncpy(this->name, name, 39);
this->name[39] = '\0';
}
Magazin::Magazin(char* name, int max_items) : Magazin(name) {
this->max_items = max_items;
}
Magazin::Magazin(char* name, int max_items, int max_categories) : Magazin(name, max_items) {
this->max_categories = max_categories;
}
Magazin::Magazin(char* name, int max_items, int max_categories, double closing_time, double opening_time)
: Magazin(name, max_items, max_categories) {
this->closing_time = closing_time;
this->opening_time = opening_time;
}
// Destructor
Magazin::~Magazin() {
if (products != nullptr) {
for (int i = 0; i < max_items; ++i) {
delete products[i];
}
delete[] products;
}
auto it = find(instances.begin(), instances.end(), this);
if (it != instances.end()) {
instances.erase(it);
}
}
// Modifiers
void Magazin::AddCategories(char* id_of_category) {
categories.push_back(id_of_category);
}
void Magazin::RemoveCategories(char* id_of_category) {
auto it = products;
while (it != products ) {
if (strcmp((*it)->GetName(), id_of_category) == 0) {
delete *it;
// Compact the array
std::move(it + 1, products + max_items, it);
} else {
++it;
}
}
}
void Magazin::SetMaxItems(int max_items) {
this->max_items = max_items;
}
void Magazin::SetMaxCategories(int max_categories) {
this->max_categories = max_categories;
}
void Magazin::SetOpeningTime(double opening_time) {
this->opening_time = opening_time;
}
void Magazin::SetClosingTime(double closing_time) {
this->closing_time = closing_time;
}
void Magazin::AddProfit(int profit) {
this->profit += profit;
}
void Magazin::AddItems(int items) {
this->items_in_store += items;
}
void Magazin::RemoveItems(int items) {
this->items_in_store -= items;
}
void Magazin::IncreaseSoldItems(int increment) {
this->sold_items += increment;
}
void Magazin::SetName(char* name) {
strncpy(this->name, name, 39);
this->name[39] = '\0';
}
// Printers
void Magazin::PrintCategories() {
system("CLS");
for (int i = 0; i < this->categories.size(); i++) {
cout << i + 1 << ". " << this->categories[i] << endl;
}
}
void Magazin::PrintProducts() {
for (int i = 0; i < this->max_items; i++) {
if (products[i] != nullptr) {
cout << "Product: " << products[i]->GetName() << endl;
}
}
}
void Magazin::PrintDetails() {
cout << "Store Name: " << this->name << std::endl;
cout << "Profit: " << this->profit << endl;
cout << "Items in store: " << this->items_in_store << endl;
cout << "Max Items: " << this->max_items << std::endl;
cout << "Max Categories: " << this->max_categories << endl;
cout << "Closing Time: " << this->closing_time << endl;
cout << "Opening Time: " << this->opening_time << endl;
}
void Magazin::PrintInstances() {
int i = 1;
for (const auto& instance : instances) {
cout << i << ". " << instance << endl;
i++;
}
}
// Return functions
int Magazin::GetSoldItems() {
return this->sold_items;
}
int Magazin::GetMaxItems() {
return this->max_items;
}
double Magazin::GetClosingTime() {
return this->closing_time;
}
double Magazin::GetOpeningTime() {
return this->opening_time;
}
int Magazin::GetProfit() {
return this->profit;
}
int Magazin::GetItemsInStore() {
return this->items_in_store;
}
char* Magazin::GetName() {
return this->name;
}
bool Magazin::HasInstances() {
return !instances.empty();
}