-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlcohol.cpp
48 lines (39 loc) · 1.25 KB
/
Alcohol.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
#include "Alcohol.hpp"
#include <stdexcept>
Alcohol::Alcohol(const std::string& name, size_t amount, size_t basePrice, size_t percentage)
: Cargo(name, amount, basePrice), percentage_(percentage) {}
Cargo& Alcohol::operator+=(size_t amount) {
if (amount_ + amount > MAX_AMOUNT_OF_CARGO) {
throw std::out_of_range("Maximum amount of alcohol reached!");
}
amount_ += amount;
return *this;
}
Cargo& Alcohol::operator-=(size_t amount) {
amount_ -= amount;
return *this;
}
bool Alcohol::operator==(const Cargo& cargo) const {
if (typeid(cargo) == typeid(Alcohol)) {
const Alcohol* alcohol = static_cast<const Alcohol*>(&cargo);
return name_ == alcohol->getName() && basePrice_ == alcohol->getBasePrice() &&
percentage_ == alcohol->getPercentage();
}
return false;
}
size_t Alcohol::getPrice() const {
return basePrice_ * (percentage_ / MAX_PERCENTAGE) + 2;
}
void Alcohol::nextDay() {
daysUntilOnePercentEvaporates_--;
if (percentage_ > 0 && daysUntilOnePercentEvaporates_ == 0) {
percentage_--;
daysUntilOnePercentEvaporates_ = 5;
}
}
size_t Alcohol::getPercentage() const {
return percentage_;
}
bool Alcohol::isExpired() const {
return percentage_;
}