Pr13

 Pizza parlor accepting maximum M orders. Orders are served in first come first served basis. Order

once placed cannot be cancelled. Write C++ program to simulate the system using circular queue

using array.

Objectives:-

1. To understand concept of circular queue.





#include <iostream>

#include <string>

using namespace std;


class PizzaParlor {

private:

    string* orders;    // Array to store pizza orders

    int front;         // Front pointer (for serving orders)

    int rear;          // Rear pointer (for placing orders)

    int capacity;      // Maximum capacity (M orders)

    int size;          // Current number of orders


public:

    // Constructor to initialize the queue with a given capacity

    PizzaParlor(int M) {

        capacity = M;

        orders = new string[capacity];

        front = -1;

        rear = -1;

        size = 0;

    }


    // Function to check if the queue is full

    bool isFull() {

        return size == capacity;

    }


    // Function to check if the queue is empty

    bool isEmpty() {

        return size == 0;

    }


    // Function to place an order (add to rear)

    void placeOrder(const string& order) {

        if (isFull()) {

            cout << "Sorry, the pizza parlor is full. Cannot accept more orders." << endl;

            return;

        }

        if (rear == -1) {  // If the queue is empty

            front = rear = 0;

        } else {

            rear = (rear + 1) % capacity;  // Circular increment

        }

        orders[rear] = order;

        size++;

        cout << "Order placed: " << order << endl;

    }


    // Function to serve an order (remove from front)

    void serveOrder() {

        if (isEmpty()) {

            cout << "No orders to serve!" << endl;

            return;

        }

        cout << "Order served: " << orders[front] << endl;

        front = (front + 1) % capacity;  // Circular increment

        size--;

        if (isEmpty()) {

            front = rear = -1;  // Reset pointers when the queue is empty

        }

    }


    // Function to display the current orders in the queue

    void displayQueue() {

        if (isEmpty()) {

            cout << "No orders in the queue!" << endl;

            return;

        }

        cout << "Current orders in the queue: ";

        int i = front;

        while (i != rear) {

            cout << orders[i] << " ";

            i = (i + 1) % capacity;

        }

        cout << orders[rear] << endl;

    }


    // Destructor to free dynamically allocated memory

    ~PizzaParlor() {

        delete[] orders;

    }

};


int main() {

    int maxOrders;

    cout << "Enter maximum number of orders the pizza parlor can accept: ";

    cin >> maxOrders;


    PizzaParlor parlor(maxOrders);


    // Simulating the pizza parlor's operations

    parlor.placeOrder("Margherita");

    parlor.placeOrder("Pepperoni");

    parlor.placeOrder("BBQ Chicken");

    parlor.placeOrder("Vegetarian");


    parlor.displayQueue();


    parlor.serveOrder();

    parlor.displayQueue();


    parlor.placeOrder("Hawaiian");

    parlor.placeOrder("Meat Lovers");


    parlor.displayQueue();


    parlor.serveOrder();

    parlor.displayQueue();


    return 0;

}


// Explanation:

// PizzaParlor Class:


// Data Members:

// orders[]: An array to store pizza orders.

// front: Pointer to the front of the queue (serving point).

// rear: Pointer to the rear of the queue (order placement point).

// capacity: The maximum number of orders the pizza parlor can accept.

// size: The current number of orders in the queue.

// Methods:


// isFull(): Checks if the queue is full by comparing size with capacity.

// isEmpty(): Checks if the queue is empty by comparing size with 0.

// placeOrder(): Adds a new order to the rear of the queue. If the queue is full, it displays an error message.

// serveOrder(): Removes an order from the front of the queue (FCFS). If the queue is empty, it displays a message indicating there are no orders to serve.

// displayQueue(): Displays all current orders in the queue, starting from the front to the rear.

// Destructor: Cleans up the dynamically allocated memory for the order array.

// Main Function:


// Initializes the pizza parlor with a maximum capacity of orders (maxOrders).

// Simulates placing and serving orders by calling the placeOrder(), serveOrder(), and displayQueue() functions.

Comments

Popular posts from this blog

pr7