#include #include #include #include "Business.h" //extracts a string value for the given field std::string extractJsonString(const std::string &line, const std::string &field) { std::string pattern = "\"" + field + "\":"; size_t pos = line.find(pattern); if (pos == std::string::npos) {return "";} //in case pattern not found pos += pattern.length(); //skip any whitespace while (pos < line.size() && isspace(line[pos])) {pos++;} if (pos >= line.size() || line[pos] != '\"') {return "";} //in case the opening quote is not found pos++; //skip the opening quote size_t endPos = line.find("\"", pos); if (endPos == std::string::npos) {return "";} //in case the closing quote is not found return line.substr(pos, endPos - pos); } //extracts an integer value for the given field int extractJsonInt(const std::string &line, const std::string &field) { std::string pattern = "\"" + field + "\":"; size_t pos = line.find(pattern); if (pos == std::string::npos) {return -1;} //in case the pattern is not found pos += pattern.length(); //skip any whitespace while (pos < line.size() && isspace(line[pos])) {pos++;} size_t endPos = pos; //skip the negative sign if (line[endPos] == '-') {endPos++;} //skip the digits while (endPos < line.size() && isdigit(line[endPos])) {endPos++;} std::string numStr = line.substr(pos, endPos - pos); int value = std::stoi(numStr); if (value == 0) {return -1;} //in case the number is not found return value; } //extracts a double value for the given field double extractJsonDouble(const std::string &line, const std::string &field) { std::string pattern = "\"" + field + "\":"; size_t pos = line.find(pattern); if (pos == std::string::npos) {return 0.0;} // in case the pattern is not found pos += pattern.length(); //skip any whitespace while (pos < line.size() && isspace(line[pos])) {pos++;} if (line[pos] == '\"') { //in case the data is quoted pos++; size_t endPos = line.find("\"", pos); if (endPos == std::string::npos) {return 0.0;} std::string numStr = line.substr(pos, endPos - pos); double value = std::stod(numStr); if (value == 0.0) {return 0.0;} //in case the number is not found return value; } else { //in case the data is not quoted size_t endPos = pos; while (endPos < line.size() && (isdigit(line[endPos]) || line[endPos] == '.' || line[endPos]=='-')) { endPos++; } std::string numStr = line.substr(pos, endPos - pos); double value = std::stod(numStr); if (value == 0.0) {return 0.0;} //in case the number is not found return value; } } //extracts a price value for the given field int extractJsonPrice(const std::string &line, const std::string &field) { std::string pattern = "\"" + field + "\":"; size_t pos = line.find(pattern); if (pos == std::string::npos) {return -1;} //in case the pattern is not found pos += pattern.length(); //skip any whitespace while (pos < line.size() && isspace(line[pos])) {pos++;} if (line[pos] == '\"') { pos++; size_t endPos = line.find("\"", pos); if (endPos == std::string::npos) {return -1;} std::string numStr = line.substr(pos, endPos - pos); if (numStr == "None") {return -1;} //in case the number is not found return std::stoi(numStr); } else { size_t endPos = pos; while (endPos < line.size() && (isdigit(line[endPos]) || line[endPos]=='-')) { endPos++; } std::string numStr = line.substr(pos, endPos - pos); if (numStr == "None") {return -1;} //in case the number is not found return std::stoi(numStr); } } //load the business data from a JSON line Business::Business(const std::string &jsonLine) { name = extractJsonString(jsonLine, "name"); categories = extractJsonString(jsonLine, "categories"); rating = extractJsonDouble(jsonLine, "stars"); price = extractJsonPrice(jsonLine, "RestaurantsPriceRange2"); review_count = extractJsonInt(jsonLine, "review_count"); city = extractJsonString(jsonLine, "city"); postal_code = extractJsonString(jsonLine, "postal_code"); } std::string Business::getName() const {return name;} std::string Business::getCategories() const {return categories;} double Business::getRating() const {return rating;} int Business::getPrice() const {return price;} int Business::getReviewCount() const {return review_count;} std::string Business::getCity() const {return city;} std::string Business::getPostalCode() const {return postal_code;} //returns a string representing the star rating. std::string Business::getStarString() const { int fullStars = static_cast(rating); double fraction = rating - fullStars; std::string stars; for (int i = 0; i < fullStars; ++i) stars += "\u2605"; if (fraction >= 0.5) stars += "\u00BD"; return stars; }