0% found this document useful (0 votes)
12 views3 pages

301pass cpp2024

Uploaded by

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

301pass cpp2024

Uploaded by

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

#include <iostream>

#include <string>
using namespace std;

class Part {
public:
int partID;
string partName;
int quantity;
Part* next;

Part(int partID, string partName, int quantity)


: partID(partID), partName(partName), quantity(quantity), next(nullptr) {}
};

class Inventory {
public:
Part* head;

Inventory() : head(nullptr) {}

// Function to add a new part to the inventory linked list


void addPart(int partID, string partName, int quantity) {
Part* newPart = new Part(partID, partName, quantity);
if (!head) { // If list is empty, set head to the new part
head = newPart;
} else {
Part* current = head;
while (current->next) {
current = current->next;
}
current->next = newPart; // Add new part at the end of the list
}
}

// Function to delete a part from the inventory based on part ID


void deletePart(int partID) {
if (!head) {
cout << "Inventory is empty. Cannot delete part." << endl;
return;
}

if (head->partID == partID) { // If the head needs to be deleted


Part* temp = head;
head = head->next;
delete temp;
cout << "Part ID " << partID << " deleted from the inventory." << endl;
return;
}

Part* current = head;


Part* previous = nullptr;
while (current && current->partID != partID) {
previous = current;
current = current->next;
}

if (!current) {
cout << "Part ID " << partID << " not found." << endl;
return;
}

previous->next = current->next;
delete current;
cout << "Part ID " << partID << " deleted from the inventory." << endl;
}

// Function to update the quantity of an existing part


void updateQuantity(int partID, int newQuantity) {
Part* current = head;
while (current) {
if (current->partID == partID) {
current->quantity = newQuantity;
cout << "Quantity of Part ID " << partID << " updated to " <<
newQuantity << "." << endl;
return;
}
current = current->next;
}
cout << "Part ID " << partID << " not found in the inventory." << endl;
}

// Function to find and display a part by its part ID


void findPart(int partID) {
Part* current = head;
while (current) {
if (current->partID == partID) {
cout << "Part Found - ID: " << current->partID << ", Name: " <<
current->partName
<< ", Quantity: " << current->quantity << endl;
return;
}
current = current->next;
}
cout << "Part ID " << partID << " not found in the inventory." << endl;
}

// Function to print the entire inventory


void printInventory() {
cout << "Part ID Part Name Quantity" << endl;
cout << "-------------------------------------" << endl;
Part* current = head;
while (current) {
cout << current->partID << " " << current->partName;
cout << " " << current->quantity << endl;
current = current->next;
}
cout << endl;
}
};

int main() {
cout << "----------------------------------------------" << endl;
cout << "Computer Parts Management System [BC230203256]" << endl; // Change to
your student ID
cout << "----------------------------------------------" << endl;

Inventory storeInventory;
// Pre-populated data for demonstration
storeInventory.addPart(101, "SSD", 25);
storeInventory.addPart(102, "RAM", 40);
storeInventory.addPart(103, "HDD", 15);

cout << endl << "Inventory Added." << endl;


storeInventory.printInventory(); // Print initial inventory

cout << "Finding Part ID 101:" << endl;


storeInventory.findPart(101); // Test finding part with ID 101
cout << endl;

cout << "After deleting SSD and updating RAM Quantity..." << endl << endl;

storeInventory.deletePart(101); // Delete part with ID 101


storeInventory.updateQuantity(102, 50); // Update quantity for RAM

storeInventory.printInventory(); // Print updated inventory

return 0;
}

You might also like