Nofxthepirate

Chapter 3 exercises

Jan 30th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.41 KB | None | 0 0
  1. //Exercise 3.9
  2.  
  3. #include <string>
  4.  
  5. class Account {
  6. public:
  7.  
  8.     Account (std::string accountName, int initialBalance)
  9.         : name{accountName} {
  10.             if (initialBalance > 0){
  11.                 balance = initialBalance;
  12.             }
  13.         }
  14.  
  15.     void deposit(int depositAmount){
  16.         if (depositAmount > 0) {
  17.             balance += depositAmount;
  18.         }
  19.  
  20.     }
  21.  
  22.     void withdraw(int withdrawAmount){
  23.         if (balance > withdrawAmount) {
  24.             balance -= withdrawAmount;
  25.         }
  26.         if (withdrawAmount > balance) {
  27.             std::cout << "Withdrawal amount exceeded account balance." << std::endl;
  28.         }
  29.     }
  30.  
  31.     int getBalance() const{
  32.         return balance;
  33.     }
  34.  
  35.     void setName(std::string accountName){
  36.         name = accountName;
  37.     }
  38.  
  39.     std::string getName() const{
  40.         return name;
  41.     }
  42. private:
  43.     std::string name;
  44.     int balance{0};
  45. };
  46.  
  47.  
  48.  
  49. //Exercise 3.10 header file
  50. //This header file contains the Invoice class for creating simple invoices
  51. class Invoice{
  52.  
  53. public:
  54.     Invoice(std::string partNumber, std::string partName, int partCost, int howMany)
  55.         : partNum{partNumber}, partDescrip{partName} {
  56.             if (partCost > 0){
  57.                 partPrice = partCost;
  58.             }
  59.             else partPrice = 0;
  60.             if (howMany > 0) {
  61.                 partQuantity = howMany;
  62.             }
  63.             else partQuantity = 0;
  64.         }
  65.  
  66. std::string getNum() const {
  67.     return partNum;
  68. }
  69.  
  70. void setNum(std::string partNumber) {
  71.     partNum = partNumber;
  72.     }
  73.  
  74. std::string getDescrip() const {
  75.     return partDescrip;
  76.     }
  77.  
  78. void setDescrip(std::string partName) {
  79.     partDescrip = partName;
  80. }
  81.  
  82. int getPrice() const{
  83.      return partPrice;
  84. }
  85.  
  86. void setPrice(int partCost) {
  87.     if (partCost > 0)
  88.         partPrice = partCost;
  89.     else partPrice = 0;
  90. }
  91.  
  92. int getQuantity() const {
  93.     return partQuantity;
  94. }
  95.  
  96. void setQuantity(int howMany) {
  97.     if (howMany > 0)
  98.         partQuantity = howMany;
  99.     else partQuantity = 0;
  100. }
  101. int getInvoiceAmount() {
  102.     int invoiceAmount{};
  103.     invoiceAmount = getPrice() * getQuantity();
  104.     return invoiceAmount;
  105. }
  106.  
  107.  
  108. private:
  109.     std::string partNum;
  110.     std::string partDescrip;
  111.     int partPrice;
  112.     int partQuantity;
  113. };
  114.  
  115.  
  116.  
  117. //Exercise 3.10 main file
  118. //This program demonstrates the functions of the Invoice.h class
  119.  
  120. #include <iostream>
  121. #include <string>
  122. #include "Invoice.h"
  123. using namespace std;
  124.  
  125. string partNumber{};
  126. string partName{};
  127. int partCost{};
  128. int howMany{};
  129. int main()
  130. {
  131.     Invoice invoice1{"12345", "Hammer", 10, 5};
  132.  
  133.    cout << "Part Number: " << invoice1.getNum() << endl;
  134.    cout << "Part Description: " << invoice1.getDescrip() << endl;
  135.    cout << "Price per part: " << invoice1.getPrice() << endl;
  136.    cout << "Quantity: " << invoice1.getQuantity() << endl;
  137.    cout << "Invoice Total: " << invoice1.getInvoiceAmount() << endl;
  138.  
  139.    cout << "Please enter a part number: " << endl;
  140.    getline (cin, partNumber);
  141.    invoice1.setNum(partNumber);
  142.    invoice1.getNum();
  143.  
  144.    cout << "Please enter a part description: " << endl;
  145.    getline (cin, partName);
  146.    invoice1.setDescrip(partName);
  147.    invoice1.getDescrip();
  148.  
  149.    cout << "Please enter a price per item: " << endl;
  150.    cin >> partCost;
  151.    invoice1.setPrice(partCost);
  152.    invoice1.getPrice();
  153.  
  154.    cout << "Please enter the number of parts: " << endl;
  155.    cin >> howMany;
  156.    invoice1.setQuantity(howMany);
  157.    invoice1.getQuantity();
  158.    invoice1.getInvoiceAmount();
  159.  
  160.    cout << "New Part Number: " << invoice1.getNum() << endl;
  161.    cout << "New Part Description: " << invoice1.getDescrip() << endl;
  162.    cout << "New Price per part: " << invoice1.getPrice() << endl;
  163.    cout << "New Quantity: " << invoice1.getQuantity() << endl;
  164.    cout << "New Invoice Total: " << invoice1.getInvoiceAmount() << endl;
  165.  
  166.    return 0;
  167. }
  168.  
  169.  
  170.  
  171. //Exercise 3.11 header file
  172. class Employee {
  173. public:
  174.     Employee(std::string name1, std::string name2, int earnings)
  175.         :firstName{name1}, lastName{name2} {
  176.             if (earnings > 0)
  177.                 monthSalary = earnings;
  178.             else monthSalary = 0;
  179.         }
  180.  
  181.     std::string getFirstName() const {
  182.         return firstName;}
  183.  
  184.     std::string getLastName() const {
  185.         return lastName;}
  186.  
  187.     int getYearSalary() const {
  188.         return monthSalary*12;}
  189.  
  190.     void raise() {
  191.         monthSalary *= 1.1;
  192.     }
  193.  
  194. private:
  195.     std::string firstName;
  196.     std::string lastName;
  197.     int monthSalary;
  198.  
  199. };
  200.  
  201.  
  202. //Exercise 3.11 main file
  203. #include <iostream>
  204. #include <string>
  205. #include "Employee.h"
  206. using namespace std;
  207.  
  208. int main()
  209. {
  210.  
  211.     Employee employee1{"Jim", "Halpert", 5000};
  212.     Employee employee2{"Dwight", "Schrute", 4000};
  213.  
  214.     cout << "Employee 1 Name: " << employee1.getFirstName() << " " << employee1.getLastName() << endl;
  215.     cout << "Employee 1 Yearly Salary: " << employee1.getYearSalary() << "\n" << endl;
  216.     cout << "Employee 2 Name: " << employee2.getFirstName() << " " << employee2.getLastName() << endl;
  217.     cout << "Employee 2 Yearly Salary: " << employee2.getYearSalary() << endl;
  218.  
  219.     cout << "\nEmployees are given a 10% raise" << endl;
  220.     employee1.raise();
  221.     employee2.raise();
  222.  
  223.     cout << "\nEmployee 1 Name: " << employee1.getFirstName() << " " << employee1.getLastName() << endl;
  224.     cout << "Employee 1 Yearly Salary with 10% raise: " << employee1.getYearSalary() << "\n" << endl;
  225.     cout << "Employee 2 Name: " << employee2.getFirstName() << " " << employee2.getLastName() << endl;
  226.     cout << "Employee 2 Yearly Salary with 10% raise: " << employee2.getYearSalary() << endl;
  227.  
  228. }
  229.  
  230.  
  231. //Exercise 3.12 header file
  232.  
  233. class Date{
  234.  
  235. public:
  236.     Date (int whatMonth, int whatDay, int whatYear)
  237.         : day{whatDay}, year{whatYear} {
  238.             if ( (0 < whatMonth) && (whatMonth <= 12) )
  239.                 month = whatMonth;
  240.             else month = 1;
  241.         }
  242.  
  243.     int getDay() const {
  244.         return day;}
  245.  
  246.     void setDay(int whatDay) {
  247.         day = whatDay;
  248.     }
  249.  
  250.     int getMonth() const {
  251.         return month;
  252.     }
  253.  
  254.     void setMonth(int whatMonth) {
  255.             if ( (0 < whatMonth) && (whatMonth <= 12) )
  256.                 month = whatMonth;
  257.             else month = 1;
  258.     }
  259.  
  260.     int getYear() const {
  261.         return year;
  262.     }
  263.  
  264.     void setYear(int whatYear) {
  265.         year = whatYear;
  266.     }
  267.  
  268.     void displayDate() {
  269.         std::cout << getMonth() << "/" << getDay() << "/" << getYear() << std::endl;
  270.     }
  271. private:
  272.     int month;
  273.     int day;
  274.     int year;
  275. };
  276.  
  277.  
  278.  
  279. //Exercise 3.12 main file
  280.  
  281.  
  282. #include <iostream>
  283. #include <string>
  284. #include "Date.h"
  285. using namespace std;
  286.  
  287. int whatDay{};
  288. int whatMonth{};
  289. int whatYear{};
  290. int main()
  291. {
  292.     Date theDate{1, 29, 2018};
  293.  
  294.     cout << "Initialized date: ";
  295.     theDate.displayDate();
  296.  
  297.     cout << "set a new month:";
  298.     cin >> whatMonth;
  299.     theDate.setMonth(whatMonth);
  300.  
  301.     cout << "set a new day:";
  302.     cin >> whatDay;
  303.     theDate.setDay(whatDay);
  304.  
  305.     cout << "set a new year:";
  306.     cin >> whatYear;
  307.     theDate.setYear(whatYear);
  308.  
  309.     cout << "The new date is: ";
  310.     theDate.displayDate();
  311.  
  312.  
  313. }
  314.  
  315.  
  316. //Exercise 3.13 - Removing duplicated code
  317.  
  318.  
  319. // Fig. 3.9: AccountTest.cpp
  320. // Displaying and updating Account balances.
  321. #include <iostream>
  322. #include "Account.h"
  323.  
  324. using namespace std;
  325.  
  326. void displayAccount (Account accountToDisplay) {
  327.  cout << "Account: " << accountToDisplay.getName()
  328.  << " balance is $" << accountToDisplay.getBalance() << endl;
  329.  }
  330.  
  331. int main()
  332. {
  333.    Account account1{"Jane Green", 50};
  334.    Account account2{"John Blue", -7};
  335.  
  336.  
  337.    // display initial balance of each object
  338.    displayAccount(account1);
  339.    displayAccount(account2);
  340.  
  341.    cout << "\n\nEnter deposit amount for account1: "; // prompt
  342.    int depositAmount;
  343.    cin >> depositAmount; // obtain user input
  344.    cout << "adding " << depositAmount << " to account1 balance";
  345.    account1.deposit(depositAmount); // add to account1's balance
  346.  
  347.    // display balances
  348.    displayAccount(account1);
  349.    displayAccount(account2);
  350.  
  351.    cout << "\n\nEnter deposit amount for account2: "; // prompt
  352.    cin >> depositAmount; // obtain user input
  353.    cout << "adding " << depositAmount << " to account2 balance";
  354.    account2.deposit(depositAmount); // add to account2 balance
  355.  
  356.    // display balances
  357.    displayAccount(account1);
  358.    displayAccount(account2);
  359. }
  360.  
  361. /**************************************************************************
  362.  * (C) Copyright 1992-2017 by Deitel & Associates, Inc. and               *
  363.  * Pearson Education, Inc. All Rights Reserved.                           *
  364.  *                                                                        *
  365.  * DISCLAIMER: The authors and publisher of this book have used their     *
  366.  * best efforts in preparing the book. These efforts include the          *
  367.  * development, research, and testing of the theories and programs        *
  368.  * to determine their effectiveness. The authors and publisher make       *
  369.  * no warranty of any kind, expressed or implied, with regard to these    *
  370.  * programs or to the documentation contained in these books. The authors *
  371.  * and publisher shall not be liable in any event for incidental or       *
  372.  * consequential damages in connection with, or arising out of, the       *
  373.  * furnishing, performance, or use of these programs.                     *
  374.  *************************************************************************/
Add Comment
Please, Sign In to add comment