Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Exercise 3.9
- #include <string>
- class Account {
- public:
- Account (std::string accountName, int initialBalance)
- : name{accountName} {
- if (initialBalance > 0){
- balance = initialBalance;
- }
- }
- void deposit(int depositAmount){
- if (depositAmount > 0) {
- balance += depositAmount;
- }
- }
- void withdraw(int withdrawAmount){
- if (balance > withdrawAmount) {
- balance -= withdrawAmount;
- }
- if (withdrawAmount > balance) {
- std::cout << "Withdrawal amount exceeded account balance." << std::endl;
- }
- }
- int getBalance() const{
- return balance;
- }
- void setName(std::string accountName){
- name = accountName;
- }
- std::string getName() const{
- return name;
- }
- private:
- std::string name;
- int balance{0};
- };
- //Exercise 3.10 header file
- //This header file contains the Invoice class for creating simple invoices
- class Invoice{
- public:
- Invoice(std::string partNumber, std::string partName, int partCost, int howMany)
- : partNum{partNumber}, partDescrip{partName} {
- if (partCost > 0){
- partPrice = partCost;
- }
- else partPrice = 0;
- if (howMany > 0) {
- partQuantity = howMany;
- }
- else partQuantity = 0;
- }
- std::string getNum() const {
- return partNum;
- }
- void setNum(std::string partNumber) {
- partNum = partNumber;
- }
- std::string getDescrip() const {
- return partDescrip;
- }
- void setDescrip(std::string partName) {
- partDescrip = partName;
- }
- int getPrice() const{
- return partPrice;
- }
- void setPrice(int partCost) {
- if (partCost > 0)
- partPrice = partCost;
- else partPrice = 0;
- }
- int getQuantity() const {
- return partQuantity;
- }
- void setQuantity(int howMany) {
- if (howMany > 0)
- partQuantity = howMany;
- else partQuantity = 0;
- }
- int getInvoiceAmount() {
- int invoiceAmount{};
- invoiceAmount = getPrice() * getQuantity();
- return invoiceAmount;
- }
- private:
- std::string partNum;
- std::string partDescrip;
- int partPrice;
- int partQuantity;
- };
- //Exercise 3.10 main file
- //This program demonstrates the functions of the Invoice.h class
- #include <iostream>
- #include <string>
- #include "Invoice.h"
- using namespace std;
- string partNumber{};
- string partName{};
- int partCost{};
- int howMany{};
- int main()
- {
- Invoice invoice1{"12345", "Hammer", 10, 5};
- cout << "Part Number: " << invoice1.getNum() << endl;
- cout << "Part Description: " << invoice1.getDescrip() << endl;
- cout << "Price per part: " << invoice1.getPrice() << endl;
- cout << "Quantity: " << invoice1.getQuantity() << endl;
- cout << "Invoice Total: " << invoice1.getInvoiceAmount() << endl;
- cout << "Please enter a part number: " << endl;
- getline (cin, partNumber);
- invoice1.setNum(partNumber);
- invoice1.getNum();
- cout << "Please enter a part description: " << endl;
- getline (cin, partName);
- invoice1.setDescrip(partName);
- invoice1.getDescrip();
- cout << "Please enter a price per item: " << endl;
- cin >> partCost;
- invoice1.setPrice(partCost);
- invoice1.getPrice();
- cout << "Please enter the number of parts: " << endl;
- cin >> howMany;
- invoice1.setQuantity(howMany);
- invoice1.getQuantity();
- invoice1.getInvoiceAmount();
- cout << "New Part Number: " << invoice1.getNum() << endl;
- cout << "New Part Description: " << invoice1.getDescrip() << endl;
- cout << "New Price per part: " << invoice1.getPrice() << endl;
- cout << "New Quantity: " << invoice1.getQuantity() << endl;
- cout << "New Invoice Total: " << invoice1.getInvoiceAmount() << endl;
- return 0;
- }
- //Exercise 3.11 header file
- class Employee {
- public:
- Employee(std::string name1, std::string name2, int earnings)
- :firstName{name1}, lastName{name2} {
- if (earnings > 0)
- monthSalary = earnings;
- else monthSalary = 0;
- }
- std::string getFirstName() const {
- return firstName;}
- std::string getLastName() const {
- return lastName;}
- int getYearSalary() const {
- return monthSalary*12;}
- void raise() {
- monthSalary *= 1.1;
- }
- private:
- std::string firstName;
- std::string lastName;
- int monthSalary;
- };
- //Exercise 3.11 main file
- #include <iostream>
- #include <string>
- #include "Employee.h"
- using namespace std;
- int main()
- {
- Employee employee1{"Jim", "Halpert", 5000};
- Employee employee2{"Dwight", "Schrute", 4000};
- cout << "Employee 1 Name: " << employee1.getFirstName() << " " << employee1.getLastName() << endl;
- cout << "Employee 1 Yearly Salary: " << employee1.getYearSalary() << "\n" << endl;
- cout << "Employee 2 Name: " << employee2.getFirstName() << " " << employee2.getLastName() << endl;
- cout << "Employee 2 Yearly Salary: " << employee2.getYearSalary() << endl;
- cout << "\nEmployees are given a 10% raise" << endl;
- employee1.raise();
- employee2.raise();
- cout << "\nEmployee 1 Name: " << employee1.getFirstName() << " " << employee1.getLastName() << endl;
- cout << "Employee 1 Yearly Salary with 10% raise: " << employee1.getYearSalary() << "\n" << endl;
- cout << "Employee 2 Name: " << employee2.getFirstName() << " " << employee2.getLastName() << endl;
- cout << "Employee 2 Yearly Salary with 10% raise: " << employee2.getYearSalary() << endl;
- }
- //Exercise 3.12 header file
- class Date{
- public:
- Date (int whatMonth, int whatDay, int whatYear)
- : day{whatDay}, year{whatYear} {
- if ( (0 < whatMonth) && (whatMonth <= 12) )
- month = whatMonth;
- else month = 1;
- }
- int getDay() const {
- return day;}
- void setDay(int whatDay) {
- day = whatDay;
- }
- int getMonth() const {
- return month;
- }
- void setMonth(int whatMonth) {
- if ( (0 < whatMonth) && (whatMonth <= 12) )
- month = whatMonth;
- else month = 1;
- }
- int getYear() const {
- return year;
- }
- void setYear(int whatYear) {
- year = whatYear;
- }
- void displayDate() {
- std::cout << getMonth() << "/" << getDay() << "/" << getYear() << std::endl;
- }
- private:
- int month;
- int day;
- int year;
- };
- //Exercise 3.12 main file
- #include <iostream>
- #include <string>
- #include "Date.h"
- using namespace std;
- int whatDay{};
- int whatMonth{};
- int whatYear{};
- int main()
- {
- Date theDate{1, 29, 2018};
- cout << "Initialized date: ";
- theDate.displayDate();
- cout << "set a new month:";
- cin >> whatMonth;
- theDate.setMonth(whatMonth);
- cout << "set a new day:";
- cin >> whatDay;
- theDate.setDay(whatDay);
- cout << "set a new year:";
- cin >> whatYear;
- theDate.setYear(whatYear);
- cout << "The new date is: ";
- theDate.displayDate();
- }
- //Exercise 3.13 - Removing duplicated code
- // Fig. 3.9: AccountTest.cpp
- // Displaying and updating Account balances.
- #include <iostream>
- #include "Account.h"
- using namespace std;
- void displayAccount (Account accountToDisplay) {
- cout << "Account: " << accountToDisplay.getName()
- << " balance is $" << accountToDisplay.getBalance() << endl;
- }
- int main()
- {
- Account account1{"Jane Green", 50};
- Account account2{"John Blue", -7};
- // display initial balance of each object
- displayAccount(account1);
- displayAccount(account2);
- cout << "\n\nEnter deposit amount for account1: "; // prompt
- int depositAmount;
- cin >> depositAmount; // obtain user input
- cout << "adding " << depositAmount << " to account1 balance";
- account1.deposit(depositAmount); // add to account1's balance
- // display balances
- displayAccount(account1);
- displayAccount(account2);
- cout << "\n\nEnter deposit amount for account2: "; // prompt
- cin >> depositAmount; // obtain user input
- cout << "adding " << depositAmount << " to account2 balance";
- account2.deposit(depositAmount); // add to account2 balance
- // display balances
- displayAccount(account1);
- displayAccount(account2);
- }
- /**************************************************************************
- * (C) Copyright 1992-2017 by Deitel & Associates, Inc. and *
- * Pearson Education, Inc. All Rights Reserved. *
- * *
- * DISCLAIMER: The authors and publisher of this book have used their *
- * best efforts in preparing the book. These efforts include the *
- * development, research, and testing of the theories and programs *
- * to determine their effectiveness. The authors and publisher make *
- * no warranty of any kind, expressed or implied, with regard to these *
- * programs or to the documentation contained in these books. The authors *
- * and publisher shall not be liable in any event for incidental or *
- * consequential damages in connection with, or arising out of, the *
- * furnishing, performance, or use of these programs. *
- *************************************************************************/
Add Comment
Please, Sign In to add comment