-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItem.cpp
50 lines (42 loc) · 1.18 KB
/
Item.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
#include "Item.hpp"
#include <iostream>
#include <stdexcept>
Item::Item(const std::string& name, size_t amount, size_t basePrice, Rarity rarity)
: Cargo(name, amount, basePrice), rarity_(rarity)
{}
bool Item::operator==(const Cargo& cargo) const {
if (typeid(cargo) == typeid(Item)) {
const Item* item = static_cast<const Item*>(&cargo);
return name_ == item->getName() && basePrice_ == item->getBasePrice() &&
rarity_ == item->getRarity();
}
return false;
}
Cargo& Item::operator+=(size_t amount) {
if (amount_ + amount > MAX_AMOUNT_OF_CARGO) {
throw std::out_of_range("Maximum amount of items reached!");
}
amount_ += amount;
return *this;
}
Cargo& Item::operator-=(size_t amount) {
if (amount <= amount_) {
amount_ -= amount;
}
return *this;
}
void Item::nextDay() {
daysToDestruction_--;
if (daysToDestruction_ == 0) {
std::cout << "The item has been destroyed\n";
}
}
size_t Item::getPrice() const {
return basePrice_ * static_cast<size_t>(rarity_);
}
Item::Rarity Item::getRarity() const {
return rarity_;
}
bool Item::isExpired() const {
return daysToDestruction_;
}