Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

FILL IN PARTS THAT SAY //to be completed Main.cpp //**************************************************************************************** // // INCLUDE FILES // //**************************************************************************************** #include #include #include #include #include Catalog.h #include GetLine.h using

FILL IN PARTS THAT SAY "//to be completed"

Main.cpp

//**************************************************************************************** // // INCLUDE FILES // //**************************************************************************************** #include #include #include #include

#include "Catalog.h" #include "GetLine.h"

using namespace std;

//**************************************************************************************** // // CONSTANT DEFINITIONS // //****************************************************************************************

//**************************************************************************************** // // CLASSES, TYPEDEFS AND STRUCTURES // //****************************************************************************************

//**************************************************************************************** // // PUBLIC DATA // //****************************************************************************************

//**************************************************************************************** // // PRIVATE DATA // //****************************************************************************************

//**************************************************************************************** // // FUNCTION PROTOTYPES // //**************************************************************************************** uint64_t TestAddCategory();

uint64_t TestAddProduct();

uint64_t TestLoad();

uint64_t TestShowAll();

uint64_t TestShowCategory();

uint64_t TestShowProduct();

bool Verify(stringstream& actualStream, stringstream& expectedStream);

//**************************************************************************************** // // MAIN PROGRAM // //**************************************************************************************** int main (int argc, char * const argv[]) { //************************************************************************************ // LOCAL DATA uint64_t score; //************************************************************************************ // EXECUTABLE STATEMENTS score = 0; score += TestAddCategory(); score += TestAddProduct(); score += TestLoad(); score += TestShowProduct(); score += TestShowCategory(); score += TestShowAll();

cout << endl << "Passed: " << score << " out of 18 tests" << endl; cout << endl << "** Finished **" << endl;

// system("pause"); return(0); }

//**************************************************************************************** // // TestAddCategory // //**************************************************************************************** uint64_t TestAddCategory() { //************************************************************************************ // LOCAL DATA stringstream actualStream; Catalog catalog; stringstream expectedStream; uint64_t score;

bool success; //************************************************************************************ // EXECUTABLE STATEMENTS cout << "TestAddCategory" << endl;

score = 0; cout << " Valid category." << endl; success = catalog.AddCategory(1, "Category One"); success &= catalog.AddCategory(2, "Category Two"); success &= catalog.AddCategory(3, "Category Three"); uint64_t categoryCount = catalog.GetCategoryCount(); cout << " Category count." << endl; if (categoryCount == 3) { score += 1; cout << " Pass." << endl; } else { cout << " Fail. Actual: " << categoryCount << ". Expected: 3" << endl; }

cout << " Duplicate category." << endl; success &= catalog.AddCategory(3, "Category Duplicate Three"); if (!success) { cout << " Pass." << endl; score += 1; } else { cout << " Fail. Duplicate not detected." << endl; }

return(score); }

//**************************************************************************************** // // TestAddProduct // //**************************************************************************************** uint64_t TestAddProduct() { //************************************************************************************ // LOCAL DATA stringstream actualStream; Catalog catalog; stringstream expectedStream; uint64_t score; bool success; //************************************************************************************ // EXECUTABLE STATEMENTS cout << endl << "TestAddProduct" << endl; score = 0; cout << " Valid category." << endl; success = catalog.AddCategory(1, "Category One"); success &= catalog.AddProduct(1, 1, "Product One"); success &= catalog.AddProduct(1, 2, "Product Two"); success &= catalog.AddProduct(1, 3, "Product Three"); cout << " Product count for category 1." << endl; int64_t productCount = catalog.GetProductCount(1); if (productCount == 3) { score += 1; cout << " Pass." << endl; } else { cout << " Fail. Actual: " << productCount << ". Expected: 3" << endl; } cout << " Duplicate product." << endl; success = catalog.AddProduct(1, 3, "Product Duplicate Three"); if (!success) { cout << " Pass." << endl; score += 1; } else { cout << " Fail. Duplicate not detected." << endl; } cout << " Nonexistent category." << endl; success &= catalog.AddProduct(2, 3, "Nonexistent category"); if (!success) { cout << " Pass." << endl; score += 1; } else { cout << " Fail. Nonexistent category not detected." << endl; } return(score); }

//**************************************************************************************** // // TestLoad // //**************************************************************************************** uint64_t TestLoad() { //************************************************************************************ // LOCAL DATA stringstream actualStream; Catalog catalog1; Catalog catalog2; uint64_t categoryCount; stringstream expectedStream; uint64_t productCount; uint64_t score; bool success;

//************************************************************************************ // EXECUTABLE STATEMENTS cout << endl << "TestLoad" << endl; score = 0; cout << " Load small catalog." << endl; cout << " Category count." << endl; success = catalog1.Load("SmallCatalog.txt"); if (success) { categoryCount = catalog1.GetCategoryCount(); if (categoryCount == 3) { score += 1; cout << " Pass." << endl; } else { cout << " Fail. Actual: " << categoryCount << ". Expected: 3" << endl; }

cout << " Product count for category Jewelry." << endl; productCount = catalog1.GetProductCount(40506); if (productCount == 3) { score += 1; cout << " Pass." << endl; } else { cout << " Fail. Actual: " << productCount << ". Expected: 3" << endl; }

cout << " Product count for category Toys." << endl; productCount = catalog1.GetProductCount(78162); if (productCount == 3) { score += 1; cout << " Pass." << endl; } else { cout << " Fail. Actual: " << productCount << ". Expected: 3" << endl; }

} else { cout << " Fail. Couldn't load file." << endl; }

cout << " Load large catalog." << endl; success = catalog2.Load("LargeCatalog.txt"); if (success) { cout << " Category count." << endl; categoryCount = catalog2.GetCategoryCount(); if (categoryCount == 18) { score += 1; cout << " Pass." << endl; } else { cout << " Fail. Actual: " << categoryCount << ". Expected: 18" << endl; } cout << " Product count for category 8." << endl; productCount = catalog2.GetProductCount(8); if (productCount == 149) { score += 1; cout << " Pass." << endl; } else { cout << " Fail. Actual: " << productCount << ". Expected: 149" << endl; } } else { cout << " Fail. Couldn't load file." << endl; } return(score); }

//**************************************************************************************** // // TestShowAll // //**************************************************************************************** uint64_t TestShowAll() { //************************************************************************************ // LOCAL DATA stringstream actualStream; Catalog catalog; stringstream expectedStream; uint64_t score; stringstream stream; bool success; //************************************************************************************ // EXECUTABLE STATEMENTS cout << endl << "TestShowAll" << endl; score = 0; cout << " Empty catalog." << endl; success = catalog.ShowAll(actualStream); if (!success) { cout << " Pass." << endl; score += 1; } else { cout << " Fail." << endl; }

cout << " Populated catalog." << endl; success = catalog.AddCategory(1, "Category One"); success &= catalog.AddProduct(1, 1, "Product One"); success &= catalog.AddProduct(1, 2, "Product Two"); success &= catalog.AddProduct(1, 3, "Product Three"); success = catalog.AddCategory(2, "Category Two"); success &= catalog.AddProduct(2, 4, "Product Four"); success &= catalog.AddProduct(2, 5, "Product Five"); success &= catalog.AddProduct(2, 6, "Product Six"); catalog.ShowAll(actualStream); expectedStream << "Category\t1\tCategory One" << endl << "1\tProduct One" << endl << "2\tProduct Two" << endl << "3\tProduct Three" << endl << "Category\t2\tCategory Two" << endl << "4\tProduct Four" << endl << "5\tProduct Five" << endl << "6\tProduct Six"; if (Verify(actualStream, expectedStream)) { cout << " Pass." << endl; score += 1; } else { cout << " Fail." << endl; } return(score); }

//**************************************************************************************** // // TestShowCategory // //**************************************************************************************** uint64_t TestShowCategory() { //************************************************************************************ // LOCAL DATA stringstream actualStream; Catalog catalog; stringstream expectedStream; uint64_t score; stringstream stream; bool success; //************************************************************************************ // EXECUTABLE STATEMENTS cout << endl << "TestShowCategory" << endl; score = 0; cout << " Empty catalog." << endl; success = catalog.ShowCategory(actualStream, 2); if (!success) { cout << " Pass." << endl; score += 1; } else { cout << " Fail. ShowCategory should return false when catalog is empty." << endl; } cout << " Populated catalog." << endl; success &= catalog.AddCategory(1, "Category One"); success &= catalog.AddProduct(1, 1, "Product One"); success &= catalog.AddProduct(1, 2, "Product Two"); success &= catalog.AddProduct(1, 3, "Product Three"); success = catalog.AddCategory(2, "Category Two"); success &= catalog.AddProduct(2, 4, "Product Four"); success &= catalog.AddProduct(2, 5, "Product Five"); success &= catalog.AddProduct(2, 6, "Product Six"); success = catalog.ShowCategory(actualStream, 2); if (success) { expectedStream << "Category\t2\tCategory Two" << endl << "4\tProduct Four" << endl << "5\tProduct Five" << endl << "6\tProduct Six"; if (Verify(actualStream, expectedStream)) { cout << " Pass." << endl; score += 1; } else { cout << " Fail." << endl; } } else { cout << " Fail." << endl; } cout << " Nonexistent category." << endl; success = catalog.ShowCategory(actualStream, 3); if (!success) { cout << " Pass." << endl; score += 1; } else { cout << " Fail." << endl; }

return(score); }

//**************************************************************************************** // // TestShowProduct // //**************************************************************************************** uint64_t TestShowProduct() { //************************************************************************************ // LOCAL DATA stringstream actualStream; Catalog catalog; stringstream expectedStream; uint64_t score; stringstream stream; bool success; //************************************************************************************ // EXECUTABLE STATEMENTS cout << endl << "TestShowProduct" << endl; score = 0; success = catalog.AddCategory(1, "Category One"); success &= catalog.AddProduct(1, 1, "Product One"); success &= catalog.AddProduct(1, 2, "Product Two"); success &= catalog.AddProduct(1, 3, "Product Three"); success = catalog.AddCategory(2, "Category Two"); success &= catalog.AddProduct(2, 4, "Product Four"); success &= catalog.AddProduct(2, 5, "Product Five"); success &= catalog.AddProduct(2, 6, "Product Six");

cout << " Valid category and product." << endl; success = catalog.ShowProduct(actualStream, 1, 3); if (success) { expectedStream << "3\tProduct Three"; if (Verify(actualStream, expectedStream)) { cout << " Pass." << endl; score += 1; } else { cout << " Fail." << endl; } } else { cout << " Fail." << endl; } cout << " Nonexistent category." << endl; success = catalog.ShowProduct(actualStream, 3, 3); if (!success) { cout << " Pass." << endl; score += 1; } else { cout << " Fail." << endl; } cout << " Valid category, nonexistent product." << endl; success = catalog.ShowProduct(actualStream, 1, 4); if (!success) { cout << " Pass." << endl; score += 1; } else { cout << " Fail." << endl; } return(score); }

//**************************************************************************************** // // Verify // //**************************************************************************************** bool Verify(stringstream& actualStream, stringstream& expectedStream) { //************************************************************************************ // LOCAL DATA vector actualText; vector expectedText;

string line;

bool success; //************************************************************************************ // EXECUTABLE STATEMENTS while (true) { GetLine(actualStream, line); if (!actualStream.fail()) { actualText.push_back(line); } else { break; } } while (true) { GetLine(expectedStream, line); if (!expectedStream.fail()) { expectedText.push_back(line); } else { break; } } if (actualText != expectedText) { cout << "Actual text doesn't match expected text." << endl; cout << "Actual:" << endl; for (size_t i = 0; i < actualText.size(); ++i) { cout << actualText[i] << endl; } cout << "Expected:" << endl; for (size_t i = 0; i < expectedText.size(); ++i) { cout << expectedText[i] << endl; } success = false; } else { success = true; }

