Problem Statement:

Due to the Coronavirus pandemic Government has asked all the major grocery stores to serve the customers as soon as possible so that the gathering of the people may be avoided. In this regard all the grocery stores have decided that there will be two queues for the customers at each grocery store. One queue is the express queue and second is the normal queue. Express queue will have the customers which have bought 5 or less than 5  grocery items. Similarly normal queue will have the customers with grocery items greater than 5. These queues will help the customer to choose the  line according to their grocery shopping requirements and they will have to wait in their queue accordingly. The idea is to develop a C++ program that could serve the purpose of FIFO (first come first served)for both express and normal service queues so that each customer may select queue according to his/her shopping requirements. The program must have the features to:

  • Record the information of the customer (name, shopping information etc.) and identify the type of customer so that he/she may select express queue or normal queue)
  • Simulate both express and normal queues i.e., count the number of customers and the serving time of each customer in each queue.

The program must have the following functional features:

  1. Record information of each customer that would be the customer name, customer id, number of grocery items purchased.
  2. Send each customer to either express queue or normal queue based on the number  of grocery items customer has purchased.
  3. Keep the count of customers in each queue. Whenever a customer enters or exits a queue display the total number of customers in that queue
  4. Print the information of the customers of both the queues.

_________________________________________________________________

(Code for Solution)
#include<iostream>
#include<string>
using namespace std;
class CustomerNode{
	public:
		int cust_id = 0;
		string cust_name = "";
		int item_pur = 0;
		CustomerNode *next;
};
class CustomerQueue{
	public:
		CustomerNode *front, *rear;
		CustomerQueue(){
			front=rear='\0';
		}
		void addCustomer(int,string,int);
		void removeCustomer(char,int);
		void display(CustomerNode*,string,int);
};
void CustomerQueue::addCustomer(int id,string name, int item){
	CustomerNode *newNode = new CustomerNode;
	
	newNode->cust_id = id;
	newNode->cust_name = name;
	newNode->item_pur = item;
	newNode->next = '\0';
	
	if(front=='\0'){
		front = rear = newNode;
		rear->next = '\0';
	}
	else{
		while(rear->next !='\0'){
			rear = rear->next;
		}
		rear->next = newNode;
		rear = newNode;
	}
}
void CustomerQueue::removeCustomer(char q,int count){
	CustomerNode *temp = front;
	string queue;
	if(front == '\0'){
		rear='\0';
		if(q == 'n'){
			queue = "Express ";
		}
		else{
			queue = "Normal ";
		}
		cout<<"-----"<<queue<<"Queue Customer Information-----"<<endl;
		cout<<queue<<"queue is empty"<<endl;
	}
	else{
		if(q == 'n'){
			queue = "Express ";
		}
		else{
			queue = "Normal ";
		}
		cout<<"-----"<<queue<<"Queue Customer Information-----"<<endl;
		display(temp,queue,count);
		delete(temp);
	}
}
void CustomerQueue::display(CustomerNode* temp,string queue, int count){
	temp = front;
	while(temp !='\0'){
		cout<<"Customer Name = "<<temp->cust_name<<endl;
		cout<<"Customer ID = "<<temp->cust_id<<endl;
		cout<<"Number of Grocery Items Purchased = "<<temp->item_pur<<endl;
		cout<<"Total customers in "<<queue<<"Queue = "<<count<<endl;
		temp=temp->next;
	}
}
int main(){
	CustomerQueue express,normal;
	int id = 0, item = 0,exp_count = 0,nor_count = 0;
	string name;
	bool check = true;
	char choice,exp= 'e',nor= 'n';
	while(check){
		cout<<"-------------Enter Customer Information ------------"<<endl;
		cout<<"Customer ID = ";
		cin>>id;
		cout<<"Customer Name = ";
		cin>>name;
		cout<<"Grocery Items Purchased = ";
		cin>>item;
		if(item<=5){
			cout<<"-----Customer Entered in Normal Queue-----"<<endl;
			normal.addCustomer(id,name,item);
			nor_count++;
			cout<<"Total Number of customers in Normal Queue ="<<nor_count<<endl;
		}
		else{
			cout<<"-----Customer Entered in Express Queue-----"<<endl;
			express.addCustomer(id,name,item);
			exp_count++;
			cout<<"Total Number of customers in Express Queue ="<<exp_count<<endl;
		}
		cout<<"\n Do want to enter another Customer? Enter Y or y for Yes and N or n for No = ";
		cin>>choice;
		if(choice == 'n' || choice == 'N'){
			express.removeCustomer(exp,exp_count);
			normal.removeCustomer(nor,nor_count);
			check = false;
			
		}
	}
	return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *