-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductFile.cpp
More file actions
116 lines (100 loc) · 2.05 KB
/
ProductFile.cpp
File metadata and controls
116 lines (100 loc) · 2.05 KB
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
#include "ProductFile.h"
#include <iostream>
using namespace std;
void swap(Product* a, Product* b)
{
Product temp = *a;
*a = *b;
*b = temp;
}
void ProductFile::printByCategory(const char* category) const
{
if (category == "")
{
for (int i = 0; i < this->productsCount; i++)
{
this->products[i].print();
}
}
else
{
for (int i = 0; i < this->productsCount; i++)
{
if (strcmp(this->products[i].getCategory(), category) == 0)
{
this->products[i].print();
}
}
}
}
void ProductFile::printAlphabetically() const
{
Product* tempArr = new Product[this->productsCount];
for (int i = 0; i < productsCount; i++)
{
tempArr[i] = products[i];
}
for (int i = 0; i < productsCount - 1; i++)
{
for (int j = 0; j < productsCount - i - 1; j++)
{
if (strcmp(tempArr[j].getName(), tempArr[j + 1].getName()) > 0)
{
swap(&tempArr[j], &tempArr[j + 1]);
}
}
}
for (int i = 0; i < productsCount; i++)
{
tempArr[i].print();
}
}
void ProductFile::printByPrice(const char* order) const
{
Product* tempArr = new Product[this->productsCount];
for (int i = 0; i < productsCount; i++)
{
tempArr[i] = products[i];
}
for (int i = 0; i < productsCount - 1 ; i++)
{
for (int j = 0; j < productsCount - i - 1; j++)
{
if (strcmp(order, "from lower") == 0)
{
if (tempArr[j].getPrice() > tempArr[j + 1].getPrice())
{
swap(&tempArr[j], &tempArr[j + 1]);
}
}
if (strcmp(order, "from higher") == 0)
{
if (tempArr[j].getPrice() < tempArr[j + 1].getPrice())
{
swap(&tempArr[j], &tempArr[j + 1]);
}
}
}
}
for (int i = 0; i < productsCount; i++)
{
tempArr[i].print();
}
}
void ProductFile::addProduct(Product product)
{
Product* temp = new Product[productsCount];
for (int i = 0; i < productsCount; i++)
{
temp[i] = this->products[i];
}
delete[] products;
this->productsCount++;
products = new Product[productsCount];
for (int i = 0; i < productsCount - 1; i++)
{
this->products[i] = temp[i];
}
this->products[productsCount - 1] = product;
delete[] temp;
}