-
Notifications
You must be signed in to change notification settings - Fork 0
/
Order.cpp
95 lines (78 loc) · 1.75 KB
/
Order.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
#include "Order.hpp"
Order::Order(int number)
{
this->number = number;
items = new std::vector<Item*>();
}
std::string Order::to_string()
{
std::stringstream stream;
stream << "-----------------";
stream << std::endl << "#ticket " << number << std::endl;
for(int i = 0; i < items->size(); i++)
{
stream << items->at(i)->to_string() << std::endl;
}
stream << "Total: $";
stream << std::fixed << std::setprecision(Item::MONEY_PRECISION) << calculateTotal();
stream << std::endl;
return stream.str();
}
std::string Order::to_string_with_index()
{
std::stringstream stream;
for(int i = 0; i < items->size(); i++)
{
stream << i << ") " << items->at(i)->to_string() << std::endl;
}
return stream.str();
}
void Order::addItem(Item* item)
{
for(int i = 0; i < items->size(); i++)
{
if(items->at(i)->isEquals(item))
{
items->at(i)->increaseQuantity();
return;
}
}
items->push_back(item);
}
void Order::removeItem(Item* item)
{
for(int i = 0; i < items->size(); i++)
{
if(items->at(i)->isEquals(item))
{
if(items->at(i)->getQuantity() > 1)
items->at(i)->decreaseQuantity();
else
items->erase(items->begin()+i);
return;
}
}
}
void Order::setItem(Item* item, int index)
{
items->at(index) = item;
}
std::vector<Item*>* Order::getItems()
{
return this->items;
}
float Order::calculateTotal()
{
float total = 0;
for(int i = 0; i < items->size(); i++)
total += items->at(i)->getValue();
return total;
}
int Order::getNumber()
{
return number;
}
void Order::setNumber(int number)
{
this->number = number;
}