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

  1. //BUDGET2.CPP - Budget program in the "C++ as a better C"
  2. //              version.
  3.  
  4. #include <iostream.h>
  5.  
  6. //the maximum number of accounts you can have
  7. const int maxAccounts = 10;
  8.  
  9. //Checking - this describes checking accounts
  10. struct Checking
  11. {
  12.    unsigned accountNumber;
  13.    float    balance;
  14. } chkAcnts[maxAccounts];
  15.  
  16. //Savings - you can probably figure this one out
  17. struct Savings
  18. {
  19.    unsigned accountNumber;
  20.    float    balance;
  21.    int      noWithdrawals;
  22. } svgAcnts[maxAccounts];
  23.  
  24. //prototype declarations
  25. void process(Checking &checking);      //Note 1
  26. void process(Savings &savings);
  27.  
  28. //inline functions
  29. //init(Checking) - initialize a checking account
  30. inline void init(Checking &checking)   //Note 2
  31. {
  32.    cout << "Enter account number:";
  33.    cin  >> checking.accountNumber;
  34.    checking.balance = 0.0F;
  35. }
  36.  
  37. //init(Savings) - initialize a savings account
  38. inline void init(Savings  &savings)
  39. {
  40.    cout << "Enter account number:";
  41.    cin  >> savings.accountNumber;
  42.    savings.balance = 0.0F;
  43.    savings.noWithdrawals = 0;
  44. }
  45.  
  46. //main - accumulate the initial input and output totals
  47. int main()
  48. {
  49.    //loop until someone enters an æXÆ or æxÆ
  50.    int noChkAccounts = 0;    //count the number of accounts
  51.    int noSvgAccounts = 0;
  52.    char     accountType;     //S or C
  53.  
  54.    unsigned keepLooping = 1;
  55.    while (keepLooping)
  56.    {
  57.       cout << "Enter S for Savings, "
  58.               "C for Checking, X for exit\n";
  59.       cin  >> accountType;
  60.  
  61.       switch (accountType)
  62.       {
  63.          case 'c':
  64.          case 'C':
  65.             if (noChkAccounts < maxAccounts)
  66.             {
  67.                init(chkAcnts[noChkAccounts]);
  68.                process(chkAcnts[noChkAccounts]);
  69.                noChkAccounts++;
  70.             }
  71.             else
  72.             {
  73.                cout << "No more room for checking accounts\n";
  74.             }
  75.             break;
  76.  
  77.          case 's':
  78.          case 'S':
  79.             if (noSvgAccounts < maxAccounts)
  80.             {
  81.                init(svgAcnts[noSvgAccounts]);
  82.                process(svgAcnts[noSvgAccounts]);
  83.                noSvgAccounts++;
  84.             }
  85.             else
  86.             {
  87.                cout << "No more room for savings accounts\n";
  88.             }
  89.             break;
  90.  
  91.          case 'x':
  92.          case 'X':
  93.             keepLooping = 0;
  94.             break;
  95.  
  96.          default:
  97.             cout << "I didn't get that.\n";
  98.       }
  99.    }
  100.  
  101.    //now present totals
  102.    float chkTotal = 0.0F;      //total of all checking accounts
  103.    cout << "Checking accounts:\n";
  104.    for (int i = 0; i < noChkAccounts; i++)  //Note 3
  105.    {
  106.       cout << "Account " << chkAcnts[i].accountNumber
  107.            << " = "      << chkAcnts[i].balance
  108.            << "\n";
  109.       chkTotal += chkAcnts[i].balance;
  110.    }
  111.    float svgTotal = 0.0F;      //total of all savings accounts
  112.    cout << "Savings accounts:\n";
  113.    for (i = 0; i < noSvgAccounts; i++)      //Note 4
  114.    {
  115.       cout << "Account "             << svgAcnts[i].accountNumber
  116.            << " = "                  << svgAcnts[i].balance
  117.            << " (no. withdrawals = " << svgAcnts[i].noWithdrawals
  118.            << ")\n";
  119.       svgTotal += svgAcnts[i].balance;
  120.    }
  121.  
  122.    float total = chkTotal + svgTotal;
  123.    cout << "Total for checking accounts = " << chkTotal << "\n";
  124.    cout << "Total for savings accounts  = " << svgTotal << "\n";
  125.    cout << "Total worth                 = " << total << "\n";
  126.    return 0;
  127. }
  128.  
  129. //process(Checking) - input the data for a checking account
  130. void process(Checking &checking)
  131. {
  132.    cout << "Enter positive number for deposit,\n"
  133.            "negative for check, 0 to terminate";
  134.  
  135.    float transaction;
  136.    do
  137.    {
  138.       cout << ":";
  139.       cin  >> transaction;
  140.  
  141.       //is it a deposit?
  142.       if (transaction > 0.0F)
  143.       {
  144.          checking.balance += transaction;
  145.       }
  146.  
  147.       //how about withdrawal?
  148.       if (transaction < 0.0F)
  149.       {
  150.          //withdrawal
  151.          transaction = -transaction;
  152.          if (checking.balance < transaction)   //Note 5
  153.          {
  154.             cout << "Insufficient funds: balance "
  155.                  << checking.balance
  156.                  << ", check "
  157.                  << transaction
  158.                  << "\n";
  159.          }
  160.          else
  161.          {
  162.             checking.balance -= transaction;
  163.  
  164.             //if balance falls too low, charge service fee
  165.             if (checking.balance < 500.00F)
  166.             {
  167.                checking.balance -= 0.20F;
  168.             }
  169.          }
  170.       }
  171.    } while (transaction != 0.0F);
  172. }
  173.  
  174. //process(Savings) - input the data for a savings account
  175. void process(Savings &savings)
  176. {
  177.    cout << "Enter positive number for deposit,\n"
  178.            "negative for withdrawal, 0 to terminate";
  179.  
  180.    float transaction;
  181.    do
  182.    {
  183.       cout << ":";
  184.       cin  >> transaction;
  185.  
  186.       //is this a deposit?
  187.       if (transaction > 0.0F)
  188.       {
  189.          savings.balance += transaction;
  190.       }
  191.  
  192.       //is it a withdrawal?
  193.       if (transaction < 0.0F)
  194.       {
  195.          transaction = -transaction;
  196.          if (savings.balance < transaction)
  197.          {
  198.             cout << "Insufficient funds: balance "
  199.                  << savings.balance
  200.                  << ", withdrawal "
  201.                  << transaction
  202.                  << "\n";
  203.          }
  204.          else
  205.          {
  206.             if (++savings.noWithdrawals > 1)
  207.             {
  208.                savings.balance -= 5.00F;
  209.             }
  210.             savings.balance -= transaction;
  211.          }
  212.       }
  213.    } while (transaction != 0.0F);
  214. }
  215.