This repository has been archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStock.cpp
190 lines (156 loc) · 3.71 KB
/
Stock.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
#include "Stock.h"
bool Stock::UploadToFile()
{
std::ofstream myfile(file_name); //Erases the contents!!
int res;
if (myfile.good())
{
res = 1;
myfile << size_filled << endl;
for (int i = 0; i < size_filled; i++)
{
myfile << stock_array[i].name << ' ' << stock_array[i].quantity << ' ' << stock_array[i].price << endl;
}
}
else
{
res = 0;
}
myfile.close();
return res;
}
int Stock::DownloadFromFile()
{
//File structure:
//size_filled
//stock_array[i].name stock_array[i].quantity stock_array[i].price
std::fstream myfile(file_name);
int res;
if (myfile.good() && (myfile.peek() != std::fstream::traits_type::eof()))
{
res = 1;
myfile >> size_filled;
myfile.ignore();
if (size_filled > size_allocated)
{
std::cout << "Not enough stock capacity to save all the products, some data may be lost\n";
size_filled = size_allocated;
}
for (int i = 0; (i < size_filled) && (i < size_allocated); i++)
{
myfile >> stock_array[i].name; //Maybe it works, I'm not sure
myfile >> stock_array[i].quantity;
myfile >> stock_array[i].price;
myfile.ignore();
for (int j = 0; stock_array[i].name[j - 1] != '\0'; j++)
{
if (stock_array[i].name[j] == '\0')
{
stock_array[i].name_len_filled = j;
}
}
}
}
else if (myfile.good())
{
res = -1;
}
else if (!myfile.good())
{
res = 0;
}
myfile.close();
return res; //Really it's fine
}
Stock::Stock()
{
stock_array = new Product[STOCK_CAPACITY];
size_allocated = STOCK_CAPACITY;
size_filled = 0;
file_name = FILE_NAME_STOCK;
}
Stock::~Stock()
{
delete[] stock_array;
}
void Stock::CheckOut()
{
double sum_price = 0;
cout << "\n\n--------------------CHECKOUT--------------------\n";
cout << "index" << setw(SETW_PARAM) << "name" << setw(SETW_PARAM) << "quantity" << setw(SETW_PARAM) << "price" << endl;
for (int i = 0; (i < size_filled) && (i < size_allocated); i++)
{
std::cout << i << " " << setw(SETW_PARAM) << stock_array[i].name << setw(SETW_PARAM) << stock_array[i].quantity << setw(SETW_PARAM) << stock_array[i].price << endl;
sum_price += stock_array[i].quantity * stock_array[i].price;
}
cout << "Total cost: " << sum_price;
cout << "\n\n";
}
int Stock::Search(char* myname)
{
int res = -1;
for (int i = 0; (i < size_filled) && (i < size_allocated); i++)
{
if (stock_array[i].LexicographicallyCompareNames(myname) == -1)
{
res = i;
}
}
return res;
}
void Stock::ChangeQuantity(int index, int delta)
{
if ((delta < 0) && (stock_array[index].quantity >= -delta))
{
stock_array[index].quantity += delta;
}
else if (delta > 0)
{
stock_array[index].quantity += delta;
}
if (stock_array[index].quantity == 0)
{
RemoveProduct(index);
}
}
void Stock::RemoveProduct(int index)
{
for (int i = index; i < size_filled - 1; i++)
{
stock_array[i] = stock_array[i + 1];
}
size_filled--;
}
void Stock::AddNewProduct(Product myProduct)
{
int index_to_insert_at = -1;
for (int i = 0; i < size_filled; i++)
{
if (myProduct.LexicographicallyCompareNames(stock_array[i]) == 1)
{
index_to_insert_at = i;
}
else if (myProduct.LexicographicallyCompareNames(stock_array[i]) == -1)
{
stock_array[i].quantity += myProduct.quantity;
return;
}
}
if (index_to_insert_at == -1)
{
index_to_insert_at = size_filled;
}
if (size_allocated > size_filled)
{
for (int i = size_filled; i > index_to_insert_at; i--)
{
stock_array[i] = stock_array[i - 1];
}
stock_array[index_to_insert_at] = myProduct;
size_filled += 1;
}
else
{
cout << "No free space in stock." << endl;
}
}