Last active
August 29, 2015 14:14
-
-
Save Jules-Baratoux/d1032383aec979f2cbfb to your computer and use it in GitHub Desktop.
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
// ----------------------------------------------------------------------------- | |
// hw3.cpp | |
// ----------------------------------------------------------------------------- | |
#include <cstdlib> | |
#include "SavingsAccount.h" | |
using JulesBaratoux::SavingsAccount; | |
int main(void) | |
{ | |
SavingsAccount::tests::constructors(); | |
SavingsAccount::tests::getSavingsBalance(); | |
SavingsAccount::tests::setAnnualInterestRate(); | |
SavingsAccount::tests::applyMonthlyInterest(); | |
return EXIT_SUCCESS; | |
} |
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
// ----------------------------------------------------------------------------- | |
// SavingsAccount.cpp | |
// ----------------------------------------------------------------------------- | |
#include <iostream> | |
using std::endl; | |
#include <cassert> | |
#include "utils.h" | |
#include "SavingsAccount.h" | |
using JulesBaratoux::SavingsAccount; | |
double SavingsAccount::annualInterestRate = 0; | |
static bool setAnnualInterestRate_done = false; | |
// ----------------------------------------------------------------------------- | |
SavingsAccount::SavingsAccount(double initialBalance) | |
: savingsBalance(initialBalance) | |
{ | |
if (initialBalance < 0) | |
{ | |
warn << "initialBalance is negative. Settng savingsBalance to 0." << endl; | |
savingsBalance = 0; | |
} | |
} | |
void SavingsAccount::tests::constructors(void) | |
{ | |
// default | |
{ | |
SavingsAccount account; | |
assert(account.savingsBalance == 0); | |
} | |
// by parameter | |
{ | |
// normal | |
{ | |
SavingsAccount account(42); | |
assert(account.savingsBalance == 42); | |
} | |
// abnormal | |
{ | |
SavingsAccount account(-42); | |
assert(account.savingsBalance == 0); | |
} | |
} | |
} | |
// ----------------------------------------------------------------------------- | |
double SavingsAccount::getSavingsBalance(void) const | |
{ | |
return savingsBalance; | |
} | |
void SavingsAccount::tests::getSavingsBalance(void) | |
{ | |
SavingsAccount account(10); | |
assert(account.getSavingsBalance() == 10); | |
} | |
// ----------------------------------------------------------------------------- | |
void SavingsAccount::setAnnualInterestRate(double newInterestRate) | |
{ | |
if (newInterestRate < 0) | |
{ | |
warn << "newInterestRate is negative. Settng annualInterestRate to 0." << endl; | |
annualInterestRate = 0; | |
} | |
else | |
{ | |
annualInterestRate = newInterestRate; | |
} | |
} | |
void SavingsAccount::tests::setAnnualInterestRate(void) | |
{ | |
// normal | |
{ | |
SavingsAccount::setAnnualInterestRate(42); | |
assert(SavingsAccount::annualInterestRate == 42); | |
} | |
// abnormal | |
{ | |
SavingsAccount::setAnnualInterestRate(-42); | |
assert(SavingsAccount::annualInterestRate == 0); | |
} | |
setAnnualInterestRate_done = true; | |
} | |
// ----------------------------------------------------------------------------- | |
void SavingsAccount::applyMonthlyInterest(void) | |
{ | |
// Add the monthly interest earned for the account (1/12 of the annual interest) | |
savingsBalance *= 1 + annualInterestRate / 12; | |
} | |
void SavingsAccount::tests::applyMonthlyInterest(void) | |
{ | |
if (not setAnnualInterestRate_done) setAnnualInterestRate(); | |
const double savingsBalance = 10; | |
const double annualInterestRate = 12; | |
SavingsAccount account(savingsBalance); | |
SavingsAccount::setAnnualInterestRate(annualInterestRate); | |
account.applyMonthlyInterest(); | |
assert(account.savingsBalance == savingsBalance * (1 + annualInterestRate / 12)); | |
} | |
// ----------------------------------------------------------------------------- | |
SavingsAccount::~SavingsAccount(void) {} |
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
// ----------------------------------------------------------------------------- | |
// SavingsAccount.h | |
// ----------------------------------------------------------------------------- | |
#pragma once | |
namespace JulesBaratoux | |
{ | |
struct SavingsAccount | |
{ | |
explicit SavingsAccount(double initialBalance = 0); | |
static void setAnnualInterestRate(double newInterestRate); | |
double getSavingsBalance() const; | |
void applyMonthlyInterest(void); | |
~SavingsAccount(void); | |
struct tests | |
{ | |
static void constructors(void); | |
static void getSavingsBalance(void); | |
static void setAnnualInterestRate(void); | |
static void applyMonthlyInterest(void); | |
}; | |
private: | |
double savingsBalance; | |
static double annualInterestRate; | |
friend struct tests; | |
}; | |
} |
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
// ----------------------------------------------------------------------------- | |
// utils.h | |
// ----------------------------------------------------------------------------- | |
#pragma once | |
// Windows... | |
#ifdef WIN32 | |
# define or || | |
# define and && | |
# define not ! | |
#endif | |
#define warn std::cerr \ | |
<< "warning: In function ‘" << __FUNCTION__ << "’:" << std::endl \ | |
<< "\t" |
Author
Jules-Baratoux
commented
Feb 2, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment