Advertisement
algore87

PG1Lab2_2

Apr 21st, 2014
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.31 KB | None | 0 0
  1. //////////////// A 3 - INVOICE Class //////////////////
  2. // PG1Lab2 - Exercise 3
  3. // Invoice.h
  4. // Author:  Alexander Schott
  5. // Date:    21.04.2014
  6.  
  7. #include <iostream>
  8. #include <string>
  9.  
  10. class Invoice{
  11. public:
  12.    Invoice(std::string, std::string, int, double);
  13.    void setPartNumber(std::string);
  14.    std::string getPartNumber() const;
  15.    void setPartDescription(std::string);
  16.    std::string getPartDescription() const;
  17.    void setQuantity(int);
  18.    int getQuantity() const;
  19.    void setPricePerItem(double);
  20.    double getPricePerItem() const;
  21.    double computeInvoiceAmount();
  22. private:
  23.    std::string partNumber;
  24.    std::string partDescription;
  25.    int quantity;
  26.    double pricePerItem;
  27. };
  28.  
  29. // PG1Lab2 - Exercise 3
  30. // Invoice.cpp
  31. // Author:  Alexander Schott
  32. // Date:    21.04.2014
  33.  
  34. #include "Invoice.h"
  35. using namespace std;
  36.  
  37. // constructor
  38. Invoice::Invoice(string number, string description, int size, double price)
  39. : partNumber(number), partDescription(description), quantity(size), pricePerItem(price){
  40.  
  41. }
  42.  
  43. void Invoice::setPartNumber(string number){
  44.    partNumber = number;
  45. }
  46.  
  47. string Invoice::getPartNumber() const{
  48.    return partNumber;
  49. }
  50.  
  51. void Invoice::setPartDescription(string description){
  52.    partDescription = description;
  53. }
  54.  
  55. string Invoice::getPartDescription() const{
  56.    return partDescription;
  57. }
  58.  
  59. void Invoice::setQuantity(int size){
  60.    if (size < 0) quantity = 0;
  61.    else quantity = size;
  62. }
  63.  
  64. int Invoice::getQuantity() const{
  65.    return quantity;
  66. }
  67.  
  68. void Invoice::setPricePerItem(double price){
  69.    if (price < 0.0) pricePerItem = 0.0;
  70.    else pricePerItem = price;
  71. }
  72.  
  73. double Invoice::getPricePerItem() const{
  74.    return pricePerItem;
  75. }
  76.  
  77. double Invoice::computeInvoiceAmount(){
  78.    return quantity*pricePerItem;
  79. }
  80.  
  81. // PG1Lab2 - Exercise 3
  82. // main.cpp
  83. // Author:  Alexander Schott
  84. // Date:    20.04.2014
  85.  
  86. #include "Invoice.h"
  87. using namespace std;
  88.  
  89. int main(){
  90.    Invoice Hammer("123.h3", "Hammer Typ 3", 23, 12.95);
  91.    // Hammer.setPricePerItem(-3.3);    // test negative pricePerItem
  92.    // Hammer.setQuantity(-11);         // test negative quantity
  93.    cout << "----------------------- Invoice " <<
  94.       "-----------------------" << endl;
  95.    cout << "Part #:\tDescription:\tQuantity:\tPrice per Item:\n"
  96.       << Hammer.getPartNumber() << "\t" << Hammer.getPartDescription() << "\t"
  97.       << Hammer.getQuantity() << "\t\t" << Hammer.getPricePerItem() << "\n------"
  98.       << "-------------------------------------------------" << endl;
  99.    cout << "Total: " << Hammer.computeInvoiceAmount() << endl;
  100.    return 0;
  101. }
  102.  
  103. //////////////// A 4 - DATE Class //////////////////
  104. // PG1Lab2 - Exercise 4
  105. // Date.h
  106. // Author:  Alexander Schott
  107. // Date:    21.04.2014
  108. #include <string>
  109.  
  110. class Date{
  111. public:
  112.    Date(int, int, int);
  113.    void setDay(int);
  114.    int getDay() const;
  115.    void setMonth(int);
  116.    int getMonth() const;
  117.    void setYear(int);
  118.    int getYear() const;
  119.    std::string toString() const;
  120. private:
  121.    int day;
  122.    int month;
  123.    int year;
  124. };
  125.  
  126. // PG1Lab2 - Exercise 4
  127. // Date.cpp
  128. // Author:  Alexander Schott
  129. // Date:    21.04.2014
  130.  
  131. #include <iostream>
  132. #include "Date.h"
  133. using namespace std;
  134.  
  135. Date::Date(int d, int m , int y){
  136.    setDay(d);
  137.    setMonth(m);
  138.    setYear(y);
  139. }
  140.  
  141. void Date::setDay(int d){
  142.    day = d;
  143.  
  144. }
  145.  
  146. int Date::getDay() const{
  147.    return day;
  148. }
  149.  
  150. void Date::setMonth(int m){
  151.    if (m < 1 || m > 12) {
  152.       cout << "month value not accepted! 1 <= month <= 12!" << endl <<
  153.          "month is set to 1" << endl;
  154.       month = 1;
  155.    }
  156.    else month = m;
  157. }
  158.  
  159. int Date::getMonth() const{
  160.    return month;
  161. }
  162.  
  163. void Date::setYear(int y){
  164.    year = y;
  165. }
  166.  
  167. int Date::getYear() const{
  168.    return year;
  169. }
  170.  
  171. string Date::toString() const{
  172.    return to_string(getDay()) + "." + to_string(getMonth()) + "." + to_string(getYear());
  173. }
  174.  
  175. // PG1Lab2 - Exercise 4
  176. // main.cpp
  177. // Author:  Alexander Schott
  178. // Date:    21.04.2014
  179. #include <iostream>
  180. #include "Date.h"
  181. using namespace std;
  182.  
  183. int main(){
  184.    int d = 0, m = 0, y = 0;
  185.    Date TestDate(1, 1, 2000);
  186.    cout << "TestDate: " + TestDate.toString() << endl;
  187.    cout << "Input a Testdate (day,month,year): ";
  188.    cin >> d >> m >> y;
  189.    TestDate.setDay(d);
  190.    TestDate.setMonth(m);
  191.    TestDate.setYear(y);
  192.    cout << "TestDate: " + TestDate.toString() << endl;
  193.    return 0;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement