Skip to content

Commit 3f08155

Browse files
authored
Merge pull request coders-school#89 from mariuszlisowski/poprawkaOperatoraPorownania
Implemented comparison operator in Alcohol class
2 parents b7323cd + eb14b19 commit 3f08155

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

shm/src/Alcohol.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
#include <stdexcept>
44

5-
Alcohol::Alcohol(const std::string& name, size_t amount, size_t basePrice, size_t percentage)
6-
: Cargo(name, amount, basePrice)
7-
, percentage_(percentage)
8-
{}
5+
Alcohol::Alcohol(const std::string& name, size_t amount, size_t basePrice, size_t percentage)
6+
: Cargo(name, amount, basePrice), percentage_(percentage) {}
97

108
Cargo& Alcohol::operator+=(size_t amount) {
119
if (amount_ + amount > MAX_AMOUNT_OF_CARGO) {
@@ -20,9 +18,13 @@ Cargo& Alcohol::operator-=(size_t amount) {
2018
return *this;
2119
}
2220

23-
bool Alcohol::operator==(const Cargo& alcohol) const {
24-
// TODO:
25-
return alcohol.getAmount() == amount_ ? true : false;
21+
bool Alcohol::operator==(const Cargo& cargo) const {
22+
if (typeid(cargo) == typeid(Alcohol)) {
23+
const Alcohol* alcohol = static_cast<const Alcohol*>(&cargo);
24+
return name_ == alcohol->getName() && basePrice_ == alcohol->getBasePrice() &&
25+
percentage_ == alcohol->getPercentage();
26+
}
27+
return false;
2628
}
2729

2830
size_t Alcohol::getPrice() const {
@@ -33,7 +35,6 @@ size_t Alcohol::getPrice() const {
3335
void Alcohol::nextDay() {
3436
}
3537

36-
3738
size_t Alcohol::getPercentage() const {
3839
return percentage_;
3940
}

0 commit comments

Comments
 (0)