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

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