Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Catalog.h: contains declarations of the Catalog class. You may add other declara

ID: 3713292 • Letter: C

Question

Catalog.h: contains declarations of the Catalog class. You may add other declarations as needed for your implementation. Do not add definitions (code) to this file; definitions of functions belong in catalog.cpp. [TO BE COMPLETED]

Catalog.cpp: contains skeletons for the required member functions. You may add other functions, member and nonmember, as needed for your implementation. [TO BE COMPLETED]

#endif

#ifndef Catalog_h #define Catalog_h //**************************************************************************************** // // INCLUDE FILES // //**************************************************************************************** #include <iostream> #include <string> 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 doesn’t 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 doesn’t exist. int64_t GetProductCount(uint64_t categoryNumber); //Load the catalog from a file, given the file’s name. //Return false if the catalog can’t be loaded, either because the file doesn’t exist or isn’t 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 doesn’t exist in the catalog or if the product number doesn’t 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 doesn’t 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

// //**************************************************************************************** #include <fstream> #include <iostream> #include <string> #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; }

Explanation / Answer

#include <stdlib.h>

#include <string.h>

#include <stdio.h>

#include <ttclasses/TTInclude.h>

#include <ttclasses/TTCatalog.h>

#include "testprog_utils.h"

#include "tt_version.h"

//----------------------------------------------------------------------

const char * usage_string =

" catalog [<DSN> | -connstr <connection string>] "

" options: ";

//----------------------------------------------------------------------

int

main (int argc, char ** argv)

{

// ---------------------------------------------------------------

// Parse command-line arguments

// ---------------------------------------------------------------

ParamParser parser (usage_string);

char connStr[256];

if (1 == argc) {

/* Default the DSN and UID */

sprintf(connStr, "dsn=%s;%s", DEMODSN, UID);

} else {

/* old behaviour */

connStr[0] = '';;

}

parser.processArgs(argc, argv, connStr);

cerr << endl << "Connecting to TimesTen <" << connStr << ">" << endl << endl;

// ---------------------------------------------------------------

// ---------------------------------------------------------------

// Set up TTClasses logging

// ---------------------------------------------------------------

ofstream output("catalog.txt");

TTGlobal::setLogStream(output) ;

TTGlobal::setLogLevel(TTLog::TTLOG_ERR) ; // TTLOG_WARN: default log level

// ---------------------------------------------------------------

// ---------------------------------------------------------------

// Set up signal handling

// This code simply registers the signal-handling.

//

// Subsequent calls to StopRequested() and TerminateIfRequested()

// stop the execution loops and rolls back/disconnects/exits gracefully.

//

// --> NB: since this program is so short, we don't actually need to

// call StopRequested() or TerminateIfRequested() anywhere (there's no

// loop). See the other TTClasses sample programs for a more thorough

// demonstration of signal-handling in a longer-running program (i.e.,

// one with a loop with database calls).

// ---------------------------------------------------------------

int couldNotSetupSignalHandling = HandleSignals();

if (couldNotSetupSignalHandling) {

cerr << "Could not set up signal handling. Aborting." << endl ;

exit(-1);

}

// ---------------------------------------------------------------

TTConnection conn ;

try {

conn.Connect(connStr, TTConnection::DRIVER_COMPLETE);

}

catch (TTStatus st) {

cerr << "Error connecting to TimesTen " << st << endl ;

if (st.rc != SQL_SUCCESS_WITH_INFO)

exit(-1);

}

cerr << endl << "Connected -- now looking at the system catalog ..." << endl ;

cerr << "(printing information about user tables and views only)" << endl ;

TTCatalog cat (&conn) ;

try {

cat.fetchCatalogData() ;

}

catch (TTStatus st) {

cerr << "Error getting the catalog data for TimesTen: " << st;

// print out the catalog information before exiting

}

try {

// Disconnect immediately -- no need to keep the connection open,

// since all of the necessary database access was done in the

// TTCatalog::fetchCatalogData() method.

conn.Disconnect();

}

catch (TTStatus st) {

cerr << "Error disconnecting from TimesTen: " << st;

// print out the catalog information before exiting

}

int i,j,k, num_columns ;

int num_tables = cat.getNumTables() ;

for ( i = 0 ; i < num_tables ; i++ )

{

const TTCatalogTable & table = cat.getTable(i) ;

if (table.isSystemTable())

continue;

cerr << endl ;

if (table.getNumColumns() == 0)

cerr << "View #";

else

cerr << "Table #";

cerr << i << " : "

<< table.getTableOwner() << "."

<< table.getTableName() << endl ;

num_columns = table.getNumColumns() ;

for ( j = 0 ; j < num_columns ; j++ )

{

const TTCatalogColumn & column = table.getColumn(j) ;

cerr << " Column #" << j << " : " << column.getColumnName() << endl ;

}

// NB: in TimesTen, SQLSpecialColumns() just returns information about ROWID

// --> i.e., it's true for every table, so this info's not particularly

// helpful.

num_columns = table.getNumSpecialColumns() ;

for ( j = 0 ; j < num_columns ; j++ )

{

const TTCatalogSpecialColumn & column = table.getSpecialColumn(j) ;

cerr << " Special Column #" << j << " : "

<< column.getColumnName() << endl ;

}

int num_indexes = table.getNumIndexes() ;

for ( j = 0 ; j < num_indexes ; j++ )

{

const TTCatalogIndex & idx = table.getIndex(j) ;

cerr << "Index # " << j << " : " << idx.getIndexOwner() << "."

<< idx.getIndexName() << endl ;

cerr << " (is an index on table " << idx.getIndexOwner() << "."

<< idx.getTableName() << ")" << endl ;

if ( idx.getType() == PRIMARY_KEY )

cerr << " Primary Key (Hash Index)" << endl ;

else if ( idx.getType() == TTREE_INDEX )

cerr << " T-Tree Index" << endl ;

else

cerr << " 'Other' Index type" << endl ;

if ( idx.getType() == TTREE_INDEX )

{

if ( idx.isUnique() )

cerr << " (UNIQUE index)" << endl ;

else

cerr << " (not a unique index)" << endl ;

}

cerr << " Index columns are: " << endl ;

num_columns = idx.getNumColumns() ;

for ( k = 0 ; k < num_columns ; k++ )

{

cerr << " " << idx.getIndexOwner() << "."

<< idx.getTableName() << "."

<< idx.getColumnName (k) << endl ;

}

}

}

cerr << endl;

cerr << "Summary:" << endl;

cerr << "--------" << endl;

cerr << "There are " << cat.getNumUserTables() << " user tables and views:" << endl;

for (i = 0; i < cat.getNumUserTables() ; i++)

{

const TTCatalogTable & table = cat.getUserTable(i) ;

cerr << " (" << i << ") "

<< table.getTableOwner() << "."

<< table.getTableName() << endl ;

}

cerr << endl;

cerr << "Done looking at the system catalog." << endl ;

return 0 ;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote