customer

mail@pastecode.io avatar
unknown
java
a month ago
9.2 kB
2
Indexable
Never
package assignments.assignment3.systemCLI;

import assignments.assignment3.Restaurant;
import assignments.assignment3.Menu;
import assignments.assignment3.Order;
import assignments.assignment3.User;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Scanner;

public class CustomerSystemCLI extends UserSystemCLI {
    private Scanner input = new Scanner(System.in);
    private ArrayList<Restaurant> restaurants;
    private User user;

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

    public void setUser(User user) {
        this.user = user;
    }

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

    //Menampilkan menu
    @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: ");
    }

    //Menangani pesanan
    protected void handleBuatPesanan() {
        System.out.println("Buat Pesanan");
        System.out.print("Nama Restoran: ");
        String namaRestoran = input.nextLine();
        Restaurant resto = restaurants.stream()
                                      .filter(r -> r.getNama().equalsIgnoreCase(namaRestoran))
                                      .findFirst()
                                      .orElse(null);
        if (resto == null) {
            System.out.println("Restoran tidak terdaftar pada sistem. Silakan masukkan lagi.");
            return;
        }

        String tanggal = getValidDate();
        System.out.print("Jumlah Pesanan: ");
        int jumlahPesanan = Integer.parseInt(input.nextLine());
        ArrayList<Menu> items = new ArrayList<>();
        for (int i = 0; i < jumlahPesanan; i++) {
            System.out.print("Masukkan nama menu: ");
            String namaMenu = input.nextLine();
            Menu menu = resto.getMenu().stream()
                              .filter(m -> m.getNamaMakanan().equalsIgnoreCase(namaMenu))
                              .findFirst()
                              .orElse(null);
            if (menu == null) {
                System.out.println("Menu tidak tersedia.");
                return;
            }
            items.add(menu);
        }

        String orderId = generateOrderID(resto.getNama(), tanggal, user.getNomorTelepon());
        Order order = new Order(orderId, tanggal, calculateOngkir(user.getLokasi()), resto, items);
        user.getOrderHistory().add(order);
        System.out.println("Pesanan dengan ID " + orderId + " diterima!");
    }

    // Mengecek dan mengembalikan tanggal yang valid
    private String getValidDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        sdf.setLenient(false);
        while (true) {
            System.out.print("Tanggal Pemesanan (DD/MM/YYYY): ");
            String tanggal = input.nextLine();
            try {
                sdf.parse(tanggal);
                return tanggal;
            } catch (Exception e) {
                System.out.println("Masukkan tanggal sesuai format (DD/MM/YYYY)!");
            }
        }
    }

    //Mencetak Bill pesanan
    protected void handleCetakBill() {
        System.out.println("Cetak Bill");
        System.out.print("Masukkan Order ID: ");
        String orderId = input.nextLine();
        Order order = user.getOrderHistory().stream()
                           .filter(o -> o.getOrderId().equals(orderId))
                           .findFirst()
                           .orElse(null);
        if (order == null) {
            System.out.println("Order ID tidak dapat ditemukan.");
            return;
        }
        System.out.println(generateBill(order));
    }

    //Menampilkan menu
    protected void handleLihatMenu() {
        System.out.println("Lihat Menu");
        System.out.print("Nama Restoran: ");
        String namaRestoran = input.nextLine();
        Restaurant resto = restaurants.stream()
                                      .filter(r -> r.getNama().equalsIgnoreCase(namaRestoran))
                                      .findFirst()
                                      .orElse(null);
        if (resto == null) {
            System.out.println("Restoran tidak terdaftar pada sistem.");
            return;
        }
        System.out.println("Menu:");
        for (Menu menu : resto.getMenu()) {
            System.out.println(menu.getNamaMakanan() + " Rp" + menu.getHarga());
        }
    }

    protected void handleBayarBill() {
        System.out.println("Bayar Bill");
        System.out.print("Masukkan Order ID: ");
        String orderId = input.nextLine();
        Order order = user.getOrderHistory().stream()
                           .filter(o -> o.getOrderId().equals(orderId) && !o.isOrderFinished())
                           .findFirst()
                           .orElse(null);
        if (order == null) {
            System.out.println("Order ID tidak dapat ditemukan atau sudah dibayar.");
            return;
        }

        System.out.println("Total biaya: Rp" + order.getTotalHarga());
        System.out.println("Pilih metode pembayaran (1. Credit Card, 2. Debit):");
        int pilihan = Integer.parseInt(input.nextLine());
        if (pilihan == 1) {
            System.out.println("Proses pembayaran dengan Credit Card...");
        } else {
            System.out.println("Proses pembayaran dengan Debit Card...");
        }
        System.out.println("Pembayaran berhasil.");
        order.setOrderFinished(true); 
    }

    //Mencetak Saldo
    protected void handleCekSaldo() {
        System.out.println("Cek Saldo");
        System.out.println("Saldo saat ini: Rp" + user.getSaldo());
    }

    //Membuat ID pesanan
    private String generateOrderID(String namaRestoran, String tanggalOrder, String noTelepon) {
        String restoran = namaRestoran.length() >= 4 ? namaRestoran.substring(0, 4).toUpperCase() : namaRestoran.toUpperCase();
        String tanggal = tanggalOrder.replaceAll("/", "");
        int sum = 0;
        for (char c : noTelepon.toCharArray()) {
            if (Character.isDigit(c)) {
                sum += Character.getNumericValue(c);
            }
        }
        String telepon = String.format("%02d", sum % 100);
        return restoran + tanggal + telepon;  
    }

    //Menghitung ongkir
    private int calculateOngkir(String lokasi) {
        switch (lokasi.toUpperCase()) {
            case "P": return 10000;
            case "U": return 20000;
            case "T": return 30000;
            case "S": return 40000;
            case "B": return 50000;
            default: return 15000; 
        }
    }

    //Mencetak Bill
    private String generateBill(Order order) {
        StringBuilder bill = new StringBuilder("Bill:\n");
        bill.append("Order ID: ").append(order.getOrderId()).append("\n");
        bill.append("Tanggal Pemesanan: ").append(order.getTanggal()).append("\n");
        bill.append("Restaurant: ").append(order.getResto().getNama()).append("\n");
        bill.append("Lokasi Pengiriman: ").append(order.getLokasi()).append("\n");
        bill.append("Status Pengiriman: ").append(order.isOrderFinished() ? "Finished" : "Not Finished").append("\n");
        bill.append("Pesanan:\n");
        for (Menu item : order.getItems()) {
            bill.append("- ").append(item.getNamaMakanan()).append(" Rp").append(String.format("%,.2f", item.getHarga())).append("\n");
        }
        int ongkir = calculateOngkir(order.getLokasi());
        bill.append("Biaya Ongkos Kirim: Rp").append(String.format("%,d", ongkir)).append("\n");
        double totalBiaya = order.getItems().stream().mapToDouble(Menu::getHarga).sum() + ongkir;
        bill.append("Total Biaya: Rp").append(String.format("%,.2f", totalBiaya)).append("\n");
        return bill.toString();
    }
}
Leave a Comment