return(success); }

#if 0 //**************************************************************************************** // // Class::Function // //**************************************************************************************** void Class::Function() { //************************************************************************************ // LOCAL DATA

//************************************************************************************ // EXECUTABLE STATEMENTS

return; }

#endif

-----------------------------------------------------------------------------------------------------

Catalog.cpp

//**************************************************************************************** // // INCLUDE FILES // //**************************************************************************************** #include #include #include

#include "Catalog.h"

using namespace std;

bool Catalog::AddCategory(uint64_t categoryNumber, const string& name) { // to be completed return true; }

bool Catalog::AddProduct(uint64_t categoryNumber, uint64_t productNumber, const string& name) { // to be completed return true; }

uint64_t Catalog::GetCategoryCount() { // to be completed return 0; }

int64_t Catalog::GetProductCount(uint64_t categoryNumber) { // to be completed return 1; }

bool Catalog::Load(const string& fileName) { // to be completed return true; }

//**************************************************************************************** // // Show... methods // // The stream argument allows thess functions to write its output to any output stream, // such as cout or a file, that the caller passes. The argument's name "stream" should // be used just as you would use "cout": // // stream << "X = " << x << endl; // // The output will go where the caller intended without special action by this function. // //****************************************************************************************

bool Catalog::ShowProduct(ostream& stream, uint64_t categoryNumber, uint64_t productNumber) { // to be completed return true; }

bool Catalog::ShowCategory(ostream& stream, uint64_t categoryNumber) { // to be completed return true; }

bool Catalog::ShowAll(ostream& stream) { // to be completed return true; }

?-------------------------------------------------------------------------------------

Catalog.h

#ifndef Catalog_h #define Catalog_h

//**************************************************************************************** // // INCLUDE FILES // //**************************************************************************************** #include #include

using namespace std;

//**************************************************************************************** // // CONSTANT DEFINITIONS // //****************************************************************************************

//**************************************************************************************** // // CLASSES, TYPEDEFS AND STRUCTURES // //****************************************************************************************

class Catalog { public: //Given a category number and name, add it to the catalog. It will have an empty product list. //Return false if the category number already exists in the catalog, true otherwise. bool AddCategory(uint64_t categoryNumber, const string& name); //Return the number of categories in the catalog uint64_t GetCategoryCount();

//Given a category number, a product number, and a product name, add the product to the catalog. //Return false if the category number doesnt exist in the catalog or if the product number already exists within the category, true otherwise. bool AddProduct(uint64_t categoryNumber, uint64_t productNumber, const string& name); //Given a category number, return the number of products in the category; return -1 if the category doesnt exist. int64_t GetProductCount(uint64_t categoryNumber); //Load the catalog from a file, given the files name. //Return false if the catalog cant be loaded, either because the file doesnt exist or isnt in the correct format. bool Load(const string& fileName); //Given a category number and a product number, show the product number and name separated by a tab. //Return false if the category number doesnt exist in the catalog or if the product number doesnt exist within the category. bool ShowProduct(ostream& stream, uint64_t categoryNumber, uint64_t productNumber);

//Given a category number, show only its products in order by product number. //Return false if the category number doesnt exist in the catalog.Use the same format as the text file in Load. bool ShowCategory(ostream& stream, uint64_t categoryNumber);

//Show the entire catalog, category by category, in order by category number.Under each category, show its products in order by product number.Use the same format as the text file in Load. bool ShowAll(ostream& stream);

private: // map declaration goes here

};

//**************************************************************************************** // // FUNCTION PROTOTYPES // //****************************************************************************************

#endif

--------------------------------------------------------------------------------------------------------

GetLine.cpp

// DO NOT EDIT THIS FILE

//**************************************************************************************** // // INCLUDE FILES // //**************************************************************************************** #include #include

#include "GetLine.h"

using namespace std;

//**************************************************************************************** // // CONSTANT DEFINITIONS // //****************************************************************************************

//**************************************************************************************** // // CLASSES, TYPEDEFS AND STRUCTURES // //****************************************************************************************

//**************************************************************************************** // // MACROS // //****************************************************************************************

//**************************************************************************************** // // PUBLIC DATA // //****************************************************************************************

//**************************************************************************************** // // PRIVATE DATA // //****************************************************************************************

//**************************************************************************************** // // FUNCTION PROTOTYPES // //****************************************************************************************

//**************************************************************************************** // // GetLine // // This function reads a series of characters from a stream and puts it into a string, // stopping when it sees a line break: // 1. A carriage return (CR) // 2. A line feed (LF) // 3. A CR/LF pair // 4. A LF/CR pair // The #3 and #4 pairs are a single line break. // The line break (any of the four) is consumed but not added to the output string. // // The return status is true if at least one character, which can be a line break, is read; // the status is false if an end-of-file is immediately encountered. // //**************************************************************************************** bool GetLine(istream& stream, string& text) { //************************************************************************************ // LOCAL DATA int c; int p; bool success;

//************************************************************************************ // EXECUTABLE STATEMENTS text.erase(); success = false; while (true) { c = stream.get(); if (stream.good()) { success = true; if (c == ' ') { p = stream.peek(); if (p == ' ') { stream.ignore(); } break; } else if (c == ' ') { p = stream.peek(); if (p == ' ') { stream.ignore(); } break; } else { text += c; } } else { break; } }

// If we reached the end of file, but at least one character was seen, // including any delimiter, clear the stream's state so the caller won't // ignore the last line of a file. if (success) { stream.clear(); }

return(success); }

//**************************************************************************************** // // GetLine // // This function reads a series of characters from a stream and puts it into a string, // stopping when it sees any character from a specified delimiter set. The delimiter // is consumed but not added to the output string. // //**************************************************************************************** bool GetLine(istream& stream, string& text, const string& delimiter) { //************************************************************************************ // LOCAL DATA const uint32_t initialMask = 0x80000000;

const uint32_t columnMask = 0x1F;

const uint32_t rowMask = 0x07;

const uint32_t rowShift = 5;

char c;

uint32_t flag[8];

uint32_t i;

uint32_t mask;

uint32_t row;

uint32_t shift;

bool success;

//************************************************************************************ // EXECUTABLE STATEMENTS text.erase();

// Set up flags array. for (row = 0; row < (sizeof(flag) / sizeof(flag[0])); ++row) { flag[row] = 0; } for (i = 0; i < delimiter.size(); ++i) { c = delimiter[i]; row = (c >> rowShift) & rowMask; shift = c & columnMask; mask = initialMask >> shift; flag[row] |= mask; }

// Get characters until a delimiter is seen or the end of the file // is reached. success = false; while (true) { stream.get(c); if (stream.good()) { // Remember that at least one character has been seen. success = true;

// Check for a delimiter. row = (c >> rowShift) & rowMask; shift = c & columnMask; mask = initialMask >> shift; if ((flag[row] & mask) == 0) { // Character isn't a delimiter, save it. text += c; } else { // Character is a delimiter, leave loop. break; } } else { break; } }

// If we reached the end of file, but at least one character was seen, // including any delimiter, clear the stream's state so the caller won't // ignore the last line of a file. if (success) { stream.clear(); }

return(success); }

----------------------------------------------------------------------------------------------

GetLine.h

// DO NOT EDIT THIS FILE

#ifndef GetLine_h #define GetLine_h

//**************************************************************************************** // INCLUDE FILES #include #include

using namespace std;

//**************************************************************************************** // CONSTANT DEFINITIONS

//**************************************************************************************** // CLASSES, TYPEDEFS AND STRUCTURES

//**************************************************************************************** // FUNCTION PROTOTYPES bool GetLine(istream& stream, string& text);

bool GetLine(istream& stream, string& text, const string& delimiter);

#endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 3 Lnai 6323

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

3642159389, 978-3642159381

More Books

Students also viewed these Databases questions

Question

How is intelligence defined?

Answered: 1 week ago