Advertisement
Guest User

CHike Product.cpp

a guest
Mar 23rd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.29 KB | None | 0 0
  1.  
  2. #define _CRT_SECURE_NO_WARNINGS
  3.  
  4. #include <iostream>
  5. #include <cstring>
  6. #include "Product.h"
  7. #include "Utilities.h"
  8. #include "ErrorState.h"
  9.  
  10. using namespace std;
  11.  
  12. namespace ama {
  13.  
  14.  
  15.     Product::Product(const char ProductType) : m_product_type(ProductType) {
  16.        
  17.         m_quantity_needed = 0;
  18.         m_quantity_avail = 0;
  19.         m_pricePretax = 0.00;
  20.         m_taxable = false;
  21.         m_sku[0] = '\0';
  22.         m_unit[0] = '\0';
  23.         m_product_name = nullptr;
  24.     }
  25.  
  26.     Product::Product(const char* SKU, const char* name, const char* unit, double pricePretax, int quantityNeeded, int quantityAvailable, bool taxable) : m_product_type ('N'){
  27.    
  28.         if (name == nullptr) {
  29.            
  30.             *this = Product();
  31.         }
  32.         else{
  33.  
  34.             this->m_product_name = nullptr;
  35.             this->m_quantity_needed = quantityNeeded;
  36.             this->m_quantity_avail = quantityAvailable;
  37.             this->m_pricePretax = pricePretax;
  38.             this->m_taxable = taxable;
  39.             strcpy(this->m_sku, SKU);
  40.             strcpy(this->m_unit, unit);
  41.             this->m_product_name = new char[strlen(name) + 1];
  42.         }
  43.     }
  44.  
  45.  
  46.     void Product::myDestructor() {
  47.    
  48.         delete[] this->m_product_name;
  49.         this->m_product_name = nullptr;
  50.     }
  51.  
  52.     Product::~Product() {
  53.        
  54.         myDestructor();
  55.     }
  56.  
  57.  
  58.     int Product::operator+=(int cnt) {
  59.        
  60.         if (cnt > 0) {
  61.        
  62.             this->m_quantity_avail += cnt;
  63.         }
  64.         return this->m_quantity_avail;
  65.     }
  66.  
  67.  
  68.     bool Product::operator==(const char* sku) const {
  69.    
  70.         if (strcmp(this->m_sku, sku)== 0) {
  71.        
  72.             return true;
  73.         }
  74.         else {
  75.            
  76.             return false;
  77.         }
  78.     }
  79.  
  80.     bool Product::operator> (const char* sku) const {
  81.    
  82.         if (strcmp(this->m_sku, sku)>0) {
  83.        
  84.             return true;
  85.         }
  86.         else {
  87.            
  88.             return false;
  89.         }
  90.     }
  91.  
  92.     bool Product::operator> (const Product& rhsProduct) const {
  93.    
  94.         if (strcmp(this->m_product_name, rhsProduct.m_product_name) > 0) {
  95.  
  96.             return true;
  97.         }
  98.         else {
  99.  
  100.             return false;
  101.         }
  102.     }
  103.  
  104.     int Product::qtyAvailable() const {
  105.        
  106.         return this->m_quantity_avail;
  107.     }
  108.  
  109.  
  110.     int Product::qtyNeeded() const {
  111.    
  112.         return this->m_quantity_needed;
  113.     }
  114.  
  115.  
  116.     double Product::total_cost() const {
  117.    
  118.         double taxAmt = 0.00;
  119.         double totalCost = 0.00;
  120.        
  121.         if (m_taxable == true)
  122.         {
  123.             taxAmt = m_pricePretax * tax_rate;
  124.         }
  125.        
  126.         totalCost = m_pricePretax + taxAmt;
  127.  
  128.         return (m_quantity_avail * totalCost);
  129.     }
  130.  
  131.  
  132.     void Product::message(const char* pText) {
  133.    
  134.         ProductErrors.message();
  135.     }
  136.  
  137.     bool Product::isClear() const{
  138.        
  139.         if (ProductErrors.operator bool() == true) {
  140.            
  141.             return true;
  142.         }
  143.         else {
  144.            
  145.             return false;
  146.         }
  147.     }
  148.  
  149.  
  150.  
  151.     bool Product::isEmpty() const {
  152.    
  153.         if(m_product_name == nullptr) {
  154.            
  155.             return true;
  156.         }
  157.         else {
  158.            
  159.             return false;
  160.         }
  161.     }
  162.  
  163.  
  164.     Product::Product(const Product& other) : m_product_type(other.m_product_type) {
  165.    
  166.         myDestructor();
  167.         *this = other;
  168.     }
  169.  
  170.     Product& Product::operator=(const Product& other) {
  171.    
  172.         if (this != &other) {
  173.        
  174.             *this = Product(other);
  175.         }
  176.         return *this;
  177.     }
  178.  
  179.  
  180.     std::istream& Product::read(std::istream& in, bool interractive) {
  181.    
  182.         int isQuantityNeeded = 0;
  183.         int isQuantityAvail = 0;
  184.         double ispricePretax = 0.00;
  185.         char taxed;
  186.         bool isTaxable = false;
  187.         char isSku[max_length_sku + 1];
  188.         char* isProductname = nullptr;
  189.         isProductname = new char [max_length_name + 1];
  190.         char isUnit[max_length_unit + 1];
  191.  
  192.         if (interractive == true) {
  193.  
  194.             cout.width(max_length_label);
  195.             cout << "SKU: " << endl;
  196.             in >> isSku;
  197.  
  198.             cout.width(max_length_label);
  199.             cout << "Name (no spaces): " << endl;
  200.             in >> isProductname;
  201.  
  202.             cout.width(max_length_label);
  203.             cout << "Unit: " << endl;
  204.             in >> isUnit;
  205.  
  206.             cout.width(max_length_label);
  207.             cout << "Taxed? (y/n): " << endl;
  208.             in >> taxed;
  209.  
  210.             if (taxed == 'Y' || taxed == 'y') {
  211.  
  212.                 isTaxable = true;
  213.             }
  214.             else if (taxed == 'N' || taxed == 'n') {
  215.  
  216.                 isTaxable = false;
  217.             }
  218.             else{
  219.                
  220.                 ProductErrors.message("Only (Y)es or (N)o are acceptable!");
  221.                 in.setstate(ios::failbit);
  222.             }
  223.  
  224.             if (!in.fail()) {
  225.                
  226.                 cout.width(max_length_label);
  227.                 cout << "Price: " << endl;
  228.                 in >> ispricePretax;
  229.  
  230.                 if (in.fail())
  231.                 {
  232.                     ProductErrors.message("Invalid Price Entry!");
  233.                     in.setstate(ios::failbit);
  234.                 }
  235.             }
  236.  
  237.             if (!in.fail()) {
  238.  
  239.                 cout.width(max_length_label);
  240.                 cout << "Quantity on hand: " << endl;
  241.                 in >> isQuantityAvail;
  242.  
  243.                 if (in.fail())
  244.                 {
  245.                     ProductErrors.message("Invalid Quantity Available Entry!");
  246.                     in.setstate(ios::failbit);
  247.                 }
  248.             }
  249.  
  250.             if (!in.fail()) {
  251.  
  252.                 cout.width(max_length_label);
  253.                 cout << "Quantity needed: " << endl;
  254.                 in >> isQuantityNeeded;
  255.  
  256.                 if (in.fail())
  257.                 {
  258.                     ProductErrors.message("Invalid Quantity Needed Entry!");
  259.                     in.setstate(ios::failbit);
  260.                 }
  261.             }
  262.  
  263.             if (!in.fail())
  264.             {
  265.                 Product theProduct = Product(isSku, isProductname, isUnit, isQuantityNeeded, isQuantityAvail, taxed);
  266.                 *this = theProduct;
  267.             }
  268.  
  269.         }
  270.         else {
  271.        
  272.             in.getline(isSku, max_length_label, ',');
  273.             in.getline(isProductname, max_length_label, ',');
  274.             in.getline(isUnit, max_length_label, ',');
  275.  
  276.             in >> ispricePretax;
  277.             in.ignore();
  278.  
  279.             in >> taxed;
  280.             in.ignore();
  281.  
  282.             in >> isQuantityAvail;
  283.             in.ignore();
  284.  
  285.             in >> isQuantityNeeded;
  286.             in.ignore();
  287.  
  288.             *this = Product(isSku, isProductname, isUnit, ispricePretax, isQuantityNeeded, isQuantityAvail, isTaxable);
  289.         }
  290.  
  291.         delete[] isProductname;
  292.         isProductname = nullptr;
  293.         return in;
  294.     }
  295.  
  296.  
  297.     std::ostream& Product::write(std::ostream& out, int writeMode) const
  298.     {
  299.         if (ProductErrors.message() != nullptr)
  300.         {
  301.             out << ProductErrors.message();
  302.             return out;
  303.         }
  304.         else if (isEmpty()){
  305.            
  306.             return out;
  307.         }
  308.         else {
  309.             if (writeMode == write_condensed) {
  310.                
  311.                 out << m_sku << ',' << m_product_name << ',' << m_unit << ',' << m_pricePretax << ',' << m_quantity_avail << ',' << m_quantity_needed << endl;
  312.             }
  313.             else{
  314.  
  315.                 out.fill(' ');
  316.                 out << m_sku;
  317.                 out.fill(' |');
  318.                 out.fill(' ');
  319.                 out << m_product_name;
  320.             }
  321.  
  322.             if (m_taxable == true){
  323.  
  324.                 out << "Price after Tax: " << m_pricePretax * 1.13 << endl;
  325.             }
  326.             else{
  327.  
  328.                 out << "Price after Tax: " << m_pricePretax << endl;
  329.             }
  330.  
  331.             if (strlen(m_product_name) > 16){
  332.  
  333.                 for (int i = 0; i < 13; i++){
  334.  
  335.                     out << m_product_name[i];
  336.                 }
  337.                 out << "...";
  338.             }
  339.             else{
  340.  
  341.                 out << m_product_name;
  342.             }
  343.         }
  344.  
  345.         return out;
  346.     }
  347.      
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement