Manager
unknown
plain_text
2 years ago
8.4 kB
5
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.Hashtable; import java.util.Scanner; /** * * @author HP */ public class Manager { /** * create ArrayList */ public static ArrayList<Fruit> fa = new ArrayList<>(); 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: return; } } } /** * 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(); fa.add(new Fruit(fruitID, fruitName, price, quantity, origin)); System.out.println("Do you want to order now (Y/N)?"); boolean isValids = true; //Input user's yes or no to playagain or not do { a = sc.nextLine(); //check their input if input is valid or not if (!(a.equalsIgnoreCase("y") || a.equalsIgnoreCase("n") || a.equalsIgnoreCase("yes") || a.equalsIgnoreCase("no"))) { System.out.println("Please input 'yes' or 'no': "); isValids = false; } else { isValids = true; } } while (isValids == false); } while (a.equalsIgnoreCase("y") || a.equalsIgnoreCase("yes")); } /** * 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; } /** * 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.printf("|%5s|%-20s|%-16s|%-5s|\n", "No.", "Fruit Name", "Origin", "Price"); for (int i = 0; i < fa.size(); i++) { Fruit e = fa.get(i); e.output(); } } } public void shopping() { if (lo.isEmpty()) { System.out.println("khogn"); } else { String a = ""; String customer = null; printFruitList(); do { System.out.println("Please choose fruit you want to buy: "); inputName(); System.out.println("Please input quantity: "); inputQuantity(); System.out.println("Please select 'Yes' if you want to buy more or 'No' stop and invoice"); boolean isValids = true; //Input user's yes or no to playagain or not do { a = sc.nextLine(); //check their input if input is valid or not if (!(a.equalsIgnoreCase("y") || a.equalsIgnoreCase("n") || a.equalsIgnoreCase("yes") || a.equalsIgnoreCase("no"))) { System.out.println("Please input 'yes' or 'no': "); isValids = false; } else { isValids = true; } } while (isValids == false); } while (a.equalsIgnoreCase("y") || a.equalsIgnoreCase("yes")); Order o = new Order(fruitID, fruitName, quantity, price, customer); o.print(); inputNameCustomer(); } } 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; } public boolean checkNameExits() { for (int i = 0; i < fa.size(); i++) { if (fa.get(i).getFruitName().equals(fruitName)) { return true; } } return false; } 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); } 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; } 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."); } public void viewOrder() { if (lo.isEmpty()) { System.out.println("khogn"); } else { double total = 0; for (String customer : ht.keySet()) { System.out.println("Customer: " + customer); lo = ht.get(customer); 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); total = 0; } } } }
Editor is loading...