Skip to content

Commit

Permalink
mechanics: draft bank feature
Browse files Browse the repository at this point in the history
  • Loading branch information
eric15342335 committed Apr 11, 2024
1 parent 01dcf57 commit a3507c0
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
50 changes: 50 additions & 0 deletions bank.cpp
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;
}
60 changes: 60 additions & 0 deletions bank.h
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

0 comments on commit a3507c0

Please sign in to comment.