From 1487934ae04ca8d660635637970e1fd3b762bf80 Mon Sep 17 00:00:00 2001 From: HanHyeJi Date: Mon, 15 May 2023 00:01:59 +0900 Subject: [PATCH] Java ToyProject upload by HanHyeJi --- me.smartstore/SmartStoreApp.java | 86 +++++++ me.smartstore/arrays/Collections.java | 16 ++ me.smartstore/arrays/DArray.java | 149 ++++++++++++ me.smartstore/customer/Customer.java | 87 +++++++ me.smartstore/customer/Customers.java | 26 +++ .../exception/ElementNotFoundException.java | 22 ++ .../exception/EmptyArrayException.java | 22 ++ .../exception/InputEndException.java | 13 ++ .../exception/InputRangeException.java | 23 ++ .../exception/NullArgumentException.java | 22 ++ me.smartstore/group/Group.java | 51 +++++ me.smartstore/group/GroupType.java | 22 ++ me.smartstore/group/Groups.java | 30 +++ me.smartstore/group/Parameter.java | 54 +++++ me.smartstore/main.java | 6 + me.smartstore/menu/CustomerMenu.java | 216 ++++++++++++++++++ me.smartstore/menu/GroupMenu.java | 173 ++++++++++++++ me.smartstore/menu/MainMenu.java | 38 +++ me.smartstore/menu/Menu.java | 77 +++++++ me.smartstore/menu/SummaryMenu.java | 174 ++++++++++++++ me.smartstore/util/Message.java | 13 ++ 21 files changed, 1320 insertions(+) create mode 100644 me.smartstore/SmartStoreApp.java create mode 100644 me.smartstore/arrays/Collections.java create mode 100644 me.smartstore/arrays/DArray.java create mode 100644 me.smartstore/customer/Customer.java create mode 100644 me.smartstore/customer/Customers.java create mode 100644 me.smartstore/exception/ElementNotFoundException.java create mode 100644 me.smartstore/exception/EmptyArrayException.java create mode 100644 me.smartstore/exception/InputEndException.java create mode 100644 me.smartstore/exception/InputRangeException.java create mode 100644 me.smartstore/exception/NullArgumentException.java create mode 100644 me.smartstore/group/Group.java create mode 100644 me.smartstore/group/GroupType.java create mode 100644 me.smartstore/group/Groups.java create mode 100644 me.smartstore/group/Parameter.java create mode 100644 me.smartstore/main.java create mode 100644 me.smartstore/menu/CustomerMenu.java create mode 100644 me.smartstore/menu/GroupMenu.java create mode 100644 me.smartstore/menu/MainMenu.java create mode 100644 me.smartstore/menu/Menu.java create mode 100644 me.smartstore/menu/SummaryMenu.java create mode 100644 me.smartstore/util/Message.java diff --git a/me.smartstore/SmartStoreApp.java b/me.smartstore/SmartStoreApp.java new file mode 100644 index 00000000..1195304d --- /dev/null +++ b/me.smartstore/SmartStoreApp.java @@ -0,0 +1,86 @@ +import customer.Customer; +import customer.Customers; +import group.Group; +import group.GroupType; +import group.Groups; +import group.Parameter; +import menu.MainMenu; + +public class SmartStoreApp { + + private final Groups allGroups = Groups.getInstance(); + private final Customers allCustomers = Customers.getInstance(); + private static SmartStoreApp smartStoreApp; + + private final MainMenu mainMenu = MainMenu.getInstance(); + + + public static SmartStoreApp getInstance() { + if (smartStoreApp == null) { + smartStoreApp = new SmartStoreApp(); + } + return smartStoreApp; + } + + private SmartStoreApp() { + } + + public void details() { + System.out.println("\n\n==========================================="); + System.out.println(" Title : SmartStore Customer Classification"); + System.out.println(" Release Date : 23.04.27"); + System.out.println(" Copyright 2023 Eunbin All rights reserved."); + System.out.println("===========================================\n"); + + } + + public SmartStoreApp test() { + allGroups.add(new Group(new Parameter(0,0), GroupType.NONE)); + allGroups.add(new Group(new Parameter(10, 100000), GroupType.GENERAL)); + allGroups.add(new Group(new Parameter(20, 200000), GroupType.VIP)); + allGroups.add(new Group(new Parameter(30, 300000), GroupType.VVIP)); + + for (int i = 0; i < 26; i++) { + Customer customer = new Customer( + Character.toString( + (char) ('a' + i)), + (char) ('a' + i) + "123", + ((int) (Math.random() * 5) + 1) * 10, + ((int) (Math.random() * 5) + 1) * 100000); + + Group highestGroup = null; + for (int j = 0; j < allGroups.size(); j++) { + Group group = allGroups.get(j); + Parameter parameter = group.getParameter(); + Integer minTime = parameter.getMinTime(); + Integer minPay = parameter.getMinPay(); + + if (minTime <= customer.getTotalTime() && minPay <= customer.getTotalPay()) { + if (highestGroup == null || (parameter.getMinPay() > highestGroup.getParameter().getMinPay() && parameter.getMinTime() > highestGroup.getParameter().getMinTime() )) { + highestGroup = group; + } + } + } + if (highestGroup != null) { + customer.setGroup(highestGroup); + } + + allCustomers.add(customer); + } + + + System.out.println("allCustomers = " + allCustomers); + System.out.println("allGroups = " + allGroups); + + allCustomers.refresh(allGroups); + + return this; + + } + + public void run() { + details(); +// allGroups.add(new Group(new Parameter(0,0), GroupType.NONE)); + mainMenu.manage(); + } +} diff --git a/me.smartstore/arrays/Collections.java b/me.smartstore/arrays/Collections.java new file mode 100644 index 00000000..e8d0c436 --- /dev/null +++ b/me.smartstore/arrays/Collections.java @@ -0,0 +1,16 @@ +package arrays; + +public interface Collections { + // 데이터를 가지고 있는 객체가 아님 + // 구현 해야하는 메소드의 정보만 가지고 있음 (인터페이스) + + int size(); + T get(int index); + void set(int index, T object); + int indexOf(T object); + void add(T object); + void add(int index, T object); + T pop(); + T pop(int index); + T pop(T object); +} diff --git a/me.smartstore/arrays/DArray.java b/me.smartstore/arrays/DArray.java new file mode 100644 index 00000000..ef428ce2 --- /dev/null +++ b/me.smartstore/arrays/DArray.java @@ -0,0 +1,149 @@ +package arrays; + +import exception.ElementNotFoundException; +import exception.EmptyArrayException; +import exception.NullArgumentException; + +import java.util.Arrays; +import java.util.Comparator; + +public class DArray implements Collections { // Dynamic Array + + protected T[] arrays; + protected static final int DEFAULT = 10; + protected int size; + protected int capacity; + + public DArray() throws ClassCastException { + arrays = (T[]) new Object[DEFAULT]; + capacity = DEFAULT; + } + + public DArray(int initial) throws ClassCastException { + arrays = (T[]) new Object[initial]; + capacity = initial; + } + + public DArray(T[] arrays) { + this.arrays = arrays; + capacity = arrays.length; + size = arrays.length; + } + + ///////////////////////////////////////// + // add, set, get, pop, indexOf, size, capacity (for dynamic-sized array) + + @Override + public int size() { + return size; + } + + // 배열에 얼마나 capacity 남아있는지 외부에 알려줄 필요가 없기 때문에 으로 정의 + protected int capacity() { + return capacity; + } + + @Override + public T get(int index) throws IndexOutOfBoundsException { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); + return arrays[index]; + } + + @Override + public void set(int index, T object) throws IndexOutOfBoundsException, NullArgumentException { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); + if (object == null) throw new NullArgumentException(); + + arrays[index] = object; + } + + @Override + public int indexOf(T object) throws NullArgumentException, ElementNotFoundException { + if (object == null) throw new NullArgumentException(); // not found (instead of throwing exception) + + for (int i = 0; i < size; i++) { + if (arrays[i] == null) continue; + if (arrays[i].equals(object)) return i; + } + throw new ElementNotFoundException(); // not found + } + + // 배열의 cap이 부족한 경우 + @Override + public void add(T object) throws NullArgumentException { + if (object == null) throw new NullArgumentException(); // if argument is null, do not add null value in array + + if (size < capacity) { + arrays[size] = object; + size++; + } else { + grow(); + add(object); + } + } + + @Override + public void add(int index, T object) throws IndexOutOfBoundsException, NullArgumentException { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); + if (object == null) throw new NullArgumentException(); + + if (size < capacity) { + for (int i = size-1; i >= index ; i--) { + arrays[i+1] = arrays[i]; + } + arrays[index] = object; + size++; + } else { + grow(); + add(index, object); + } + } + + @Override + public T pop() { +// if (size == 0) return null; +// +// T popElement = arrays[size-1]; +// arrays[size-1] = null; +// size--; +// return popElement; + return pop(size-1); + } + + @Override + public T pop(int index) throws IndexOutOfBoundsException { + if (size == 0) throw new EmptyArrayException(); + if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); + + T popElement = arrays[index]; + arrays[index] = null; // 삭제됨을 명시적으로 표현 + + for (int i = index+1; i < size; i++) { + arrays[i-1] = arrays[i]; + } + arrays[size-1] = null; + size--; + return popElement; + } + + @Override + public T pop(T object) { + return pop(indexOf(object)); + } + + protected void grow() { + capacity *= 2; // doubling + arrays = java.util.Arrays.copyOf(arrays, capacity); + + // size는 그대로 + } + + @Override + public String toString() { + String toStr = ""; + for (int i = 0; i < size; i++) { + toStr += (arrays[i] + "\n"); + } + return toStr; + } +} diff --git a/me.smartstore/customer/Customer.java b/me.smartstore/customer/Customer.java new file mode 100644 index 00000000..81573e51 --- /dev/null +++ b/me.smartstore/customer/Customer.java @@ -0,0 +1,87 @@ +package customer; + +import group.Group; + +import java.util.Objects; + +public class Customer { + private String customerName; + private String customerID; + private Integer customerTotalTime; + private Integer customerTotalPay; + private Group group; + + public Customer() {} + + public Customer(String customerName, String customerID, Integer totalTime, Integer totalPay) { + this.customerName = customerName; + this.customerID = customerID; + this.customerTotalTime = totalTime; + this.customerTotalPay = totalPay; + } + + public String getCustomerName() { + return customerName; + } + + public void setCustomerName(String customerName) { + this.customerName = customerName; + } + + public String getCustomerID() { + return customerID; + } + + public void setCustomerID(String customerID) { + this.customerID = customerID; + } + + public Integer getTotalTime() { + return customerTotalTime; + } + + public void setTotalTime(Integer totalTime) { + this.customerTotalTime = totalTime; + } + + public Integer getTotalPay() { + return customerTotalPay; + } + + public void setTotalPay(Integer totalPay) { + this.customerTotalPay = totalPay; + } + + public Group getGroup() { + return group; + } + + public void setGroup(Group group) { + this.group = group; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Customer customer = (Customer) o; + return customerID.equals(customer.customerID); + } + + @Override + public int hashCode() { + return Objects.hash(customerID); + } + + @Override + public String toString() { + return "Customer{" + + "UserID='" + customerID + + ", Name='" + customerName + + ", SpentTime=" + customerTotalTime + + ", totalPay=" + customerTotalPay + + ", group= " + group + + '}'; + + } +} diff --git a/me.smartstore/customer/Customers.java b/me.smartstore/customer/Customers.java new file mode 100644 index 00000000..774f7710 --- /dev/null +++ b/me.smartstore/customer/Customers.java @@ -0,0 +1,26 @@ +package customer; + +import arrays.DArray; +import group.Group; +import group.Groups; + +import java.util.Arrays; +import java.util.Comparator; + +public class Customers extends DArray { + private static Customers allCustomers; + + public static Customers getInstance() { + if (allCustomers == null) { + allCustomers = new Customers(); + } + return allCustomers; + } + + public void refresh(Groups groups) { + // 1. 분류 기준이 바뀔 때 + // 2. 새로운 고객이 들어올 때 + // refresh 함수 호출을 통해 그룹 다시 설정 + } + +} diff --git a/me.smartstore/exception/ElementNotFoundException.java b/me.smartstore/exception/ElementNotFoundException.java new file mode 100644 index 00000000..f163a3e8 --- /dev/null +++ b/me.smartstore/exception/ElementNotFoundException.java @@ -0,0 +1,22 @@ +package exception; + +public class ElementNotFoundException extends RuntimeException { + public ElementNotFoundException() { + } + + public ElementNotFoundException(String message) { + super(message); + } + + public ElementNotFoundException(String message, Throwable cause) { + super(message, cause); + } + + public ElementNotFoundException(Throwable cause) { + super(cause); + } + + public ElementNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/me.smartstore/exception/EmptyArrayException.java b/me.smartstore/exception/EmptyArrayException.java new file mode 100644 index 00000000..4ece8b3b --- /dev/null +++ b/me.smartstore/exception/EmptyArrayException.java @@ -0,0 +1,22 @@ +package exception; + +public class EmptyArrayException extends RuntimeException { + public EmptyArrayException() { + } + + public EmptyArrayException(String message) { + super(message); + } + + public EmptyArrayException(String message, Throwable cause) { + super(message, cause); + } + + public EmptyArrayException(Throwable cause) { + super(cause); + } + + public EmptyArrayException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/me.smartstore/exception/InputEndException.java b/me.smartstore/exception/InputEndException.java new file mode 100644 index 00000000..defe48fe --- /dev/null +++ b/me.smartstore/exception/InputEndException.java @@ -0,0 +1,13 @@ +package exception; + +import util.Message; + +public class InputEndException extends RuntimeException { + public InputEndException() { + super(Message.ERR_MSG_INPUT_END); + } + + public InputEndException(String message) { + super(message); + } +} diff --git a/me.smartstore/exception/InputRangeException.java b/me.smartstore/exception/InputRangeException.java new file mode 100644 index 00000000..875f8b41 --- /dev/null +++ b/me.smartstore/exception/InputRangeException.java @@ -0,0 +1,23 @@ +package exception; + +public class InputRangeException extends RuntimeException { + + public InputRangeException() { + } + + public InputRangeException(String message) { + super(message); + } + + public InputRangeException(String message, Throwable cause) { + super(message, cause); + } + + public InputRangeException(Throwable cause) { + super(cause); + } + + public InputRangeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/me.smartstore/exception/NullArgumentException.java b/me.smartstore/exception/NullArgumentException.java new file mode 100644 index 00000000..14349091 --- /dev/null +++ b/me.smartstore/exception/NullArgumentException.java @@ -0,0 +1,22 @@ +package exception; + +public class NullArgumentException extends RuntimeException { + public NullArgumentException() { + } + + public NullArgumentException(String message) { + super(message); + } + + public NullArgumentException(String message, Throwable cause) { + super(message, cause); + } + + public NullArgumentException(Throwable cause) { + super(cause); + } + + public NullArgumentException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/me.smartstore/group/Group.java b/me.smartstore/group/Group.java new file mode 100644 index 00000000..9f7546e2 --- /dev/null +++ b/me.smartstore/group/Group.java @@ -0,0 +1,51 @@ +package group; + +import java.util.Objects; + +public class Group { + private Parameter parameter; + private GroupType groupType; + + public Group() { + } + + public Group(Parameter parameter, GroupType groupType) { + this.parameter = parameter; + this.groupType = groupType; + } + + public Parameter getParameter() { + return parameter; + } + + public void setParameter(Parameter parameter) { + this.parameter = parameter; + } + + public GroupType getGroupType() { + return groupType; + } + + public void setGroupType(GroupType groupType) { + this.groupType = groupType; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Group group = (Group) o; + return Objects.equals(parameter, group.parameter) && groupType == group.groupType; + } + + @Override + public int hashCode() { + return Objects.hash(parameter, groupType); + } + + @Override + public String toString() { + return "GroupType: " + groupType + "\n" + + "parameter: " + parameter; + } +} diff --git a/me.smartstore/group/GroupType.java b/me.smartstore/group/GroupType.java new file mode 100644 index 00000000..de4729ea --- /dev/null +++ b/me.smartstore/group/GroupType.java @@ -0,0 +1,22 @@ +package group; + +public enum GroupType { + + N("해당없음"), G("일반고객"), V("우수고객"), VV("최우수고객"), + NONE("해당없음"), GENERAL("일반고객"), VIP("우수고객"), VVIP("최우수고객"); + + + String groupType = ""; + + GroupType(String groupType) { + this.groupType = groupType; + } + + public GroupType replaceFullName() { + if (this == N) return NONE; + else if (this == G) return GENERAL; + else if (this == V) return VIP; + else if (this == VV) return VVIP; + return this; + } +} diff --git a/me.smartstore/group/Groups.java b/me.smartstore/group/Groups.java new file mode 100644 index 00000000..19a4826a --- /dev/null +++ b/me.smartstore/group/Groups.java @@ -0,0 +1,30 @@ +package group; + +import arrays.DArray; + +import java.util.Arrays; + +public class Groups extends DArray { + + private static Groups allGroups; + + public static Groups getInstance() { + if (allGroups == null) { + allGroups = new Groups(); + } + return allGroups; + } + + private Groups() {} + + public Group find(GroupType groupType) { + for (int i = 0; i < allGroups.size; i++) { + if (allGroups.get(i).getGroupType() == groupType) { + return allGroups.get(i); + } + } + return null; + } + + +} diff --git a/me.smartstore/group/Parameter.java b/me.smartstore/group/Parameter.java new file mode 100644 index 00000000..b9312aca --- /dev/null +++ b/me.smartstore/group/Parameter.java @@ -0,0 +1,54 @@ +package group; + +import java.util.Objects; + +public class Parameter { + + private Integer minTime; + private Integer minPay; + + public Parameter() { + } + + public Parameter(Integer minTime, Integer minPay) { + this.minTime = minTime; + this.minPay = minPay; + } + + public Integer getMinTime() { + return minTime; + } + + public void setMinTime(Integer minTime) { + this.minTime = minTime; + } + + public Integer getMinPay() { + return minPay; + } + + public void setMinPay(Integer minPay) { + this.minPay = minPay; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Parameter parameter = (Parameter) o; + return minTime.equals(parameter.minTime) && minPay.equals(parameter.minPay); + } + + @Override + public int hashCode() { + return Objects.hash(minTime, minPay); + } + + @Override + public String toString() { + return "Parameter{" + + "minimumSpentTime=" + minTime + + ", minimumTotalPay=" + minPay + + '}'; + } +} diff --git a/me.smartstore/main.java b/me.smartstore/main.java new file mode 100644 index 00000000..d17c7c98 --- /dev/null +++ b/me.smartstore/main.java @@ -0,0 +1,6 @@ +public class main { + public static void main(String[] args) { + SmartStoreApp.getInstance().test().run(); +// SmartStoreApp.getInstance().run(); + } +} diff --git a/me.smartstore/menu/CustomerMenu.java b/me.smartstore/menu/CustomerMenu.java new file mode 100644 index 00000000..35977d09 --- /dev/null +++ b/me.smartstore/menu/CustomerMenu.java @@ -0,0 +1,216 @@ +package menu; + +import customer.Customer; +import customer.Customers; +import exception.InputEndException; +import exception.InputRangeException; +import group.Group; +import group.GroupType; +import group.Groups; +import group.Parameter; +import util.Message; + +import java.util.InputMismatchException; + + +public class CustomerMenu implements Menu { + + private final Customers allCustomers = Customers.getInstance(); + private final Groups allGroups = Groups.getInstance(); + + private static CustomerMenu customerMenu; + + public static CustomerMenu getInstance() { + if (customerMenu == null) { + customerMenu = new CustomerMenu(); + } + return customerMenu; + } + + private CustomerMenu() { + } + + @Override + public void manage() { + System.out.println(); + while (true) { // 서브 메뉴 페이지를 유지하기 위한 while + try { + int choice = chooseMenu(new String[]{ + "Add Customer", + "View Customer", + "Update Customer", + "Delete Customer", + "Back"}); + if (choice == 1) { + addCustomer(); + } else if (choice == 2) { + viewCustomer(); + } else if (choice == 3) { + updateCustomer(); + } else if (choice == 4) { + deleteCustomer(); + } else { + break; // choice == 5 + } + } catch (InputMismatchException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + } + } + } + + public void addCustomer() { + System.out.println("How many customers to input?"); + String numStr = nextLine(Message.END_MSG); + try { + Integer num = Integer.parseInt(numStr); + for (int i = 1; i <= num; i++) { + System.out.println("\n" + "====== Customer " + i + " Info. ======" + "\n"); + addDetailCustomer(); + + } + } catch (NumberFormatException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } + } + + public void addDetailCustomer() { + Customer customer = new Customer(); + while (true) { // 서브 메뉴 페이지를 유지하기 위한 while + int choice = chooseMenu(new String[]{ + "Customer Name", + "Customer ID", + "Customer Spent Time", + "Customer Total Pay", + "Back"}); + + if (choice == 1) { + System.out.println("\n" + "Input Customer's Name:"); + String name = nextLine(Message.END_MSG); + customer.setCustomerName(name); + + } else if (choice == 2) { + System.out.println("\n" + "Input Customer's ID:"); + String id = nextLine(Message.END_MSG); + customer.setCustomerID(id); + + } else if (choice == 3) { + System.out.println("\n" + "Input Customer's Spent Time:"); + Integer time = nextInt(Message.END_MSG); + customer.setTotalTime(time); + + } else if (choice == 4) { + System.out.println("\n" + "Input Customer's Total Pay:"); + Integer pay = nextInt(Message.END_MSG); + customer.setTotalPay(pay); + } else { + setCustomerGroup(customer); + allCustomers.add(customer); + break; // choice == 5 + } + } + + } + + private void setCustomerGroup(Customer customer) { + Group highestGroup = null; + for (int j = 0; j < allGroups.size(); j++) { + Group group = allGroups.get(j); + Parameter parameter = group.getParameter(); + Integer minTime = parameter.getMinTime(); + Integer minPay = parameter.getMinPay(); + + Integer optionalCustomerTime = customer.getTotalTime() == null ? 0 : customer.getTotalTime(); + Integer optionalCustomerPay = customer.getTotalPay() == null ? 0 : customer.getTotalPay(); + + if (minTime <= optionalCustomerTime && minPay <= optionalCustomerPay) { + if (highestGroup == null || (parameter.getMinPay() > highestGroup.getParameter().getMinPay() && parameter.getMinTime() > highestGroup.getParameter().getMinTime())) { + highestGroup = group; + } + } + } + if (highestGroup != null) { + customer.setGroup(highestGroup); + } + } + + public void viewCustomer() { + System.out.println("\n" + "======= Customer Info. ======="); + int num = allCustomers.size(); + for(int i=1; i<=num; i++){ + System.out.print("No. " + i + " => "); + System.out.println(allCustomers.get(i-1)); + } + } + + public void updateCustomer() { + viewCustomer(); + System.out.println("\n" + "Which customer ( 1 ~ " + allCustomers.size() + " )?"); + + Integer customerNO = chooseCustomer(); + Customer customer = allCustomers.get(customerNO-1); + while (true) { // 서브 메뉴 페이지를 유지하기 위한 while + int choice = chooseMenu(new String[]{ + "Customer Name", + "Customer ID", + "Customer Spent Time", + "Customer Total Pay", + "Back"}); + + if (choice == 1) { + System.out.println("\n" + "Input Customer's Name:"); + String name = nextLine(Message.END_MSG); + customer.setCustomerName(name); + + } else if (choice == 2) { + System.out.println("\n" + "Input Customer's ID:"); + String id = nextLine(Message.END_MSG); + customer.setCustomerID(id); + + } else if (choice == 3) { + System.out.println("\n" + "Input Customer's Spent Time:"); + Integer time = nextInt(Message.END_MSG); + customer.setTotalTime(time); + + } else if (choice == 4) { + System.out.println("\n" + "Input Customer's Total Pay:"); + Integer pay = nextInt(Message.END_MSG); + customer.setTotalPay(pay); + } else { + customer.setGroup(null); + setCustomerGroup(customer); + break; // choice == 5 + } + } + } + + + public int chooseCustomer() { + while ( true ) { + try { + Integer customerNum = 0; + try { + customerNum = nextInt(); + } catch (NumberFormatException e) { + throw new InputMismatchException(); + } + if (customerNum >= 1 && customerNum <= allCustomers.size()) + return customerNum; + throw new InputRangeException(); // choice 가 범위에 벗어 + } catch (InputMismatchException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } catch (InputRangeException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + } + + public void deleteCustomer() { + viewCustomer(); + System.out.println("\n" + "Which customer ( 1 ~ " + allCustomers.size() + " )?"); + Integer customerNO = chooseCustomer(); + allCustomers.pop(customerNO-1); + viewCustomer(); + } +} diff --git a/me.smartstore/menu/GroupMenu.java b/me.smartstore/menu/GroupMenu.java new file mode 100644 index 00000000..60c319df --- /dev/null +++ b/me.smartstore/menu/GroupMenu.java @@ -0,0 +1,173 @@ +package menu; + +import exception.InputEndException; +import exception.InputRangeException; +import group.Group; +import group.GroupType; +import group.Parameter; +import util.Message; +import group.Groups; +import customer.Customers; + +public class GroupMenu implements Menu{ + + private final Groups allGroups = Groups.getInstance(); + private final Customers allCustomers = Customers.getInstance(); + + private static GroupMenu groupMenu; + + public static GroupMenu getInstance() { + if (groupMenu == null) { + groupMenu = new GroupMenu(); + } + return groupMenu; + } + + private GroupMenu() {} + + @Override + public void manage() { + while ( true ) { // 서브 메뉴 페이지를 유지하기 위한 while + int choice = chooseMenu(new String[]{ + "Set Parameter", + "View Parameter", + "Update Parameter", + "Back"}); + + if (choice == 1) { + setParameter(); + } else if (choice == 2) { + viewParameter(); + } else if (choice == 3) { + updateParameter(); + } + else { + break; // choice == 4 + } + } + } + + public GroupType chooseGroup() { + while (true) { + try { + System.out.print("Which group (GENERAL (G), VIP (V), VVIP (VV))? "); + System.out.println(); + String choice = nextLine(Message.END_MSG); + + GroupType groupType = GroupType.valueOf(choice).replaceFullName(); + return groupType; + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + return null; + } catch (IllegalArgumentException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + } + + + + public void setParameter() { // 초기화할 때만 호출 가능 + while ( true ) { + GroupType groupType = chooseGroup(); + if (groupType == null) break; + + // GroupType에 해당하는 group 객체를 찾아야 함 + Group group = allGroups.find(groupType); + if (group != null && group.getParameter() != null) { // group.getParameter()이 null이 아니면 이미 초기화됨 + System.out.println("\n" + group.getGroupType() + " group already exists."); + System.out.println("\n" + group); + } else { + Parameter parameter = new Parameter(); + this.setDetailParameter(parameter); + group = new Group(parameter, groupType); + // time, pay 사용자 입력받은 후, 설정 필요 + group.setParameter(parameter); + + allGroups.add(group); + allCustomers.refresh(allGroups); // 파라미터가 변경되었거나 추가되는 경우, 고객 분류를 다시 해야함 + } + } + + } + + /* + allGroups.add(new Group(new Parameter(10, 100000), GroupType.GENERAL)); + allGroups.add(new Group(new Parameter(20, 200000), GroupType.VIP)); + allGroups.add(new Group(new Parameter(30, 300000), GroupType.VVIP)); + */ + public void setDetailParameter(Parameter parameter) { + while ( true ) { // 서브 메뉴 페이지를 유지하기 위한 while + int choice = chooseMenu(new String[]{ + "Minimum Spent Time", + "Minimum Total Pay", + "Back"}); + + if (choice == 1) { + // Minimum Spent Time + System.out.println("Input Minimum Spent Time: "); + Integer minTime = Integer.parseInt(nextLine(Message.END_MSG)); + parameter.setMinTime(minTime); + } + + else if (choice == 2) { + // Minimum Total Pay + System.out.println("Input Minimum Total Pay: "); + Integer minPay = Integer.parseInt(nextLine(Message.END_MSG)); + parameter.setMinPay(minPay); + + } + + else break; // choice == 3 + } + } + + public void viewParameter() { + while (true) { + GroupType groupType = chooseGroup(); + if (groupType == null) break; + + // GroupType에 해당하는 group 객체를 찾아야 함 + Group group = allGroups.find(groupType); + if (group != null && group.getParameter() != null) { + System.out.println("GroupType: " + group.getGroupType() + "\n" + + "Parameter: " + group.getParameter()); + } else if (group == null) { + System.out.println("GroupType: " + groupType + "\n" + + "Parameter: " + new Parameter()); + } + } + } + + public void updateParameter() { + while ( true ) { + GroupType groupType = chooseGroup(); + if (groupType == null) break; + + // GroupType에 해당하는 group 객체를 찾아야 함 + + Group group = allGroups.find(groupType); + if (group != null && group.getParameter() != null) { + System.out.println("GroupType: " + group.getGroupType() + "\n" + + "Parameter: " + group.getParameter()); + setDetailParameter(group.getParameter()); + int index = allGroups.indexOf(group); + allGroups.set(index, group); + } else if (group == null) { + System.out.println("GroupType: " + groupType + "\n" + + "Parameter: " + new Parameter()); + + Parameter parameter = new Parameter(); + this.setDetailParameter(parameter); + group = new Group(parameter, groupType); + // time, pay 사용자 입력받은 후, 설정 필요 + group.setParameter(parameter); + allGroups.add(group); + } + + allCustomers.refresh(allGroups); // 파라미터가 변경되었거나 추가되는 경우, 고객 분류를 다시 해야함 + } + } + } + + diff --git a/me.smartstore/menu/MainMenu.java b/me.smartstore/menu/MainMenu.java new file mode 100644 index 00000000..5369a3e3 --- /dev/null +++ b/me.smartstore/menu/MainMenu.java @@ -0,0 +1,38 @@ +package menu; + +public class MainMenu implements Menu{ + private static MainMenu mainMenu; + + private final CustomerMenu customerMenu = CustomerMenu.getInstance(); + private final GroupMenu groupMenu = GroupMenu.getInstance(); + private final SummaryMenu summaryMenu = SummaryMenu.getInstance(); + + public static MainMenu getInstance() { + if (mainMenu == null) { + mainMenu = new MainMenu(); + } + return mainMenu; + } + + private MainMenu() {} + + @Override + public void manage() { + while ( true ) { // 프로그램 실행 while + int choice = mainMenu.chooseMenu(new String[] { + "Parameter", + "Customer", + "Classification Summary", + "Quit"}); + + if (choice == 1) groupMenu.manage(); + else if (choice == 2) customerMenu.manage(); + else if (choice == 3) summaryMenu.manage(); + else { // choice == 4 + System.out.println("Program Finished"); + break; + } + } + + } +} diff --git a/me.smartstore/menu/Menu.java b/me.smartstore/menu/Menu.java new file mode 100644 index 00000000..1c3c87cf --- /dev/null +++ b/me.smartstore/menu/Menu.java @@ -0,0 +1,77 @@ +package menu; + +import exception.InputEndException; +import exception.InputRangeException; +import util.Message; + +import java.awt.*; +import java.util.InputMismatchException; +import java.util.Scanner; + +public interface Menu { + Scanner scanner = new Scanner(System.in); + + default String nextLine() { + return scanner.nextLine().toUpperCase(); + } + + default Integer nextInt(String end) { + System.out.println("** Press 'end', if you want to exit! **"); + String numStr = scanner.nextLine(); + try { + if (numStr.equals(end)) { + throw new InputEndException(); + } + return Integer.parseInt(numStr); + } catch (NumberFormatException e) { + throw new InputMismatchException(); + } + } + + default Integer nextInt() { + String numStr = scanner.nextLine(); + try { + return Integer.parseInt(numStr); + } catch (NumberFormatException e) { + throw new InputMismatchException(); + } + } + + default String nextLine(String end) throws InputEndException { + System.out.println("** Press 'end', if you want to exit! **"); + String str = scanner.nextLine().toUpperCase(); + if (str.equals(end)) throw new InputEndException(); + return str; + } + + default int chooseMenu(String[] menus) { + while ( true ) { // 예외 복구 while + try { + System.out.println("==============================="); + for (int i = 0; i < menus.length; i++) { + System.out.printf(" %d. %s\n", i + 1, menus[i]); + } + System.out.println("==============================="); + System.out.print("Choose One: "); + String choiceStr = nextLine(); + int choice = 0; + try { + choice = Integer.parseInt(choiceStr); + } catch (NumberFormatException e) { + throw new InputMismatchException(); + } + if (choice >= 1 && choice <= menus.length) return choice; + throw new InputRangeException(); // choice 가 범위에 벗어남 + + } catch (InputMismatchException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } catch (InputRangeException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + + + } + + void manage(); +} diff --git a/me.smartstore/menu/SummaryMenu.java b/me.smartstore/menu/SummaryMenu.java new file mode 100644 index 00000000..d1a82a3b --- /dev/null +++ b/me.smartstore/menu/SummaryMenu.java @@ -0,0 +1,174 @@ +package menu; + +import arrays.DArray; +import customer.Customer; +import customer.Customers; +import exception.InputEndException; +import exception.InputRangeException; +import group.Group; +import group.GroupType; +import group.Groups; +import group.Parameter; +import util.Message; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.InputMismatchException; + +public class SummaryMenu implements Menu { + + private final Groups allGroups = Groups.getInstance(); + private final Customers allCustomers = Customers.getInstance(); + + private static SummaryMenu summaryMenu; + + public static SummaryMenu getInstance() { + if (summaryMenu == null) { + summaryMenu = new SummaryMenu(); + } + return summaryMenu; + } + + private SummaryMenu() {} + + @Override + public void manage() { + while ( true ) { // 서브 메뉴 페이지를 유지하기 위한 while + try { + int choice = chooseMenu(new String[]{ + "Summary", + "Summary (Sorted By Name)", + "Summary (Sorted By Time)", + "Summary (Sorted By Pay)", + "Back"}); + if (choice == 1) { + summary(); + } else if (choice == 2) { + summaryByName(); + } else if (choice == 3) { + summaryByTime(); + } else if (choice == 4) { + summaryByPay(); + } else { + break; // choice == 5 + } + } catch (InputMismatchException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + } + } + } + + public void summary () { + + int cnt = allGroups.size(); + for (int i = 0; i < cnt; i++) { + Group group = allGroups.get(i); + GroupType grouptype = group.getGroupType(); + int minTime = group.getParameter().getMinTime(); + int minPay = group.getParameter().getMinPay(); + System.out.println("\n" + "==============================" + "\n" + + "Group : " + grouptype + " ( Time : " + minTime + ", Pay : " + minPay + " )" + "\n" + + "=============================="); + + int idx = 1; + for(int j=0; j "); + System.out.println(allCustomers.get(j)); + } + } + } + } + + public void summaryByName() { + String choice = summaryAorD(); + //?? + int cnt = allGroups.size(); + for (int i = 0; i < cnt; i++) { + Group group = allGroups.get(i); + GroupType grouptype = group.getGroupType(); + int minTime = group.getParameter().getMinTime(); + int minPay = group.getParameter().getMinPay(); + System.out.println("\n" + "==============================" + "\n" + + "Group : " + grouptype + " ( Time : " + minTime + ", Pay : " + minPay + " )" + "\n" + + "=============================="); + + int idx = 1; + for(int j=0; j "); + System.out.println(allCustomers.get(j)); + } + } + + } + } + + public String summaryAorD() { + while (true) { + try { + System.out.println("Which order (ASCENDING (A), DESCENDING (D))?" + "\n"); + String choice = nextLine(Message.END_MSG); + + if (choice.equals("A") || choice.equals("B")) + return choice; + throw new IllegalArgumentException(); // choice 가 범위에 벗어 + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + return null; + } catch (IllegalArgumentException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_TYPE); + } + } + } + + public void summaryByTime() { + String choice = summaryAorD(); + //?? + int cnt = allGroups.size(); + for (int i = 0; i < cnt; i++) { + Group group = allGroups.get(i); + GroupType grouptype = group.getGroupType(); + int minTime = group.getParameter().getMinTime(); + int minPay = group.getParameter().getMinPay(); + System.out.println("\n" + "==============================" + "\n" + + "Group : " + grouptype + " ( Time : " + minTime + ", Pay : " + minPay + " )" + "\n" + + "=============================="); + + int idx = 1; + for(int j=0; j "); + System.out.println(allCustomers.get(j)); + } + } + } + + } + + public void summaryByPay() { + String choice = summaryAorD(); + //?? + int cnt = allGroups.size(); + for (int i = 0; i < cnt; i++) { + Group group = allGroups.get(i); + GroupType grouptype = group.getGroupType(); + int minTime = group.getParameter().getMinTime(); + int minPay = group.getParameter().getMinPay(); + System.out.println("\n" + "==============================" + "\n" + + "Group : " + grouptype + " ( Time : " + minTime + ", Pay : " + minPay + " )" + "\n" + + "=============================="); + + int idx = 1; + for(int j=0; j "); + System.out.println(allCustomers.get(j)); + } + } + } + } + +} diff --git a/me.smartstore/util/Message.java b/me.smartstore/util/Message.java new file mode 100644 index 00000000..7d33cb17 --- /dev/null +++ b/me.smartstore/util/Message.java @@ -0,0 +1,13 @@ +package util; + +public interface Message { + String ERR_MSG_INVALID_ARR_EMPTY = "No Customers. Please input one first."; + String ERR_MSG_NULL_ARR_ELEMENT = "Elements in Array has null. Array can't be sorted."; + String ERR_MSG_INVALID_INPUT_NULL = "Null Input. Please input something."; + String ERR_MSG_INVALID_INPUT_EMPTY = "Empty Input. Please input something."; + String ERR_MSG_INVALID_INPUT_RANGE = "Invalid Input. Please try again."; + String ERR_MSG_INVALID_INPUT_TYPE = "Invalid Type for Input. Please try again."; + String ERR_MSG_INVALID_INPUT_FORMAT = "Invalid Format for Input. Please try again."; + String ERR_MSG_INPUT_END = "END is pressed. Exit this menu."; + String END_MSG = "END"; +}