Answer to Question #211637 in C++ for Aniqa

Question #211637

Ali has decided to go Online with his fast food store. You have been hired by him to make a system for its sale of fast food items.

To create this program you have to create a C++ Program. The information that you are dealing with is name of Food item, category of Food (Desi, italian) price of Food item and stock to know how much the Food item is present in the stock.

First you need to create a menu which has the following options:

1.       Add a new Food item.

2.       Buy a Fruit item and display it.

a.     Upon buying the fruit item should get decreased by the quantity bought.

b.    If the quantity bought is 2, stock should be decreased by 2.

3.       Display fruits that have stocks lower than 10.

4.      Display Least expensive fruit Item from the list.


1
Expert's answer
2021-06-29T00:11:29-0400


#include<iostream>
#include<bits/stdc++.h>
using namespace std;


class food{
	public:
	string item;
	string category;
	int price; 
	int stock;
};


bool compare(food a, food b)
{
	return a.stock < b.stock;
}


bool compare1(food a, food b)
{
	return a.price < b.price;
}


int main()
{
	map<string, vector<food>>m;
	while(1)
	{
	cout<<"1. Add a new food item"<<endl;
	cout<<"2. Buy a food and display it"<<endl;
	cout<<"3. Display food that have stocks lower than 10"<<endl;
	cout<<"4. Display Least expensive food Item from the list"<<endl;
	cout<<"5. Exit"<<endl<<endl;
	cout<<"Enter your choice according to above options : ";
	int option;
	cin>>option;
	if(option == 5)
	{
		break;
	}
	switch(option)
	{
		case 1:
			{
			food ob;
			cout<<endl<<"Enter name of food item to be added : ";
			cin>>ob.item;
			cout<<"Enter food category : ";
			cin>>ob.category;
			cout<<"Enter price of food : ";
			cin>>ob.price;
			cout<<"Enter its stock available : ";
			cin>>ob.stock;
			m[ob.category].push_back(ob);
			cout<<"Food item added successfully"<<endl;
			break;
		}
			
		case 2:
			{
			string s;
			cout<<endl<<"Categories"<<endl;
			cout<<"------------------"<<endl;
			for(auto it = m.begin(); it!= m.end(); it++)
			{
				cout<<it->first<<endl;
			}
			cout<<"Enter the category of food item u want to buy : ";
			cin>>s;
			cout<<"Available food items of the enteres category"<<endl;
			cout<<endl<<endl;
			if(m[s].empty())
			{
				cout<<endl<<"No items avalable"<<endl;
			}
			else{


			bool ans = false;
			cout<<"Food Item"<<setw(20)<<"Stock Available"<<endl;
			cout<<"--------------------------------------------"<<endl;
			for(auto it = m.begin(); it!= m.end(); it++)
			{
				if(it->first == s)
				{
					ans = true;
					vector<food>v = it->second;
					for(int j=0; j < v.size(); j++)
					{
						cout<<v[j].item<<setw(20)<<v[j].stock<<endl;
					}
					break;
				}
			}
		
			cout<<"Select the item u want : ";
			string s1;
			cin>>s1;
			cout<<"Enter how much quantity u want : ";
			int n;
			cin>>n;
			for(auto it = m.begin(); it!= m.end(); it++)
			{
				if(it->first == s)
				{
					ans = true;
					vector<food>v = it->second;
					for(int j=0; j < it->second.size(); j++)
					{
						if(it->second[j].item == s1)
						{
							if(it->second[j].stock < n)
							{
								cout<<"Stock is less"<<endl;
							}
							else{
								it->second[j].stock = it->second[j].stock - n;
								cout<<"Items bought successfully"<<endl<<endl;				 
							}
						}
					}
					break;
				}
			}
			
			if(ans == false)
			{
				cout<<"Entered food item is not available"<<endl;
			}
			
			break;
		}	
	}
	case 3:
		{
			for(auto it = m.begin(); it!= m.end(); it++)
			{
				sort(it->second.begin(), it->second.end(), compare);
			}
			
			cout<<"Category"<<setw(20)<<endl<<"Food item"<<setw(20)<<"Stock"<<endl;
			cout<<"----------------------------------------"<<endl;
			for(auto it = m.begin(); it!= m.end(); it++)
			{
				for(int i=0; i<it->second.size(); i++)
				{
					if(it->second[i].stock < 10)
					{
						cout<<it->first<<setw(20)<<it->second[i].item<<setw(20)<<it->second[i].stock<<endl;
					}
					else{
						break;
					}
				}
			}	
			break;
				
		}
		
	case 4:
		{
			for(auto it = m.begin(); it!= m.end(); it++)
			{
				sort(it->second.begin(), it->second.end(), compare1);
			}
			
			cout<<"Category"<<setw(20)<<endl<<"Food item"<<setw(20)<<"Price"<<endl;
			cout<<"----------------------------------------"<<endl;
			for(auto it = m.begin(); it!= m.end(); it++)
			{
				for(int i=0; i<it->second.size(); i++)
				{
					if(it->second[i].stock < 10)
					{
						cout<<it->first<<setw(20)<<it->second[i].item<<setw(20)<<it->second[i].price<<endl;
					}
					else{
						break;
					}
				}
			}	
			break;
		}
	}
	
	cout<<endl<<endl;
}
	
}













Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS