COMP 2012 Object-Oriented Programming and Data Structures

Lab 7 Friends, static method/data

Bond Trading System

In this lab, we are going to use knowledge of static method and friend class to implement a simple bond trading system. The source code is available here.

Overview

There are 3 roles in the bond trading system: Bond Issuer, Bond Holder, and Bond Certificate. From time to time, Bond Issuer will issue a certain amount of bonds and specify interest rate and period. Bond holders can purchase a certain amount of bonds with a discount (interest rate), and get a bond certificate. When bond maturity date comes (period arrives), Bond Holder can get back their money plus the interest from bond issuer. In this lab, the unit of time is year (period, maturity date, and residual time are all calculated by years)

Some terms you may find helpful.

  • Face Value: bond value printed on top of certificate.
  • Interest Rate: rate of interest from purchasing the bond. (e.g., one buys 100 bonds with 10% interest rate, the face value on certificate will be 110).
  • Maturity: time when the bond issuer must repay the face value to the bond holder.
  • Residual time: remaining time before maturity.
  • The descriptions of classes and members in classes are as follows:
    • class CertificateBase: Base Class for Bond Certificate.
      • int residualTime_;:
      • The time remaining until maturity day comes.
      • double faceValue_;:
      • The value of bonds.
      • TestMaturity(int years):
      • Decrease the residualTime_ by years, and return true if marturity date comes.
      • double GetFaceValue():
      • Return the face value
      • void Maturity():
      • Pure virtual function
    • class Certificate: Template Class for Certificate, corresponding to one kind of bond of bondType. Most methods and data are declared as private, only accessed by friend class Issuer (including Constructor/Destructor).
      • static int transactionsNum_;:
      • The number of certificates of bondType.
      • static double interestRate_;:
      • The interest rate of bondType.
      • static int period_;:
      • The period of bondType, representing the number of years to wait for marturity.
      • Issuer* issuer_;:
      • The issuer that issues this certificate.
      • void Maturity():
      • Represent bond maturity, invoke settlement method of Issuer class to finish the transaction.
      • void SetRateAndPeriod(double rate, int year):
      • [TODO] Modify the interest rate and period of the bond.
      • Certificate(double value, Issuer* issuer):
      • [TODO] Increase transactionsNum_ by one, initialize the issuer and face value of the certificate, set residual time as the period (static) of this bond.
      • ~Certificate():
      • [TODO] Decrease transactionsNum_ by one.
    • class IssuerBase: Base Class for Bond Issuer
      • int numOfBond_:
      • The number of bond hold by issuer.
      • std::string name_:
      • The name of issuer.
      • IssuerBase(std::string name):
      • Constructor.
      • void IssueBond(int amount):
      • Issue a certain amount of bond, initialize numOfBond_.
    • class Issuer: Template Class for Bond Issuer, corresponding to one kind of bond of bondType, should be friend class of the corresponding Certificate class.
      • Issuer(std::string name):
      • Constructor.
      • virtual void Settlement(Certificate* Certificate):
      • Transaction complete, delete the certificate.
      • virtual CertificateBase* SellBond(int amount):
      • [TODO] Sell a certain amount of bonds to customer. The face value of the certificate should be (1 + interest rate) * amount.
      • void SetRateAndPeriod(double rate, int year):
      • [TODO] Modify the interest rate and period of bond controlled by the issuer, you can invoke that static method of Certificate class you have implemented
    • class BondHolder: Template Class for Bond Issuer, corresponding to one kind of bond of bondType.
      • double profit_;:
      • Profit earned from bond transaction.
      • std::string name_;:
      • Name of bond holder.
      • void PurchaseBond(IssuerBase* issuer, int amount):
      • Buying amount of bond from issuer, decrease profit by the expense.
      • void Elapse(int year):
      • Representng time elapses by years, need to test whether certificate's maturity date comes and delete those from certificate list, and increase the profit by the face value of the deleted certificate.
      • BondHolder(std::string name):
      • Constructor
      • ~BondHolder():
      • Destructor, assume all the certificates in the list reach maturity date.

    Lab tasks

    1. Finish the TODOs in Issuer.h, Certificate.h. Except for completing blank class method, you also need to declare friend class and initialize static data.
    2. Compile the project. Notice we add sanitizer in Makefile, if your computer does not support you can delete -fsanitize=address flag to suppress erro like cannot find -lasan.
    3. Run lab7, check with the expected output below:
    4. [Transaction round 1]
      ----------------------
       Issuer: HK Bond
      ----------------------
      Num of ongoing transaction: 2
      Num of bond pending sold : 45000
      ----------------------
       Issuer: New York Bond
      ----------------------
      Num of ongoing transaction: 2
      Num of bond pending sold : 64000
      ----------------------
       Issuer: London Bond
      ----------------------
      Num of ongoing transaction: 2
      Num of bond pending sold : 84000
      
      [1 years later]
      John holds 1 certificates, and the current profit is -9800
      Tom holds 2 certificates, and the current profit is -12000
      Lucy holds 1 certificates, and the current profit is -9600
      
      [Transaction round 2]
      ----------------------
       Issuer: HK Bond
      ----------------------
      Num of ongoing transaction: 2
      Num of bond pending sold : 32000
      ----------------------
       Issuer: New York Bond
      ----------------------
      Num of ongoing transaction: 4
      Num of bond pending sold : 49000
      ----------------------
       Issuer: London Bond
      ----------------------
      Num of ongoing transaction: 4
      Num of bond pending sold : 46000
      
      [2 years later]
      John holds 1 certificates, and the current profit is -18450
      Tom holds 1 certificates, and the current profit is -16240
      Lucy holds 0 certificates, and the current profit is 3000
      
      [Transaction round 3]
      ----------------------
       Issuer: HK Bond
      ----------------------
      Num of ongoing transaction: 2
      Num of bond pending sold : 16000
      ----------------------
       Issuer: New York Bond
      ----------------------
      Num of ongoing transaction: 2
      Num of bond pending sold : 33000
      ----------------------
       Issuer: London Bond
      ----------------------
      Num of ongoing transaction: 4
      Num of bond pending sold : 31000
      
      [3 years later]
      John holds 0 certificates, and the current profit is 5800
      Tom holds 0 certificates, and the current profit is 5420
      Lucy holds 0 certificates, and the current profit is 5600
      ----------------------
       Issuer: HK Bond
      ----------------------
      Num of ongoing transaction: 0
      Num of bond pending sold : 16000
      ----------------------
       Issuer: New York Bond
      ----------------------
      Num of ongoing transaction: 0
      Num of bond pending sold : 33000
      ----------------------
       Issuer: London Bond
      ----------------------
      Num of ongoing transaction: 0
      Num of bond pending sold : 31000

    Submission Deadline and Guidelines:

    The due date and time will be 10-min after your lab session. You are supposed to upload the following files in an archive to ZINC:

    - Certificate.h  Issuer.h
    
    You can submit your codes multiple times by the deadline. Only the last submission will be graded.
    Page maintained by
    • Chengfeng Ye
    • Last Modified:
    Homepage