Lab13.3

mail@pastecode.io avatar
unknown
c_cpp
2 months ago
18 kB
3
Indexable
Never
#include <bits/stdc++.h>
using namespace std;
#define pb push_back

// indicate different type of the systems
enum System{ FOOD = 0, SHOP, PICK_UP };

// the data structure of the cuisine or the groceries
struct Item{
    System method;// add item in the different systems, "Food Delivery", "Shops", and "Pick-Up"
    string store_name;// which store this item belongs to
    string number;// the cuisine or the groceries numebr, ex: 2-1
    string name;// the cuisine or the groceries name
    int price;// the cuisine or the groceries price
};

// the data structure of the member
struct Member{
    char identity;// seller='s', customer='c'
    string name;// member name
    string password;// use password to log in
    vector<Item> cart;
};

// the data structure of the store
struct Store{
    string name;// the store name
    string number;// the store number
    int delivery_fee;// the delivery fee of each store
    vector<Item> item_list;// the list of all items in this store
};

void initialize_store(vector<vector<Store>> &sto){
    vector<Item> t;
    t.pb({FOOD,"Wang shih jhen sin tang","1-1","Bubble Tea",75});
    t.pb({FOOD,"Wang shih jhen sin tang","1-2","Cheese-Topped Black Tea",85});
    sto[0].pb({"Wang shih jhen sin tang","1",29,t});

    t.clear();
    t.pb({FOOD,"Taiwan di er jia yan su ji","2-1","Boneless Salted Crispy Chicken",71});
    t.pb({FOOD,"Taiwan di er jia yan su ji","2-2","Egg Tofu",78});
    t.pb({FOOD,"Taiwan di er jia yan su ji","2-3","Green Beans",52});
    sto[0].pb({"Taiwan di er jia yan su ji","2",49,t});

    t.clear();
    t.pb({FOOD,"Fang kong nai ba","3-1","Sakura Shrimp Fried Rice",140});
    t.pb({FOOD,"Fang kong nai ba","3-2","Squid Fried Noodles",130});
    t.pb({FOOD,"Fang kong nai ba","3-3","Grilled King Oyster Mushrooms",50});
    sto[0].pb({"Fang kong nai ba","3",39,t});

    t.clear();
    t.pb({PICK_UP,"Wang shih jhen sin tang","1-1","Bubble Tea",75});
    t.pb({PICK_UP,"Wang shih jhen sin tang","1-2","Cheese-Topped Black Tea",85});
    sto[2].pb({"Wang shih jhen sin tang","1",0,t});

    t.clear();
    t.pb({PICK_UP,"Taiwan di er jia yan su ji","2-1","Boneless Salted Crispy Chicken",71});
    t.pb({PICK_UP,"Taiwan di er jia yan su ji","2-2","Egg Tofu",78});
    t.pb({PICK_UP,"Taiwan di er jia yan su ji","2-3","Green Beans",52});
    sto[2].pb({"Taiwan di er jia yan su ji","2",0,t});

    t.clear();
    t.pb({PICK_UP,"Fang kong nai ba","3-1","Sakura Shrimp Fried Rice",140});
    t.pb({PICK_UP,"Fang kong nai ba","3-2","Squid Fried Noodles",130});
    t.pb({PICK_UP,"Fang kong nai ba","3-3","Grilled King Oyster Mushrooms",50});
    sto[2].pb({"Fang kong nai ba","3",0,t});

    t.clear();
    t.pb({SHOP,"Walmart","1-1","Tissue",124});
    t.pb({SHOP,"Walmart","1-2","Light Bulbs",99});
    sto[1].pb({"Walmart", "1",49,t});

    t.clear();

    t.pb({SHOP,"Target","2-1","Shampoo",239});
    t.pb({SHOP,"Target","2-2","Toothpaste ",129});
    sto[1].pb({"Target", "2",39,t});

    t.clear();
    t.pb({SHOP,"Costco","3-1","Milk",259});
    t.pb({SHOP,"Costco","3-2","Doritos",165});
    t.pb({SHOP,"Costco","3-3","Croissant",199});
    sto[1].pb({"Costco", "3",29,t});
}


void List_Restaurants(vector<vector<Store>> sto,int mod) {
    while (1) {
        cout << endl;
        cout << "*** List Restaurants ***" << endl;
        cout << "No. Restaurant                         Delivery Fee          " << endl;
        for(Store i:sto[mod]){
            cout << left << setw(3) << i.number << " " << setw(35) << i.name << "NTD" << right << setw(5) << i.delivery_fee << endl;
        }
        cout << "Enter number to the restaurant or enter back to the " << (mod == 2 ? "Pick-Up System" :"Food Delivery System")  << endl;
        cout << ">>>";
        string ch;
        cin >> ch;
        bool flag = 1;
        if (ch == "back") {
            break;
        }else{
            int in = 0;
            for(Store i:sto[mod]){
                if(i.number == ch){
                    cout << endl;
                    cout << "*** " << i.name << " ***" << endl;
                    cout << "No. Cuisine                            Price" << endl;
                    for(Item j : i.item_list){
                        cout << j.number << " " << left << setw(35) << j.name << "NTD" << right << setw(5) << j.price << endl;
                    }
                    flag = 0;
                    break;
                }
            }
        }
        if(flag){
            cout << "Unknown command" << endl;
        }
    }

    cout << endl;
}

void List_Shops(vector<vector<Store>> sto) {
    while (1) {
        cout << endl;
        cout << "*** List Shops ***" << endl;
        for(Store i:sto[1]){
            cout << left << setw(3) << i.number << " " << setw(35) << i.name << "NTD" << right << setw(5)<< i.delivery_fee << endl;
        }

        cout << "Enter number to the shop or enter back to the Shops System" << endl;
        cout << ">>>";
        string ch;
        cin >> ch;
        bool flag = 1;

        for(Store i:sto[1]) {
            if(i.number == ch){
                cout << endl;
                cout << "*** " << i.name << " ***" << endl;
                cout << "No. Groceries                          Price" << endl;
                for(Item j:i.item_list)
                    cout << left << j.number << " " << setw(35) << j.name << "NTD" << right << setw(5) << j.price << endl;
                flag = 0;
                break;
            }
        }
        if (ch == "back")
            break;
        if(flag)
            cout << "Unknown command" << endl ;
    }

    cout << endl;
}


void Add_Items_to_Cart(vector<Member> &user, vector<vector<Store>> sto, int mod, int in, int is_l) {
    cout << endl;

    if(!is_l){
        cout << "Please log in first." << endl;
        cout << endl;
        return;
    }

    cout << "*** Add Items to Cart ***" << endl;
    while (1) {
        int a, b;

        string m;
        if (mod == 1)
            m = "item";
        else
            m = "cuisine";

        cout << "Enter item number to add it to cart: ";

        string s; cin >> s;
        string ss = s.substr(0,s.find_first_of('-'));

        bool flag = 1;
        for(Store i : sto[mod]){
            if(i.number == ss){
                for(Item j : i.item_list){
                    if(j.number == s){
                        user[in].cart.push_back(j);
                        flag = 0;
                    }
                }
            }
            if(!flag) break;
        }
        if(flag)
             cout << "Wrong number, please enter again!" << endl;
        else break;
    }

    cout << "Added successfully!" << endl;
    cout << endl;
}

void Payment(vector<Member> user, vector<vector<Store>> sto,int in, int is_l) {
    cout << "*** Payment ***" << endl;
    if(!is_l){
        cout << "Please log in first." << endl;
        cout << endl;
        return;
    }
    if (user[in].cart.size()) {
        cout << endl;
        cout << "*** This is " << user[in].name << "'s order ***" << endl;
        cout << "Item                               Price" << endl;
        cout << "-------------------------------------------" << endl;

        vector<Store> de;
        int sum = 0;

        for (Item i : user[in].cart) {
            cout << left << setw(35) << i.name << "NTD" << right << setw(5) << i.price << endl;
            sum += i.price;

            if (i.method == 2)
                continue;

            bool ff = 1;

            for (Store j : de)
                if (j.name == i.store_name)
                    ff = 0;

            if (ff){
                for(Store j:sto[i.method]){
                    if(j.name == i.store_name){
                        de.pb(j);
                        break;
                    }
                }
            }
        }

        for (Store i : de) {
            cout << left << setw(35) << "Delivery Fee" << "NTD" << right << setw(5) << i.delivery_fee << endl;
            sum += i.delivery_fee;
        }

        cout << "-------------------------------------------" << endl;
        cout << left << setw(35) << "Total" << "NTD" << right << setw(5) << sum << endl;
    } else {
        cout << "You don't have any item in cart!" << endl;
    }

    cout << endl;

}


void food_delivery(vector<Member> &user, vector<vector<Store>> sto, int in, int is_l) {
    while (1) {
        cout << "**********************************" << endl;
        cout << "***    Food Delivery System    ***" << endl;
        cout << "**********************************" << endl;
        cout << "Enter 1 to List Restaurants" << endl;
        cout << "Enter 2 to Add Items to Cart" << endl;
        cout << "Enter back to the Homepage" << endl;
        cout << ">>>";
        string ch;
        cin >> ch;

        if (ch == "1")
            List_Restaurants(sto,0);
        else if (ch == "2")
            Add_Items_to_Cart(user, sto, 0, in, is_l);
        else if (ch == "back")
            break;
        else
            cout << "Unknown command" << endl << endl;
    }

    cout << endl;
}

void shop_System(vector<Member> &user,vector<vector<Store>> sto, int in, int is_l) {

    while (1) {
        cout << "**********************************" << endl;
        cout << "***        Shops System        ***" << endl;
        cout << "**********************************" << endl;
        cout << "Enter 1 to List Shops" << endl;
        cout << "Enter 2 to Add Items to Cart" << endl;
        cout << "Enter back to the Homepage" << endl;
        cout << ">>>";
        string ch;
        cin >> ch;

        if (ch == "1")
            List_Shops(sto);
        else if (ch == "2")
            Add_Items_to_Cart(user,sto, 1, in, is_l);
        else if (ch == "back")
            break;
        else
            cout << "Unknown command" << endl << endl;
    }

    cout << endl;
}

void pick_up(vector<Member> &user, vector<vector<Store>> sto, int in, int is_l) {
    while (1) {
        cout << "**********************************" << endl;
        cout << "***       Pick-Up System       ***" << endl;
        cout << "**********************************" << endl;
        cout << "Enter 1 to List Restaurants" << endl;
        cout << "Enter 2 to Add Items to Cart" << endl;
        cout << "Enter back to the Homepage" << endl;
        cout << ">>>";
        string ch;
        cin >> ch;

        if (ch == "1")
            List_Restaurants(sto,2);
        else if (ch == "2")
            Add_Items_to_Cart(user, sto, 2, in , is_l);
        else if (ch == "back")
            break;
        else
            cout << "Unknown command" << endl << endl;
    }
    cout << endl;
}

void Sign_Up(vector<Member> &user){

    cout << endl;
    Member nn;
    cout << "*** Sign Up ***" << endl;
    cout << "Enter Username: ";
    cin >> nn.name;
    cout << "Enter Password: ";
    cin >> nn.password;
    cout << "Which identity do you want to sign up for?" << endl;
    cout << "Enter 1 to become a customer" << endl;
    cout << "Enter 2 to become a seller" << endl;
    cout << ">>>";
    string ch; cin >> ch;
    if(ch != "1" && ch != "2"){
        cout << "Unknown command" << endl;
        cout << "Fail to sign up! Go back to the Member system." << endl;
        cout << endl;
        return;
    }
    nn.identity = (ch == "1"?'c':'s');
    user.push_back(nn);

    cout << "Welcome! You are now a member of Food Delivery System." << endl;
    cout << endl;
}

void Log_In(int &is_l, int &in, vector<Member> &user){

    cout << endl;
    cout << "*** Log In ***" << endl;
    cout << "Enter Username: ";
    string s; cin >> s;

    cout << "Enter Password: ";
    string ps; cin >> ps;

    bool sl=0;

    int i = 0;
    for(; i< user.size(); i++){
        if(user[i].name == s && user[i].password == ps){
            sl = 1;
            break;
        }
    }
    if(sl){
        cout << "Welcome, "<< s <<"!" << endl;
        is_l = (user[i].identity=='c'?1:2);
        in = i;
    }else{
        cout << "Fail to find your data." << endl;
        cout << "Please sign up first." << endl;
    }

    cout << endl;
}

void Load_Member_Information(bool &is_f, vector<Member> &user){
    cout << endl;
    cout << "*** Load Member Information ***" << endl;
    cout << "Enter the member file name: ";
    string s; cin >> s;
    ifstream f;
    f.open(s);

    if(!f.is_open()){
        cout << "File could not be opened." << endl;
        cout << endl;
        return;
    }
    is_f = 1;
    cout << "The file " << s <<" has been loaded..." << endl;
    cout << endl;

    string n, ps, cc;
    f >> n >> ps >> cc;
    Member nn;

    while(f >> nn.name >> nn.password >> cc){
        nn.identity = cc[0];
        user.push_back(nn);
    }
    f.close();
}

void Member_system(int &is_l, bool &is_f, int &in, vector<Member> &user){
    while (1) {

        cout << "**********************************" << endl;
        cout << "***       Member system        ***" << endl;
        cout << "**********************************" << endl;

        if(!is_f){

            cout << "Enter 1 to Load Member Information" << endl;
            cout << "Enter back to the Homepage" << endl;
            cout << ">>>";
            string ch;
            cin >> ch;
            if (ch == "1")
                Load_Member_Information(is_f, user);
            else if (ch == "back")
                break;
            else
                cout << "Unknown command" << endl<< endl;
        }else if(!is_l){
            cout << "Enter 2 to Log In" << endl;
            cout << "Enter 3 to Sign Up" << endl;
            cout << "Enter back to the Homepage" << endl;
            cout << ">>>";
            string ch;
            cin >> ch;
            if (ch == "2")
                Log_In(is_l, in, user);
            else if(ch == "3")
                Sign_Up(user);
            else if (ch == "back")
                break;
            else
                cout << "Unknown command" << endl << endl;

        }else if(is_l){
            cout << "You are log in." << endl;
            cout << "Enter 4 to log out" << endl;
            cout << "Enter back to the Homepage" << endl;
            cout << ">>>";

            string ch;
            cin >> ch;
            if (ch == "4"){
                is_l = 0;
                cout << "You are logging out..." << endl;
                break;

            }
            else if (ch == "back")
                break;
            else
                cout << "Unknown command" << endl << endl;
        }
    }

    cout << endl;

}

void Add_shops(vector<vector<Store>> &sto, int mod){
    cout << endl;
    cout << "Enter the " << (mod == 1?"shop":"restaurant") << " number: ";
    Store temp;
    cin >> temp.number;
    cin.ignore();
    cout << "Enter the " << (mod == 1?"shop":"restaurant") << " name: ";
    getline(cin, temp.name);
    cout << "Enter the delivery fee: ";
    cin >> temp.delivery_fee;
    cout << endl;
    cout << "Add items..." << endl;

    while(1){
        cout << "Enter the " << (mod == 1?"groceries": "cuisine") << " number: ";
        Item tt;
        tt.method = System(mod);
        tt.store_name = temp.name;
        cin >> tt.number;
        cin.ignore();
        cout << "Enter the " << (mod == 1?"groceries": "cuisine") << " name: ";
        getline(cin, tt.name);
        cout << "Enter the " << (mod == 1?"groceries": "cuisine") << " price: ";
        cin >> tt.price;

        temp.item_list.pb(tt);
        cout << "Add item successfully~ Continue? (y/n) ";
        char ch; cin >> ch;
        cout << endl;
        if(ch == 'n')break;
    }

    if(mod == 2){
        sto[0].pb(temp);
        temp.delivery_fee = 0;
    }
    sto[mod].pb(temp);
}

void add_store(vector<vector<Store>> &sto){
    string ch;

    while (1) {
        cout << "*** Add Store ***" << endl;
        cout << "Enter 1 to add restaurants" << endl;
        cout << "Enter 2 to add shops" << endl;
        cout << "Enter back to the Homepage" << endl;
        cout << ">>>";

        string ch;
        cin >> ch;

        if (ch == "1")
            Add_shops(sto, 2);
        else if (ch == "2")
            Add_shops(sto, 1);
        else if (ch == "back")
            break;
        else
            cout << "Unknown command" << endl << endl;
    }
    cout << endl;


}

signed main() {

    vector<Member> user;
    vector<vector<Store>> sto(3);
    int in;
    int is_l = 0;
    bool is_f = 0;
    initialize_store(sto);

    while (1) {
        cout << "**********************************" << endl;
        cout << "***   Food Delivery Homepage   ***" << endl;
        cout << "**********************************" << endl;
        cout << "Enter 1 to Food Delivery System" << endl;
        cout << "Enter 2 to Shops System" << endl;
        cout << "Enter 3 to Pick-Up System" << endl;
        cout << "Enter 4 to Member System" << endl;
        cout << "Enter 5 to Payment" << endl;
        if(is_l==2)cout << "Enter 6 to Add Stores" << endl;
        cout << "Enter exit() to quit" << endl;
        cout << ">>>";
        string ch;
        cin >> ch;

        if (ch == "exit()")
            break;

        if (ch == "1") {
            cout << endl;
            food_delivery(user, sto, in, is_l);
        } else if (ch == "2") {
            cout << endl;
            shop_System(user, sto, in, is_l);
        } else if (ch == "3") {
            cout << endl;
            pick_up(user, sto, in, is_l);
        }else if(ch == "4"){
            cout << endl;
            Member_system(is_l, is_f, in, user);
        } else if (ch == "5") {
            cout << endl;
            Payment(user, sto, in, is_l);

        } else if(ch=="6" && is_l==2){
            cout << endl;
            add_store(sto);

        }else {
            cout << "Unknown command" << endl;
            cout << endl;
        }
    }

}
Leave a Comment