pos

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

using namespace std;

// Struktur data
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;
};

// Array untuk menyimpan data
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;

// Fungsi untuk menambahkan item ke linked list
void addItem(int id, const string& name, float price) {
    Item* newItem = new Item{id, name, price, nullptr};
    if (!items) {
        items = newItem;
    } else {
        Item* temp = items;
        while (temp->next) {
            temp = temp->next;
        }
        temp->next = newItem;
    }
}

// Fungsi untuk menambahkan metode pembayaran ke linked list
void addPaymentMethod(int id, const string& name) {
    PaymentMethod* newMethod = new PaymentMethod{id, name, nullptr};
    if (!paymentMethods) {
        paymentMethods = newMethod;
    } else {
        PaymentMethod* temp = paymentMethods;
        while (temp->next) {
            temp = temp->next;
        }
        temp->next = newMethod;
    }

    paymentMethodCount++; // Increment the count after adding a new payment method
}

// Fungsi untuk menambahkan transaksi ke linked list
void addTransaction(int invoice_id, TransactionItem* items, float total_price, float tax_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 = 0.0; // Assuming you want to initialize discount_percentage to 0.0
    newTransaction->next = nullptr;
    
    if (!transactions) {
        transactions = newTransaction;
    } else {
        Transaction* temp = transactions;
        while (temp->next) {
            temp = temp->next;
        }
        temp->next = newTransaction;
    }

    transactionCount++;
}

// Fungsi untuk mengurutkan transaksi berdasarkan invoice_id menggunakan Bubble Sort
void sortTransactions() {
    if (!transactions || !transactions->next) return;

    bool swapped;
    Transaction* ptr1;
    Transaction* lptr = nullptr;

    do {
        swapped = false;
        ptr1 = transactions;

        while (ptr1->next != lptr) {
            if (ptr1->invoice_id > ptr1->next->invoice_id) {
                // Tukar data transaksi
                swap(ptr1->invoice_id, ptr1->next->invoice_id);
                swap(ptr1->items, ptr1->next->items);
                swap(ptr1->total_price, ptr1->next->total_price);
                swapped = true;
            }
            ptr1 = ptr1->next;
        }
        lptr = ptr1;
    } while (swapped);
}

// Binary Search untuk mencari transaksi berdasarkan invoice_id
Transaction* binarySearchTransaction(int invoice_id) {
    Transaction* left = transactions;
    Transaction* right = nullptr;

    // Temukan batas atas right
    for (right = transactions; right->next != nullptr; right = right->next);

    while (left <= right) {
        // Temukan indeks tengah
        Transaction* middle = left;
        for (int i = 0; i < (transactionCount - 1) / 2; i++) {
            middle = middle->next;
        }

        // Cek apakah invoice_id yang dicari berada di middle
        if (middle->invoice_id == invoice_id) {
            return middle;
        }

        // Jika invoice_id yang dicari lebih besar, cari di sebelah kanan middle
        if (middle->invoice_id < invoice_id) {
            left = middle->next;
        } else { // Jika invoice_id yang dicari lebih kecil, cari di sebelah kiri middle
            right = middle;
        }
    }

    // Jika tidak ditemukan
    return nullptr;
}

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

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

    Transaction* result = binarySearchTransaction(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";
    }
}

// Fungsi untuk menambahkan item transaksi ke linked list
TransactionItem* addTransactionItem(Item* item, int quantity) {
    TransactionItem* newItem = new TransactionItem{item, quantity, nullptr};
    return newItem;
}

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

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

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

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

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

        // Cari item berdasarkan ID
        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;
            }
        }

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

    // List tax
    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";
    }

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

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

    float discountPercentage = 0.0;
    if (choice == 'y') {
        // List diskon
        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";
        }

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

    // List payment method
    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;
    }

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

    // Menghitung total harga
    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;

    // Detail transaksi
    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";

    // Menampilkan keterangan diskon jika ada
    if (discountPercentage > 0.0) {
        cout << "Diskon: " << discountAmount << " ( " << discountPercentage << "% )\n";
    }

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

    // Simpan transaksi
    addTransaction(transactionCount + 1, transactionItems, totalPrice, taxPercentage);

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

// Mengurutkan transaksi saat pertama kali memanggil fungsi riwayatTransaksi
void riwayatTransaksi() {
    if (!transactions) {
        cout << "Riwayat transaksi kosong!\n";
        return;
    }

    // Mengurutkan transaksi sebelum menampilkan riwayat
    sortTransactions();

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

    // Ask user if they want to search for a transaction
    char choice;
    cout << "Cari transaksi berdasarkan invoice? (y/n): ";
    cin >> choice;
    if (choice == 'y') {
        searchTransaction();
    }
}

// Fungsi untuk mengelola item
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. Keluar\n";
        cout << "Pilih menu: ";
        cin >> choice;

        switch (choice) {
            case 1:
                if (!items) {
                    cout << "Item masih kosong!\n";
                } else {
                    cout << "List Item:\n";
                    Item* temp = items;
                    while (temp) {
                        cout << temp->id << ". " << temp->name << " - " << temp->price << "\n";
                        temp = temp->next;
                    }
                }
                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);
                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:
                return;
            default:
                cout << "Pilihan tidak valid.\n";
                break;
        }
    }
}

// Fungsi untuk mengelola diskon
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;
        }
    }
}

// Fungsi untuk mengelola tax
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;
        }
    }
}

// Fungsi untuk mengelola metode pembayaran
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;
        }
    }
}

// Fungsi untuk menambahkan data default
void addDefaultData() {
    // Data item default
    addItem(1, "Katsu", 10000);
    addItem(2, "Fried Rice", 20000);
    addItem(3, "Chicken", 15000);

    // Data tax default
    taxes[0] = {1, 10.0};
    taxes[1] = {2, 5.0};
    taxCount = 2;

    // Data diskon default
    diskons[0] = {1, 10.0};
    diskons[1] = {2, 20.0};
    diskonCount = 2;

    // Data metode pembayaran default
    addPaymentMethod(1, "Cash");
    addPaymentMethod(2, "BCA");
    addPaymentMethod(3, "BNI");
}

// Fungsi utama
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