Solved: 20 - Classes and Objects
This commit is contained in:
@@ -11,8 +11,8 @@ public:
|
|||||||
[[nodiscard]] auto owner() const -> const std::string&;
|
[[nodiscard]] auto owner() const -> const std::string&;
|
||||||
[[nodiscard]] auto balance() const -> double;
|
[[nodiscard]] auto balance() const -> double;
|
||||||
|
|
||||||
void deposit(double amount);
|
void deposit(double amount) pre(amount > 0);
|
||||||
auto withdraw(double amount) -> bool;
|
auto withdraw(double amount) -> bool pre(amount > 0);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string owner_;
|
std::string owner_;
|
||||||
|
|||||||
@@ -2,20 +2,23 @@
|
|||||||
|
|
||||||
namespace oop {
|
namespace oop {
|
||||||
|
|
||||||
BankAccount::BankAccount(std::string owner, double balance)
|
BankAccount::BankAccount(std::string owner, const double balance)
|
||||||
: owner_(std::move(owner)), balance_(balance) {}
|
: owner_(std::move(owner)), balance_(balance) {}
|
||||||
|
|
||||||
auto BankAccount::owner() const -> const std::string& { return owner_; }
|
auto BankAccount::owner() const -> const std::string& { return owner_; }
|
||||||
auto BankAccount::balance() const -> double { return balance_; }
|
auto BankAccount::balance() const -> double { return balance_; }
|
||||||
|
|
||||||
void BankAccount::deposit(double amount) {
|
void BankAccount::deposit(const double amount)
|
||||||
// TODO: Add amount when positive
|
{
|
||||||
(void)amount;
|
this->balance_ += amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto BankAccount::withdraw(double amount) -> bool {
|
auto BankAccount::withdraw(const double amount) -> bool
|
||||||
// TODO: Return false if insufficient funds, else subtract and return true
|
{
|
||||||
(void)amount;
|
if (this->balance_ >= amount) {
|
||||||
|
this->balance_ -= amount;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ TEST(ClassesAndObjects, DepositAndWithdraw) {
|
|||||||
EXPECT_TRUE(account.withdraw(30.0));
|
EXPECT_TRUE(account.withdraw(30.0));
|
||||||
EXPECT_DOUBLE_EQ(account.balance(), 120.0);
|
EXPECT_DOUBLE_EQ(account.balance(), 120.0);
|
||||||
EXPECT_FALSE(account.withdraw(1000.0));
|
EXPECT_FALSE(account.withdraw(1000.0));
|
||||||
|
EXPECT_DEATH(account.withdraw(-10.0), "amount > 0");
|
||||||
|
EXPECT_DEATH(account.deposit(-10.0), "amount > 0");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(ClassesAndObjects, OwnerName) {
|
TEST(ClassesAndObjects, OwnerName) {
|
||||||
|
|||||||
Reference in New Issue
Block a user