samedi 27 mars 2021

Class does not persist vector of objects after addition

My code is as shown below:

main.cpp

#include <bits/stdc++.h>
#include "Customer.h"
#include "MenuCreator.h"
#include "FoodItem.h"

MenuCreator m1;

void createCustomer() {
    Customer c1("mrg", "m@gmail.com", "9654357", "+91");
}

void createRestaurantItem(Restaurant &rest) {
    rest.addItems(FoodItem("t1"));
    rest.addItems(FoodItem("D1"));
}

void createMenu() {
    m1.createMenu("sg");
    Category c1;
    c1.setName("Non-veg");
    m1.addCategory(c1);
    Restaurant r1;
    r1.setName("ABC");
    r1.setDescription("Test Restaurant");
    createRestaurantItem(r1);
    c1.addRestaurants(r1);
}

vector<Restaurant> getRestaurantsForCategory(string category) {
    return m1.getRestaurantsForCategories(category);
}


int main() {
    createCustomer();
    createMenu();
    for (auto r: getRestaurantsForCategory("Non-veg")) {
        r.printItems();
    }
    return 0;
}

MenuCreator.h

#include <bits/stdc++.h>
#include "Menu.h"

using namespace std;

class MenuCreator {

public:

    void createMenu(string name) {
        Menu m1;
        m1.setName(name);
        menu = m1;
    }

    void addCategory(const Category &categ) {
        categories.push_back(categ);
    }

    const Menu &getMenu() const {
        return menu;
    }

    const vector<Category> &getCategories() const {
        return categories;
    }

    void addRestaurantForCategory(string name, const Restaurant restaurant) {
        for(auto categ: categories) {
            if (categ.getName() == name) {
                categ.addRestaurants(restaurant);
            }
        }
    }

    const vector<Restaurant> &getRestaurantsForCategories(string category) {
        for(auto categ: categories) {
            if(categ.getName() == category) return categ.getRestaurants();
        }
    }

private:
    Menu menu;
    vector<Category> categories;
};

Menu.h

#include<bits/stdc++.h>
#include "Category.h"

using namespace std;

class Menu {
public:
    const string &getName() const {
        return name;
    }

    void setName(const string &name) {
        Menu::name = name;
    }

private:
    string name;
    string description;
    vector<Category> categories;
};

Category.h

using namespace std;

class Category {
public:
    const string &getName() const {
        return name;
    }

    void setName(const string &name) {
        Category::name = name;
    }

    const string &getDescription() const {
        return description;
    }

    void setDescription(const string &description) {
        Category::description = description;
    }

    const vector<Restaurant> &getRestaurants() const {
        return restaurants;
    }

    void setRestaurants(const vector<Restaurant> &restaurants) {
        Category::restaurants = restaurants;
    }

    void addRestaurants(const Restaurant &rt) {
        Category::restaurants.push_back(rt);
    }

private:
    string name;
    string description;
    vector<Restaurant> restaurants;
};

FoodItem.h

#include <bits/stdc++.h>
#include <vector>
#include "FoodItem.h"

using namespace std;

class Restaurant {
public:

    Restaurant() {
        this->id = gen_random(12);
    }

    virtual ~Restaurant() {

    }

    const string &getName() const {
        return name;
    }

    void setName(const string &name) {
        Restaurant::name = name;
    }

    const string &getDescription() const {
        return description;
    }

    void setDescription(const string &description) {
        Restaurant::description = description;
    }

    double getLat() const {
        return lat;
    }

    void setLat(double lat) {
        Restaurant::lat = lat;
    }

    double getLang() const {
        return lang;
    }

    void setLang(double lang) {
        Restaurant::lang = lang;
    }

    const string &getImageUrl() const {
        return imageUrl;
    }

    void setImageUrl(const string &imageUrl) {
        Restaurant::imageUrl = imageUrl;
    }

    const string &getVideoUrl() const {
        return videoUrl;
    }

    void setVideoUrl(const string &videoUrl) {
        Restaurant::videoUrl = videoUrl;
    }

    const vector<FoodItem> &getItems() const {
        return items;
    }

    void setItems(const vector<FoodItem> &items) {
        Restaurant::items = items;
    }

    void addItems(const FoodItem &item) {
        this->items.push_back(item);
    }

    string gen_random(const int len) {
        string tmp_s;
        static const char alphanum[] =
                "0123456789"
                "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                "abcdefghijklmnopqrstuvwxyz";
        srand( (unsigned) time(NULL) * getpid());
        tmp_s.reserve(len);
        for (int i = 0; i < len; ++i)
            tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)];
        return tmp_s;

    }

    const string &getId() const {
        return id;
    }

    void printItems() {
        for(auto it: items) {
            cout<<"item: "<<it.getName()<<endl;
        }
    }


private:
    string id;
    string name;
    string description;
    double lat;
    double lang;
    string imageUrl;
    string videoUrl;
    string createdAt;
    string updatedAt;
    vector<FoodItem> items;
};

Restaurant.h

#include <bits/stdc++.h>
#include <vector>
#include "FoodItem.h"

using namespace std;

class Restaurant {
public:

    Restaurant() {
        this->id = gen_random(12);
    }

    virtual ~Restaurant() {

    }

    const string &getName() const {
        return name;
    }

    void setName(const string &name) {
        Restaurant::name = name;
    }

    const string &getDescription() const {
        return description;
    }

    void setDescription(const string &description) {
        Restaurant::description = description;
    }

    double getLat() const {
        return lat;
    }

    void setLat(double lat) {
        Restaurant::lat = lat;
    }

    double getLang() const {
        return lang;
    }

    void setLang(double lang) {
        Restaurant::lang = lang;
    }

    const string &getImageUrl() const {
        return imageUrl;
    }

    void setImageUrl(const string &imageUrl) {
        Restaurant::imageUrl = imageUrl;
    }

    const string &getVideoUrl() const {
        return videoUrl;
    }

    void setVideoUrl(const string &videoUrl) {
        Restaurant::videoUrl = videoUrl;
    }

    const vector<FoodItem> &getItems() const {
        return items;
    }

    void setItems(const vector<FoodItem> &items) {
        Restaurant::items = items;
    }

    void addItems(const FoodItem &item) {
        this->items.push_back(item);
    }

    string gen_random(const int len) {
        string tmp_s;
        static const char alphanum[] =
                "0123456789"
                "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                "abcdefghijklmnopqrstuvwxyz";
        srand( (unsigned) time(NULL) * getpid());
        tmp_s.reserve(len);
        for (int i = 0; i < len; ++i)
            tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)];
        return tmp_s;

    }

    const string &getId() const {
        return id;
    }

    void printItems() {
        for(auto it: items) {
            cout<<"item: "<<it.getName()<<endl;
        }
    }


private:
    string id;
    string name;
    string description;
    double lat;
    double lang;
    string imageUrl;
    string videoUrl;
    string createdAt;
    string updatedAt;
    vector<FoodItem> items;
};

The problem is inside int main function r.printItems() is not printing anything. What am I missing here ?

Aucun commentaire:

Enregistrer un commentaire