home *** CD-ROM | disk | FTP | other *** search
/ C++ for Dummies (3rd Edition) / C_FD.iso / BUDGETS / BUDGET3.CPP < prev    next >
C/C++ Source or Header  |  1996-09-15  |  7KB  |  279 lines

  1. //BUDGET3.CPP - Budget program with real classes for
  2. //              the first time. Now we transform the
  3. //              C structs into classes. This is starting
  4. //              to look like something.
  5.  
  6. #include <iostream.h>
  7.  
  8. //the maximum number of accounts one can have
  9. const int maxAccounts = 10; 
  10.  
  11. //Checking - this describes checking accounts
  12. class Checking                         //Note 1
  13. {
  14.   public:
  15.    Checking()                          //Note 2
  16.    {
  17.       accountNumber = 0;
  18.       balance = 0.0F;
  19. }
  20.    void init()                         //Note 3
  21.    {
  22.       cout << "Enter account number:";
  23.       cin  >> accountNumber;
  24.    }
  25.  
  26.    //access functions                  //Note 4
  27.    int accountNo()
  28.    {
  29.       return accountNumber;
  30.    }
  31.    float acntBalance()
  32.    {
  33.       return balance;
  34.    }
  35.  
  36.    //transaction functions             //Note 5
  37.    void deposit(float amount)
  38.    {
  39.       balance += amount;
  40.    }
  41.    void withdrawal(float amount);
  42.  
  43.    //display function for displaying self on æcoutÆ
  44.    void display()                      //Note 6
  45.    {
  46.       cout << "Account " << accountNumber
  47.            << " = "      << balance
  48.            << "\n";
  49.    }
  50.  
  51.   protected:                           //Note 7
  52.    unsigned accountNumber;
  53.    float    balance;
  54. };
  55. //withdrawal - this member function is too big to
  56. //             be inlined
  57. void Checking::withdrawal(float amount)
  58. {
  59.    if (balance < amount )
  60.    {
  61.       cout << "Insufficient funds: balance " << balance
  62.            << ", check "                     << amount
  63.            << "\n";
  64.    }
  65.    else
  66.    {
  67.       balance -= amount;
  68.  
  69.       //if balance falls too low, charge service fee
  70.       if (balance < 500.00F)
  71.       {
  72.          balance -= 0.20F;
  73.       }
  74.    }
  75. }
  76.  
  77. //Savings - you can probably figure this one out
  78. class Savings
  79. {
  80.   public:
  81.    Savings()
  82.    {
  83.       accountNumber = 0;
  84.       balance = 0.0F;
  85.       noWithdrawals = 0;
  86.    }
  87.    void init()
  88.    {
  89.       cout << "Enter account number:";
  90.       cin  >> accountNumber;
  91.    }
  92.  
  93.    //access functions
  94.    int accountNo()
  95.    {
  96.       return accountNumber;
  97.    }
  98.    float acntBalance()
  99.    {
  100.       return balance;
  101.    }
  102.  
  103.    //transaction functions
  104.    void deposit(float amount)
  105.    {
  106.       balance += amount;
  107.    }
  108.    void withdrawal(float amount);
  109.  
  110.    //display function - display self to cout
  111.    void display()
  112.    {
  113.       cout << "Account "             << accountNumber
  114.            << " = "                  << balance
  115.            << " (no. withdrawals = " << noWithdrawals
  116.            << ")\n";
  117.    }
  118.  
  119.   protected:
  120.    unsigned accountNumber;
  121.    float    balance;
  122.    int      noWithdrawals;
  123. };
  124. void Savings::withdrawal(float amount)
  125. {
  126.    if (balance < amount)
  127.    {
  128.       cout << "Insufficient funds: balance " << balance
  129.            << ", withdrawal "                << amount
  130.            << "\n";
  131.    }
  132.    else
  133.    {
  134.       if (++noWithdrawals > 1)
  135.       {
  136.          balance -= 5.00F;
  137.       }
  138.       balance -= amount;
  139.    }
  140. }
  141.  
  142. //prototype declarations
  143. void process(Checking &checking);
  144. void process(Savings &savings);
  145.  
  146. //checking and savings account objects
  147. Checking chkAcnts[maxAccounts];        //Note 8
  148. Savings svgAcnts[maxAccounts];
  149.  
  150. //main - accumulate the initial input and output totals
  151. int main()
  152. {
  153.    /*loop until someone enters an æXÆ or æxÆ*/
  154.    int noChkAccounts = 0;    //count the number of accounts
  155.    int noSvgAccounts = 0;
  156.    char     accountType;     //S or C
  157.  
  158.    unsigned keepLooping = 1;
  159.    while (keepLooping)
  160.    {
  161.       cout << "Enter S for Savings, "
  162.               "C for Checking, X for exit\n";
  163.       cin  >> accountType;
  164.  
  165.       switch (accountType)
  166.       {
  167.          case 'c':
  168.          case 'C':
  169.             if (noChkAccounts < maxAccounts) 
  170. {
  171.                chkAcnts[noChkAccounts].init(); //Note 9
  172.                process(chkAcnts[noChkAccounts]);
  173.                noChkAccounts++;
  174.             }
  175.             else
  176.             {
  177.                cout << "No more room for checking accounts\n";
  178.             }
  179.             break;
  180.  
  181.          case 's':
  182.          case 'S':
  183.             if (noSvgAccounts < maxAccounts)
  184.             {
  185.                svgAcnts[noSvgAccounts].init();
  186.                process(svgAcnts[noSvgAccounts]);
  187.                noSvgAccounts++;
  188.             }
  189.             else
  190.             {
  191.                cout << "No more room for savings accounts\n";
  192.             }
  193.             break;
  194.  
  195.          case 'x':
  196.          case 'X':
  197.             keepLooping = 0;
  198.             break;
  199.  
  200.          default:
  201.             cout << "I didn't get that.\n";
  202.       }
  203.    }
  204.  
  205.    //now present totals
  206.    float chkTotal = 0.0F;      //total of all checking accounts
  207.    cout << "Checking accounts:\n";
  208.    int i;
  209.    for (i = 0; i < noChkAccounts; i++)
  210.    {
  211.       chkAcnts[i].display();           //Note 10
  212.       chkTotal += chkAcnts[i].acntBalance();
  213.    }
  214.    float svgTotal = 0.0F;      //total of all savings accounts
  215.    cout << "Savings accounts:\n";
  216.    for (i = 0; i < noSvgAccounts; i++)
  217.    {
  218.       svgAcnts[i].display();
  219.       svgTotal += svgAcnts[i].acntBalance();
  220.    }
  221.  
  222.    float total = chkTotal + svgTotal;
  223.    cout << "Total for checking accounts = " << chkTotal << "\n";
  224.    cout << "Total for savings accounts  = " << svgTotal << "\n";
  225.    cout << "Total worth                 = " << total << "\n";
  226.    return 0;
  227. }
  228.  
  229. //process(Checking) - input the data for a checking account*/
  230. void process(Checking &checking)
  231. {
  232.    cout << "Enter positive number for deposit,\n"
  233.            "negative for check, 0 to terminate";
  234.  
  235.    float transaction;
  236.    do
  237.    {
  238.       cout << ":";
  239.       cin  >> transaction;
  240.  
  241.       //deposit
  242.       if (transaction > 0.0F)
  243.       {
  244.          checking.deposit(transaction);
  245.       }
  246.  
  247.       //withdrawal
  248.       if (transaction < 0.0F)
  249.       {
  250.          checking.withdrawal(-transaction);
  251.       }
  252.    } while (transaction != 0.0F);
  253. }
  254. //process(Savings) - input the data for a savings account
  255. void process(Savings &savings)
  256. {
  257.    cout << "Enter positive number for deposit,\n"
  258.            "negative for withdrawal, 0 to terminate";
  259.  
  260.    float transaction;
  261.    do
  262.    {
  263.       cout << ":";
  264.       cin  >> transaction;
  265.  
  266.       //deposit
  267.       if (transaction > 0.0F)
  268.       {
  269.          savings.deposit(transaction);
  270.       }
  271.  
  272.       //withdrawal
  273.       if (transaction < 0.0F)
  274.       {
  275.          savings.withdrawal(-transaction);
  276.       }
  277.    } while (transaction != 0.0F);
  278. }
  279.