diff --git a/README.md b/README.md index 52c5e21..c53bdfe 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ Project 1 Group members: -Ada Lovelace adalovelace@csu.fullerton.edu +Sarah Nuno saritanu@csu.fullerton.edu -Charles Babbage charlesbab@csu.fullerton.edu +Momtaz Afredi mafredi1@csu.fullerton.edu + +Elias Perez elias@csu.fullerton.edu diff --git a/catalog.hpp b/catalog.hpp index ff7d1d8..692eeeb 100644 --- a/catalog.hpp +++ b/catalog.hpp @@ -1,4 +1,3 @@ - // catalog.hpp // CSUF CPSC 131, Fall 2016, Project 1 // @@ -13,99 +12,166 @@ // or cereal. class Product { public: - // Create a product with a given code, name, and price. - // - // code is intended to be a UPC code (bar code) or PLU code in a - // string. - // - // name may be any string. - // - // price is in units of dollars. It must be positive, or else this - // function throws std::invalid_argument. - Product(const std::string& code, - const std::string& name, - double price) { - // TODO: implement this function properly - throw std::logic_error("not implemented yet"); - } - - ~Product() { } - - // Accessors. - const std::string& getCode() const { return code; } - const std::string& getName() const { return name; } - double getPrice() const { return price; } - + // Create a product with a given code, name, and price. + // + // code is intended to be a UPC code (bar code) or PLU code in a + // string. + // + + Product() { + code = "NULL"; + name = "NULL"; + price = 0; + } + + // name may be any string. + // + // price is in units of dollars. It must be positive, or else this + // function throws std::invalid_argument. + Product(const std::string& _code, + const std::string& _name, + double _price) { + // TODO: implement this function properly + if (_price < 0 || _code.length() > 14) { + throw std::invalid_argument("price must be positive and code length must be 14"); + } + else { + name = _name; + code = _code; + price = _price; + } + } + + + // Accessors. + const std::string& getCode() const { return code; } + const std::string& getName() const { return name; } + double getPrice() const { return price; } + private: - std::string code, name; - double price; + std::string code, name; + double price; }; // A catalog represents a collection of all of the products available // at a store. class Catalog { public: - // Create a new catalog. - // - // maxProducts is the maximum number of products that can be - // stored. It must be positive, or else this function throws - // std::invalid_argument. - Catalog(int maxProducts) { - // TODO: implement this function properly - throw std::logic_error("not implemented yet"); - } - - ~Catalog() { - // TODO: implement this function properly - throw std::logic_error("not implemented yet"); - } - - // Accessors. - int getMaxProducts() const { - // TODO: implement this function properly - throw std::logic_error("not implemented yet"); - } - - int getNumProducts() const { - // TODO: implement this function properly - throw std::logic_error("not implemented yet"); - } - - // Return true when the catalog cannot fit any more products. - bool isFull() const { - // TODO: implement this function properly - throw std::logic_error("not implemented yet"); - } - - // Add a new product to the catalog with a given code and name. - // - // code, name, and price have the same meaning as in a Product. If - // price is invalid, throw std::invalid_argument. - // - // If this catalog is already full, throw overflow_error. - // - // Code must be different from all the product codes already in the - // database. If it's not, throw std::invalid_argument. - void addProduct(const std::string& code, - const std::string& name, - double price) { - // TODO: implement this function properly - throw std::logic_error("not implemented yet"); - } - - // Find a product by its code. - // - // code is a product code. - // - // Returns a const reference to the product with the matching code. - // - // Throw std::invalid_argument if no product with that code exists - // in the catalog. - const Product& findCode(const std::string& code) const { - // TODO: implement this function properly - throw std::logic_error("not implemented yet"); - } + // Create a new catalog. + // + // maxProducts is the maximum number of products that can be + // stored. It must be positive, or else this function throws + // std::invalid_argument. + Catalog(int _maxProducts) { + // TODO: implement this function properly + if (_maxProducts > 130000) + { + _maxProducts = 130000; + } + maxProducts = _maxProducts; + if (maxProducts <= 0) + { + throw std::invalid_argument("max must be positive integer"); + } + else + { + ptr = new Product[maxProducts]; + } + + } + + ~Catalog() { + // TODO: implement this function properly + delete[] ptr; + ptr = NULL; + + } + + // Accessors. + int getMaxProducts() const { + // TODO: implement this function properly + return maxProducts; + } + + int getNumProducts() const { + // TODO: implement this function properly + return numProducts; + } + + // Return true when the catalog cannot fit any more products. + bool isFull() const { + // TODO: implement this function properly + if (numProducts == maxProducts) { + return true; + + } + else { + return false; + } + } + + // Add a new product to the catalog with a given code and name. + // + // code, name, and price have the same meaning as in a Product. If + // price is invalid, throw std::invalid_argument. + // + // If this catalog is already full, throw overflow_error. + // + // Code must be different from all the product codes already in the + // database. If it's not, throw std::invalid_argument. + void addProduct(const std::string& _code, + const std::string& _name, + double _price) { + // TODO: implement this function properly + + if (isFull()) + { + throw std::overflow_error("No entry can be made, Catalog is full"); + } + if (!isFull()) + { + if (_price <= 0.0) + throw std::invalid_argument("Price must be greater than 0."); + else + { + for (int i = 0; i < numProducts; i++) + { + if (ptr[i].getCode() == _code) + throw std::invalid_argument("Already exist"); + } + } + } + Product temp(_code, _name, _price); + ptr[numProducts] = temp; + numProducts++; + } + + // Find a product by its code. + // + // code is a product code. + // + // Returns a const reference to the product with the matching code. + // + // Throw std::invalid_argument if no product with that code exists + // in the catalog. + const Product& findCode(const std::string& _code) const + { + // TODO: implement this function properly + for (int i = 0; i < numProducts; i++) + { + if (ptr[i].getCode() == _code) + { + return ptr[i]; + } + + } + throw std::invalid_argument("search failed"); + } + private: - // TODO: add data members + // TODO: add data members + Product *ptr; + int maxProducts; + int numProducts = 0; };