#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <fstream>
#include <stdio.h>
#include <cmath>

using namespace std;




class item {
    
protected:
    
    string category;
    string name;
    double price;
    int quantity;
    bool checkedin;

    
public:
    // construcctor
    item() {
        name = "";
        category = "";
        price = 0;
        quantity = 0;
        
    }
    
    
    item (string sName, string sCategory, double sPrice, int sQuantity) {
        name = sName;
        category = sCategory;
        price = sPrice;
        quantity = sQuantity;
        
    }
    
    string getItemName() {
        return name;
    }
    
    string getItemCategory() {
        return category;
    }
    
    double getItemPrice() {
        return price;
    }
    
    int getItemQuantity() {
        return quantity;
    }

    
    void checkout(){
        checkedin = false;
    }
    
    void returnItem(){
        checkedin = true; 
    }
    // print function

    virtual void print() {
        cout << "Name: "<< name << endl;
        cout << "Category: " << category << endl;
        cout << "Price: " << price << endl;
        cout << "Quantity: " << quantity << endl;
    }
    
    

};


class Fruit :public item {
    
    double weight;
    
public:
    
    Fruit() :item() {
        weight = 0;
    }
    
    Fruit (string sName, string sCategory, double sPrice, int sQuantity, double sWeight) :
    item (sName, sCategory, sPrice, sQuantity)
    {
        weight = sWeight;
    }
    
    double getFruitWeight() {
        return weight;
    }
    
    void print(){
        item::print();
        cout << "Weight: " << weight<< " Pounds" << endl;
        
    }
        
    
};


class Meat :public item {
    
    double weight;
    
public:
    
    Meat() :item() {
        weight = 0;
    }
    
    Meat (string sName, string sCategory, double sPrice, int sQuantity, double sWeight) :
    item (sName, sCategory, sPrice, sQuantity)
    {
        weight = sWeight;
    }
    
    double getMeatWeight() {
        return weight;
    }
    
    void print(){
        item::print();
        cout << "Weight: " << weight<< " Pounds" << endl;
        
    }
    
    
};



class Vegetables :public item {
    
    double weight;
    
public:
    
    Vegetables() :item() {
        weight = 0;
    }
    
    Vegetables (string sName, string sCategory, double sPrice, int sQuantity, double sWeight) :
    item (sName, sCategory, sPrice, sQuantity)
    {
        weight = sWeight;
    }
    
    double getVegetablesWeight() {
        return weight;
    }
    
    void print(){
        item::print();
        cout << "Weight: " << weight<< " Pounds" << endl;
        
    }
    
    
};



//define method to check by model

bool checkByName(string name, vector<item*> &list)

{
    
    //Declare flag variable
    bool flag = false;
    for (int temp = 0; temp<list.size(); temp++)
        if ((*list[temp]).getItemName() == name)
            
        {
            temp = true;
            
            (*list[temp]).print();
            
        }
    
    return flag;
    
}

//define method to check by category

bool checkByCategory(string category, vector<item*> &list)

{
    
    bool flag = false;
    
    for (int temp = 0; temp<list.size(); temp++)
        
        if ((*list[temp]).getItemCategory() == category)
            
        {
            
            flag = true;
            
            (*list[temp]).print();
            
        }
    
    return flag;
    
}



void marketMenu()

{
    
    cout << "Main Menu" << endl;
    
    cout << "1. Search Inventory." << endl;
    cout << "2. Sell Items" << endl;
    cout << "3. Return Items." << endl;
    cout << "4. Add Items to inventory" << endl;
    cout << "5. Updated Items " << endl;
    cout << "6. Exit " << endl;

    
    cout << endl;
    
}



int main () {
    
    
    
    //file
    
    ifstream fin("itemInventory.txt");
    
    //Declare local variables
    
    
    
    string name;
    string category;
    int quantity;
    float price;
    double weight;
    
    vector<item*> itemlist;
        
    int option;
    
    int choicesearch;
    
    //check condition and load file
    
    if (fin.is_open())
        
    {
        
        while (fin >> category >> name >> quantity >> price)
            
        {
            
            if (category == "Fruit")
                
            {
                
                fin >> weight;
                
                int flag = checkByName(name, itemlist);
                
                if (flag == 0)
                    
                    itemlist.push_back(new Fruit(category, name, quantity, price, weight));
                
                else
                    
                    cout << "Item exists" << endl;
                
            }
            
            else if (category == "Vegetables")
                
            {
                
                fin >> weight;
                
                
                int flag = checkByName(name, itemlist);
                
                if (flag == 0)
                    
                    itemlist.push_back(new Vegetables(category, name, quantity, price, weight));
                
                else
                    
                    cout << "Item exists" << endl;
                
            }
            
            else if (category == "Meat")
                
            {
                
                fin >> weight;
                
                int flag = checkByName(name, itemlist);
                
                if (flag == 0)
                    
                    itemlist.push_back(new Meat(category, name, quantity, price, weight));
                
                else
                    
                    cout << "Item exists" << endl;
                
            }
            

        
        fin.close();
        
    }
    
    
    while (1)
        
    {
        
        //display menu
        
        marketMenu();
        
        cout << "Enter ur choice:";
        
        cin >> option;
        
        switch (option)
        
        {
                
                //Search inventory
                
            case 1:
                
            {
                
                cout << "Select \n 1. Search By Name.\n 2. Search By Category" << endl;
                
                cout << "Ur Option:";
                
                cin >> choicesearch;
                
                if (choicesearch == 1)
                    
                {
                    
                    cout << "Enter Name:";
                    
                    cin >> name;
                    
                    bool fg = checkByName(name, itemlist);
                    
                    if (fg == false)
                        
                        cout << "Item Not Found" << endl;
                    
                }
                
                else if (choicesearch == 2)
                    
                {
                    
                    cout << "Enter Category:";
                    
                    cin >> category;
                    
                    bool fg = checkByCategory(category, itemlist);
                    
                    if (fg == false)
                        
                        cout << "Item Not Found" << endl;
                    
                }
                
                
            }
                
                break;
                
            
                
            case 2:
                
            {
                
                choicesearch = 0;
                
                cout << "Select \n 1. Sell " << endl;
                
                cout << "Ur option:";
                
                cin >> choicesearch;
                
                cout << "Enter Name:";
                
                cin >> name;
                
                bool pk = checkByName(name,itemlist);
                
                if (pk == 0)
                    
                    cout << "Item Not Found" << endl;
                
                else
                    
                {
                    
                    if (choicesearch == 1)
                        
                    {
                        
                        for (int temp = 0; temp<itemlist.size(); temp++)
                            
                            if ((*itemlist[temp]).getItemName() == name)
                                
                            {
                                
                                itemlist.erase(itemlist.begin() + temp);
                                
                                cout << "Item Sold Succesfully." << endl;
                                
                                cout << "Inventory" << endl;
                                
                                for (int temp = 0; temp<itemlist.size(); temp++)
                                    
                                    (*itemlist[temp]).print();
                                
                                break;
                                
                            }
                        
                    }
                    
                    
                }
                
            }
                
                break;
                
                
            case 3:
                
            {
                
                cout << "Enter Name:";
                
                cin >> name;
                
                bool pk = checkByName(name, itemlist);
                
                if (pk == false)
                    
                    cout << "Item Not Found" << endl;
                
                else
                    
                {
                    
                            
                    for (int temp = 0; temp<itemlist.size(); temp++)
                                
                        if ((*itemlist[temp]).getItemName() == name)
                            
                        {
                            
                            itemlist.push_back(itemlist[temp]);
                            
                            itemlist.erase(itemlist.begin() + temp);
                            
                            cout << "Item Returned to Inventory" << endl;
                            
                            cout << "Inventory" << endl;
                            
                            for (int temp = 0; temp<itemlist.size(); temp++)
                                
                                (*itemlist[temp]).print();
                            
                            break;
                            
                        }
                    
                }
                
            }
                
                break;
                
                
            case 4:
                
            {
                
                cout << "Enter Name:";
                
                cin >> name;
                
                cout << "Enter Price:";
                
                cin >> price;
                
                cout << "Enter Quantity:";
                
                cin >> quantity;
                
                
                cout << "Enter category:";
                
                cin >> category;
                
                
           
                
                if (category == "Vegetables")
                    
                {
                    
                    cout << "Enter Weight of the Vegetables:";
                    
                    cin >> weight;
                    
                    int flag = checkByName(name, itemlist);
                    
                    if (flag == 0)
                        
                        itemlist.push_back(new Vegetables(category, name, quantity, price, weight));
                    
                    else
                        
                        cout << "Item exists" << endl;
                    
                }
                
                else if (category == "Fruits")
                    
                {
                    
                    cout << "Enter Weight of Fruits:";
                    
                    cin >> weight;
                    
                    int flag = checkByName(name, itemlist);
                    
                    if (flag == 0)
                        
                        itemlist.push_back(new Fruit(category, name, quantity, price, weight));
                    
                    else
                        
                        cout << "Item exists" << endl;
                    
                }
                
                else if (category == "Meat")
                    
                {
                    
                    cout << "Enter Weight of Meat:";
                    
                    cin >> weight;
                    
                    int flag = checkByName(name, itemlist);
                    
                    if (flag == 0)
                        
                        itemlist.push_back(new Meat(category, name, quantity, price, weight));
                    
                    else
                        
                        cout << "Item exists" << endl;
                    
                }
                
                cout << "Inventory" << endl;
                
                for (int temp = 0; temp<itemlist.size(); temp++)
                    
                    (*itemlist[temp]).print();
                
            }
                
                break;
                
                //exit
                
            case 5:
                
                exit(0);
                
                //default
                
            default:
                
                cout << "Invalid choice" << endl;
                
        }
        
    }
    
    return 0;
}

}
