manager

mail@pastecode.io avatar
unknown
plain_text
a year ago
11 kB
7
Indexable
Never
/*
 * 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.Hashtable;
import java.util.Scanner;

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

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

    /**
     * create ArrayList Order
     */
    public static ArrayList<Order> lo = 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() {
        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 = "";
        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/update more fruit (Y/N)?");
            vd.checkYN();
        } while (true);
    }

    /**
     * update Fruit
     */
    public void updateFruit() {
        boolean isUpdated = false;
        for (Fruit f : fa) {
            if (f.getFruitName().equalsIgnoreCase(fruitName) && f.getOrigin().equalsIgnoreCase(origin)) {
                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 input = "";
            String customer = null;
            do {

                printFruitList();
                System.out.println("Please choose a fruit you want to buy: ");
                inputName();
                System.out.println("Please input the quantity: ");
                inputQuantity();

                do {
                    System.out.println("Do you want to order now? (Y/N)");
                    input = sc.nextLine().toLowerCase();
                } while (!input.equals("y") && !input.equals("n") && !input.equals("yes") && !input.equals("no"));

                if (input.equals("y") || input.equals("yes")) {
                    inputNameCustomer();
                    Order o = new Order(fruitID, fruitName, quantity, price, customer);
                    o.print();

                }
//              else{
//                   return;
//                }
            } while (input.equals("n") || input.equals("no"));

        }
    }

    /**
     * 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;
    }

    /**
     * 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|%-10s|%-10s|%-5s|\n", "No.", "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("--------------------------------------------------------");
        }
    }

    /**
     * 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;
    }

    /**
     * input Quantity
     */
    public void inputQuantity() {
        boolean enoughQuantity;
        do {
            quantity = vd.checkInputQuantity();
            enoughQuantity = checkEnoughQuantity();
            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);
    }

    /**
     * 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;
    }

    /**
     * input Name Customer
     */
    public void inputNameCustomer() {
        System.out.println("Input your name: ");
        String customer = vd.checkInputWord();
        lo.add(new Order(fruitID, fruitName, quantity, price, customer));
        // ht.put(customer, lo);
        System.out.println("Add Sucessful.");

    }

    // FUNCTION 2
    /**
     * viewOrder Function 2 format bill
     */
    public void viewOrder() {
        if (lo.isEmpty()) {
            System.out.println("No orders.");
        } else {
            double total = 0;
            String currentCustomer = null;

            for (Order order : lo) {
                if (!order.getCustomer().equals(currentCustomer)) {
                    if (currentCustomer != null) {
                        System.out.printf("|%48s|%8s|\n", "Total", total);
                        System.out.println("------------------------------------------------------------");
                        total = 0;
                    }
                    System.out.println("Customer: " + order.getCustomer());
                    currentCustomer = order.getCustomer();
                    System.out.println("------------------------------------------------------------");
                    System.out.printf("|%-10s|%20s|%16s|%8s|\n", "Product", "Quantity", "Price", "Amount");
                }
                if (order.getCustomer().equalsIgnoreCase(currentCustomer)) {
                    System.out.printf("|%-10s|%20s|%16s|%8s|\n", order.getFruitName(), order.getQuantity(), order.getPrice(), order.getPrice() * order.getQuantity());
                    total += order.getPrice() * order.getQuantity();
                }
            }
            System.out.printf("|%48s|%8s|\n", "Total", total);
            System.out.println("------------------------------------------------------------");
        }
    }
//    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;
//            }
//        }
//    }

}