/*
* Account.h
* AgileBook
*
* Created by James Coplien on 9/2/08.
* Copyright 2011 Gertrud & Cope. All rights reserved.
*
*/#ifndef _ACCOUNT_H#define _ACCOUNT_H#include <string>
using namespace std;
class Account {
public:
string accountID(void) const;
Account(void);
private:
int acct;
};
#endif
/*
* CheckingAccount.h
* AgileBook
*
* Created by James Coplien on 9/17/08.
* Copyright 2011 Gertrud & Cope. All rights reserved.
*
*/#ifndef _CHECKINGACCOUNT_H#define _CHECKINGACCOUNT_H#include "Account.h"#include "Currency.h"#include "TransferMoneySource.h"#include "MyTime.h"#include <string>
using namespace std;
class XferMoneyContext;
class PayBillsContext;
class CheckingAccount:
public Account,
public TransferMoneySink {
public:
CheckingAccount(void);
private:
// These functions can be virtual if there are// specializations of CheckingAccount
Currency availableBalance(void);
void decreaseBalance(Currency);
void increaseBalance(Currency);
void updateLog(string, MyTime, Currency);
private:
Currency availableBalance_;
};
#endif
/*
* InvestmentAccount.h
* AgileBook
*
* Created by James Coplien on 9/2/08.
* Copyright 2011 Gertrud & Cope. All rights reserved.
*
*/#ifndef _INVESTMENTACCOUNT_H#define _INVESTMENTACCOUNT_H#include "Account.h"#include "Currency.h"#include "TransferMoneySource.h"#include "MyTime.h"#include <string>
using namespace std;
class XferMoneyContext;
class PayBillsContext;
class InvestmentAccount:
public Account,
public TransferMoneySource {
public:
friend class TransferMoneySource;
InvestmentAccount(void);
Currency availableBalance(void);
void increaseBalance(Currency);
void decreaseBalance(Currency);
void updateLog(string, MyTime, Currency);
private:
Currency availableBalance_;
};
/*
* InvestmentAccount.cpp
* AgileBook
*
* Created by James Coplien on 9/2/08.
* Copyright 2011 Gertrud & Cope. All rights reserved.
*
*/#include "InvestmentAccount.h"#include "PayBillsContext.h"#include "TransferMoneyContext.h"#include <string>#include <iostream>
using namespace std;
InvestmentAccount::InvestmentAccount(void):
availableBalance_(Euro(0.00))
{
}
Currency InvestmentAccount::availableBalance(void) {
std::cout << "InvestmentAccount::availableBalance returns "
<< availableBalance_ << std::endl;
return availableBalance_;
}
void
InvestmentAccount::increaseBalance(Currency c) {
std::cout << "InvestmentAccount::increaseBalance(" << c << ")" << std::endl;
availableBalance_ += c;
}
void
InvestmentAccount::decreaseBalance(Currency c) {
std::cout << "InvestmentAccount::decreaseBalance(" << c << ")" << std::endl;
availableBalance_ -= c;
}
void
InvestmentAccount::updateLog(string s, MyTime, Currency c) {
std::cout << "account: " << accountID() << " InvestmentAccount::updateLog(\"" << s << "\", Time, " << c << ")" << std::endl;
}
#endif
/*
* SavingsAccount.h
* AgileBook
*
* Created by James Coplien on 9/2/08.
* Copyright 2011 Gertrud & Cope. All rights reserved.
*
*/#ifndef _SAVINGSACCOUNT_H#define _SAVINGSACCOUNT_H#include "Account.h"#include "TransferMoneySource.h"#include "MyTime.h"#include <string>
using namespace std;
class XferMoneyContext;
class PayBillsContext;
class SavingsAccount:
public Account,
public TransferMoneySink {
friend class TransferMoneySink; // optional
public:
SavingsAccount(void);
private:
// These functions can be virtual if there are// specializations of SavingsAccount
Currency availableBalance(void);
void decreaseBalance(Currency);
void increaseBalance(Currency);
void updateLog(string, MyTime, Currency);
private:
Currency availableBalance_;
};
/*
* Globals.h
* AgileBook
*
* Created by James Coplien on 9/2/08.
* Copyright 2011 Gertrud & Cope. All rights reserved.
*
*/#ifndef _GLOBALS_H#define _GLOBALS_H#include "MyTime.h"
extern void endTransaction(void);
extern void beginTransaction(void);
extern MyTime DateTime(void);
#endif
/*
* GlobalsImplementation.h
* AgileBook
*
* Created by James Coplien on 9/2/08.
* Copyright 2011 Gertrud & Cope. All rights reserved.
*
*/#include "MyTime.h"#include <iostream>
void endTransaction(void) {
std::cout << "::endTransaction(void)" << std::endl;
}
void beginTransaction(void) {
std::cout << "::beginTransaction(void)" << std::endl;
}
MyTime DateTime(void) {
return 0;
}
/*
* MyExceptions.h
* AgileBook
*
* Created by James Coplien on 9/2/08.
* Copyright 2011 Gertrud & Cope. All rights reserved.
*
*/#ifndef _MYEXCEPTIONS_H#define _MYEXCEPTIONS_H
class InsufficientFunds {
public:
InsufficientFunds(void);
};
#endif
/*
* MyTime.h
* AgileBook
*
* Created by James Coplien on 9/2/08.
* Copyright 2011 Gertrud & Cope. All rights reserved.
*
*/#ifndef _MYTIME_H#define _MYTIME_H#include <string>
class MyTime {
public:
MyTime(void);
MyTime(long long);
MyTime(std::string timeAsString);
~MyTime();
MyTime(const MyTime &t);
MyTime &operator=(const MyTime &t);
bool operator>(const MyTime &t);
};
#endif
/*
* MyTimeImplementation.h
* AgileBook
*
* Created by James Coplien on 9/2/08.
* Copyright 2011 Gertrud & Cope. All rights reserved.
*
*/#include "MyTime.h"
MyTime::MyTime(long long) { }
MyTime::MyTime(std::string timeAsString) { }
MyTime::~MyTime() { }
MyTime::MyTime(const MyTime &t) { }
MyTime &MyTime::operator=(const MyTime &t) { return *this; }
bool MyTime::operator>(const MyTime &t) { return true; }
/*
* Context.h
* AgileBook
*
* Created by James Coplien on 1/14/09.
* Copyright 2011 Gertrud & Cope. All rights reserved.
*
*/#ifndef _CONTEXT_H#define _CONTEXT_H
class Context {
public:
Context(void);
virtual ~Context();
public:
static Context *currentContext_;
private:
Context *parentContext_;
};
/*
* Context.cpp
* AgileBook
*
* Created by James Coplien on 1/14/09.
* Copyright 2011 Gertrud & Cope. All rights reserved.
*
*/#include "Context.h"
Context::Context(void) {
parentContext_ = currentContext_;
currentContext_ = this;
}
Context::~Context() {
currentContext_ = parentContext_;
}
Context *Context::currentContext_ = NULL;
#endif
/*
* TransferMoneyContext.h
* AgileBook
*
* Created by James Coplien on 9/13/08.
* Copyright 2011 Gertrud & Cope. All rights reserved.
*
*/// TransferMoneyContext knows how to find the objects for a given// use case invocation; it associates those objects with// the roles they play in a Use Case of this type; and it// publishes those interface bindings for use by the// method-ful roles that participate in the use case.#ifndef _XFERMONEYCONTEXT_H#define _XFERMONEYCONTEXT_H#include "Account.h"#include "Context.h"#include "Currency.h"
class MoneySource;
class MoneySink;
class TransferMoneyContext: public Context
{
public:
TransferMoneyContext(void);
TransferMoneyContext(Currency amount, MoneySource *src, MoneySink *destination);
void doit(void);
MoneySource *sourceAccount(void) const;
MoneySink *destinationAccount(void) const;
Currency amount(void) const;
private:
void lookupBindings(void);
MoneySource *sourceAccount_;
MoneySink *destinationAccount_;
Currency amount_;
};
/*
* TransferMoneyContext.cpp
* AgileBook
*
* Created by James Coplien on 9/13/08.
* Copyright 2011 Gertrud & Cope. All rights reserved.
*
*/#include "TransferMoneyContext.h"#include "Currency.h"#include "MoneySource.h"#include "MoneySink.h"#include "InvestmentAccount.h"#include "SavingsAccount.h"
TransferMoneyContext::TransferMoneyContext(void): Context()
{
lookupBindings();
}
TransferMoneyContext::TransferMoneyContext(Currency amount, MoneySource *source, MoneySink *destination):
Context()
{
// Copy the rest of the stuff
sourceAccount_ = source;
destinationAccount_ = destination;
amount_ = amount;
}
void
TransferMoneyContext::doit(void)
{
sourceAccount()->transferTo(amount());
}
void
TransferMoneyContext::lookupBindings(void)
{
// These are somewhat arbitrary and for illustrative// purposes. The simulate a database lookup
InvestmentAccount *investmentAccount = new InvestmentAccount;
investmentAccount->increaseBalance(Euro(100.00)); // prime it with some money
sourceAccount_ = investmentAccount;
destinationAccount_ = new SavingsAccount;
destinationAccount_->increaseBalance(Euro(500.00)); // start it off with money
amount_ = Euro(30.00);
}
MoneySource*
TransferMoneyContext::sourceAccount(void) const
{
return sourceAccount_;
}
MoneySink*
TransferMoneyContext::destinationAccount(void) const
{
return destinationAccount_;
}
Currency
TransferMoneyContext::amount(void) const
{
return amount_;
}
#endif
/*
* PayBillsContext.h
* AgileBook
*
* Created by James Coplien on 9/13/08.
* Copyright 2011 Gertrud & Cope. All rights reserved.
*
*/#ifndef _PAYBILLSCONTEXT_H#define _PAYBILLSCONTEXT_H#include "Account.h"#include "Currency.h"#include "Context.h"#include <list>
class MoneySource;
class Creditor;
class PayBillsContext: public Context
{
public:
PayBillsContext(void);
void doit(void);
MoneySource *sourceAccount(void) const;
std::list creditors(void) const;
private:
void lookupBindings(void);
MoneySource *sourceAccount_;
std::list creditors_;
};
/*
* PayBillsContext.cpp
* AgileBook
*
* Created by James Coplien on 9/17/08.
* Copyright 2011 Gertrud & Cope. All rights reserved.
*
*/#include "PayBillsContext.h"#include "MoneySource.h"#include "MoneySink.h"#include "InvestmentAccount.h"#include "SavingsAccount.h"#include "Creditor.h"
PayBillsContext::PayBillsContext(void): Context()
{
lookupBindings();
}
void
PayBillsContext::doit(void)
{
sourceAccount()->payBills();
}
void
PayBillsContext::lookupBindings(void)
{
// These are somewhat arbitrary and for illustrative// purposes. The simulate a database lookup
InvestmentAccount *investmentAccount = new InvestmentAccount;
investmentAccount->increaseBalance(Euro(100.00)); // prime it with some money
sourceAccount_ = investmentAccount;
creditors_.push_back(new ElectricCompany);
creditors_.push_back(new GasCompany);
}
MoneySource*
PayBillsContext::sourceAccount(void) const
{
return sourceAccount_;
}
std::list
PayBillsContext::creditors(void) const
{
return creditors_;
}
#endif
//// main.cpp// AgileBook//// Created by James Coplien on 9/2/08.// Copyright Gertrud & Cope 2011. All rights reserved.//#include "TransferMoneyContext.h"#include "PayBillsContext.h"
int main() {
TransferMoneyContext *aNewUseCase = new TransferMoneyContext;
aNewUseCase->doit();
delete aNewUseCase;
PayBillsContext *anotherNewUseCase = new PayBillsContext;
anotherNewUseCase->doit();
delete anotherNewUseCase;
}