Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <cstring>
- #include "Product.h"
- #include "Utilities.h"
- #include "ErrorState.h"
- using namespace std;
- namespace ama {
- Product::Product(const char ProductType) : m_product_type(ProductType) {
- m_quantity_needed = 0;
- m_quantity_avail = 0;
- m_pricePretax = 0.00;
- m_taxable = false;
- m_sku[0] = '\0';
- m_unit[0] = '\0';
- m_product_name = nullptr;
- }
- Product::Product(const char* SKU, const char* name, const char* unit, double pricePretax, int quantityNeeded, int quantityAvailable, bool taxable) : m_product_type ('N'){
- if (name == nullptr) {
- *this = Product();
- }
- else{
- this->m_product_name = nullptr;
- this->m_quantity_needed = quantityNeeded;
- this->m_quantity_avail = quantityAvailable;
- this->m_pricePretax = pricePretax;
- this->m_taxable = taxable;
- strcpy(this->m_sku, SKU);
- strcpy(this->m_unit, unit);
- this->m_product_name = new char[strlen(name) + 1];
- }
- }
- void Product::myDestructor() {
- delete[] this->m_product_name;
- this->m_product_name = nullptr;
- }
- Product::~Product() {
- myDestructor();
- }
- int Product::operator+=(int cnt) {
- if (cnt > 0) {
- this->m_quantity_avail += cnt;
- }
- return this->m_quantity_avail;
- }
- bool Product::operator==(const char* sku) const {
- if (strcmp(this->m_sku, sku)== 0) {
- return true;
- }
- else {
- return false;
- }
- }
- bool Product::operator> (const char* sku) const {
- if (strcmp(this->m_sku, sku)>0) {
- return true;
- }
- else {
- return false;
- }
- }
- bool Product::operator> (const Product& rhsProduct) const {
- if (strcmp(this->m_product_name, rhsProduct.m_product_name) > 0) {
- return true;
- }
- else {
- return false;
- }
- }
- int Product::qtyAvailable() const {
- return this->m_quantity_avail;
- }
- int Product::qtyNeeded() const {
- return this->m_quantity_needed;
- }
- double Product::total_cost() const {
- double taxAmt = 0.00;
- double totalCost = 0.00;
- if (m_taxable == true)
- {
- taxAmt = m_pricePretax * tax_rate;
- }
- totalCost = m_pricePretax + taxAmt;
- return (m_quantity_avail * totalCost);
- }
- void Product::message(const char* pText) {
- ProductErrors.message();
- }
- bool Product::isClear() const{
- if (ProductErrors.operator bool() == true) {
- return true;
- }
- else {
- return false;
- }
- }
- bool Product::isEmpty() const {
- if(m_product_name == nullptr) {
- return true;
- }
- else {
- return false;
- }
- }
- Product::Product(const Product& other) : m_product_type(other.m_product_type) {
- myDestructor();
- *this = other;
- }
- Product& Product::operator=(const Product& other) {
- if (this != &other) {
- *this = Product(other);
- }
- return *this;
- }
- std::istream& Product::read(std::istream& in, bool interractive) {
- int isQuantityNeeded = 0;
- int isQuantityAvail = 0;
- double ispricePretax = 0.00;
- char taxed;
- bool isTaxable = false;
- char isSku[max_length_sku + 1];
- char* isProductname = nullptr;
- isProductname = new char [max_length_name + 1];
- char isUnit[max_length_unit + 1];
- if (interractive == true) {
- cout.width(max_length_label);
- cout << "SKU: " << endl;
- in >> isSku;
- cout.width(max_length_label);
- cout << "Name (no spaces): " << endl;
- in >> isProductname;
- cout.width(max_length_label);
- cout << "Unit: " << endl;
- in >> isUnit;
- cout.width(max_length_label);
- cout << "Taxed? (y/n): " << endl;
- in >> taxed;
- if (taxed == 'Y' || taxed == 'y') {
- isTaxable = true;
- }
- else if (taxed == 'N' || taxed == 'n') {
- isTaxable = false;
- }
- else{
- ProductErrors.message("Only (Y)es or (N)o are acceptable!");
- in.setstate(ios::failbit);
- }
- if (!in.fail()) {
- cout.width(max_length_label);
- cout << "Price: " << endl;
- in >> ispricePretax;
- if (in.fail())
- {
- ProductErrors.message("Invalid Price Entry!");
- in.setstate(ios::failbit);
- }
- }
- if (!in.fail()) {
- cout.width(max_length_label);
- cout << "Quantity on hand: " << endl;
- in >> isQuantityAvail;
- if (in.fail())
- {
- ProductErrors.message("Invalid Quantity Available Entry!");
- in.setstate(ios::failbit);
- }
- }
- if (!in.fail()) {
- cout.width(max_length_label);
- cout << "Quantity needed: " << endl;
- in >> isQuantityNeeded;
- if (in.fail())
- {
- ProductErrors.message("Invalid Quantity Needed Entry!");
- in.setstate(ios::failbit);
- }
- }
- if (!in.fail())
- {
- Product theProduct = Product(isSku, isProductname, isUnit, isQuantityNeeded, isQuantityAvail, taxed);
- *this = theProduct;
- }
- }
- else {
- in.getline(isSku, max_length_label, ',');
- in.getline(isProductname, max_length_label, ',');
- in.getline(isUnit, max_length_label, ',');
- in >> ispricePretax;
- in.ignore();
- in >> taxed;
- in.ignore();
- in >> isQuantityAvail;
- in.ignore();
- in >> isQuantityNeeded;
- in.ignore();
- *this = Product(isSku, isProductname, isUnit, ispricePretax, isQuantityNeeded, isQuantityAvail, isTaxable);
- }
- delete[] isProductname;
- isProductname = nullptr;
- return in;
- }
- std::ostream& Product::write(std::ostream& out, int writeMode) const
- {
- if (ProductErrors.message() != nullptr)
- {
- out << ProductErrors.message();
- return out;
- }
- else if (isEmpty()){
- return out;
- }
- else {
- if (writeMode == write_condensed) {
- out << m_sku << ',' << m_product_name << ',' << m_unit << ',' << m_pricePretax << ',' << m_quantity_avail << ',' << m_quantity_needed << endl;
- }
- else{
- out.fill(' ');
- out << m_sku;
- out.fill(' |');
- out.fill(' ');
- out << m_product_name;
- }
- if (m_taxable == true){
- out << "Price after Tax: " << m_pricePretax * 1.13 << endl;
- }
- else{
- out << "Price after Tax: " << m_pricePretax << endl;
- }
- if (strlen(m_product_name) > 16){
- for (int i = 0; i < 13; i++){
- out << m_product_name[i];
- }
- out << "...";
- }
- else{
- out << m_product_name;
- }
- }
- return out;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement