0% found this document useful (0 votes)
9 views5 pages

Cs301 Assignment Solution

The document contains a C++ program for an inventory management system using a queue data structure. It defines classes for products and the queue, allowing for the addition and removal of products, as well as displaying the first and last products in the queue. The program simulates adding smartphones, tablets, and smartwatches to inventory and selling two products, demonstrating the queue functionality.

Uploaded by

ksa.r859ab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Cs301 Assignment Solution

The document contains a C++ program for an inventory management system using a queue data structure. It defines classes for products and the queue, allowing for the addition and removal of products, as well as displaying the first and last products in the queue. The program simulates adding smartphones, tablets, and smartwatches to inventory and selling two products, demonstrating the queue functionality.

Uploaded by

ksa.r859ab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CS301 C++ Code: Inventory Management with Queue

#include <iostream>
#include <string>
using namespace std;

class Product {
private:
int data; // Serial number
int productId; // Unique Product ID
Product* next;

public:
Product() {
data = 0;
productId = 0;
next = nullptr;
}

int getData() {
return data;
}

void setData(int d) {
data = d;
}

int getProductId() {
return productId;
}

void setProductId(int pid) {


productId = pid;
}

Product* getNext() {
return next;
}

void setNext(Product* nextNode) {


next = nextNode;
}
};

class Queue {
private:
Product* front;
Product* rear;

public:
Queue() {
front = rear = nullptr;
}

void enqueue(int data, int pid) {


Product* temp = new Product();
temp->setData(data);
temp->setProductId(pid);
temp->setNext(nullptr);

if (rear == nullptr) {
front = rear = temp;
} else {
rear->setNext(temp);
rear = temp;
}

cout << "Added -> Serial: " << data << ", Product ID: " << pid << endl;
}

int dequeue() {
if (front == nullptr) return -1;

Product* temp = front;


int pid = temp->getProductId();
cout << "Sold product with serial: " << temp->getData() << " and ID: " << pid << endl;
front = front->getNext();
if (front == nullptr) rear = nullptr;
delete temp;
return pid;
}

void displayFront() {
if (front)
cout << "First product in queue -> Serial: " << front->getData() << ", Product ID: " <<
front->getProductId() << endl;
}

void displayRear() {
if (rear)
cout << "Last product in queue -> Serial: " << rear->getData() << ", Product ID: " <<
rear->getProductId() << endl;
}
};

int main() {
string vuid = "BC123456742";
cout << "Student ID: " << vuid << endl << "=============================" << endl;

int last_four = 6742;


int last_three = 742;
int serial = 1;
int product_id = last_four;

Queue q;

cout << "7 smartphones being added to inventory:" << endl;


for (int i = 0; i < last_three % 10; i++) {
q.enqueue(serial++, product_id++);
}

cout << "\n8 tablets being added to inventory:" << endl;


for (int i = 0; i < (last_three / 10) % 10; i++) {
q.enqueue(serial++, product_id++);
}
cout << "\n9 smart watches being added to inventory:" << endl;
for (int i = 0; i < (last_three / 100) % 10; i++) {
q.enqueue(serial++, product_id++);
}

cout << "\n";


q.displayFront();
q.displayRear();

cout << "\nSelling 2 products..." << endl;


q.dequeue();
q.dequeue();

cout << "\n";


q.displayFront();

return 0;
}
CS301 Assignment: Inventory Queue Simulation
Student ID: BC123456742

7 smartphones being added to inventory:

Added -> Serial: 1, Product ID: 6742

Added -> Serial: 2, Product ID: 6743

Added -> Serial: 3, Product ID: 6744

Added -> Serial: 4, Product ID: 6745

Added -> Serial: 5, Product ID: 6746

Added -> Serial: 6, Product ID: 6747

Added -> Serial: 7, Product ID: 6748

8 tablets being added to inventory:

Added -> Serial: 8, Product ID: 6749

Added -> Serial: 9, Product ID: 6750

Added -> Serial: 10, Product ID: 6751

Added -> Serial: 11, Product ID: 6752

Added -> Serial: 12, Product ID: 6753

Added -> Serial: 13, Product ID: 6754

Added -> Serial: 14, Product ID: 6755

Added -> Serial: 15, Product ID: 6756

9 smart watches being added to inventory:

Added -> Serial: 16, Product ID: 6757

Added -> Serial: 17, Product ID: 6758

Added -> Serial: 18, Product ID: 6759

Added -> Serial: 19, Product ID: 6760

Added -> Serial: 20, Product ID: 6761

Added -> Serial: 21, Product ID: 6762

Added -> Serial: 22, Product ID: 6763

Added -> Serial: 23, Product ID: 6764

Added -> Serial: 24, Product ID: 6765

First product in queue -> Serial: 1, Product ID: 6742

Page 1
CS301 Assignment: Inventory Queue Simulation
Last product in queue -> Serial: 24, Product ID: 6765

Selling 2 products...

Sold product with serial: 1 and ID: 6742

Sold product with serial: 2 and ID: 6743

First product in queue -> Serial: 3, Product ID: 6744

Page 2

You might also like