Skip to content

Commit

Permalink
Merge pull request #95 from CS179K-Summer23/filter
Browse files Browse the repository at this point in the history
Filter
  • Loading branch information
cwong165 authored Aug 20, 2023
2 parents 83a937e + 2f483f1 commit cee8fa9
Show file tree
Hide file tree
Showing 6 changed files with 425 additions and 12 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"utility": "cpp",
"limits": "cpp",
"*.tcc": "cpp",
"ostream": "cpp"
"ostream": "cpp",
"chrono": "cpp",
"random": "cpp",
"valarray": "cpp"
}
}
55 changes: 55 additions & 0 deletions data/products.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,61 @@
"price": 13.0,
"quantity": 313,
"sku": "321"
},
{
"barcode": "123",
"category": "NA",
"description": "NA",
"expiration_date": "NA",
"id": "123",
"name": "hotdog",
"price": 13.0,
"quantity": 123,
"sku": "NA"
},
{
"barcode": "123123",
"category": "NA",
"description": "NA",
"expiration_date": "NA",
"id": "123123",
"name": "dog",
"price": 123.0,
"quantity": 2,
"sku": "NA"
},
{
"barcode": "12323123",
"category": "NA",
"description": "NA",
"expiration_date": "NA",
"id": "12323123",
"name": "alpha",
"price": 123.0,
"quantity": 2,
"sku": "NA"
},
{
"barcode": "200",
"category": "NA",
"description": "NA",
"expiration_date": "NA",
"id": "200",
"name": "400",
"price": 3333.0,
"quantity": 333333,
"sku": "NA"
},
{
"barcode": "12321313",
"category": "NA",
"description": "NA",
"expiration_date": "NA",
"id": "12321313",
"name": "pony",
"price": 300000.0,
"quantity": 2,
"sku": "NA"
}
]
}
27 changes: 17 additions & 10 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,25 +136,32 @@ void displayFilterMenu() {
std::cout << "\n=========== Advanced Filter ===========\n";
std::cout << "1. Filter by Price Range\n";
std::cout << "2. Filter by Category\n";
std::cout << "3. Back to Product Management\n";
std::cout << "Please enter your choice (1-3): ";
std::cout << "3. Filter by Name Alphabetically\n";
std::cout << "4. Filter by Quantity Range\n";
std::cout << "5. Filter by Prefix\n";
std::cout << "6. Back to Product Management\n";
std::cin >> choice;
switch (choice) {
case 1:
std::cout << "Enter Price Range (e.g., 10-20): ";
std::cin >> filterQuery;
std::cout <<
"Filtering by Price Range. (This functionality is not yet implemented, please be patience.)\n";
filterPriceRange();
displayFilterMenu();
break;
case 2:
std::cout << "Enter Category: ";
std::cin >> filterQuery;
std::cout <<
"Filtering by Category. (This functionality is not yet implemented, please be patience.)\n";
filterCategory();
displayFilterMenu();
break;
case 3:
filterName();
displayFilterMenu();
break;
case 4:
filterQuantityRange();
displayFilterMenu();
break;
case 5:
filterPrefix();
displayFilterMenu();
case 6:
displayProductManagementMenu();
break;
default:
Expand Down
Binary file modified main.exe
Binary file not shown.
195 changes: 195 additions & 0 deletions src/filter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
#include <iostream>
#include <vector>
#include <chrono>
#include <fstream>
#include "../libraries/json.hpp"

using namespace std;
using json = nlohmann::json;

class Filter{
public:
Filter(const string& productsPath){
_path = productsPath;
loadFromFile();
}

virtual vector<json> apply() const = 0;

protected:
string _path;
json _data;

void loadFromFile() {
ifstream pPath(_path);
if (!pPath.is_open()) {
cerr << "Error opening file: " << _path << endl;
return;
}
pPath >> _data;
pPath.close();
}
};



class PriceRangeFilter : public Filter{
public:
PriceRangeFilter(const string& productsPath, const double& minPrice, const double& maxPrice) : Filter(productsPath){
_minPrice = minPrice;
_maxPrice = maxPrice;
}

vector<json> apply() const override{
vector<json> filteredData;

// Apply price range filtering logic using _data, _minPrice, and _maxPrice
for (const auto& product : _data["products"]) {
double price = product["price"];
if (price >= _minPrice && price <= _maxPrice) {
filteredData.push_back(product);
}
}

return filteredData;
}
private:
double _minPrice;
double _maxPrice;
};

class ExpirationDateFilter : public Filter {
public:
ExpirationDateFilter(const string& productsPath, const string& expirationDate)
: Filter(productsPath), _expirationDate(expirationDate) {
}

vector<json> apply() const override {
vector<json> filteredData;

// Convert the expiration date string to a time_point
auto targetTime = convertToDatePoint(_expirationDate);

// Apply expiration date filtering logic using _data and targetTime
for (const auto& product : _data["product"]) {
string itemExpiration = product["expiration"];
auto itemTime = convertToDatePoint(itemExpiration);

if (itemTime <= targetTime) {
filteredData.push_back(product);
}
}

return filteredData;
}

private:
string _expirationDate;

// Convert a date string to a time_point
static chrono::system_clock::time_point convertToDatePoint(const string& dateStr) {
// Assuming the dateStr is in the format "YYYY-MM-DD"
struct std::tm tm = {};
sscanf(dateStr.c_str(), "%d-%d-%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
tm.tm_year -= 1900;
tm.tm_mon -= 1;

time_t time = mktime(&tm);
return chrono::system_clock::from_time_t(time);
}
};

class CategoryFilter : public Filter{
public:
CategoryFilter(const string& productsPath, const string& name) : Filter(productsPath){
_name = name;
}

vector<json> apply() const override{
vector<json> filteredData;

for (const auto& product : _data["products"]) {
string name = product["category"];
if (_name == name) {
filteredData.push_back(product);
}
}

return filteredData;
}
private:
string _name;
};

class NameFilter : public Filter {
public:
NameFilter(const string& productsPath) : Filter(productsPath){

}

vector<json> apply() const override {
vector<json> filteredData;

for (const auto& product : _data["products"]) {
filteredData.push_back(product);
}

sort(filteredData.begin(), filteredData.end(), [](const json& a, const json& b) {
string nameA = a["name"];
string nameB = b["name"];
return nameA < nameB;
});


return filteredData;
}
};

class QuantityFilter : public Filter {
public:
QuantityFilter(const string& productsPath, const int& minQuantity, const int& maxQuantity) : Filter(productsPath){
_minQuantity = minQuantity;
_maxQuantity = maxQuantity;
}

vector<json> apply() const override {
vector<json> filteredData;

// Apply price range filtering logic using _data, _minQuantity, and _maxQuantity
for (const auto& product : _data["products"]) {
double price = product["quantity"];
if (price >= _minQuantity && price <= _maxQuantity) {
filteredData.push_back(product);
}
}

return filteredData;
}
private:
int _minQuantity;
int _maxQuantity;
};

class PrefixFilter : public Filter {
public:
PrefixFilter(const string& productsPath, const string& prefix) : Filter(productsPath){
_prefix = prefix;
}

vector<json> apply() const override {
vector<json> filteredData;

for (const auto& product : _data["products"]) {
const string productName = product["name"];

if (productName.compare(0, _prefix.length(), _prefix) == 0) {
filteredData.push_back(product);
}
}

return filteredData;
}

private:
string _prefix;
};
Loading

0 comments on commit cee8fa9

Please sign in to comment.