// Search function to find a specific row of data and return it as a string // // param[in]: columnName identifies the name of the column to be searched // columnName as "all" means the whole table is returned // param[in]: valueToFind identifies the value to be searched for in the column // return: a string which contains a concatenation of all values in the row found in the database table // if multiple values exist, return all rows with that value, where // each row is separated by a new line // throw: DoesNotExistException when trying to find a row that doesn't exist string Invoice::search(string columnName, string valueToFind) throw(DoesNotExistException) { ifstream infstream; // ifstream to be used to read invoice.txt string returnString = ""; // string used to store the value to be returned string currentRow; // string used to store the current row string invoice_id, date; //strings to store the invoice_id and date in the current row // delimiter used to store positions of the delimiters '|' in the current row int delimPos; infstream.open(fileName); if(infstream.is_open()) { // while loop continues as long as there is another line in the text file while(infstream.good()) { getline(infstream, currentRow); // store next line of textfile in currentRow // break when an empty string is assigned to currentRow // which occurss if there are no more valid entries in the table if (currentRow.empty()) break; delimPos = currentRow.find('|'); // finds position of delimiter invoice_id = currentRow.substr(0, delimPos); // stores first string as invoice_id date = currentRow.substr(delimPos+1); // stores second string as date // if searching by invoice_id if(columnName == "invoice_id" && invoice_id == valueToFind) { returnString = currentRow + "\n"; //since invoice_id is unique returns current row break; } // if searching by date else if(columnName == "date" && date == valueToFind) { returnString += currentRow + "\n"; //adds any row that contains same date } // return the whole table else if(columnName == "all") returnString += currentRow + "\n"; } } infstream.close(); // throws DoesNotExistException if nothing was found if(returnString == "") throw DoesNotExistException(valueToFind + " does not exist in column: " + columnName); return returnString; }
// Search function to find a specific row of data and return it as a string // // param[in]: columnName identifies the name of the column to be searched // param[in]: valueToFind identifies the value to be searched for in the column // return: a string which contains a concatenation of all values in the row found in the database table // if multiple values exist, return all rows with that value, where // each row is separated by a new line // throw: DoesNotExistException when trying to find a row that doesn't exist string Summary::search(string columnName, string valueToFind) throw(DoesNotExistException) { ifstream infstream; // ifstream to be used to read summary.txt string returnString; // string used to store the value to be returned string currentRow; // string used to store the current row string product_id, total_quantity; //strings to store the product_id and total_quantity in current row // delimiter used to store positions of the delimiters '|' in the current row int delimPos; infstream.open(fileName); if(infstream.is_open()) { // while loop continues as long as there is another line in the text file while(infstream.good()) { getline(infstream, currentRow); // store next line of textfile in currentRow // break when an empty string is assigned to currentRow // which occurss if there are no more valid entries in the table if (currentRow.empty()) break; delimPos = currentRow.find('|'); // finds position of delimiter product_id = currentRow.substr(0, delimPos); // stores first string as product_id total_quantity = currentRow.substr(delimPos+1); // stores second string as total_quantity // if searching by product_id if(columnName == "product_id" && product_id == valueToFind) { returnString = currentRow + "\n"; //since product_id is unique, returns current row break; } // if searching by total_quantity else if(columnName == "total_quantity" && total_quantity == valueToFind) { returnString += currentRow + "\n"; //adds any row that contains same total_quantity } } } infstream.close(); // throws DoesNotExistException if nothing was found if(returnString == "") throw DoesNotExistException(valueToFind + " does not exist in column: " + columnName); return returnString; }
string Product :: search(string columnName, string valueToFind) throw(DoesNotExistException) { // bool to tell whether a match was found or not bool resultFound = false; // string to be used to return the results of a search string returnString; // string to contain the contents of a row received from the file string rowReceive; // ints to store the position of the first, second, third, and fourth delimiters int delimiter; int delimiter2; int delimiter3; int delimiter4; // strings to contain the data from the current row retrieved from the file string productID; string categoryID; string productDescription; string productName; string productPrice; // opens category.txt productInFile.open(productTextFile); // ensures that categoryInFile is open if(productInFile.is_open()) { // initially clears all text from returnString returnString.clear(); // while loop continues as long as there is another line in the text file while(productInFile.good()) { // retrieves the next line in categoryInFile and assigns it to the string rowReceive getline(productInFile, rowReceive); // finds the first delimiter position and assigns it to int delimiter delimiter = rowReceive.find('|'); // finds the second delimiter position and assigns it to int delimiter2 delimiter2 = rowReceive.find('|', delimiter+1); // finds the third delimiter position and assigns it to int delimiter3 delimiter3 = rowReceive.find('|', delimiter2+1); // finds the fourth delimiter position and assigns it to int delimiter4 delimiter4 = rowReceive.find('|', delimiter3+1); // retrieves the product_ID from the row data and assigns it to productID productID = rowReceive.substr(0,delimiter); // retrieves the category_ID from the row data and assigns it to categoryID categoryID = rowReceive.substr(delimiter+1, delimiter2-delimiter-1); // retrieves the product description from the row data and assigns it to productDescription productDescription = rowReceive.substr(delimiter2+1, delimiter3-delimiter2-1); // retrieves the product name from the row data and assigns it to productName productName = rowReceive.substr(delimiter3+1, delimiter4-delimiter3-1); // retrieves the price from the row data and assigns it to productPrice productPrice = rowReceive.substr(delimiter4+1); // checks if columnName (argument) is "product_id" and if product_id data of current row matches // valueToFind (argument) if(columnName == "product_id" && atoi(productID.c_str()) == atoi(valueToFind.c_str())) { // concatenates the row that matched the search arguments to the string returnString // along with a line break at the end returnString += rowReceive + "\r\n"; resultFound = true; } // checks if columnName (argument) is "category_id" and if category_id data of current row matches // valueToFind (argument) else if(columnName == "category_id" && atoi(categoryID.c_str()) == atoi(valueToFind.c_str())) { // concatenates the row that matched the search arguments to the string returnString // along with a line break at the end returnString += rowReceive + "\r\n"; resultFound = true; } // checks if columnName (argument) is "description" and if product description data of current row matches // valueToFind (argument) else if(columnName == "description" && atoi(productDescription.c_str()) == atoi(valueToFind.c_str())) { // concatenates the row that matched the search arguments to the string returnString // along with a line break at the end returnString += rowReceive + "\r\n"; resultFound = true; } // checks if columnName (argument) is "name" and if product name data of current row matches // valueToFind (argument) else if(columnName == "name" && productName == valueToFind) { // concatenates the row that matched the search arguments to the string returnString // along with a line break at the end returnString += rowReceive + "\r\n"; resultFound = true; } // checks if columnName (argument) is "price" and if product price data of current row matches // valueToFind (argument) else if(columnName == "price" && atof(productPrice.c_str()) == atof(valueToFind.c_str())) { // concatenates the row that matched the search arguments to the string returnString // along with a line break at the end returnString += rowReceive + "\r\n"; resultFound = true; } } } // closes product.txt productInFile.close(); if(!resultFound) throw DoesNotExistException("Product Does Not Exist"); // \return returnString is returned as a result of the search function return returnString; }