-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01dcf57
commit a3507c0
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* @file bank.cpp | ||
* @brief Implementation of the Bank mechanics | ||
*/ | ||
#include "bank.h" | ||
// #include "random_price.h" // generate_interest_rate() | ||
|
||
void Bank::init(void) { | ||
/** Initialize initial bank state */ | ||
double balance = 0; | ||
// double interest_rate = generate_interest_rate(); | ||
} | ||
|
||
bool Bank::deposit(double & wallet, double amount) { | ||
/** | ||
* Accept two parameters: current wallet balance (pass-by-reference) | ||
* and the amount to deposit. If the wallet has enough money, deposit | ||
* the amount to the bank account and return true. Otherwise, return false. | ||
*/ | ||
if (wallet >= amount) { | ||
wallet -= amount; | ||
balance += amount; | ||
return true; | ||
} | ||
/** else */ return false; | ||
} | ||
|
||
bool Bank::withdraw(double & wallet, double amount) { | ||
/* | ||
Accept two parameters: current wallet balance (pass-by-reference) | ||
and the amount to withdraw. If the bank account has enough money, | ||
withdraw the amount from the bank account and return true. Otherwise, | ||
return false. | ||
*/ | ||
if (balance >= amount) { | ||
wallet += amount; | ||
balance -= amount; | ||
return true; | ||
} | ||
/** else */ return false; | ||
} | ||
|
||
void Bank::next_round(void) { | ||
/** | ||
* Simply multiply the balance with the interest rate | ||
* Should be called when proceed to next round | ||
*/ | ||
balance *= (1 + interest_rate); | ||
return; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#ifndef BANK_H | ||
#define BANK_H | ||
|
||
class Bank { | ||
public: | ||
/** Initialize initial bank state */ | ||
void init(void); | ||
|
||
/** | ||
* Deposit money to the bank account. | ||
* @param wallet current wallet balance (pass-by-reference) | ||
* @param amount amount to deposit | ||
* @return true if the deposit is successful, false otherwise | ||
*/ | ||
|
||
bool deposit(double & wallet, double amount); | ||
|
||
/** | ||
* Withdraw money from the bank account. | ||
* @param wallet current wallet balance (pass-by-reference) | ||
* @param amount amount to withdraw | ||
* @return true if the withdrawal is successful, false otherwise | ||
*/ | ||
bool withdraw(double & wallet, double amount); | ||
|
||
/** | ||
* Proceed to the next round, calculate the new balance based on the interest rate. | ||
*/ | ||
void next_round(void); | ||
|
||
/** | ||
* Get the current bank balance. | ||
* @return the current bank balance | ||
*/ | ||
double get_balance(void) { | ||
return balance; | ||
} | ||
|
||
/** | ||
* Get the current interest rate. | ||
* @return the current interest rate | ||
*/ | ||
double get_interest_rate(void) { | ||
return interest_rate; | ||
} | ||
|
||
/** | ||
* Set the interest rate. | ||
* @param rate the new interest rate | ||
*/ | ||
void set_interest_rate(double rate) { | ||
interest_rate = rate; | ||
} | ||
|
||
private: | ||
double balance; | ||
double interest_rate; | ||
}; | ||
|
||
#endif |