Quick actions

cmd+k|ctrl+k

Navigation

Languages

Tugas Algoritma 14.1

Snippet info

Language

Cpp

Visibility

public

Author

rosdianafitriyani

Created

2025-01-15T03:55:18.885535Z

Updated

2025-01-15T03:55:18.885535Z

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype> // Untuk fungsi toupper
using namespace std;

void garis() {
    cout << "==========================================================\n";
}

// Data menu ayam
struct MenuItem {
    char kode;
    string jenis;
    int harga;
};

MenuItem menu[3] = {
    {'D', "Dada", 12000},
    {'P', "Paha", 8500},
    {'S', "Sayap", 10000}
};

// Function Return Value with Parameter
int cariHarga(char kode) {
    for (int i = 0; i < 3; i++) {
        if (menu[i].kode == kode) {
            return menu[i].harga;
        }
    }
    return 0; // Jika kode salah
}

// Function Return Value Without Parameter
string ambilNamaPembeli() {
    string nama;
    cout << "Masukkan nama pembeli: <input> ";
    getline(cin, nama);
    return nama;
}

// Function NOT Return Value (With Parameter)
void tampilkanTotal(int total) {
    cout << "Total Bayar : " << total << endl;
}

// Function NOT Return Value (Without Parameter)
void tampilkanHeader() {
    cout << "\n";
    garis();
    cout << "Gerobak Fried Chicken\n";
    garis();
    cout << "| No | Kode  | Jenis      | Harga     | Jml | Total      |\n";
    garis();
}

// Function yang Memanggil Function Lain
int hitungTotalItem(char kode, int jumlah) {
    int harga = cariHarga(kode);
    return harga * jumlah;
}

// Function NOT return value (Without Parameter)
void ketentuan() {
    cout << "Harus terdapat function return value dengan parameter." << endl;
    cout << "Harus terdapat function return value TANPA parameter." << endl;
    cout << "Harus terdapat function NOT return value dengan parameter." << endl;
    cout << "Harus terdapat function NOT return value TANPA parameter." << endl;
    cout << "Harus terdapat function yang memanggil function lain." << endl;
    cout << "Harus terdapat variable bertipe array." << endl;
    cout << "Harus terdapat struktur IF." << endl;
    cout << "Harus terdapat struktur SWITCH CASE." << endl;
    cout << "Harus terdapat struktur looping." << endl;
}

int main() {
    // Menampilkan menu terlebih dahulu
    garis();
    cout << "Gerobak Fried Chicken";
    cout << "\n";
    garis();
    cout << "| Kode | Jenis | Harga    |";
    cout << "\n";
    garis();
    cout << "|   D  | Dada  | 12000    |";
    cout << "\n|   P  | Paha  | 8500     |";
    cout << "\n|   S  | Sayap | 10000    |";
    cout << "\n";
    garis();

    // Memanggil fungsi ketentuan untuk menunjukkan ketentuan program
    ketentuan();

    // Ambil nama pembeli dan jumlah beli
    garis();
    string namaPembeli = ambilNamaPembeli();
    
    int jumlahData;
    cout << "\nMasukkan jumlah beli : <input> ";
    cin >> jumlahData;
    cout << "\n";

    // Variabel bertipe array
    char kodeBeli[jumlahData];
    int jumlahBeli[jumlahData];
    int totalHarga[jumlahData];

    int totalBayar = 0;

    // Mengambil data pembelian
    for (int i = 0; i < jumlahData; i++) {
        cout << "\n\nData ke              : " << i + 1 << endl;
        cout << "Masukkan kode [D/P/S]: <input> ";
        cin >> kodeBeli[i];
        kodeBeli[i] = toupper(kodeBeli[i]); // Ubah input ke huruf besar
        cout << "\nJumlah beli          : <input> ";
        cin >> jumlahBeli[i];

        // Menggunakan SWITCH CASE
        switch (kodeBeli[i]) {
            case 'D':
            case 'P':
            case 'S':
                totalHarga[i] = hitungTotalItem(kodeBeli[i], jumlahBeli[i]);
                break;
            default:
                totalHarga[i] = 0; // Kode tidak valid
                break;
        }

        totalBayar += totalHarga[i];
    }

    // Cetak tabel
    tampilkanHeader();
    for (int i = 0; i < jumlahData; i++) {
        // Struktur IF untuk menentukan jenis ayam
        string jenis = (kodeBeli[i] == 'D') ? "Dada" :
                       (kodeBeli[i] == 'P') ? "Paha" :
                       (kodeBeli[i] == 'S') ? "Sayap" : "Salah Kode";

        cout << "| " << setw(2) << i + 1 << " | " << setw(5) << kodeBeli[i]
             << " | " << setw(10) << jenis
             << " | " << setw(9) << cariHarga(kodeBeli[i])
             << " | " << setw(3) << jumlahBeli[i]
             << " | " << setw(10) << totalHarga[i] << " |\n";
    }
    garis();

    // Hitung pajak dan grand total
    int pajak = totalBayar * 0.1; // Pajak 10%
    int grandTotal = totalBayar + pajak;

    tampilkanTotal(totalBayar);
    cout << "Pajak       : " << pajak << endl;
    cout << "Grand Total : " << grandTotal << endl;

    // Footer
    garis();
    cout << "Nama : " << namaPembeli << endl;
    cout << "Email: [email protected]\n";

    return 0;
}
INFO