Implement a simple business rating application program in C++. The program will
ID: 3572870 • Letter: I
Question
Implement a simple business rating application program in C++. The program will allow a reviewer to enter attributes (including ratings) associated with various types businesses. The program will manage a list of these attributes for individual businesses as a node within a single linked list stored in memory. This can be a global variable for simplicity. The program must use the “list” API in the C++ standard template library (STL). The program must implement at least one class that will hold the following variables (input validation is only required where indicated):
businessId — A string variable that will hold an automatically generated character string as the business identifier. The string must be in the form NN-LLL-NN, where N is a number from 0 to 9, and L is a character from A to Z. For example: 32-HGF-67. The businessId is a unique identifier for each business in the linked list and repeats are not allowed.
category — An integer variable from 1 to 7 that associates a business with one of the following categories: 1=Restaurants, 2=Bars, 3=Fitness, 4=Theaters, 5=Shopping, 6=Deals and Discounts, 7=Everything. Input Validation Required: The program must re-prompt the user if the entered category does not match one of the valid category numbers.
businessName -- A string variable to hold the name of the business.
description - A string variable to hold a brief description of the business. Spaces between words must be allowed so that all words in the description can be entered.
businessCity - A string variable to hold the name of the city where the business is located.
businessState - A string variable to hold the state abbreviation of where the business is located. Input Validation Required: The program must re-prompt the user if the entered state if it is more than two characters.
reviewersName - A string variable to hold the name of the reviewer or the reviewer’s alias. Multiple words for this field are required. Spaces between words must be allowed so that multiple words in the description can be entered.
myCity -- A string variable to hold the name of the city where the reviewer is located.
myState -- A string variable to hold the state abbreviation of where the reviewer is located.
date -- A string variable to hold the date that the review was entered. The entered date must be in the form mm/dd/yyyy. Input Validation Required: The program must re-prompt the user to re-enter the date if it is not in the correct format.
starRating — An integer variable to the reviewer’s rating for the number of stars as the rating (worst=1 to best=5). Input Validation Required: The program must re-prompt the user if the entered rating if it is not between the numbers 1 to 5.
reviewerComments -- A string variable that allows spaces between words to so that the reviewer can enter his or her review in the form of a sentence.
Provide the appropriate methods to set and get the data for each of these class variables. For example setBusinessName(string businessName) and string getBusinessName(). The Book Inventory provides an example on how this can be done. In addition, the main program must provide the following functionality:
1. When the program is first started, it must read a data file called reviews.dat. The program will not prompt the user for the name of the data file. The name of the file must be hard-coded in the program. If the file exists, the program will load the data for each business into a global linked list. If the file does not exist, the program will start with an empty linked list.
2. The program will provide a simple text-based user interface that manages the all of the businesses within a linked list. Each business must be placed in linked list as an object that holds all of the properties associated with it as mentioned above. The user interface will allow the reviewer to perform the following:
(a) Enter Review – allows the reviewer to enter all of the fields associated with a review for given business, except for the businessId, which will be automatically generated by the program as previously mentioned. After the fields are entered, the program will place the business object in the global linked list.
(b) Edit Review – allows the reviewer to edit the fields for a given business that is in the linked list (except for the bussnessId). The program must prompt the reviewer to enter the bussnessId as the key to find the business to edit. Print a message if the business is not found in the linked list. For simplicity, the program may re-prompt the user to re-enter all of the fields associated with the given review for the business; but again, it must reuse the bussnessId value.
(c) Delete Review – allows the reviewer to delete a review from the linked list using the businessId as the key. The program must display a message if the provided businessId is not found in the linked list.
(d) Display Reviews – displays all of the businesses within the linked list along with their associated properties. When displaying the category, the program must print the category number along with the category name. In addition, this option must print the total number of businesses in the linked list.
(e) Filter Reviews by Category – displays all of the businesses within the linked list for a given category. The program will prompt the user to enter a category number. Display an error message if the category is invalid, otherwise, display all of the businesses for the category along with their associated fields. In addition, this option must print the total number of businesses in the linked list for the given category.
(f) Search for Business – allows the user to find a business. The program will prompt the user to enter the businessId and will display all of the fields associated with the given business if it is found. The program must display an error message if the business is not found in the linked list.
(g) Exit System – before the program exits, it must save all of the data in the linked list to the data file. I recommend using a standard text file with one field in the object per line. At this point, if the file does not exist, the program will create it.
The code should be in C++.
Explanation / Answer
#include <list>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
class node{
string businessId;
int category; //has to be 1 to 7
string businessName;
string description; //spaces between words is allowwed
string businessCity;
string businessState; //must be less than or equal to 2 character
string reviewerName; //spaces are allowed
string myCity;
string myState;
string date; //format: mm/dd/yyyy
int starRating; //1 to 5
string reviewerComments; //spaces are allowed
public:
void setBusinessId( string bId ){
for(int i = 0; i < 7; i++){
if( i == 2 || i == 6 ){
if( bId[i] != '-'){
cout << "-------" << endl;
cout << "Error : Invalid Business Id" << endl;
}
}
else if( i >= 3 && i <= 5 ){
if( bId[i] < 'A' || bId[i] > 'Z' ){
cout << "Error: Invalid Business Id" << endl;
return;
}
}
else{
if( bId[i] < '0' || bId[i] > '9' ){
cout << "0099999999" << endl;
cout << "Error: Invalid Business Id" << endl;
return;
}
}
}
businessId = bId;
}
void setCategory( int cat ){
if( cat< 1 || cat > 7 ){
//not prompting for input, to allow more freedom of usage
cout << "Error : Invalid Category" << endl;
return;}
category = cat;
}
void setBusinessName( string bName ){
businessName = bName;
}
void setDescription( string descrpn ){
description = descrpn;
}
void setBusinessCity( string city ){
businessCity = city;
}
void setBusinessState( string bState ){
if( bState.size() > 2 ){
cout << "Error : Invalid Business State"<< endl;
return;
}
businessState = bState;
}
void setReviewerName( string rName ){
reviewerName = rName;
}
void setMyCity( string city ){
myCity = city;
}
void setMyState( string state ){
myState = state;
}
void setMyDate( string Date ){
if( Date.size() != 10 || Date[2] != '/' || Date[5] != '/' ){
cout << "Error : Invalid Format of Date"<< endl;
return;
}
for(int i = 0; i < 9; i++){
if( i== 2 || i == 5){
continue;
}
if( Date[i] < '0' || Date[i] > '9'){
cout << "Error : Invalid Date string"<< endl;
return;
}
}
int month = ((int)(Date[0]-'0'))*10 + ((int)(Date[1]-'0'));
if( month < 1 || month > 12 ){
cout << "Error : Invalid Month" << endl;
return;}
int day = ((int)(Date[3]-'0'))*10 + ((int)(Date[4]-'0'));
if( day < 1 || day > 31 ){
cout << "Error : Invalid Date" << endl;
return; }
date = Date;
}
void setStarRating( int rating ){
if( rating < 1 || rating > 5 ){
cout << "Error : Invalid Rating"<< endl;
return; }
starRating = rating;
}
void setReviewerComments( string comm ){
reviewerComments = comm;
}
string getBusinessId(){
return businessId;
}
int getCategory(){
return category;
}
string getBusinessName(){
return businessName;
}
string getDescription(){
return description;
}
string getBusinessCity(){
return businessCity;
}
string getBusinessState(){
return businessState;
}
string getReviewerName(){
return reviewerName;
}
string getMyCity(){
return myCity;
}
string getMyState(){
return myState;
}
string getMyDate(){
return date;
}
int getStarRating(){
return starRating;
}
string getReviewerComments(){
return reviewerComments;
}
};
void printNode( node* temp ){
cout << "Business Id: "<< temp->getBusinessId() << endl;
cout << "Category: "<<temp->getCategory() << " ";
string itIs[] = {"","Restuarants","Bars","Fitness","Theaters","Shopping","Deals and Discounts","Everything"};
cout << itIs[ temp->getCategory() ] << endl;
cout << "Business Name: "<<temp->getBusinessName() << endl;
cout << "Description: "<< temp->getDescription() << endl;
cout << "Business City: "<<temp->getBusinessCity() << endl;
cout << "Business State: "<<temp->getBusinessState() << endl;
cout << "Reviewer Name: "<<temp->getReviewerName() << endl;
cout << "City: "<<temp->getMyCity() << endl;
cout << "State: "<<temp->getMyState() << endl;
cout << "Date: "<<temp->getMyDate() << endl;
cout << "Star Rating:" <<temp->getStarRating() << endl;
cout << "Reviewer Comments: "<<temp->getReviewerComments() << endl;
cout << endl;
}
void editNode( node* review ){
int category; //has to be 1 to 7
string businessName;
string description; //spaces between words is allowwed
string businessCity;
string businessState; //must be less than or equal to 2 character
string reviewerName; //spaces are allowed
string myCity;
string myState;
string date; //format: mm/dd/yyyy
int starRating; //1 to 5
string reviewerComments; //spaces are allowed
cout << "Enter category: ";
cin >> category;
review->setCategory( category );
cout << "Enter Business Name: ";
cin >> businessName;
review->setBusinessName( businessName );
cout << "Enter Description: ";
getline(cin ,description);
getline(cin ,description);
review->setDescription( description );
cout << "Enter Business City: ";
cin >> businessCity;
review->setBusinessCity( businessCity );
cout << "Enter Business State: ";
cin >> businessState;
review->setBusinessState( businessState );
cout << "Enter Reviewer Name: ";
getline(cin,reviewerName);
getline(cin,reviewerName);
review->setReviewerName( reviewerName );
cout << "Enter City: ";
cin >> myCity;
review->setMyCity( myCity );
cout << "Enter State: ";
cin >> myState;
review->setMyState( myState );
cout << "Enter Date: ";
cin >> date;
review->setMyDate( date );
cout << "Enter Star Rating: ";
cin >> starRating;
review->setStarRating( starRating );
cout << "Enter Reviewer Comments: ";
getline(cin ,reviewerComments );
getline(cin ,reviewerComments );
review->setReviewerComments( reviewerComments );
}
void printMenu(){
cout << endl;
cout << "Enter e for entering new review" << endl;
cout << "Enter t for editing a review" << endl;
cout << "Enter d for deleting a review" << endl;
cout << "Enter l for displaying review" << endl;
cout << "Enter f for filtering reviews by category" << endl;
cout << "Enter s for searching a review" << endl;
cout << "Enter x for exiting the menu "<< endl;
cout << endl;
}
int main(){
list< node* > ourList;
ifstream in("reviews.dat");
//stores each element of node class in new line, empty line when one business data finishes
srand(time(NULL));
string temp;
string businessId;
int category; //has to be 1 to 7
string businessName;
string description; //spaces between words is allowwed
string businessCity;
string businessState; //must be less than or equal to 2 character
string reviewerName; //spaces are allowed
string myCity;
string myState;
string date; //format: mm/dd/yyyy
int starRating; //1 to 5
string reviewerComments; //spaces are allowed
while( getline(in, businessId ) ){
getline(in, temp);
category = atoi( temp.c_str() );
getline( in, businessName);
getline( in, description);
getline( in, businessCity);
getline( in, businessState);
getline( in, reviewerName);
getline( in, myCity);
getline( in, myState);
getline( in, date);
getline( in, temp);
starRating = atoi( temp.c_str() );
getline( in, reviewerComments);
node* newNode = new node();
newNode->setBusinessId( businessId );
newNode->setCategory( category );
newNode->setBusinessName( businessName );
newNode->setDescription( description );
newNode->setBusinessCity( businessCity );
newNode->setBusinessState( businessState );
newNode->setReviewerName( reviewerName );
newNode->setMyCity( myCity );
newNode->setMyState( myState );
newNode->setMyDate( date );
newNode->setStarRating( starRating );
newNode->setReviewerComments( reviewerComments );
getline(in,temp); //reading empty line
ourList.push_back( newNode );
}
char character;
while(1){
printMenu();
cin >> character;
if( character == 'e' ){ //enter review
node* theNode = new node();
string bId = "";
for(int i = 0; i < 2; i++){
bId = bId + (char)(rand()%10 + '0'); }
bId = bId + '-';
for(int i = 0; i < 3; i++){
bId = bId + (char)(rand()%26 + 'A'); }
bId = bId + '-';
for(int i = 0; i < 2; i++){
bId = bId + (char)(rand()%10 + '0'); }
cout << "Business Id: "<<bId << endl;
theNode->setBusinessId( bId );
editNode( theNode);
ourList.push_back( theNode);
}
else if( character == 't' ){ //edit review
cout << "Enter business id of review to edit: ";
string bId;
cin >> bId;
list<node*>::iterator it = ourList.begin();
for( ; it != ourList.end(); it++ ){
if( (*it)->getBusinessId() == bId ){
editNode( *it );
break;
}
}
if( it == ourList.end() ){
cout << "Error : Business Id doesn't exist" << endl;
}
}
else if( character == 'd' ){ //delete review
string bId;
cout << "Enter Business Id: ";
cin >> bId;
list<node*>::iterator it = ourList.begin();
for( ; it != ourList.end(); it++ ){
if( (*it)->getBusinessId() == bId ){
ourList.erase( it );
break;
}
}
if( it == ourList.end() ){
cout << "Error : Business Id doesn't exist" << endl;
}
}
else if( character == 'l' ){ //display review
for( list<node*>::iterator it=ourList.begin(); it != ourList.end(); it++){
printNode( *it );
}
cout << "Total Business reviews : "<<ourList.size() << endl;
}
else if( character == 'f' ){ //filter review
int category;
cout << "Enter category: ";
cin >> category;
if( category< 1 || category > 7 ){
cout << "Error : Invalid Category" << endl;
continue;
}
int totalSuch = 0;
for( list<node*>::iterator it = ourList.begin(); it != ourList.end(); it++){
if( (*it)->getCategory() == category ){
totalSuch++;
printNode( *it );
}
}
cout << "Total Businesses for given category are: " << totalSuch << endl;
}
else if( character == 's' ){ //search review
string bId;
cout << "Enter Business Id: ";
cin >> bId;
list<node*>::iterator it = ourList.begin();
for( ; it != ourList.end(); it++ ){
if( (*it)->getBusinessId() == bId ){
printNode( *it );
break;
}
}
if( it == ourList.end() ){
cout << "Error : Business Id doesn't exist" << endl;
}
}
else if( character == 'x' ){ //exit review
ofstream out("reviews.dat");
for( list<node*>::iterator it = ourList.begin(); it != ourList.end(); it++){
node* temp = *it;
out << temp->getBusinessId() << endl;
out << temp->getCategory() << endl;
out << temp->getBusinessName() << endl;
out << temp->getDescription() << endl;
out << temp->getBusinessCity() << endl;
out << temp->getBusinessState() << endl;
out << temp->getReviewerName() << endl;
out << temp->getMyCity() << endl;
out << temp->getMyState() << endl;
out << temp->getMyDate() << endl;
out << temp->getStarRating() << endl;
out << temp->getReviewerComments() << endl;
out << endl;
}
break;
}
else{
cout << "Invalid Option, Enter Again"<<endl;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.