Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
5.7 kB
8
Indexable
Never
 public void shopping() {
    if (fa.isEmpty()) { // Check if the fruit store is empty
        System.out.println("The fruit store is empty, please add fruit!");
    } else {
        String customer = null; // Initialize the customer's name
        printFruitList(); // Print the list of available fruits
        String continueShopping = "";
        do {
            int id = inputIDOrder(); // Get the fruit ID from the user
            System.out.println("Input quantity:");
            inputQuantity(id); // Get the quantity from the user
            for (int i = 0; i < fa.size(); i++) { // Loop through the list of fruits
                if (fa.get(i).getFruitID() == id && fa.get(i).getQuantity() >= quantity) { // Check if the fruit ID matches and if there's enough quantity
                    double price = fa.get(i).getPrice(); // Get the price of the fruit
                    Order o = new Order(id, fa.get(i).getFruitName(), quantity, price, customer); // Create an order
                    o.print(); // Print the order details
                    System.out.println("Do you want to buy? (Yes/No)");
                    boolean isValid = true;
                    String response = "";
                    do {
                        response = sc.nextLine(); // Get user input
                        if (!(response.equalsIgnoreCase("y") || response.equalsIgnoreCase("n") || response.equalsIgnoreCase("yes") || response.equalsIgnoreCase("no"))) { // Check if input is valid
                            System.out.println("Please input 'yes' or 'no': ");
                            isValid = false;
                        } else {
                            isValid = true;
                        }
                    } while (!isValid);
                    if (response.equalsIgnoreCase("y") || response.equalsIgnoreCase("yes")) { // If user wants to buy
                        System.out.println("Input your name: ");
                        customer = vd.checkInputWord(); // Get customer name
                        lo.add(new Order(id, fa.get(i).getFruitName(), quantity, price, customer)); // Add order to the list
                        ht.put(customer, lo); // Add order to the hash table
                        System.out.println("Add Successful!!!");
                        // update quantity
                        Fruit fruit = fa.get(i); // Get the fruit object
                        fruit.setQuantity(fruit.getQuantity() - quantity); // Update the quantity of the fruit
                        if (fruit.getQuantity() == 0) { // If the fruit is out of stock
                            fa.remove(fruit); // Remove the fruit from the list
                            System.out.println("The fruit is out of stock and has been removed from the list.");
                            printFruitList(); // Print the updated list of fruits
                        }
                    }
                    break; // Exit loop if the fruit is found and quantity is enough
                } else if (i == fa.size() - 1) { // If the fruit is not found or there's not enough quantity
                    System.out.println("The store does not have enough quantity to meet your needs.");
                    System.out.print("Please enter a smaller quantity: ");
                }
            }

            // Ask if the user wants to continue shopping
            do {
                System.out.println("Do you want to continue shopping? (Yes/No)");
                continueShopping = sc.nextLine();   
                
            } while (!continueShopping.equalsIgnoreCase("yes") 
                    && !continueShopping.equalsIgnoreCase("y") 
                    && !continueShopping.equalsIgnoreCase("no") 
                    && !continueShopping.equalsIgnoreCase("n"));

        } while (continueShopping.equalsIgnoreCase("yes") || continueShopping.equalsIgnoreCase("y"));
    }
}


    /**
     * view order.  viewOrderByCustomer
     */
  public String inputNameCustomer() {
        System.out.println("Input your name: ");
        String customer = vd.checkInputName();
        lo.add(new Order(fruitID, fruitName, quantity, price, customer));
        ht.put(customer, lo);
        System.out.println("Add Successful.");
        return customer;
    }

    /** 
     * view order fun2.
     */
    public void viewOrderByCustomer() {
        if (lo.isEmpty()) {
            System.out.println("No orders.");
        } else {
            double total = 0;
            String currentCustomer = null;
            for (Order order : lo) {
                if (!order.getCustomer().equals(currentCustomer)) { // check input name
                    if (currentCustomer != null) {
                        System.out.printf("|%54s|%7s$|\n", "Total", total);
                        total = 0;
                    }
                    System.out.println("Customer: " + order.getCustomer()); // print
                    currentCustomer = order.getCustomer();
                    System.out.printf("|%-5s|%-10s|%20s|%16s|%8s|\n", "ID.", "Product", "Quantity", "Price", "Amount");
                }
                if (order.getCustomer().equalsIgnoreCase(currentCustomer)) {

                    System.out.printf("|%-5s|%-10s|%20s|%15s$|%7s$|\n", order.getFruitID(), order.getFruitName(), order.getQuantity(), order.getPrice(), order.getPrice() * order.getQuantity());
                    total += order.getPrice() * order.getQuantity();
                }
            }
            System.out.printf("|%54s|%7s$|\n", "Total", total);
        }
    }