manager

 avatar
user_5924904
plain_text
2 years ago
14 kB
3
Indexable
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package l03;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//import java.util.Hashtable;
import java.util.Scanner;

/**
 *
 * @author HP
 */
public class Manager {

    /**
     * create ArrayList Fruit
     */
    public static ArrayList<Fruit> fa = new ArrayList<>();

    /**
     * create ArrayList Order use save order customer
     */
    public static ArrayList<Order> fo = new ArrayList<>();
    //Hashtable<String, ArrayList<Order>> ht = new Hashtable<>();
    Validation vd = new Validation();
    Scanner sc = new Scanner(System.in);
    private int fruitID;
    private String fruitName;
    private double price;
    private int quantity;
    private String origin;

    /**
     * format menu
     */
    public void menu() {
        System.out.println("   FRUIT SHOP SYSTEM   ");
        System.out.println("1. Create Fruit        ");
        System.out.println("2. View orders         ");
        System.out.println("3. Shopping (for buyer)");
        System.out.println("4. Exit                ");
        System.out.print("Please choose: ");
    }

    /**
     * open menu
     */
    public void openMenu() {
        data();
        while (true) {
            menu();
            int choice = vd.checkInputIntLimit();
            switch (choice) {
                case 1:
                    createFruit();
                    break;
                case 2:
                    viewOrder();
                    break;
                case 3:
                    shopping();
                    break;
                case 4:
                    System.out.println("Thank you for using the service!");
                    System.out.println("SEE YOU LATER!");
                    return;
            }
        }
    }

    // FUNCTION 1
    /**
     * use create fruit function 1
     *
     */
    public void createFruit() {
//        String a = "";
        printFruitList();
        do {
            System.out.println("Please input Fruit ID:");
            inputID();
            System.out.println("Please input Fruit name:");
            fruitName = vd.checkInputWord();
            System.out.println("Please input price:");
            price = vd.checkInputPrice();
            System.out.println("Please input quantity:");
            quantity = vd.checkInputQuantity();
            System.out.println("Please input origin:");
            origin = vd.checkInputWord();
            updateFruit();
            System.out.println("Do you want to create more fruit (Y/N)?");
            if (!vd.checkYN()) {
                return;
            }
            System.out.println("Add sucessful!");
        } while (true);

    }

    /**
     * update Fruit
     */
    public void updateFruit() {
        boolean isUpdated = false;
        for (Fruit f : fa) {
            if (f.getFruitName().equalsIgnoreCase(fruitName) && f.getOrigin().equalsIgnoreCase(origin)) {
                System.out.println("Do you want to update more the new data of the product?Please choose 'Yes' or 'No'.");
                if (!vd.checkYN()) {
                    return;
                }
                f.setQuantity(f.getQuantity() + quantity);
                f.setOrigin(origin);
                f.setPrice(price);
                isUpdated = true;
                break;
            }
        }
        if (!isUpdated) {
            fa.add(new Fruit(fruitID, fruitName, price, quantity, origin));
        }
    }

    /**
     * Input ID again
     */
    public void inputID() {
        do {
            fruitID = vd.checkInputInt();
            if (checkIDExits()) {
                System.out.println("The ID already exists in the data, please enter another !");
                System.out.print("Enter again: ");
            }
        } while (checkIDExits());
    }

    /**
     * check id already exists
     *
     * @return
     */
    public boolean checkIDExits() {
        for (int i = 0; i < fa.size(); i++) {
            if (fa.get(i).getFruitID() == fruitID) {
                return true;
            }
        }
        return false;
    }

    //FUNCTION 3
    /**
     * Shopping Function3
     *
     */
    public void shopping() {
        
            if (fa.isEmpty()) {
                System.out.println("The store has no products!");
            } else {
                String customer = null;
                printFruitList();
                System.out.println("Please choose a fruit you want to buy: ");
                inputName();
                System.out.println("Please choose the origin of the fruit");
                inputOrigin();
                System.out.println("Please input the quantity: ");
                inputQuantity();
            System.out.println("Do you want to order now? (Y/N)");
            if (!vd.checkYN()) {
                return;
            } else {
                Fruit fruit = fruitByItem();
                fruit.setQuantity(fruit.getQuantity() - quantity);
                double amount = fruit.getPrice() * quantity;
                Order o = new Order(fruitID, fruitName, quantity, origin, fruit.getPrice(), amount, customer);
                o.print();
                //inputNameCustomer();
                System.out.println("Input your name: ");
                customer = vd.checkInputWord();
                fo.add(new Order(fruitID, fruitName, quantity, origin, fruit.getPrice(), amount, customer));
                System.out.println("Add Sucessful.");
                if (fruit.getQuantity() == 0) {
                    fa.remove(fruit);
                    System.out.println("The fruit has been sold out and removed from the list.");
                }
//                //System.out.println(fruitID + fruitName + quantity + origin + price + amount + customer);
            }
        }
    
    }

    /**
     * check fruit name exists in the store
     *
     * @return
     */
    public String inputName() {
        do {
            fruitName = vd.checkInputWord();
            if (!checkNameExits()) {
                System.out.println("The fruit you want to buy is not in the store yet.");
                System.out.print("Please choose another fruit: ");
            }
        } while (!checkNameExits());

        return fruitName;
    }

    /**
     * check the right fruit origin and enough quantity when order
     *
     * @return
     */
    public Fruit fruitByItem() {
        boolean a = true;
        while (a) {
            for (Fruit fruit : fa) {
                if (fruit.getFruitName().equalsIgnoreCase(fruitName) && fruit.getOrigin().equalsIgnoreCase(origin) && fruit.getQuantity() >= quantity) {
                    return fruit;
                }
            }
        }
        return null;
    }

    /**
     * check Name Exits
     *
     * @return
     */
    public boolean checkNameExits() {
        for (int i = 0; i < fa.size(); i++) {
            if (fa.get(i).getFruitName().equals(fruitName)) {
                return true;
            }
        }
        return false;
    }

    /**
     * check fruit origin exists in the store
     *
     * @return
     */
    public String inputOrigin() {
        do {
            origin = vd.checkInputWord();
            if (!checkOriginExits()) {
                System.out.println("The fruit you want to buy from " + origin + " the store does not have!");
                System.out.print("Can you choose another origin: ");
            }
        } while (!checkOriginExits());

        return fruitName;
    }

    /**
     * check Name Exits
     *
     * @return
     */
    public boolean checkOriginExits() {
        for (int i = 0; i < fa.size(); i++) {
            if (fa.get(i).getOrigin().equals(origin) && fa.get(i).getFruitName().equals(fruitName)) {
                return true;
            }
        }
        return false;
    }

    /**
     * input Name Customer
     */
//    public void inputNameCustomer() {
//        System.out.println("Input your name: ");
//        String customer = vd.checkInputWord();
//        System.out.println("Add Sucessful.");
//        fo.add(new Order(fruitID, fruitName, quantity, origin, price, amount, customer));
//        System.out.println(fruitID + fruitName +  quantity + origin+  price+ amount +customer);
//    }
    /**
     * Show table information
     */
    public void printFruitList() {
        if (fa.isEmpty()) {
            System.out.println("There are currently no customer orders");
        } else {
            System.out.println("List of Fruit:");
            System.out.println("-------------------------------------------------------------");
            System.out.printf("|%-5s|%-20s|%-15s|%-10s|%-5s|\n", "ID", "Fruit Name", "Origin", "Quantity", "Price");
            System.out.println("-------------------------------------------------------------");
            for (int i = 0; i < fa.size(); i++) {
                Fruit e = fa.get(i);
                e.output();
            }
            System.out.println("-------------------------------------------------------------");
        }
    }

    /**
     * input Quantity
     *
     * @return
     */
    public int inputQuantity() {
        boolean enoughQuantity = false;
        do {
            quantity = vd.checkInputQuantity();
            enoughQuantity = false;
            for (Fruit f : fa) {
                if (f.getFruitName().equals(fruitName) && f.getQuantity() >= quantity) {
                    enoughQuantity = true;
                    break;
                }
            }
            if (!enoughQuantity) {
                System.out.println("The store does not have enough quantity to meet your needs.");
                System.out.print("Please enter a smaller quantity: ");
            }
        } while (!enoughQuantity);
        return quantity;
    }

    /**
     * check quantity in store
     *
     * @return
     */
    public boolean checkEnoughQuantity() {
        for (int i = 0; i < fa.size(); i++) {
            if (fa.get(i).getFruitName().equals(fruitName) && fa.get(i).getQuantity() >= quantity) {
                return true;
            }
        }
        return false;
    }

    // FUNCTION 2
    /**
     * viewOrder Function 2 format bill
     */
    public void viewOrder() {
        if (fo.isEmpty()) {
            System.out.println("There are currently no customers to order.");
        } else {
            // create a Map to store orders by customer name
            Map<String, List<Order>> ordersByCustomer = new HashMap<>();
            for (Order order : fo) {
                String customerName = order.getCustomer();
                // use getdefault to limit null data check
                List<Order> ordersForCustomer = ordersByCustomer.getOrDefault(customerName, new ArrayList<>());
                ordersForCustomer.add(order);
                ordersByCustomer.put(customerName, ordersForCustomer);
            }

            // print orders by customer name
            for (String customerName : ordersByCustomer.keySet()) {
                double total = 0;
                System.out.println("Customer: " + customerName);
                System.out.println("-----------------------------------------------------------------------");
                System.out.printf("|%-10s|%-15s|%20s|%10s|%10s|\n", "Product", "Origin", "Quantity", "Price", "Amount");
                System.out.println("-----------------------------------------------------------------------");
                for (Order order : ordersByCustomer.get(customerName)) {
                    System.out.printf("|%-10s|%-15s|%20s|%9s$|%9s$|\n", order.getFruitName(), order.getOrigin(), order.getQuantity(), order.getPrice(), order.getPrice() * order.getQuantity());
                    total += order.getPrice() * order.getQuantity();
                }
                System.out.println("-----------------------------------------------------------------------");
                System.out.printf("|%58s|%9s$|\n", "Total", total);
                System.out.println("-----------------------------------------------------------------------");
            }
        }

    }

    /**
     * create data available
     */
    public void data() {
        fa.add(new Fruit(1, "orange", 2, 2, "campuchia"));
        fa.add(new Fruit(2, "lemon", 3, 3, "vietnam"));
        fa.add(new Fruit(3, "apple", 4, 4, "malaysia"));
    }
//    public void viewOrder() {
//        if (lo.isEmpty()) {
//            System.out.println("There are currently no customers");
//        } else {
//            double total = 0;
//            for (String customer : ht.keySet()) {
//                System.out.println("Customer: " + customer);
//                lo = ht.get(customer);
//                System.out.println("------------------------------------------------------------");
//                System.out.printf("|%-10s|%20s|%16s|%8s|\n", "Product", "Quantity", "Price", "Amount");
//                for (Order o : lo) {
//                    if (o.getCustomer().equalsIgnoreCase(customer)) {
//                        System.out.printf("|%-10s|%20s|%16s|%8s|\n", o.getFruitName(), o.getQuantity(), o.getPrice(), o.getPrice() * o.getQuantity());
//                        total += o.getPrice() * o.getQuantity();
//                    }
//
//                }
//                System.out.printf("|%48s|%8s|\n", "Total", total);
//                System.out.println("------------------------------------------------------------");
//                total = 0;
//            }
//        }
//    }

}
Editor is loading...