Advertisement
Guest User

Untitled

a guest
Aug 27th, 2014
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.04 KB | None | 0 0
  1. /*****************************************************************************
  2.  * This program produces a personalized loan amortization table for the user.*
  3.  *****************************************************************************/
  4.  
  5. #include <iostream>         // Needed for I/o
  6. #include <iomanip>          // Need for formatting
  7. #include <cmath>            // Needed for the pow function
  8. #include <string>           // Needed to read strings
  9. #include <fstream>          // Needed to manipulate files
  10. #include <windows.h>        // Needed to change screen colors
  11.  
  12. using namespace std;        // namespace std
  13.  
  14.  /****************************************************************************
  15.  *         Display Message 1 - This void function displays a message!        *
  16.  *****************************************************************************/
  17. void displaymessage1()
  18. {
  19.     cout << "\nHello and Welcome to MidSouthern Savings Bank's Loan Amortization Calculator!\n\n";
  20. }
  21.  
  22. /*****************************************************************************
  23. *        Display Message 2 - This void function displays another message!    *
  24. ******************************************************************************/
  25. void displaymessage2()
  26. {
  27.     cout << "\nWe Appreciate your business and hope you have\na wonderful experience working with us!\n\n"
  28.     "If you have any concerns or complaints please call 1-800-EAT-SHIT\n\n";
  29. }
  30.  
  31. /******************************************************************************
  32. *          Function to return numerical value 1                               *
  33. ******************************************************************************/
  34. int sum(int num1, int num2);       // Function prototype
  35.  
  36. /******************************************************************************
  37. *          Function to return numerical value 2                               *
  38. ******************************************************************************/
  39. int calc(int num3, int num4);       // Function prototype
  40.  
  41. /******************************************************************************
  42. *          Function to return Boolean value                                   *
  43. ******************************************************************************/
  44. bool isEven(int);                   // Function prototype
  45.  
  46. /******************************************************************************
  47. *          Functions for decision structure and loop                          *
  48. ******************************************************************************/
  49. void displayMenu();                 // Function prototype
  50. int getChoice();                    // Function prototype
  51. void makeSmile();                   // Function prototype
  52.  
  53. // Rectangle class declaration
  54. class Rectangle
  55. {
  56.     public:
  57.         double length;
  58.         double width;
  59.         double stories;
  60.         void setLength(double);
  61.         void setWidth(double);
  62.         void setHeight(double);
  63.         double getLength();
  64.         double getWidth();
  65.         double getHeight();
  66.         double getArea();
  67. };
  68.  
  69. /*************************************************************
  70.  *                            main                           *
  71.  *************************************************************/
  72. int main()                  // Start Main Program
  73. {
  74.         Rectangle box;         // Declare a Rectangle object
  75.     double boxLength, boxWidth, boxHeight;
  76.  
  77.     //Get box length and width
  78.     cout << "This will calculate the square footage area of your house.\n";
  79.     cout << "What is the length? ";
  80.     cin  >> boxLength;
  81.     cout << "What is the width? ";
  82.     cin  >> boxWidth;
  83.     cout << "How many stories? ";
  84.     cin  >> boxHeight;
  85.  
  86.     // Call member functions to set box dimensions
  87.     box.setLength(boxLength);
  88.     box.setWidth(boxWidth);
  89.     box.setHeight(boxHeight);
  90.  
  91.     // Call member functions to get box information to display
  92.     cout << "\nHere is your house's data:\n";
  93.     cout << "Square Footage Area of your home is  : " << box.getArea()   << endl;
  94.  
  95.     double loan,            // Loan amount
  96.            rate,            // Annual interest rate
  97.            moInterestRate,  // Monthly interest rate
  98.            years,           // Years of loan
  99.            balance,         // Monthly balance
  100.            term,            // Used to calculate payment
  101.            payment,         // Monthly payment
  102.            age;             // User's age
  103.  
  104.     int    totalage,        // Payoff age
  105.            numPayments,     // Number of payments
  106.            totalyear,       // Payoff year
  107.            colorchoice,     // Color choice
  108.            curryear;        // current year
  109.  
  110.     char   ans;             // answer for run again
  111.  
  112.     const  string bankname = "MidSouthern Savings Bank";    // Constant
  113.  
  114.     string username;
  115.  
  116.     ofstream outputFile;            // Open file and write message to user
  117.     outputFile.open ("C:\\Users\\joebrady\\Desktop\\names.txt");
  118.     outputFile << "Thanks\n";
  119.     outputFile << "For\n";
  120.     outputFile << "Your\n";
  121.     outputFile << "Business!";
  122.     outputFile.close();
  123.  
  124. displaymessage1();                  // Void Functions Display Message 1
  125.  
  126. do
  127. {
  128.                                     // Get loan information
  129.     cout << "Please type your name and hit Enter\n";
  130.     cin  >> username;
  131.     cout << "Please enter your age and hit Enter\n";
  132.     cin  >> age;
  133.     cout << "Please enter the current year and hit Enter\n";
  134.     cin  >> curryear;
  135.     cout << "Enter your Loan amount in numbers only and hit Enter\n";
  136.     cin  >> loan;
  137.     cout << "Enter your Annual interest rate (entered as a decimal, example: .05 for 5%) and hit Enter\n";
  138.     cin  >> rate;
  139.     cout << "Enter the Total number of Years of your loan and hit Enter\n";
  140.     cin  >> years;
  141.     cout << "\n\n\n";
  142.     while (years <= 0)          // Variable Defined Loop to correct invalid data
  143. {
  144.     cout << "Number must be positive. Please try again dummy: " << flush;
  145.     cin >> years;
  146. }
  147.  
  148.     // Calculate monthly payment
  149.     // A zero interest loan must be handled as a special case to avoid
  150.     // a divide by 0 in the payment formula used for loans interest.
  151.     numPayments = static_cast<int>(12 * years);
  152.     moInterestRate = rate / 12.0;
  153.     if (rate == 0)
  154.         payment = loan / numPayments;
  155.     else
  156.     {   term = pow((1 + moInterestRate), numPayments);
  157.         payment = (loan * moInterestRate * term) / (term - 1.0);
  158.     }
  159.  
  160.     // Display monthly payment
  161.     cout << fixed << showpoint << setprecision(2);
  162.     cout << "Your Monthly payment is: $" << payment << endl;
  163.     cout << username << " owes " << bankname << endl;
  164.  
  165.     // Display report header
  166.     cout << endl;
  167.     cout << setw(5)  << "Month"     << setw(10) << "Interest";
  168.     cout << setw(10) << "Principal" << setw(9)  << "Balance" << endl;
  169.     cout << "----------------------------------\n";
  170.  
  171.     balance = loan;       // Remaining balance starts out as full loan amount
  172.  
  173.     // Produce a listing for each month
  174.     for (int month = 1; month <= numPayments; month++)
  175.     {
  176.         double moInterest,          // Amount of pmt that pays interest
  177.                principal;           // Amount of pmt that lowers the balance
  178.  
  179.         // Calculate amount paid for this month's interest and principal
  180.         moInterest = moInterestRate * balance;  // Calculate interest first
  181.         if (month != numPayments)               // If not the final month
  182.             principal = payment - moInterest;   // rest of pmt goes to principal
  183.  
  184.         else                                    // It's the last month so
  185.         {   principal = balance;                // pay exact final balance
  186.             payment = balance + moInterest;
  187.         }
  188.         // Calculate new loan balance           // Only principal reduces the
  189.         balance -= principal;                   // balance, not the whole pmt
  190.  
  191.         // Display this month's payment figures
  192.         cout << setw(4)  << month     << setw(10) << moInterest;
  193.         cout << setw(10) << principal << setw(10) << balance << endl;
  194.     }
  195.  
  196. if (loan == 100000)      // If Else If Usage
  197.     cout << "\nCongratulations! You bought something crappy.\n Dont forget to Pay Your Bill!\n";
  198. else if (loan == 120000)
  199.     cout << "\nCongratulations! You bought something nice!\nDont forget to Pay Your Bill!\n";
  200. else if (loan == 200000)
  201.     cout << "\nWhoa, you really want to be in this much debt?\nDon't forget to Pay Your Damn Bills!\n";
  202. else if (loan != 100000, loan != 120000, loan != 200000)
  203.     cout << "\nPay up or Else\n";
  204.  
  205.      totalage = sum(years, age);   // Function to return numerical value 1
  206.  
  207.      cout << "\nYou will be " << totalage << " years old before you pay off this loan!\n\n";
  208.  
  209.      totalyear = sum(years, curryear);    // Function to return numerical value 2
  210.  
  211.      cout << "The year will be " << totalyear << ".\n\n";
  212.  
  213.      if (isEven(totalage))                // Function to return Boolean Value
  214.         cout << "We don't think you will make it, say hello to our Repo-Man, Donny\n\n";
  215.      else
  216.         cout << "We think you will do just fine!\n\n";
  217.  
  218.     ifstream inFile;            // Open file and read message to user
  219.     string name;
  220.     inFile.open ("C:\\Users\\joebrady\\Desktop\\names.txt");
  221.     inFile >> name;
  222.     cout << name << endl;
  223.     inFile >> name;
  224.     cout << name << endl;
  225.     inFile >> name;
  226.     cout << name << endl;
  227.     inFile >> name;
  228.     cout << name << endl;
  229.  
  230.     outputFile.close();
  231.  
  232.     displaymessage2();
  233.  
  234. const int quit = 6;
  235.     int colorChoice;
  236.     // Get the handle to standard output device (the console)
  237.     HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
  238.  
  239.     do
  240.     {   SetConsoleTextAttribute(screen, 7);  // Set to white on black
  241.         displayMenu();                       // for menu display
  242.         colorChoice = getChoice();
  243.  
  244.         if (colorChoice != quit)
  245.         {   SetConsoleTextAttribute(screen, colorChoice + 9);
  246.             makeSmile();
  247.         }
  248.     } while (colorChoice != quit);
  249.  
  250. cout << "\n\nDo you want to run this program again? y/n?";  // User Defined loop using Do-while
  251. cin >> ans;
  252. } while ((ans == 'y') || (ans == 'Y'));
  253.  
  254. return 0;
  255.     }
  256.  
  257. /**************************************************************
  258. *     Sum function to return numerical value 1                *
  259. **************************************************************/
  260. int sum(int num1, int num2)
  261. {
  262.     return num1 + num2;
  263. }
  264. /**************************************************************
  265. *     Sum function to return numerical value 2                *
  266. **************************************************************/
  267. int calc(int num3, int num4)
  268. {
  269.     return num3 + num4;
  270. }
  271. /**************************************************************
  272. *     Function to return Boolean Value True/False             *
  273. **************************************************************/
  274. bool isEven(int number)
  275. {
  276.     if (number % 2 == 0)
  277.         return true;
  278.     else
  279.         return false;
  280. }
  281. /***************************************************************
  282. *      This functions displays the menu of color choices       *
  283. ***************************************************************/
  284. void displayMenu()
  285. {
  286.     cout << "Just for fun, What is your favorite color?  Pick one\n\n"
  287.          << "Enter\n1 for Green\n2 for Blue\n3 for Red\n4 for Purple\n5 for Yellow\nHit 6 to continue with the program\n";
  288. }
  289. /**************************************************
  290.  *                    getChoice                   *
  291.  *  This function inputs, validates, and returns  *
  292.  *  the user's menu choice.                       *
  293.  **************************************************/
  294. int getChoice()
  295. {
  296.     int choice;
  297.  
  298.     cin >> choice;
  299.     while (choice < 1 || choice > 6)
  300.     {   cout << "The only valid choices are 1-6.  Please re-enter. ";
  301.         cin  >> choice;
  302.     }
  303.     return choice;
  304. }
  305. /**************************************************************************
  306. * This function draws a smiley face in the users selected color           *
  307. **************************************************************************/
  308. void makeSmile()
  309. {
  310.     cout << "\n\n";
  311.     cout << "                                    <>     <>    \n       ";
  312.     cout << "                                 *        \n       ";
  313.     cout << "                             \\_______/    \n\n\n   ";
  314.     cout << "                    Press Enter to Return to the menu";
  315.     cin.get();          // clear the previous \n out of the input buffer
  316.     cin.get();          // wait for the user to press enter
  317. }
  318.  
  319. // Member function implementation section
  320.  
  321. /********************************************************************
  322.  *                     Rectangle::setLength                         *
  323.  * This function sets the value of the member variable length.      *
  324.  * If the argument passed to the function is zero or greater, it is *
  325.  * copied into length. If it is negative, 1.0 is assigned to length.*
  326.  ********************************************************************/
  327. void Rectangle::setLength(double len)
  328. {
  329.     if (len >= 0)
  330.         length = len;
  331.     else
  332.     {   length = 1.0;
  333.         cout << "Invalid length. Using a default value of 1.\n";
  334.     }
  335. }
  336.  
  337. /********************************************************************
  338.  *                      Rectangle::setWidth                         *
  339.  * This function sets the value of the member variable width.       *
  340.  * If the argument passed to the function is zero or greater, it is *
  341.  * copied into width. If it is negative, 1.0 is assigned to width.  *
  342.  ********************************************************************/
  343. void Rectangle::setWidth(double w)
  344. {
  345.     if (w >= 0)
  346.         width = w;
  347.     else
  348.     {   width = 1.0;
  349.         cout << "Invalid width. Using a default value of 1.\n";
  350.     }
  351. }
  352.  
  353. /********************************************************************
  354.  *                      Rectangle::setHeight                         *
  355.  * This function sets the value of the member variable height.       *
  356.  * If the argument passed to the function is zero or greater, it is *
  357.  * copied into width. If it is negative, 1.0 is assigned to width.  *
  358.  ********************************************************************/
  359. void Rectangle::setHeight(double h)
  360. {
  361.     if (h >= 0)
  362.         stories = h;
  363.     else
  364.     {   stories < 1.0;
  365.         cout << "Invalid height. Using a default value of 1.\n";
  366.     }
  367. }
  368.  
  369. /**************************************************************
  370.  *                     Rectangle::getLength                   *
  371.  * This function returns the value in member variable length. *
  372.  **************************************************************/
  373. double Rectangle::getLength()
  374. {
  375.     return length;
  376. }
  377.  
  378. /**************************************************************
  379.  *                     Rectangle::getWidth                    *
  380.  * This function returns the value in member variable width.  *
  381.  **************************************************************/
  382. double Rectangle::getWidth()
  383. {
  384.     return width;
  385. }
  386.  
  387. /**************************************************************
  388.  *                     Rectangle::getHeigth                    *
  389.  * This function returns the value in member variable Height.  *
  390.  **************************************************************/
  391. double Rectangle::getHeight()
  392. {
  393.     return stories;
  394. }
  395. /*******************************************************************
  396.  *                        Rectangle::getArea                       *
  397.  * This function calculates and returns the area of the rectangle. *
  398.  *******************************************************************/
  399. double Rectangle::getArea()
  400. {
  401.     return length * width * stories;
  402. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement