Skip to content

Instantly share code, notes, and snippets.

@Jules-Baratoux
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jules-Baratoux/d1032383aec979f2cbfb to your computer and use it in GitHub Desktop.
Save Jules-Baratoux/d1032383aec979f2cbfb to your computer and use it in GitHub Desktop.
// -----------------------------------------------------------------------------
// 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;
}
// -----------------------------------------------------------------------------
// 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) {}
// -----------------------------------------------------------------------------
// 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;
};
}
// -----------------------------------------------------------------------------
// 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"
@Jules-Baratoux
Copy link
Author

> g++ -pedantic -Wall -o hw3 hw3.cpp  SavingsAccount.cpp && ./hw3.exe
warning: In function ‘SavingsAccount’:
        initialBalance is negative. Settng savingsBalance to 0.
warning: In function ‘setAnnualInterestRate’:
        newInterestRate is negative. Settng annualInterestRate to 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment