In: Computer Science
Use linked list and write in C++
Food ordering system
1. Place Order
2. View the food details
3. Modify food details
4. Delete food details
Order should let the customer to enter the food code, flavor, weight(kg), unit price, qty, customerID, name, address and contact number. It will also have an order id automatically assigned with a unique ID when new order is added. When view the food details, it should also calculate the unit price * qty as a bill amount. You should also be able to modify all the food details and delete food details.
Do not copy code from the internet.
Code:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
bool check = true;
int id=1;
struct Node {
int o_id;
int food_code;
char flavor[15];
int weight;
float price;
int qty;
int c_id;
char name[30];
char add[50];
char c_no[10];
struct Node* next;
}*head,*lastptr;
void add(){ //Place Order//
Node *temp;
temp = new Node;
temp->o_id =id++; //it will store unique orderid for every order
cout << "Enter food code: ";
cin >> temp->food_code;
cout << "Enter flavor: ";
cin >> temp->flavor;
cout << "Enter weight: ";
cin >> temp->weight;
cout << "Enter price: ";
cin >>temp->price;
cout << "Enter qty: ";
cin >>temp->qty;
cout << "Enter customer id: ";
cin >> temp->c_id;
cout << "Enter your name: ";
cin >> temp->name;
cout << "Enter your address: ";
cin >> temp->add;
cout << "Enter your contact number: ";
cin >> temp->c_no;
temp->next = NULL;
if(check) //if check is true then it is first order
{
head = temp;
lastptr = temp;
check = false;
}
else //false then someone has orderd food before
{
lastptr->next=temp;
lastptr=temp;
}
cout << "\nOrder placed successfully Order Id : "<<temp->o_id<<"\n";
}
void search() //View the food details//
{
Node *prev=NULL;
Node *current=NULL;
int o_id2;
cout<<"Enter order id to search:";
cin>>o_id2;
prev=head;
current=head; //point to first node
while(current!=NULL && current->o_id!=o_id2) //travese till enterd orderid is match with ordered records
{
prev=current;
current=current->next; //current pointed to matched oderid
}
if (current == NULL)
{
cout << "\nOrder id is not valid...\n"<<endl;
return;
}
cout<<"\ncustomer name: "<<current->name;
cout<<"\nbill amount: "<<(current->price*current->qty);
cout<<"\nfood code: "<<current->food_code;
cout<<"\nflavor: "<<current->flavor;
cout<<"\nweight: "<<current->weight;
cout<<"\nprice: "<<current->price;
cout<<"\nqty: "<<current->qty<<"\n";
}
void modify() //Modify food details//
{
Node *ptr;
Node *prev=NULL;
Node *current=NULL;
int o_id3;
cout<<"Enter order id to modify:";
cin>>o_id3;
prev=head;
current=head;
while(current!=NULL &¤t->o_id!=o_id3) //travese till enterd orderid is match with ordered records
{
prev=current;
current=current->next; //current pointed to matched oderid
}
if (current == NULL)
{
cout << "\nOrder id is not valid...\n"<<endl;
return;
}
cout<<"\nWhat you want to modify :"<<"\n"; //Ask what you want to modify
cout<<"1. Food code"<<"\n";
cout<<"2. Flavor"<<"\n";
cout<<"3. Weight"<<"\n";
cout<<"4. Qty"<<"\n";
cout<<"Enter your choice"<<"\n";
int temp_choice;
cin >> temp_choice;
switch(temp_choice){ //it will modify food details based on your choice
case 1 :
cout << "\nEnter new food code: ";
cin >> current->food_code;
break;
case 2 :
cout << "\nEnter new flavor: ";
cin >> current->flavor;
break;
case 3 :
cout << "\nEnter new weight: ";
cin >> current->weight;
break;
case 4:
cout << "\nEnter new qty: ";
cin >> current->qty;
break;
default:
cout<<"\n"<<"Choice not available";
break;
}
cout<<endl<<"\nRecored is Modified successfully\n";
}
void del() //Delete food details//
{
Node *ptr=NULL;
Node *prev=NULL;
Node *current=NULL;
int o_id1;
cout<<"Enter order id to Delete:"<<endl;
cin>>o_id1;
prev=head;
current=head;
while(current!=NULL && current->o_id!=o_id1) //taverse till orderid not match with ordered records
{
prev=current;
current=current->next; //current is pointed to node wich you want delete
}
if (current == NULL)
{
cout << "\nOrder id is not valid...\n"<<endl;
return;
}
prev->next = current->next; //previous node pounts to next to next node
current->next=NULL;
delete current; //delete current node
cout<<endl<<"\nOrder Deleted\n";
}
int main()
{
cout << "Food ordering system"<< "\n";
while(true){
int choice;
cout << "1. Place Order."<< "\n";
cout << "2. View the food details"<< "\n";
cout << "3. Modify food details"<< "\n";
cout << "4. Delete food details" << "\n" ;
cout << "5. Exit" << "\n" ;
cout << "Enter Your choice : ";
cin >> choice;
switch(choice){
case 1 :
add();
break;
case 2 :
search();
break;
case 3 :
modify();
break;
case 4 :
del();
break;
case 5 :
exit(0);
break;
default:
cout<<"Enter valid choice\n";
break;
}
}
return 0;
}
OUTPUT:
To order food select 1 then ask details of food and your information. After placing order it returns orderid.
After placing order you can view your order with total amount. here 2 quantity so total amount is 60
You can modify or oreder details based on your choice. Here, i want to add one quantity so total quantity is 3.
You can view order details after moditying order details. Here, after modifying quantity total amount is 90.
You can also delete food.
CODE: