POS

mail@pastecode.io avatar
unknown
c_cpp
a month ago
23 kB
5
Indexable
Never
#include <iostream>
#include <string>

using namespace std;

struct Item {
    int id;
    string name;
    float price;
    Item* next;
};

struct Diskon {
    int id;
    float percentage;
};

struct Tax {
    int id;
    float percentage;
};

struct PaymentMethod {
    int id;
    string name;
    PaymentMethod* next;
};

struct TransactionItem {
    Item* item;
    int quantity;
    TransactionItem* next;
};

struct Transaction {
    int invoice_id;
    TransactionItem* items;
    float total_price, tax_percentage, discount_percentage;
    Transaction* next;
};

// struct buat simpan riwayat transaksi yang dihapus
struct StackNode {
    Transaction* transaction;
    StackNode* next, *top;
};

StackNode deletedTransactions = {nullptr};

// queue buat manage item
struct QueueNode {
    Item* item;
    QueueNode* next;
};

struct Queue {
    QueueNode* front;
    QueueNode* rear;
};

Queue itemQueue = {nullptr, nullptr};

void enqueue(Queue& queue, Item* item) {
    QueueNode* newNode = new QueueNode{item, nullptr};
    if (!queue.front) {
        queue.front = queue.rear = newNode;
    } else {
        queue.rear->next = newNode;
        queue.rear = newNode;
    }
}

// otomatis hapus data paling awal
Item* dequeue(Queue& queue) {
    if (!queue.front) {
        return nullptr; // Queue kosong
    }
    QueueNode* temp = queue.front;
    Item* item = temp->item;
    queue.front = queue.front->next;
    if (!queue.front) {
        queue.rear = nullptr;
    }
    delete temp;
    return item;
}

// menampilkan semua item dalam queue
void displayQueue(const Queue& queue) {
    if (!queue.front) {
        cout << "Queue kosong!\n";
        return;
    }
    QueueNode* temp = queue.front;
    while (temp) {
        cout << temp->item->id << ". " << temp->item->name << " - " << temp->item->price << "\n";
        temp = temp->next;
    }
}

// push transaksi ke stack
void push(StackNode& stack, Transaction* transaction) {
    StackNode* newNode = new StackNode;
    newNode->transaction = transaction;
    newNode->next = stack.top;
    stack.top = newNode;
}

// pop transaksi dari stack
Transaction* pop(StackNode& stack) {
    if (stack.top == nullptr) {
        return nullptr; // Stack kosong
    }
    StackNode* temp = stack.top;
    Transaction* transaction = temp->transaction;
    stack.top = stack.top->next;
    delete temp;
    return transaction;
}

Item* items = nullptr;
Diskon diskons[100];
Tax taxes[100];
PaymentMethod* paymentMethods = nullptr;
Transaction* transactions = nullptr;

int itemCount = 0;
int diskonCount = 0;
int taxCount = 0;
int paymentMethodCount = 0;
int transactionCount = 0;


void addItem(int id, const string& name, float price) {
    Item* newItem = new Item{id, name, price, nullptr};
    
    // enqueue item ke dalam antrian
    enqueue(itemQueue, newItem);

    // tambahkan item ke dalam list item
    if (!items) {
        items = newItem;
    } else {
        Item* temp = items;
        while (temp->next) {
            temp = temp->next;
        }
        temp->next = newItem;
    }
}

void addPaymentMethod(int id, const string& name) {
    PaymentMethod* newMethod = new PaymentMethod;
    newMethod->id  = id;
    newMethod->name = name;
    newMethod->next = nullptr;

    if (!paymentMethods) {
        paymentMethods = newMethod;
    } else {
        PaymentMethod* temp = paymentMethods;
        while (temp->next) {
            temp = temp->next;
        }
        temp->next = newMethod;
    }

    paymentMethodCount++;
}

void addTransaction(int invoice_id, TransactionItem* items, float total_price, float tax_percentage, float discount_percentage) {
    Transaction* newTransaction = new Transaction;
    newTransaction->invoice_id = invoice_id;
    newTransaction->items = items;
    newTransaction->total_price = total_price;
    newTransaction->tax_percentage = tax_percentage;
    newTransaction->discount_percentage = discount_percentage;
    newTransaction->next = nullptr;

    if (!transactions) {
        transactions = newTransaction;
    } else {
        Transaction* temp = transactions;
        while (temp->next) {
            temp = temp->next;
        }
        temp->next = newTransaction;
    }

    transactionCount++;
}

void deleteTransaction(int invoice_id) {
    if (!transactions) {
        cout << "Tidak ada transaksi yang bisa dihapus.\n";
        return;
    }

    Transaction* temp = transactions;
    Transaction* prev = nullptr;

    while (temp && temp->invoice_id != invoice_id) {
        prev = temp;
        temp = temp->next;
    }

    if (!temp) {
        cout << "Transaksi dengan invoice_id " << invoice_id << " tidak ditemukan.\n";
        return;
    }

    if (!prev) {
        transactions = temp->next;
    } else {
        prev->next = temp->next;
    }

    push(deletedTransactions, temp);
    transactionCount--;

    cout << "Transaksi dengan invoice_id " << invoice_id << " berhasil dihapus dan disimpan ke stack.\n";
}

Transaction* linearSearchTransaction(int invoice_id) {
    Transaction* temp = transactions;
    while (temp != nullptr) {
        if (temp->invoice_id == invoice_id) {
            return temp;
        }
        temp = temp->next;
    }
    return nullptr;
}

void searchTransaction() {
    if (!transactions) {
        cout << "Belum ada transaksi!\n";
        return;
    }

    int invoice_id;
    cout << "Masukkan Invoice: ";
    cin >> invoice_id;

    Transaction* result = linearSearchTransaction(invoice_id);
    if (result) {
        cout << "Detail Transaksi:\n";
        TransactionItem* temp = result->items;
        float subtotal = 0.0;

        while (temp) {
            cout << "Nama Item: " << temp->item->name << "\n";
            cout << "Qty: " << temp->quantity << "\n";
            float itemTotal = temp->item->price * temp->quantity;
            subtotal += itemTotal;
            cout << "Total Item: " << itemTotal << "\n"; // Total harga untuk item ini

            temp = temp->next;
        }

        float taxAmount = subtotal * (result->tax_percentage / 100);
        float discountAmount = subtotal * (result->discount_percentage / 100);
        float total = subtotal + taxAmount - discountAmount;

        cout << "Subtotal: " << subtotal << "\n";
        cout << "Tax (" << result->tax_percentage << "%): " << taxAmount << "\n";
        cout << "Diskon (" << result->discount_percentage << "%): " << discountAmount << "\n";
        cout << "Total Harga: " << total << "\n";
    } else {
        cout << "Transaksi dengan invoice_id " << invoice_id << " tidak ditemukan!\n";
    }
}

TransactionItem* addTransactionItem(Item* item, int quantity) {
    TransactionItem* newItem = new TransactionItem{item, quantity, nullptr};
    return newItem;
}

void transaksi() {
    if (!items) {
        cout << "Item masih kosong!\n";
        return;
    }

    TransactionItem* transactionItems = nullptr;
    TransactionItem* lastItem = nullptr;
    int itemId, qty;
    char choice;

    while (true) {
        cout << "List Item:\n";
        Item* temp = items;
        while (temp) {
            cout << temp->id << ". " << temp->name << " - " << temp->price << "\n";
            temp = temp->next;
        }

        cout << "Pilih item (ID): ";
        cin >> itemId;

        cout << "Masukkan quantity: ";
        cin >> qty;

        temp = items;
        while (temp && temp->id != itemId) {
            temp = temp->next;
        }

        if (temp) {
            TransactionItem* newItem = addTransactionItem(temp, qty);
            if (!transactionItems) {
                transactionItems = newItem;
                lastItem = newItem;
            } else {
                lastItem->next = newItem;
                lastItem = newItem;
            }
        }

        cout << "Tambah item lagi? (y/n): ";
        cin >> choice;
        if (choice == 'n') break;
    }

    if (taxCount == 0) {
        cout << "Tax masih kosong!\n";
        return;
    }

    cout << "List Tax:\n";
    for (int i = 0; i < taxCount; i++) {
        cout << taxes[i].id << ". " << taxes[i].percentage << "%\n";
    }

    int taxId;
    cout << "Pilih tax (ID): ";
    cin >> taxId;
    float taxPercentage = taxes[taxId - 1].percentage;

    cout << "Apakah ada diskon? (y/n): ";
    cin >> choice;

    float discountPercentage = 0.0;
    if (choice == 'y') {
        if (diskonCount == 0) {
            cout << "Diskon masih kosong!\n";
            return;
        }

        cout << "List Diskon:\n";
        for (int i = 0; i < diskonCount; i++) {
            cout << diskons[i].id << ". " << diskons[i].percentage << "%\n";
        }

        int diskonId;
        cout << "Pilih diskon (ID): ";
        cin >> diskonId;
        discountPercentage = diskons[diskonId - 1].percentage;
    }

    if (paymentMethodCount == 0) {
        cout << "Metode pembayaran masih kosong!\n";
        return;
    }

    cout << "List Metode Pembayaran:\n";
    PaymentMethod* tempMethod = paymentMethods;
    while (tempMethod) {
        cout << tempMethod->id << ". " << tempMethod->name << "\n";
        tempMethod = tempMethod->next;
    }

    int paymentMethodId;
    cout << "Pilih metode pembayaran (ID): ";
    cin >> paymentMethodId;

    float subtotal = 0.0;
    TransactionItem* temp = transactionItems;
    while (temp) {
        subtotal += temp->item->price * temp->quantity;
        temp = temp->next;
    }
    float taxAmount = subtotal * taxPercentage / 100;
    float discountAmount = subtotal * discountPercentage / 100;
    float totalPrice = subtotal + taxAmount - discountAmount;

    cout << "Detail Transaksi:\n";
    temp = transactionItems;
    while (temp) {
        cout << "Nama Item: " << temp->item->name << "\n";
        cout << "Qty: " << temp->quantity << "\n";
        temp = temp->next;
    }
    cout << "Subtotal: " << subtotal << "\n";
    cout << "Tax: " << taxAmount << "\n";
    cout << "Total Harga: " << totalPrice << "\n";

    if (discountPercentage > 0.0) {
        cout << "Diskon: " << discountAmount << " ( " << discountPercentage << "% )\n";
    }

    tempMethod = paymentMethods;
    while (tempMethod) {
        if (tempMethod->id == paymentMethodId) {
            cout << "Metode Pembayaran: " << tempMethod->name << "\n";
            break;
        }
        tempMethod = tempMethod->next;
    }

    addTransaction(transactionCount + 1, transactionItems, totalPrice, taxPercentage, discountPercentage);

    cout << "Buat transaksi ulang? (y/n): ";
    cin >> choice;
    if (choice == 'y') {
        transaksi();
    }
}

void riwayatTransaksi() {
    if (!transactions) {
        cout << "Riwayat transaksi kosong!\n";
        return;
    }

    cout << "Riwayat Transaksi:\n";
    Transaction* temp = transactions;
    while (temp) {
        cout << "Invoice ID: " << temp->invoice_id << ", Total: " << temp->total_price << "\n";
        temp = temp->next;
    }

    char choice;
    cout << "Cari transaksi berdasarkan invoice? (y/n): ";
    cin >> choice;
    if (choice == 'y') {
        searchTransaction();
    }

    cout << "Hapus transaksi berdasarkan invoice? (y/n): ";
    cin >> choice;
    if (choice == 'y') {
        int invoice_id;
        cout << "Masukkan Invoice ID yang ingin dihapus: ";
        cin >> invoice_id;
        deleteTransaction(invoice_id);
    }

    cout << "Lihat transaksi yang dihapus? (y/n): ";
    cin >> choice;
    if (choice == 'y') {
        Transaction* deletedTransaction = pop(deletedTransactions);
        if (deletedTransaction) {
            cout << "Transaksi yang dihapus:\n";
            TransactionItem* temp = deletedTransaction->items;
            while (temp) {
                cout << "Nama Item: " << temp->item->name << "\n";
                cout << "Qty: " << temp->quantity << "\n";
                temp = temp->next;
            }
            cout << "Total: " << deletedTransaction->total_price << "\n";
        } else {
            cout << "Tidak ada transaksi yang dihapus dalam stack.\n";
        }
    }
}

void manageItem() {
    int choice;
    while (true) {
        cout << "Manage Item Menu\n";
        cout << "1. List Item\n";
        cout << "2. Add Item\n";
        cout << "3. Edit Item\n";
        cout << "4. Remove Item\n";
        cout << "5. Keluar\n";
        cout << "Pilih menu: ";
        cin >> choice;

        switch (choice) {
            case 1:
                displayQueue(itemQueue);
                break;
            
            case 2: {
                int id;
                string name;
                float price;
                cout << "Masukkan ID: ";
                cin >> id;
                cout << "Masukkan Nama: ";
                cin.ignore();
                getline(cin, name);
                cout << "Masukkan Harga: ";
                cin >> price;
                addItem(id, name, price); // pake enqueue
                cout << "Item berhasil ditambahkan.\n";
                break;
            }

            case 3: {
                if (!items) {
                    cout << "Item masih kosong!\n";
                } else {
                    int id;
                    cout << "Masukkan ID item yang ingin diedit: ";
                    cin >> id;
                    Item* temp = items;
                    while (temp && temp->id != id) {
                        temp = temp->next;
                    }
                    if (temp) {
                        string name;
                        float price;
                        cout << "Masukkan Nama baru: ";
                        cin >> name;
                        cout << "Masukkan Harga baru: ";
                        cin >> price;
                        temp->name = name;
                        temp->price = price;
                        cout << "Item berhasil diedit.\n";
                    } else {
                        cout << "Item tidak ditemukan.\n";
                    }
                }
                break;
            }
            
            case 4: {
                Item* item = dequeue(itemQueue);
                if (item) {
                    cout << "Item dengan ID " << item->id << " telah didequeue.\n";
                    delete item;
                } else {
                    cout << "Queue kosong!\n";
                }
                break;
            }
            case 6:
                return;
            default:
                cout << "Pilihan tidak valid.\n";
                break;
        }
    }
}

void manageDiskon() {
    int choice;
    while (true) {
        cout << "Manage Diskon Menu\n";
        cout << "1. List Diskon\n";
        cout << "2. Add Diskon\n";
        cout << "3. Edit Diskon\n";
        cout << "4. Keluar\n";
        cout << "Pilih menu: ";
        cin >> choice;

        switch (choice) {
            case 1:
                if (diskonCount == 0) {
                    cout << "Diskon masih kosong!\n";
                } else {
                    cout << "List Diskon:\n";
                    for (int i = 0; i < diskonCount; i++) {
                        cout << diskons[i].id << ". " << diskons[i].percentage << "%\n";
                    }
                }
                break;
            case 2: {
                int id;
                float percentage;
                cout << "Masukkan ID: ";
                cin >> id;
                cout << "Masukkan Persentase Diskon: ";
                cin >> percentage;
                diskons[diskonCount++] = {id, percentage};
                cout << "Diskon berhasil ditambahkan.\n";
                break;
            }
            case 3: {
                if (diskonCount == 0) {
                    cout << "Diskon masih kosong!\n";
                } else {
                    int id;
                    cout << "Masukkan ID diskon yang ingin diedit: ";
                    cin >> id;
                    bool found = false;
                    for (int i = 0; i < diskonCount; i++) {
                        if (diskons[i].id == id) {
                            float percentage;
                            cout << "Masukkan Persentase Diskon baru: ";
                            cin >> percentage;
                            diskons[i].percentage = percentage;
                            found = true;
                            cout << "Diskon berhasil diedit.\n";
                            break;
                        }
                    }
                    if (!found) {
                        cout << "Diskon tidak ditemukan.\n";
                    }
                }
                break;
            }
            case 4:
                return;
            default:
                cout << "Pilihan tidak valid.\n";
                break;
        }
    }
}

void manageTax() {
    int choice;
    while (true) {
        cout << "Manage Tax Menu\n";
        cout << "1. List Tax\n";
        cout << "2. Add Tax\n";
        cout << "3. Edit Tax\n";
        cout << "4. Keluar\n";
        cout << "Pilih menu: ";
        cin >> choice;

        switch (choice) {
            case 1:
                if (taxCount == 0) {
                    cout << "Tax masih kosong!\n";
                } else {
                    cout << "List Tax:\n";
                    for (int i = 0; i < taxCount; i++) {
                        cout << taxes[i].id << ". " << taxes[i].percentage << "%\n";
                    }
                }
                break;
            case 2: {
                int id;
                float percentage;
                cout << "Masukkan ID: ";
                cin >> id;
                cout << "Masukkan Persentase Tax: ";
                cin >> percentage;
                taxes[taxCount++] = {id, percentage};
                cout << "Tax berhasil ditambahkan.\n";
                break;
            }
            case 3: {
                if (taxCount == 0) {
                    cout << "Tax masih kosong!\n";
                } else {
                    int id;
                    cout << "Masukkan ID tax yang ingin diedit: ";
                    cin >> id;
                    bool found = false;
                    for (int i = 0; i < taxCount; i++) {
                        if (taxes[i].id == id) {
                            float percentage;
                            cout << "Masukkan Persentase Tax baru: ";
                            cin >> percentage;
                            taxes[i].percentage = percentage;
                            found = true;
                            cout << "Tax berhasil diedit.\n";
                            break;
                        }
                    }
                    if (!found) {
                        cout << "Tax tidak ditemukan.\n";
                    }
                }
                break;
            }
            case 4:
                return;
            default:
                cout << "Pilihan tidak valid.\n";
                break;
        }
    }
}

void managePaymentMethod() {
    int choice;
    while (true) {
        cout << "\nMenu Manage Payment Method\n";
        cout << "1. List Payment Method\n";
        cout << "2. Add Payment Method\n";
        cout << "3. Edit Payment Method\n";
        cout << "4. Keluar\n";
        cout << "Pilih menu: ";
        cin >> choice;

        switch (choice) {
            case 1:
                if (paymentMethodCount == 0) {
                    cout << "Belum ada metode pembayaran tersedia.\n";
                } else {
                    cout << "List Payment Method:\n";
                    PaymentMethod* temp = paymentMethods;
                    while (temp) {
                        cout << temp->id << ". " << temp->name << "\n";
                        temp = temp->next;
                    }
                }
                break;
            case 2: {
                int id;
                string name;
                cout << "Masukkan ID Payment Method: ";
                cin >> id;
                cout << "Masukkan Nama Payment Method: ";
                cin.ignore();
                getline(cin, name);
                addPaymentMethod(id, name);
                cout << "Payment Method berhasil ditambahkan.\n";
                break;
            }
            case 3: {
                if (paymentMethodCount == 0) {
                    cout << "Belum ada metode pembayaran tersedia untuk diedit.\n";
                } else {
                    int id;
                    string newName;
                    cout << "Masukkan ID Payment Method yang akan diedit: ";
                    cin >> id;
                    cout << "Masukkan Nama Payment Method baru: ";
                    cin.ignore();
                    getline(cin, newName);
                    bool found = false;
                    PaymentMethod* temp = paymentMethods;
                    while (temp) {
                        if (temp->id == id) {
                            temp->name = newName;
                            found = true;
                            cout << "Payment Method berhasil diedit.\n";
                            break;
                        }
                        temp = temp->next;
                    }
                    if (!found) {
                        cout << "Payment Method dengan ID " << id << " tidak ditemukan.\n";
                    }
                }
                break;
            }
            case 4:
                return;
            default:
                cout << "Pilihan tidak valid.\n";
                break;
        }
    }
}

void addDefaultData() {
    addItem(1, "Katsu", 10000);
    addItem(2, "Fried Rice", 20000);
    addItem(3, "Chicken", 15000);

    taxes[0] = {1, 10.0};
    taxes[1] = {2, 5.0};
    taxCount = 2;

    diskons[0] = {1, 10.0};
    diskons[1] = {2, 20.0};
    diskonCount = 2;

    addPaymentMethod(1, "Cash");
    addPaymentMethod(2, "BCA");
    addPaymentMethod(3, "BNI");
}

int main() {

    addDefaultData();

    int choice;
    while (true) {
        cout << "\n==== POINT OF SALE ====\n";
        cout << "1. Transaksi\n";
        cout << "2. Riwayat Transaksi\n";
        cout << "3. Manage Item\n";
        cout << "4. Manage Diskon\n";
        cout << "5. Manage Tax\n";
        cout << "6. Manage Metode Pembayaran\n";
        cout << "7. Exit\n";
        cout << "Pilih menu: ";
        cin >> choice;

        switch (choice) {
            case 1:
                transaksi();
                break;
            case 2:
                riwayatTransaksi();
                break;
            case 3:
                manageItem();
                break;
            case 4:
                manageDiskon();
                break;
            case 5:
                manageTax();
                break;
            case 6:
                managePaymentMethod();
                break;
            case 7:
                cout << "Terima kasih telah menggunakan program ini.\n";
                return 0;
            default:
                cout << "Pilihan tidak valid.\n";
                break;
        }
    }

    return 0;
}
Leave a Comment