lab 6 task 3

 avatar
chamanEiqbal
c_cpp
a year ago
4.5 kB
4
Indexable
Never
#include <iostream>
#include<windows.h>
using namespace std;

//classes

const int MAX_SIZE = 15;

class Car {
  private:
  int carNumber;
  string carModel;
  int fuel;
  
  public:
  Car() {
      // default constructor
  }
  Car(int carNumber, string carModel, int fuel) {
      this->carNumber = carNumber;
      this->carModel = carModel;
      this->fuel = fuel;
  }
  
  void toString() {
      cout << "Car Number:\t" <<  this->carNumber << endl;
      cout << "Car Model:\t" << this->carModel << endl;
      cout << "Car Fuel Limit: \t" << this->fuel << endl;
  }
  
  //getter setters
  void setNumber(int carNumber) {this->carNumber = carNumber;}
  void setModel (string carModel) {this->carModel = carModel;}
  void setFuel (int fuel) {this->fuel = fuel;}
  
  int getNumber() {return this->carNumber;}
  int getFuel() {return this->fuel;}
  string getModel(){return this->carModel;}
  
  
  
};

class Queue {
private:
	int front;
	int rear;
	Car queue[MAX_SIZE]; //default size
	
public:
	bool isFull() {return (rear+1) % MAX_SIZE == front;}
	bool isEmpty() {return front == -1 && rear == -1;}	
	bool isThere(Car *check) {
	    bool flag = false;
	    for(int i = front; i != rear; i=(i+1)%MAX_SIZE) {
	        if(check->getNumber() == queue[i].getNumber()) {
	            flag = true;
	            break;
	        }
	    }
	    
	    return flag;
	}

	Queue() {
		front = -1;
		rear = -1;
	}

	void enQueue(Car *newcar) {
		if(isFull()) {
			cout << "Queue overflow." << endl;
		}
		
		else if(isEmpty()) {
			front = rear = 0;
		}
		else {
			rear = rear+1 % MAX_SIZE;
		}
		
		if(!isThere(newcar)) {
		    queue[rear] = *newcar;   
		}
		else {
		    cout << "Number is already there." << endl;
		    if(rear == 0) {
		        rear = MAX_SIZE-1;
		    }
		    else {
		    rear = rear-1;
		    }
		}
	}

	void deQueue() {
		if(isEmpty()) {
			cout << "Cannot dequeue, queue underflow." << endl;
		}

		if(front == rear) {
			cout << queue[front].getModel() << " is being dequeued. " << endl;
			front = rear = -1;
		} else {cout << queue[front].getModel()<< " is being dequeued." << endl;
		front = front+1 % MAX_SIZE;
		}
	}

	void peek() {
		if(isEmpty()) {
			cout << "Queue Underflow." << endl;
		}
		else {
		    
		    cout << "Car at front of the queue: " << endl;
		    queue[front].toString();
		}
	}
	
	Car getFront() {
	   return queue[front]; 
	}

	void display() {
		
		if((isEmpty())) {cout << "Queue underflow." << endl;}
		else {
			cout << " \n \n DISPLAYING QUEUE \n ------" <<endl;
		for(int i = front; i !=rear; i = (i+1) % MAX_SIZE) {
			queue[i].toString();
			cout << endl << endl;
			}
		queue[rear].toString();
		}
	}
	
	void carsLeft() {
	    int count = 0;
	    if(isEmpty()) {cout << "No Cars Left!" << endl;}
	    else {
	        for(int i = front; i != rear; i = (i+1)%MAX_SIZE) {
	            count = count + 1;
	        }
	    }
	    
	    cout << "\n \nCars Left in the Queue: " << count << endl;
	}
};



//main

int main() {
    Queue *q = new Queue();

    q->enQueue(new Car(1234, "Car Model 1", 50)); // number = 1234
    q->enQueue(new Car(5678, "Car Model 2", 45));
    q->enQueue(new Car(9101, "Car Model 3", 55));
    q->enQueue(new Car(1234, "Car Model 4", 60)); // number = 1234 (enqueue operation will be exempted)
    q->enQueue(new Car(1516, "Car Model 5", 40));
    q->enQueue(new Car(1718, "Car Model 6", 48));
    q->enQueue(new Car(1920, "Car Model 7", 52));
    q->enQueue(new Car(2122, "Car Model 8", 47));
    q->enQueue(new Car(2324, "Car Model 9", 58));
    q->enQueue(new Car(2526, "Car Model 10", 42));
    q->enQueue(new Car(2728, "Car Model 11", 51));
    q->enQueue(new Car(2930, "Car Model 12", 53));
    q->enQueue(new Car(3132, "Car Model 13", 46));
    q->enQueue(new Car(3334, "Car Model 14", 49));
    q->enQueue(new Car(3536, "Car Model 15", 44));

    
    q->display();
    
    cout << endl;
    // deQueueing after wait time set by fuel limit of each car.
    for(int i = 0; i < 10; i++) {
        Car tempCar = q->getFront();
        int wait = tempCar.getFuel() * 100;
        
        Sleep(wait);
        cout << "After waiting " << wait << " milliseconds, ";
        q->deQueue();
    }
    
        
         
         cout << endl;
         
         q->peek();
         
         q->carsLeft();
    
    system("pause");
    return 0;
}