diff --git a/src/me/smartstore/Main.java b/src/me/smartstore/Main.java new file mode 100644 index 00000000..d43f0ab6 --- /dev/null +++ b/src/me/smartstore/Main.java @@ -0,0 +1,7 @@ +package me.smartstore; + +public class Main { + public static void main(String[] args) { + SmartStoreApp.getInstance().run(); + } +} diff --git a/src/me/smartstore/SmartStoreApp.java b/src/me/smartstore/SmartStoreApp.java new file mode 100644 index 00000000..c27d354a --- /dev/null +++ b/src/me/smartstore/SmartStoreApp.java @@ -0,0 +1,39 @@ +package me.smartstore; + +import me.smartstore.group.Group; +import me.smartstore.group.GroupType; +import me.smartstore.group.Groups; +import me.smartstore.group.Parameter; +import me.smartstore.menu.MainMenu; + +public class SmartStoreApp { + private final Groups allGroups = Groups.getInstance(); + private final MainMenu mainMenu = MainMenu.getInstance(); + + + // singleton + private static SmartStoreApp smartStoreApp; + + 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.05.10"); + System.out.println(" Copyright 2023 Jongmin No rights reserved."); + System.out.println("===========================================\n"); + } + + public void run() { + details(); + allGroups.add(new Group(new Parameter(0, 0), GroupType.NONE)); + mainMenu.manage(); + } +} diff --git a/src/me/smartstore/arrays/Collections.java b/src/me/smartstore/arrays/Collections.java new file mode 100644 index 00000000..ecfb6e60 --- /dev/null +++ b/src/me/smartstore/arrays/Collections.java @@ -0,0 +1,14 @@ +package me.smartstore.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/src/me/smartstore/arrays/DArray.java b/src/me/smartstore/arrays/DArray.java new file mode 100644 index 00000000..7e137d75 --- /dev/null +++ b/src/me/smartstore/arrays/DArray.java @@ -0,0 +1,137 @@ +package me.smartstore.arrays; + + +import me.smartstore.arrays.exception.ElementNotFoundException; +import me.smartstore.arrays.exception.EmptyArrayException; +import me.smartstore.arrays.exception.NullArgumentException; + +public class DArray implements Collections { // Dynamic Array + + protected static final int DEFAULT = 10; + + protected T[] arrays; + protected int size; + protected int capacity; + + @SuppressWarnings("unchecked") + public DArray() { + arrays = (T[]) new Object[DEFAULT]; + capacity = DEFAULT; + } + + @SuppressWarnings("unchecked") + public DArray(int initial) { + arrays = (T[]) new Object[initial]; + capacity = initial; + } + + public DArray(T[] arrays) { + this.arrays = arrays; + capacity = arrays.length; + size = arrays.length; + } + + @Override + public int size() { + return size; + } + + 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 + } + + @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() { + 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); + } + + @Override + public String toString() { + String toStr = ""; + for (int i = 0; i < size; i++) { + toStr += (arrays[i] + "\n"); + } + return toStr; + } +} diff --git a/src/me/smartstore/arrays/exception/ElementNotFoundException.java b/src/me/smartstore/arrays/exception/ElementNotFoundException.java new file mode 100644 index 00000000..a3b1e7f7 --- /dev/null +++ b/src/me/smartstore/arrays/exception/ElementNotFoundException.java @@ -0,0 +1,22 @@ +package me.smartstore.arrays.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/src/me/smartstore/arrays/exception/EmptyArrayException.java b/src/me/smartstore/arrays/exception/EmptyArrayException.java new file mode 100644 index 00000000..e85a19a8 --- /dev/null +++ b/src/me/smartstore/arrays/exception/EmptyArrayException.java @@ -0,0 +1,22 @@ +package me.smartstore.arrays.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/src/me/smartstore/arrays/exception/NullArgumentException.java b/src/me/smartstore/arrays/exception/NullArgumentException.java new file mode 100644 index 00000000..ec912d26 --- /dev/null +++ b/src/me/smartstore/arrays/exception/NullArgumentException.java @@ -0,0 +1,22 @@ +package me.smartstore.arrays.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/src/me/smartstore/customer/Customer.java b/src/me/smartstore/customer/Customer.java new file mode 100644 index 00000000..cacf5381 --- /dev/null +++ b/src/me/smartstore/customer/Customer.java @@ -0,0 +1,101 @@ +package me.smartstore.customer; + +import me.smartstore.group.Group; + +import java.util.Objects; + +public class Customer implements Comparable { + private String customerName; + private String customerId; + private Integer totalTime; + private Integer totalPay; + private Group group; // 현재 분류 기준에 의해 각 고객을 분류된 결과 + + public Group getGroup() { + return group; + } + + public void setGroup(Group group) { + this.group = group; + } + + public Customer() { + } + + public Customer(String customerId) { + this.customerId = customerId; + } + + public Customer(String customerName, String customerId) { + this.customerName = customerName; + this.customerId = customerId; + } + + public Customer(String customerName, String customerId, Integer totalTime, Integer totalPay) { + this.customerName = customerName; + this.customerId = customerId; + this.totalTime = totalTime; + this.totalPay = 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 totalTime; + } + + public void setTotalTime(Integer totalTime) { + this.totalTime = totalTime; + } + + public Integer getTotalPay() { + return totalPay; + } + + public void setTotalPay(Integer totalPay) { + this.totalPay = totalPay; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Customer customer = (Customer) o; + return Objects.equals(customerId, customer.customerId); + } + + @Override + public int hashCode() { + return Objects.hash(customerId); + } + + @Override + public String toString() { + return "Customer{" + + "userId='" + customerId + '\'' + + ", name='" + customerName + '\'' + + ", spentTime=" + totalTime + + ", totalPay=" + totalPay + + group.toStringForCustomer() + '}'; + } + @Override + public int compareTo(Customer o) { + return 0; + } + + +} diff --git a/src/me/smartstore/customer/Customers.java b/src/me/smartstore/customer/Customers.java new file mode 100644 index 00000000..42676baf --- /dev/null +++ b/src/me/smartstore/customer/Customers.java @@ -0,0 +1,60 @@ +package me.smartstore.customer; + +import me.smartstore.arrays.DArray; +import me.smartstore.group.Group; +import me.smartstore.group.Groups; + +import java.util.Arrays; +import java.util.Comparator; + +public class Customers extends DArray { + + private final Groups allGroups = Groups.getInstance(); + + // singleton + + private static Customers allCustomers; + + public static Customers getInstance() { + if (allCustomers == null) { + allCustomers = new Customers(); + } + return allCustomers; + } + + private Customers() {} + + public void refresh() { + for (int i = 0; i < allCustomers.size(); i++) { + Customer customer = allCustomers.get(i); + Group group = findCustomerGroup(customer.getTotalTime(), customer.getTotalPay()); + customer.setGroup(group); + } + } + + public Group findCustomerGroup(int Time, int Pay) { + Group group = null; + for (int i = 0; i < allGroups.size(); i++) { + Group groupPara = allGroups.get(i); + if (Time >= groupPara.getParameter().getMinimumSpentTime() && + Pay >= groupPara.getParameter().getMinimumTotalPay()) { + group = groupPara; + } + } + return group; + } + + public Customer[] toArray() { + Customer[] customer = new Customer[size]; + for (int i = 0; i < size; i++) { + customer[i] = allCustomers.get(i); + } + return customer; + } + + public Customer[] toArraySort(Comparator comparator) { + Customer[] customer = toArray(); + Arrays.sort(customer, comparator); + return customer; + } +} diff --git a/src/me/smartstore/exception/InputEmptyException.java b/src/me/smartstore/exception/InputEmptyException.java new file mode 100644 index 00000000..2053e665 --- /dev/null +++ b/src/me/smartstore/exception/InputEmptyException.java @@ -0,0 +1,14 @@ +package me.smartstore.exception; + +import me.smartstore.util.Message; + +public class InputEmptyException extends RuntimeException { + + public InputEmptyException() { + super(Message.ERR_MSG_INVALID_INPUT_EMPTY); + } + + public InputEmptyException(String message) { + super(message); + } +} diff --git a/src/me/smartstore/exception/InputEndException.java b/src/me/smartstore/exception/InputEndException.java new file mode 100644 index 00000000..f0311099 --- /dev/null +++ b/src/me/smartstore/exception/InputEndException.java @@ -0,0 +1,9 @@ +package me.smartstore.exception; + +import me.smartstore.util.Message; + +public class InputEndException extends RuntimeException { + public InputEndException() { + super(Message.ERR_MSG_INPUT_END); + } +} diff --git a/src/me/smartstore/exception/InputRangeException.java b/src/me/smartstore/exception/InputRangeException.java new file mode 100644 index 00000000..10d7b011 --- /dev/null +++ b/src/me/smartstore/exception/InputRangeException.java @@ -0,0 +1,13 @@ +package me.smartstore.exception; + +import me.smartstore.util.Message; + +public class InputRangeException extends RuntimeException { + public InputRangeException() { + super(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + + public InputRangeException(String message) { + super(message); + } +} diff --git a/src/me/smartstore/group/Group.java b/src/me/smartstore/group/Group.java new file mode 100644 index 00000000..12af4417 --- /dev/null +++ b/src/me/smartstore/group/Group.java @@ -0,0 +1,59 @@ +package me.smartstore.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 "Group{" + + "parameter=" + parameter + + ", groupType=" + groupType + + '}'; + } + + public String toStringForCustomer() { + return ", group=groupType: " + + groupType + "\nParameter: " + + parameter; + } +} diff --git a/src/me/smartstore/group/GroupType.java b/src/me/smartstore/group/GroupType.java new file mode 100644 index 00000000..1055953c --- /dev/null +++ b/src/me/smartstore/group/GroupType.java @@ -0,0 +1,19 @@ +package me.smartstore.group; + +public enum GroupType { + NONE("해당없음"), GENERAL("일반고객"), VIP("우수고객"), VVIP("최우수고객"), + N("해당없음"), G("일반고객"), V("우수고객"), VV("최우수고객"); + + 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/src/me/smartstore/group/Groups.java b/src/me/smartstore/group/Groups.java new file mode 100644 index 00000000..61747b23 --- /dev/null +++ b/src/me/smartstore/group/Groups.java @@ -0,0 +1,25 @@ +package me.smartstore.group; + +import me.smartstore.arrays.DArray; + +public class Groups extends DArray { + // singleton + 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/src/me/smartstore/group/Parameter.java b/src/me/smartstore/group/Parameter.java new file mode 100644 index 00000000..8921e24a --- /dev/null +++ b/src/me/smartstore/group/Parameter.java @@ -0,0 +1,53 @@ +package me.smartstore.group; + +import java.util.Objects; + +public class Parameter { + private Integer minimumSpentTime; + private Integer minimumTotalPay; + + public Parameter() { + } + + public Parameter(Integer minimumSpentTime, Integer minimumTotalPay) { + this.minimumSpentTime = minimumSpentTime; + this.minimumTotalPay = minimumTotalPay; + } + + public Integer getMinimumSpentTime() { + return minimumSpentTime; + } + + public void setMinimumSpentTime(Integer minimumSpentTime) { + this.minimumSpentTime = minimumSpentTime; + } + + public Integer getMinimumTotalPay() { + return minimumTotalPay; + } + + public void setMinimumTotalPay(Integer minimumTotalPay) { + this.minimumTotalPay = minimumTotalPay; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Parameter parameter = (Parameter) o; + return Objects.equals(minimumSpentTime, parameter.minimumSpentTime) && Objects.equals(minimumTotalPay, parameter.minimumTotalPay); + } + + @Override + public int hashCode() { + return Objects.hash(minimumSpentTime, minimumTotalPay); + } + + @Override + public String toString() { + return "Parameter{" + + "minimumSpentTime=" + minimumSpentTime + + ", minimumTotalPay=" + minimumTotalPay + + '}'; + } +} diff --git a/src/me/smartstore/menu/CustomerMenu.java b/src/me/smartstore/menu/CustomerMenu.java new file mode 100644 index 00000000..f3deb68b --- /dev/null +++ b/src/me/smartstore/menu/CustomerMenu.java @@ -0,0 +1,149 @@ +package me.smartstore.menu; + +import me.smartstore.customer.Customer; +import me.smartstore.customer.Customers; +import me.smartstore.exception.InputEmptyException; +import me.smartstore.exception.InputEndException; +import me.smartstore.util.Message; + +import static me.smartstore.util.Message.*; + +public class CustomerMenu implements Menu { + + private final Customers allCustomers = Customers.getInstance(); + // singleton + private static CustomerMenu customerMenu; + + public static CustomerMenu getInstance() { + if (customerMenu == null) { + customerMenu = new CustomerMenu(); + } + return customerMenu; + } + + private CustomerMenu() {} + + @Override + public void manage() { + while ( true ) { // 서브 메뉴 페이지를 유지하기 위한 while + 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; // "Back" + } + } + + private void setCustomerManage(Customer 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("\nInput Customer's Name: "); + String input = nextLine(END_MSG); + customer.setCustomerName(input); + } + else if (choice == 2) { + System.out.println("\nInput Customer's ID: "); + String input = nextLine(END_MSG); + customer.setCustomerId(input); + } + else if (choice == 3) { + System.out.println("\nInput Customer's Spent Time: "); + int input = Integer.parseInt(nextLine(END_MSG)); + customer.setTotalTime(input); + } + else if (choice == 4) { + System.out.println("\nInput Customer's Total Payment: "); + int input = Integer.parseInt(nextLine(END_MSG)); + customer.setTotalPay(input); + } + else {break;} + } + } + private void addCustomer() { + while (true) { + try { + System.out.println("How many customers to input?"); + int input = Integer.parseInt(nextLine(END_MSG)); + for (int i = 1; i <= input; i++) { + System.out.println("====== Customer " + i + " Info. ======"); + Customer customer = new Customer(); + setCustomerManage(customer); + allCustomers.add(customer); + + allCustomers.refresh(); + } + break; + } catch (NullPointerException e ) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_NULL); + } catch (NumberFormatException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } catch (InputEndException e ) { + break; + } + } + } + + private void viewCustomer() { + try { + System.out.println("\n ======= Customer Info. ======= "); + if (allCustomers.size() == 0) System.out.println(ERR_MSG_INVALID_ARR_EMPTY); + for (int i = 0; i < allCustomers.size(); i++) { + System.out.println("No. " + (i + 1) + " => " + allCustomers.get(i)); + } + + } catch (NullPointerException e ) { + + }catch (NumberFormatException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } catch (InputEmptyException e) { + System.out.println(ERR_MSG_INVALID_INPUT_NULL); + } + } + + private void updateCustomer() { + try { + if (allCustomers.size() == 0 ) System.out.println(ERR_MSG_INVALID_ARR_EMPTY); + else if (!(allCustomers.size() == 0)) { + viewCustomer(); + System.out.printf("Which customer (1 ~ %d)? \n", allCustomers.size()); + int input = Integer.parseInt(nextLine(END_MSG)); + Customer customer = allCustomers.get(input - 1); + setCustomerManage(customer); + allCustomers.refresh(); + } + } catch (NullPointerException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_NULL); + } catch (InputEndException e) { + System.out.println(ERR_MSG_INVALID_INPUT_EMPTY); + } + } + + private void deleteCustomer() { + try { + if (allCustomers.size() == 0 ) System.out.println(ERR_MSG_INVALID_ARR_EMPTY); + else if (!(allCustomers.size() == 0)) { + viewCustomer(); + System.out.printf("Which customer (1 ~ %d)? \n", allCustomers.size()); + int input = Integer.parseInt(nextLine(END_MSG)); + allCustomers.pop(input - 1); + allCustomers.refresh(); + } + } catch (InputEndException e) { + System.out.println(ERR_MSG_INPUT_END); + } catch (NumberFormatException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } + } +} diff --git a/src/me/smartstore/menu/GroupMenu.java b/src/me/smartstore/menu/GroupMenu.java new file mode 100644 index 00000000..dd712e79 --- /dev/null +++ b/src/me/smartstore/menu/GroupMenu.java @@ -0,0 +1,154 @@ +package me.smartstore.menu; + +import me.smartstore.customer.Customers; +import me.smartstore.exception.InputEndException; +import me.smartstore.exception.InputRangeException; +import me.smartstore.group.Group; +import me.smartstore.group.GroupType; +import me.smartstore.group.Groups; +import me.smartstore.group.Parameter; +import me.smartstore.util.Message; + +public class GroupMenu implements Menu { + private final Groups allGroups = Groups.getInstance(); + private final Customers allCustomers = Customers.getInstance(); + // singleton + private static GroupMenu groupMenu; + + public static GroupMenu getInstance() { + if (groupMenu == null) { + groupMenu = new GroupMenu(); + } + return groupMenu; + } + + private GroupMenu() {} + + private GroupType chooseGroup() { + while ( true ) { + try { + System.out.print("Which group (GENERAL (G), VIP (V), VVIP (VV))? "); + 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); + } + } + } + @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 + } + } + private void setParameter() { + while ( true ) { + try { + System.out.println("\nWhich group (GENERAL (G), VIP (V), VVIP (VV))?"); + String input = nextLine(Message.END_MSG); + GroupType groupType = GroupType.valueOf(input).replaceFullName(); + Group group = allGroups.find(groupType); + + if (group != null && group.getParameter() != null) { + System.out.println("\n" + group.getGroupType() + " group already exists."); + System.out.println("\nGroupType: " + group); + } else { + Parameter parameter = new Parameter(); + parameterManage(parameter); + group = new Group(parameter,groupType); + allGroups.add(group); + allCustomers.refresh(); // 파라미터가 변경되었거나 추가되는 경우, 고객 분류를 다시 해야함 + } + } catch (InputEndException e ) { + break; + } catch (InputRangeException e ) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } catch (IllegalArgumentException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + } + private void updateParameter() { + while (true) { + try { + GroupType groupType = chooseGroup(); + Group group = allGroups.find(groupType); + if (!group.equals("end") && !group.equals("END")) System.out.println("Parameter:" + group); + + Parameter parameter = group.getParameter(); + parameterManage(parameter); + + allCustomers.refresh(); + System.out.println("Parameter:" + group); + } catch (NullPointerException e) { + break; + } + } + } + private void viewParameter() { + while ( true ) { + try { + System.out.println("\nWhich group (GENERAL (G), VIP (V), VVIP (VV))?"); + String input = nextLine(Message.END_MSG); + GroupType groupType = GroupType.valueOf(input).replaceFullName(); + Group group = allGroups.find(groupType); + if (!group.equals("end") && !group.equals("END")) System.out.println("\nGroupType: " + group); + } catch (InputEndException e ) { + break; + } catch (NullPointerException e ) { + System.out.println("Parameter doesn't exist , please try again"); + } catch ( IllegalArgumentException e ) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } + } + } + + private void parameterManage(Parameter parameter) { + while ( true ) { // 프로그램 실행 while + int choice = chooseMenu(new String[]{ + "Minimum Spent Time", + "Minimum Total Pay", + "Back"}); + if (choice == 1) { + while (true) { + System.out.println("\n" + "Input Minimum Spent Time: "); + Integer minimumSpentTime = Integer.valueOf(nextLine(Message.END_MSG)); + if (minimumSpentTime <= 0) { + throw new InputRangeException(); + } + parameter.setMinimumSpentTime(minimumSpentTime); + break; + } + } + else if (choice == 2) { + while (true) { + System.out.println("\n" + "Input Minimum Total Pay: "); + Integer minimumTotalPay = Integer.valueOf(nextLine(Message.END_MSG)); + if (minimumTotalPay <= 0) { + throw new InputRangeException(); + } + parameter.setMinimumTotalPay(minimumTotalPay); + break; + } + } + else { + break; + } + } + } +} diff --git a/src/me/smartstore/menu/MainMenu.java b/src/me/smartstore/menu/MainMenu.java new file mode 100644 index 00000000..883dae76 --- /dev/null +++ b/src/me/smartstore/menu/MainMenu.java @@ -0,0 +1,38 @@ +package me.smartstore.menu; + +public class MainMenu implements Menu { + + private final CustomerMenu customerMenu = CustomerMenu.getInstance(); + private final GroupMenu groupMenu = GroupMenu.getInstance(); + private final SummaryMenu summaryMenu = SummaryMenu.getInstance(); + + private static MainMenu mainMenu; + + 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/src/me/smartstore/menu/Menu.java b/src/me/smartstore/menu/Menu.java new file mode 100644 index 00000000..3cc1d9f3 --- /dev/null +++ b/src/me/smartstore/menu/Menu.java @@ -0,0 +1,49 @@ +package me.smartstore.menu; + +import me.smartstore.exception.InputEmptyException; +import me.smartstore.exception.InputEndException; +import me.smartstore.exception.InputRangeException; +import me.smartstore.util.Message; + +import java.util.InputMismatchException; +import java.util.Scanner; + +public interface Menu { + Scanner scanner = new Scanner(System.in); + + default String nextLine() { // 하나의 프로그램 상에서 nextLine() 함수를 통해서 사용자 입력을 받음 + return scanner.nextLine().toUpperCase(); + } + + default String nextLine(String end) { + 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: "); + int choice = Integer.parseInt(nextLine()); + 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); + } catch (InputEmptyException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_NULL); + } catch ( NumberFormatException e ) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } + } + } + void manage(); // 각 서브메뉴들을 관리하는 함수 (각 서브메뉴의 최상위 메뉴) +} diff --git a/src/me/smartstore/menu/SummaryMenu.java b/src/me/smartstore/menu/SummaryMenu.java new file mode 100644 index 00000000..887a51e0 --- /dev/null +++ b/src/me/smartstore/menu/SummaryMenu.java @@ -0,0 +1,234 @@ +package me.smartstore.menu; + + +import me.smartstore.customer.Customer; +import me.smartstore.customer.Customers; +import me.smartstore.exception.InputEndException; +import me.smartstore.group.Group; +import me.smartstore.group.GroupType; +import me.smartstore.group.Groups; +import me.smartstore.group.Parameter; + +import java.util.Comparator; + +import static me.smartstore.util.Message.ERR_MSG_INPUT_END; + +public class SummaryMenu implements Menu { + + + private final Groups allGroups = Groups.getInstance(); + private final Customers allCustomers = Customers.getInstance(); + + int switcher = 1; + + // singleton + 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 + int choice = chooseMenu(new String[]{ + "Summary", + "Summary (Sorted By Name)", + "Summary (Sorted By Time)", + "Summary (Sorted By Pay)", + "Back"}); + + if (choice == 1) defaultSummary(); + else if (choice == 2) summaryByName(); + else if (choice == 3) summaryByTime(); + else if (choice == 4) summaryByPay(); + else break; // "Back" + } + } + + private void printParameter() { + System.out.println("=============================="); + System.out.println("Group : NONE ( Time : null, Pay : null )"); + System.out.println("=============================="); + } + private void printParameter(GroupType groupType) { + try { + Group groupPara = allGroups.find(groupType); + Parameter parameterG = groupPara.getParameter(); + System.out.println("=============================="); + System.out.println("Group : " + groupType + " ( Time : " + parameterG.getMinimumSpentTime() + + " pay : " + parameterG.getMinimumTotalPay() + " ) "); + System.out.println("=============================="); + } catch (NullPointerException e ){ + } + } + private void printNone(Comparator comparator) { + int countN = 0; + printParameter(); + if (comparator == null) { + Customer[] groupND = allCustomers.toArray(); + for (int i = 0; i < allCustomers.size(); i++) { + if (groupND[i].getGroup().getGroupType() == GroupType.NONE) { + System.out.println("No. " + (countN + 1) + " =>" + groupND[i]); + countN++; + } + } + } else if (comparator != null) { + Customer[] groupN = allCustomers.toArraySort(comparator); + for (int i = 0; i < allCustomers.size(); i++) { + if (groupN[i].getGroup().getGroupType() == GroupType.NONE) { + System.out.println("No. " + (countN + 1) + " =>" + groupN[i]); + countN++; + } + } + } + if (countN == 0) { + System.out.println("Null. \n"); + }else { + System.out.println("==============================\n"); + } + } + + private void print(GroupType groupType , Comparator comparator) { + + int count = 0; + printParameter(groupType); + if (comparator == null) { + Customer[] groupD = allCustomers.toArray(); + for (int i = 0; i < allCustomers.size(); i++) { + if (groupD[i].getGroup().getGroupType().equals(groupType)) { + System.out.println("No. " + (count + 1) + " =>" + groupD[i]); + count++; + } + } + } else if (comparator != null) { + Customer[] groupS = allCustomers.toArraySort(comparator); + for (int i = 0; i < allCustomers.size(); i++) { + if (groupS[i].getGroup().getGroupType().equals(groupType)) { + System.out.println("No. " + (count + 1) + " =>" + groupS[i]); + count++; + } + } + } + if (count == 0) { + System.out.println("Null. \n"); + } else { + System.out.println("==============================\n"); + } + } + + private Comparator standardSwitcher(String standard) { + + if (standard == "T") { + Comparator comparator = (c1, c2) -> { + return switcher * c1.getTotalTime().compareTo(c2.getTotalTime()); + }; + return comparator; + } + if (standard == "N" ) { + Comparator comparator = (c1, c2) -> { + return switcher * c1.getCustomerName().compareTo(c2.getCustomerName()); + }; + return comparator; + } + if (standard == "P" ) { + Comparator comparator = (c1, c2) -> { + return switcher * c1.getTotalPay().compareTo(c2.getTotalPay()); + }; + return comparator; + } + if (standard == "D") { + Comparator comparator = null; + } + return null; + } + + private void defaultSummary() { + printNone(standardSwitcher("D")); + print(GroupType.GENERAL,standardSwitcher("D")); + print(GroupType.VIP,standardSwitcher("D")); + print(GroupType.VVIP,standardSwitcher("D")); + } + + private void summaryByName() { + while ( true ) { + try { + System.out.println("Which order (ASCENDING (A), DESCENDING (D))?\n"); + String str = nextLine("END"); + if (str.equals("A") || str.equals("ASCENDING")) { + switcher = 1; + printNone(standardSwitcher("N")); + print(GroupType.GENERAL,standardSwitcher("N")); + print(GroupType.VIP,standardSwitcher("N")); + print(GroupType.VVIP,standardSwitcher("N")); + } + if (str.equals("D") || str.equals("DESCENDING")) { + switcher = -1; + printNone(standardSwitcher("N")); + print(GroupType.GENERAL,standardSwitcher("N")); + print(GroupType.VIP,standardSwitcher("N")); + print(GroupType.VVIP,standardSwitcher("N")); + } + } catch (InputEndException e) { + System.out.println(ERR_MSG_INPUT_END); + break; + } + } + } + + private void summaryByTime () { + while ( true ) { + try { + System.out.println("Which order (ASCENDING (A), DESCENDING (D))?\n"); + String str = nextLine("END"); + if (str.equals("A") || str.equals("ASCENDING")) { + switcher = 1; + printNone(standardSwitcher("T")); + print(GroupType.GENERAL,standardSwitcher("T")); + print(GroupType.VIP,standardSwitcher("T")); + print(GroupType.VVIP,standardSwitcher("T")); + } + if (str.equals("D") || str.equals("DESCENDING")) { + switcher = -1; + printNone(standardSwitcher("T")); + print(GroupType.GENERAL,standardSwitcher("T")); + print(GroupType.VIP,standardSwitcher("T")); + print(GroupType.VVIP,standardSwitcher("T")); + } + } catch (InputEndException e) { + System.out.println(ERR_MSG_INPUT_END); + break; + } + } + } + private void summaryByPay () { + while ( true ) { + try { + System.out.println("Which order (ASCENDING (A), DESCENDING (D))?\n"); + String str = nextLine("END"); + if (str.equals("A") || str.equals("ASCENDING")) { + switcher = 1; + printNone(standardSwitcher("P")); + print(GroupType.GENERAL,standardSwitcher("P")); + print(GroupType.VIP,standardSwitcher("P")); + print(GroupType.VVIP,standardSwitcher("P")); + } + if (str.equals("D") || str.equals("DESCENDING")) { + switcher = -1; + printNone(standardSwitcher("P")); + print(GroupType.GENERAL,standardSwitcher("P")); + print(GroupType.VIP,standardSwitcher("P")); + print(GroupType.VVIP,standardSwitcher("P")); + } + } catch (InputEndException e) { + System.out.println(ERR_MSG_INPUT_END); + break; + } + } + } +} diff --git a/src/me/smartstore/util/Message.java b/src/me/smartstore/util/Message.java new file mode 100644 index 00000000..6ab7c942 --- /dev/null +++ b/src/me/smartstore/util/Message.java @@ -0,0 +1,13 @@ +package me.smartstore.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"; +}