Untitled

mail@pastecode.io avatar
unknown
java
2 months ago
6.3 kB
3
Indexable
Never
// CustomerSystemCLI.java
package assignments.assignment3.systemCLI;

import assignments.assignment2.Menu;
import assignments.assignment2.Order;
import assignments.assignment2.Restaurant;
import assignments.assignment2.User;
import assignments.assignment3.payment.CreditCardPayment;
import assignments.assignment3.payment.DebitPayment;

import java.util.ArrayList;
import java.util.Scanner;

public class CustomerSystemCLI extends UserSystemCLI {
    private Scanner input;
    private ArrayList<Restaurant> restoList;
    private User user;

    public CustomerSystemCLI(Scanner input, ArrayList<Restaurant> restoList, User user) {
        this.input = input;
        this.restoList = restoList;
        this.user = user;
    }

    @Override
    public void displayMenu() {
        System.out.println("\n--------------------------------------------");
        System.out.println("Pilih menu:");
        System.out.println("1. Buat Pesanan");
        System.out.println("2. Cetak Bill");
        System.out.println("3. Lihat Menu");
        System.out.println("4. Bayar Bill");
        System.out.println("5. Cek Saldo");
        System.out.println("6. Keluar");
        System.out.println("--------------------------------------------");
        System.out.print("Pilihan menu: ");
    }

    @Override
    public boolean handleMenu(int choice){
        switch(choice){
            case 1: {
                handleBuatPesanan();
                return true;
            }
            case 2: {
                handleCetakBill();
                return true;
            }
            case 3: {
                handleLihatMenu();
                return true;
            }
            case 4: {
                handleBayarBill();
                return true;
            }
            case 5: {
                handleCekSaldo();
                return true;
            }
            case 6: {
                return false;
            }
            default: {
                System.out.println("Perintah tidak diketahui, silakan coba kembali");
                return true;
            }
        }
    }

    public void handleBuatPesanan(){
        System.out.println("Buat Pesanan");
        System.out.print("Nama Restoran: ");
        String namaRestoran = input.nextLine();
        Restaurant resto = null;
        for (Restaurant r : restoList) {
            if (r.getNama().equalsIgnoreCase(namaRestoran)) {
                resto = r;
                break;
            }
        }
        if (resto == null) {
            System.out.println("Restoran tidak terdaftar pada sistem. Silakan masukkan lagi.");
            return;
        }
        System.out.print("Tanggal Pemesanan (DD/MM/YYYY): ");
        String tanggal = input.nextLine();
        System.out.print("Jumlah Pesanan: ");
        int jumlahPesanan = input.nextInt();
        input.nextLine();
        ArrayList<Menu> items = new ArrayList<>();
        for (int i = 0; i < jumlahPesanan; i++) {
            System.out.print("Order: ");
            String namaMenu = input.nextLine();
            Menu menu = null;
            for (Menu m : resto.getMenu()) {
                if (m.getNamaMakanan().equalsIgnoreCase(namaMenu)) {
                    menu = m;
                    break;
                }
            }
            if (menu == null) {
                System.out.println("Menu tidak tersedia.");
                return;
            }
            items.add(menu);
        }
        String orderId = ""; // Anda perlu menghasilkan ID pesanan di sini
        int ongkir = 0; // Anda perlu menghitung ongkir di sini
        Order order = new Order(orderId, tanggal, ongkir, resto, items);
        user.getOrderHistory().add(order);
        System.out.println("Pesanan dengan ID " + orderId + " diterima!");
    }
    
    public void handleCetakBill(){
        System.out.println("Cetak Bill");
        System.out.print("Order ID: ");
        String orderId = input.nextLine();
        Order order = null;
        for (Order o : user.getOrderHistory()) {
            if (o.getOrderId().equals(orderId)) {
                order = o;
                break;
            }
        }
        if (order == null) {
            System.out.println("Order ID tidak dapat ditemukan.");
            return;
        }
        String bill = ""; // Anda perlu menghasilkan bill di sini
        System.out.println(bill);
    }
    
    public void handleLihatMenu(){
        System.out.println("Lihat Menu");
        System.out.print("Nama Restoran: ");
        String namaRestoran = input.nextLine();
        Restaurant resto = null;
        for (Restaurant r : restoList) {
            if (r.getNama().equalsIgnoreCase(namaRestoran)) {
                resto = r;
                break;
            }
        }
        if (resto == null) {
            System.out.println("Restoran tidak terdaftar pada sistem.");
            return;
        }
        System.out.println("Menu:");
        for (Menu m : resto.getMenu()) {
            System.out.println(m.getNamaMakanan() + " " + m.getHarga());
        }
    }
    
    public void handleBayarBill(){
        System.out.println("Bayar Bill");
        System.out.print("Order ID: ");
        String orderId = input.nextLine();
        Order order = null;
        for (Order o : user.getOrderHistory()) {
            if (o.getOrderId().equals(orderId)) {
                order = o;
                break;
            }
        }
        if (order == null) {
            System.out.println("Order ID tidak dapat ditemukan.");
            return;
        }
        if (user.getPayment().getSaldo() < order.getTotalHarga()) {
            System.out.println("Saldo tidak cukup. Silakan isi saldo.");
            return;
        }
        user.getPayment().setSaldo(user.getPayment().getSaldo() - order.getTotalHarga());
        System.out.println("Bill telah dibayar. Sisa saldo: Rp " + user.getPayment().getSaldo());
    }
    
    public void handleCekSaldo(){
        System.out.println("Cek Saldo");
        System.out.println("Sisa saldo sebesar Rp " + user.getPayment().getSaldo());
    }
}
Leave a Comment