-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShop.cc
More file actions
87 lines (72 loc) · 2.28 KB
/
Shop.cc
File metadata and controls
87 lines (72 loc) · 2.28 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
#include "Shop.h"
#include "Utils.h"
Shop::Shop() {
};
Shop::Shop(string name) : _name(name) {
}
Shop::Shop(string name, StockList stocks) :_name(name), _stocks(stocks) {
}
Shop::~Shop() {
for(Stock* stock : _stocks) {
delete stock;
}
};
string Shop::name() const {
return _name;
};
int Shop::stockSize() const {
return _size;
}
Shop::Stock& Shop::stockAtIndex(int index) {
if(index >= 0 && index < stockSize()) {
return *(_stocks[index]);
} else {
printf("Index out of bounds. Index: %d, Size: %d [Shop::stockAtIndex()]\n", index, stockSize());
abort();
}
}
Shop::EnumShopRet Shop::purchaseStock(int index, int amount, Party& party) {
if(index >= 0 && index < stockSize()) {
// amount - requested purchase size :: count - actual size of stock
int count = _stocks[index]->getCount();
int actual = (amount > count ? count : amount);
Stack& stack = _stocks[index]->getStack();
actual *= stack.count();
if(_stocks[index]->getPrice()*actual > party.money()) {
return EnumShopRet::NOT_ENOUGH_MONEY;
}
Shop::EnumShopRet ret;
if(_stocks[index]->getPrice()*actual <= party.money()) {
if(!(party.wagon().add(new Stack(stack.item(), actual)))) {
_stocks[index]->setCount(count-amount);
ret = EnumShopRet::SUCCESS;
} else {
ret = EnumShopRet::NOT_ENOUGH_SPACE;
}
}
if(ret == EnumShopRet::SUCCESS) {
party.modifyMoney(-_stocks[index]->getPrice()*actual);
}
if(_stocks[index]->getCount() <= 0) {
removeStock(index);
}
return ret;
} else {
printf("You tried to access a Stock out of bounds. Index: %d, Size: %d [Shop::purchaseStock()]\n", index, stockSize());
abort();
}
}
void Shop::addStock(Stock* stock) {
_stocks.push_back(stock);
_size++;
}
void Shop::removeStock(int index) {
if(index >= 0 && index < stockSize()) {
delete _stocks[index];
_stocks.erase(_stocks.begin()+index);
_size--;
} else {
printf("You tried to access a Stock out of bounds. Index: %d, Size: %d [Shop::removeStock()]\n", index, stockSize());
abort();
}
}