aaaaaaaaaaaa
unknown
plain_text
3 years ago
16 kB
7
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 {
Hashtable<Integer, ArrayList<Fruit>> orderTable = new Hashtable<>(); //New object of class Hashtable
Hashtable<Integer, String> buyerNameTable = new Hashtable<>(); //New object of class Hashtable
/**
* create ArrayList Fruit
*/
public static ArrayList<Fruit> fa = new ArrayList<>();
/**
* create ArrayList Order use save order customer
*/
public static ArrayList<Order> fo = new ArrayList<>();
public static ArrayList<Order> co = 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;
private double amount;
private String customer;
private int dem = 0;
private int count = 0;
/**
* 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() {
double amount = 0;
Fruit fruit = null;
if (fa.isEmpty()) {
System.out.println("The store has no products!");
} else {
do {
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();
fruit = fruitByItem();
fruit.setQuantity(fruit.getQuantity() - quantity);
amount = fruit.getPrice() * quantity;
fo.add(new Order(fruitID, fruitName, price, quantity, origin, amount));
co.add(new Order(fruitID, fruitName, quantity, origin, fruit.getPrice(), amount, customer));
if (fruit.getQuantity() == 0) {
fa.remove(fruit);
System.out.println("The fruit has been sold out and removed from the list.");
}
System.out.println("buy continute");
if (!vd.checkYN()) {
break;
}
} while (true);
System.out.println("Do you want to order now? (Y/N)");
// 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();
printBill();
System.out.println("Input your name: ");
customer = vd.checkInputWord();
ht.put(customer, fo);
System.out.println("Add Sucessful.");
co.clear();
// //System.out.println(fruitID + fruitName + quantity + origin + price + amount + customer);
}
}
public void printBill() {
double total = 0;
System.out.println("-------------------------------------------------------------");
System.out.printf("|%-10s|%20s|%16s|%10s|\n", "Product", "Quantity", "Price", "Amount");
System.out.println("-------------------------------------------------------------");
for (Order order : co) {
Order o = new Order(fruitID, fruitName, quantity, origin, price, amount, customer);
order.print();
total += order.getAmount();
}
System.out.printf("|%48s|%8s|\n", "Total", total);
System.out.println("-------------------------------------------------------------");
}
/**
* 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 (ht.keySet().isEmpty()) {
System.out.println("No orders.");
} else {
double total = 0;
String currentCustomer = null;
for (String order : ht.keySet()) {
if (customer.equals(currentCustomer)) {
if (currentCustomer != null) {
System.out.printf("|%48s|%8s|\n", "Total", total);
System.out.println("------------------------------------------------------------");
total = 0;
}
System.out.println("Customer: " + customer);
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("------------------------------------------------------------");
}
}
// if (fo.isEmpty()) {
// System.out.println("No orders.");
// } else {
// double total = 0;
// String currentCustomer = null;
// for (Order order : fo) {
// 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);
// }
// for (int dem = 1; dem <= count; dem++) {
//
// System.out.println("Customer: " + customer); // print
// System.out.printf("|%-5s|%-10s|%20s|%16s|%8s|\n", "ID.", "Product", "Quantity", "Price", "Amount");
// 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();
// }
/**
* 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 (fo.isEmpty()) {
// System.out.println("There are currently no customers");
// } else {
// for (Order f : fo) {
// fo= new ht.keySet();
// }
// }
// }
}
Editor is loading...