Object-Oriented Design Heuristics (paperback)

 <  Free Open Study  >  

// Trans. hpp: This header file contains the definition of the // classes that make up the Transaction portion of this // application. The Transaction classes are interesting in that // they span both sides of the application, i.e., they are used by // both Bank classes and ATM classes. In fact, the transactions // used by the Bank classes do not need to look anything like the // transactions used by the ATM classes. In this implementation, I // have chosen to make them one in the same. The different public // interfaces are supported via #ifdef's using ATM_SIDE and // BANK_SIDE macros to differentiate them. #ifndef _TRANS_ #define _TRANS_ #include <stdio.h> #include <iostream.h> #include <string.h> #include <math.h> #include <time.h> #include ''consts.hpp'' // Forward references class Account; class AccountList; class ATM; // A TimeStamp object encapsulates the data and time of a // transaction. Its current implementation is that of a string // produced via the time and ctime library routines. The reader // should feel free to come up with something more elaborate. class TimeStamp{ char date_time[small_string*2]; public: TimeStamp(); void print(FILE*); }; // All transactions have a TimeStamp, one account name, its PIN // number, and an amount. (Note: While Balance transactions do not // need an amount, they use it to carry back the balance value.) // Recall that protected accessor methods are perfectly // appropriate. The packetize method is used to turn the object // into a string suitable for shipping across the network. It // requires every transaction (derived classes included) to know // the format of this string, but the alternative is to have the // Transaction class grant friendship to the network or at least // provide a collection of accessor methods. The latter tends to be // problematic in that all classes gain access to the abstract // implementation of the Transaction class. This format knowledge // does not imply that a network needs to ship the format as is. It // implies only that they must use it to produce the final packets // they will ship. class Transaction{ TimeStamp timeStamp; char source_account[small_string]; char pin[small_string]; double amount; protected: Transaction(char* account, char* p, double a); char* get_source_account(); double get_amount(); void set_amount(double new_amount); public: virtual void print(FILE*); virtual char* type() = 0; //Only ATM classes use the preprocess, postprocess, and update // methods. The Transaction class for the Bank side of the // application never compiles them into the object code. #ifdef ATM_SIDE virtual int preprocess(ATM*); virtual void postprocess(ATM*); virtual void update(char* info, int count); virtual void packetize(char* buf); #endif // The process and verify_account methods are used only by the // classes on the Bank's side of the application. The Transaction // class for the ATM side of the application never compiles them // into the object code. #ifdef BANK_SIDE virtual int process(AccountList* accounts) = 0; int verify_account(Account*); virtual void packetize(int status, char* buf); #endif }; // The following four classes are the derived classes of the // abstract Transaction class : Deposit, Withdraw, Balance, and // Transfer. class Deposit : public Transaction { public: Deposit(char* account, char* p, double a); void print(FILE*); char* type(); #ifdef ATM_SIDE int preprocess(ATM*); #endif #ifdef BANK_SIDE int process(AccountList* accounts); #endif }; class Withdraw : public Transaction { public: Withdraw(char* account, char* p, double a); char* type(); #ifdef ATM_SIDE int preprocess(ATM*); void postprocess(ATM*); #endif #ifdef BANK_SIDE int process(AccountList* accounts); #endif void print(FILE*); }; class Balance : public Transaction { double balance; public: Balance(char* account, char* p); void print(FILE*); char* type(); #ifdef ATM_SIDE void update(char* info, int count); #endif #ifdef BANK_SIDE int process(AccountList* accounts); #endif }; // The Transfer class adds the additional data member of // target_account since the money needs to be transferred // someplace. class Transfer : public Transaction { char target_account[small_string]; public: Transfer(char* s_account, char* p, char* t_account, double a); void print(FILE*); char* type(); #ifdef ATM_SIDE void packetize(char* buf); #endif #ifdef BANK_SIDE int process(AccountList* accounts); #endif }; class TransactionList{ Transaction** TransList; int transnum; unsigned size; public: TransactionList(unsigned sz); ~TransactionList(); int add_trans(Transaction*); void print(FILE*); void cleanup(); }; #endif

 <  Free Open Study  >  

Категории