Stack of CD's

 avatar
unknown
plain_text
3 years ago
3.2 kB
5
Indexable
#include <iostream>
#include <stdlib.h>

using namespace std;

struct CDStack {
private:
    int top; // No of stacks
    string stack[5]; // Container
    int max_size = sizeof(stack) / sizeof(stack[0]); // Current Maximum Capacity of Stack Container

    void initialize() {
        // Initialize Container
        for (int i = 0; i < max_size; i++) {
            stack[i] = "None";
        }
    }

public:
    // Simple Constructor
    CDStack(){
        top = -1;
        initialize();
        show();
    }

    void show() {
        // Show Stack List 
        cout << " // Stack: { ";
        for (int i = 0; i < max_size; i++) {
            cout << stack[i] << " ";
        }
        cout << "} \n";
    }

    void addCD(string CD) {

        // If top is same as the max capacity of container or stack, then It is Full.
        if (top == max_size - 1) {
            cout << "Stack." << endl;
            cout << CD << " cannot be added. You have to remove others.";
            show();
        }

        // If top is not yet Full.
        else {
            top += 1;
            stack[top] = CD;
            cout << stack[top] << " is added to Stack of CD's.";
            show();
        }
    }

    bool isEmpty() { return (top == -1) ? true : false; } // 1 Line Function for checking if Empty or not.

    void removeCD() {

        // If the stack is Empty, then it cannot remove any item.
        if (isEmpty()) {
            cout << "You cannot remove a CD." << endl;
            show();
        }
    }

    void count() {
        // Count the number of items within the stack.
        cout << "Size: " << top + 1 << endl;
    }

    void peek() {
        // Peek or see what's the top value of the Stack.
        if (!isEmpty()) {
            cout << "Last CD added is " << stack[top] << ".\n\n";
        }
    }
};

int main() {
    CDStack stack; // Create CDStack Object named "Stack"
    int choice;
    bool flag = true; // Loop Stopper
    while (flag != false) {

        // Clear and Clean Processing.
        cout << "1 - Add a CD." << endl;
        cout << "2 - Remove a CD." << endl;
        cout << "3 - Show Stack of CD's Inventory." << endl;
        cout << "4 - Count the CD's." << endl;
        cout << "5 - See Stack." << endl;
        cout << "6 - Clear Console." << endl;
        cout << "0 - Exit Console." << endl;

        cout << "\n\nSelect Operation: ";
        cin >> choice;

        system("CLS");

        switch (choice) {
        case 1: {
            string name;
            cout << "Enter CD name: ";
            cin >> name;
            stack.addCD(name);
        }break;

        case 2: {
            stack.removeCD();
        }break;

        case 3: {
            stack.show();
        }break;

        case 4: {
            stack.count();
        }break;

        case 5: {
        	stack.peek();
        }break;

        case 6: {
            system("CLS");
        }break;

        case 0: {
            flag = false;
            cout << "Exited to Console." << endl;
        }break;

        default: {
            cout << "Invalid." << endl;
        }break;
        }
    }
    return 0;
}
Editor is loading...