diff --git a/src/me/smartstore/Main.java b/src/me/smartstore/Main.java new file mode 100644 index 00000000..ba2d4eae --- /dev/null +++ b/src/me/smartstore/Main.java @@ -0,0 +1,8 @@ +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..5a826143 --- /dev/null +++ b/src/me/smartstore/SmartStoreApp.java @@ -0,0 +1,43 @@ +package me.smartstore; + +import me.smartstore.customer.Customer; +import me.smartstore.customer.Customers; +import me.smartstore.group.Group; +import me.smartstore.group.GroupType; +import me.smartstore.group.Groups; +import me.smartstore.group.Parameter; +import me.smartstore.menu.*; + +public class SmartStoreApp { + private final Groups allGroups = Groups.getInstance(); + private final Customers allCustomers = Customers.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.15"); + System.out.println(" Copyright 2023 HaesolChoi All rights reserved."); + System.out.println("===========================================\n"); + } + + + public void run() { + details(); + mainMenu.manage(); + + } +} diff --git a/src/me/smartstore/arrays/Collections.java b/src/me/smartstore/arrays/Collections.java new file mode 100644 index 00000000..536be0a1 --- /dev/null +++ b/src/me/smartstore/arrays/Collections.java @@ -0,0 +1,16 @@ +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); +} \ No newline at end of file diff --git a/src/me/smartstore/arrays/DArray.java b/src/me/smartstore/arrays/DArray.java new file mode 100644 index 00000000..d6ab7f1b --- /dev/null +++ b/src/me/smartstore/arrays/DArray.java @@ -0,0 +1,155 @@ +package me.smartstore.arrays; + +import me.smartstore.exception.EmptyArrayException; +import me.smartstore.exception.ElementNotFoundException; +import me.smartstore.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; + } + + ///////////////////////////////////////// + // 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]; + } + + /** + * @param: ... + * @return: ... + * @throws: IndexOutOfBoundsException + * @throws: NullArgumentException + * */ + @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/src/me/smartstore/customer/Customer.java b/src/me/smartstore/customer/Customer.java new file mode 100644 index 00000000..de67f96d --- /dev/null +++ b/src/me/smartstore/customer/Customer.java @@ -0,0 +1,96 @@ +package me.smartstore.customer; + +import me.smartstore.group.Group; +import java.util.Objects; + + +public class Customer implements Comparable { + private String userId; + private String name; + private Integer spentTime; + private Integer totalPay; + private Group group; + + public Customer() { + } + + public Customer(String name, String userId, int spentTime, int totalPay) { + this.name = name; + this.userId = userId; + this.spentTime = Integer.valueOf(spentTime); + this.totalPay = Integer.valueOf(totalPay); + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public String getUserId() { + return this.userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public Integer getSpentTime() { + return this.spentTime; + } + + public void setSpentTime(Integer spentTime) { + this.spentTime = spentTime; + } + + public Integer getTotalPay() { + return this.totalPay; + } + + public void setTotalPay(Integer totalPay) { + this.totalPay = totalPay; + } + + public Group getGroup() { + return this.group; + } + + public void setGroup(Group group) { + this.group = group; + } + + @Override + public int compareTo(Customer o) { + if (this.name.compareTo(o.name) < 0) + return -1; + if (this.name.compareTo(o.name) == 0) + return this.userId.compareTo(o.userId); + return 1; + } + + @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(userId, customer.userId); + } + + @Override + public int hashCode() { + return Objects.hash(userId); + } + + @Override + public String toString() { + return "Customer{" + + "userId='" + userId + '\'' + + ", name='" + name + '\'' + + ", spentTime=" + spentTime + + ", totalPay=" + totalPay + + ", group=" + group + + '}'; + } +} diff --git a/src/me/smartstore/customer/Customers.java b/src/me/smartstore/customer/Customers.java new file mode 100644 index 00000000..eee93cdf --- /dev/null +++ b/src/me/smartstore/customer/Customers.java @@ -0,0 +1,97 @@ +package me.smartstore.customer; + +import me.smartstore.arrays.DArray; +import me.smartstore.group.GroupType; +import me.smartstore.group.Groups; +import me.smartstore.group.Group; +import me.smartstore.group.Parameter; +import me.smartstore.customer.Customer; +import me.smartstore.util.Util; + + +import static me.smartstore.group.Groups.allGroups; + +public class Customers extends DArray { + + // singleton + private static Customers allCustomers; + + public static Customers getInstance() { + if (allCustomers == null) { + allCustomers = new Customers(); + } + return allCustomers; + } + + private final Groups allGroups = Groups.getInstance(); + + private Customers() {} + + public void setCustomers(Customer[] customers) { + this.arrays = customers; + capacity = customers.length; + size = customers.length; + } + + public Customer[] getCustomers() { + return arrays; + } + + + public Customers findCustomers(GroupType type) { + Customers custs = new Customers(); + + for(int i = 0; i < size; ++i) { + Customer cust = get(i); + if (cust == null) return null; + + Group grp = cust.getGroup(); + if (type == GroupType.NONE) { // Customer Group == null => + if (grp == null || grp.getGroupType() == null || grp.getGroupType() == GroupType.NONE) { + custs.add(cust); + } + } else if (grp != null && grp.getGroupType() == type) { + custs.add(cust); + } + + } + + return custs; + } + + public Customers findCustomers(Group grp) { + if (grp != null) { + if (grp.getGroupType() != null) { + return findCustomers(grp.getGroupType()); + } else { + System.out.println("Customers.findCustomers() Error : No group type."); + return null; + } + } else { + System.out.println("Customers.findCustomers() Error : No group."); + return null; + } + } + + // refresh 함수가 호출되는 경우 + // 1. 분류기준 바뀔 때 + // 2. 새로운 고객이 들어올 때 + public void refresh(Groups groups) { + if (groups == null) return; + + for (int i = 0; i < size; i++) { + Customer cust = arrays[i]; + Group grp = findGroupFor(cust); + if (grp == null) { + grp = groups.find(GroupType.NONE); + } + cust.setGroup(grp); + } + + } + + public Group findGroupFor(Customer cust) { + return cust.getGroup(); + } + +} diff --git a/src/me/smartstore/exception/ArrayEmptyException.java b/src/me/smartstore/exception/ArrayEmptyException.java new file mode 100644 index 00000000..13cf8787 --- /dev/null +++ b/src/me/smartstore/exception/ArrayEmptyException.java @@ -0,0 +1,14 @@ +package me.smartstore.exception; + +import me.smartstore.util.Message; + +public class ArrayEmptyException extends RuntimeException { + + public ArrayEmptyException() { + super(Message.ERR_MSG_INVALID_ARR_EMPTY.getMessage()); + } + + public ArrayEmptyException(String message) { + super(message); + } +} diff --git a/src/me/smartstore/exception/ElementNotFoundException.java b/src/me/smartstore/exception/ElementNotFoundException.java new file mode 100644 index 00000000..b72cbc84 --- /dev/null +++ b/src/me/smartstore/exception/ElementNotFoundException.java @@ -0,0 +1,10 @@ +package me.smartstore.exception; + +public class ElementNotFoundException extends RuntimeException { + public ElementNotFoundException() { + } + + public ElementNotFoundException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/me/smartstore/exception/EmptyArrayException.java b/src/me/smartstore/exception/EmptyArrayException.java new file mode 100644 index 00000000..812bb15d --- /dev/null +++ b/src/me/smartstore/exception/EmptyArrayException.java @@ -0,0 +1,10 @@ +package me.smartstore.exception; + +public class EmptyArrayException extends RuntimeException { + public EmptyArrayException() { + } + + public EmptyArrayException(String message) { + super(message); + } +} diff --git a/src/me/smartstore/exception/InputEmptyException.java b/src/me/smartstore/exception/InputEmptyException.java new file mode 100644 index 00000000..58861400 --- /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.getMessage()); + } + + public InputEmptyException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/me/smartstore/exception/InputEndException.java b/src/me/smartstore/exception/InputEndException.java new file mode 100644 index 00000000..68611dec --- /dev/null +++ b/src/me/smartstore/exception/InputEndException.java @@ -0,0 +1,13 @@ +package me.smartstore.exception; + +import me.smartstore.util.Message; + +public class InputEndException extends RuntimeException { + public InputEndException() { + super(Message.ERR_MSG_INPUT_END.getMessage()); + } + + public InputEndException(String message) { + super(message); + } +} diff --git a/src/me/smartstore/exception/InputFormatException.java b/src/me/smartstore/exception/InputFormatException.java new file mode 100644 index 00000000..1ca8e589 --- /dev/null +++ b/src/me/smartstore/exception/InputFormatException.java @@ -0,0 +1,13 @@ +package me.smartstore.exception; + +import me.smartstore.util.Message; + +public class InputFormatException extends RuntimeException { + public InputFormatException() { + super(Message.ERR_MSG_INVALID_INPUT_FORMAT.getMessage()); + } + + public InputFormatException(String message) { + super(message); + } +} diff --git a/src/me/smartstore/exception/InputRangeException.java b/src/me/smartstore/exception/InputRangeException.java new file mode 100644 index 00000000..739fe8f1 --- /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.getMessage()); + } + + public InputRangeException(String message) { + super(message); + } +} diff --git a/src/me/smartstore/exception/InputTypeException.java b/src/me/smartstore/exception/InputTypeException.java new file mode 100644 index 00000000..ff1d543b --- /dev/null +++ b/src/me/smartstore/exception/InputTypeException.java @@ -0,0 +1,13 @@ +package me.smartstore.exception; + +import me.smartstore.util.Message; + +public class InputTypeException extends RuntimeException { + public InputTypeException() { + super(Message.ERR_MSG_INVALID_INPUT_TYPE.getMessage()); + } + + public InputTypeException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/me/smartstore/exception/NullArgumentException.java b/src/me/smartstore/exception/NullArgumentException.java new file mode 100644 index 00000000..e8b87b1d --- /dev/null +++ b/src/me/smartstore/exception/NullArgumentException.java @@ -0,0 +1,10 @@ +package me.smartstore.exception; + +public class NullArgumentException extends RuntimeException { + public NullArgumentException() { + } + + public NullArgumentException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/me/smartstore/group/Group.java b/src/me/smartstore/group/Group.java new file mode 100644 index 00000000..a27bcafd --- /dev/null +++ b/src/me/smartstore/group/Group.java @@ -0,0 +1,70 @@ +package me.smartstore.group; + +import me.smartstore.customer.Customers; +import me.smartstore.group.GroupType; +import me.smartstore.group.Parameter; + +import java.util.Objects; + +public class Group { + private GroupType groupType; + private Parameter parameter; + + public Group() { + this(null, null); + } + + public Group(GroupType groupType, Parameter parameter) { + this.groupType = groupType; + this.parameter = parameter; + } + + public GroupType getGroupType() { + return groupType; + } + + public void getGroupType(GroupType groupType) { + this.groupType = groupType; + } + + public Parameter getParameter() { + return parameter; + } + + public void setParameter(Parameter param) { + this.parameter = param; + } + + public Customers getCustomers(Customers allCustomers) { + return allCustomers.findCustomers(this.getGroupType()); + } + + public void update(GroupType groupType, Parameter parameter) { + this.groupType = groupType; + this.parameter = parameter; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Group group = (Group) o; + return groupType == group.groupType && Objects.equals(parameter, group.parameter); + } + + @Override + public int hashCode() { + return Objects.hash(groupType, parameter); + } + + @Override + public String toString() { + if (groupType == null) { + return "No group."; + } else if (parameter == null) { + return "GroupType: " + groupType + "\nParameter: null"; + } else { + return "GroupType: " + groupType + "\nParameter: " + parameter; + } + } +} \ No newline at end of file diff --git a/src/me/smartstore/group/GroupType.java b/src/me/smartstore/group/GroupType.java new file mode 100644 index 00000000..d6ccca37 --- /dev/null +++ b/src/me/smartstore/group/GroupType.java @@ -0,0 +1,22 @@ +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..6a0c9f98 --- /dev/null +++ b/src/me/smartstore/group/Groups.java @@ -0,0 +1,36 @@ +package me.smartstore.group; + +import me.smartstore.arrays.DArray; + +public class Groups extends DArray { + // singleton + public 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++) { + Group group = allGroups.get(i); + if (group.getGroupType() == groupType) { + return group; + } + } + return null; + } + + public void update(Group group) { + Group grp = find(group.getGroupType()); + if (grp != null) { + grp.setParameter(group.getParameter()); + } + + } +} diff --git a/src/me/smartstore/group/Parameter.java b/src/me/smartstore/group/Parameter.java new file mode 100644 index 00000000..9b711f41 --- /dev/null +++ b/src/me/smartstore/group/Parameter.java @@ -0,0 +1,54 @@ +package me.smartstore.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 Objects.equals(minTime, parameter.minTime) && Objects.equals(minPay, parameter.minPay); + } + + @Override + public int hashCode() { + return Objects.hash(minTime, minPay); + } + + @Override + public String toString() { + return "Parameter{" + + "minTime=" + minTime + + ", minPay=" + minPay + + '}'; + } +} diff --git a/src/me/smartstore/menu/CustomerMenu.java b/src/me/smartstore/menu/CustomerMenu.java new file mode 100644 index 00000000..0c86da91 --- /dev/null +++ b/src/me/smartstore/menu/CustomerMenu.java @@ -0,0 +1,27 @@ +package me.smartstore.menu; + +public class CustomerMenu implements Menu { + // 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"}); + } + } +} diff --git a/src/me/smartstore/menu/GroupMenu.java b/src/me/smartstore/menu/GroupMenu.java new file mode 100644 index 00000000..1016a072 --- /dev/null +++ b/src/me/smartstore/menu/GroupMenu.java @@ -0,0 +1,223 @@ +package me.smartstore.menu; + +import me.smartstore.customer.Customers; +import me.smartstore.exception.InputEmptyException; +import me.smartstore.exception.InputEndException; +import me.smartstore.exception.InputFormatException; +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; +import me.smartstore.util.Util; + +public class GroupMenu implements Menu { + + ///////////////////////////////////////// + ////////////// singleton //////////////// + private static GroupMenu groupMenu; + + public static GroupMenu getInstance() { + if (groupMenu == null) { + groupMenu = new GroupMenu(); + } + return groupMenu; + } + ///////////////////////////////////////// + ///////////////////////////////////////// + + + Groups allGroups = Groups.getInstance(); + Customers allCustomers = Customers.getInstance(); + + + public String chooseGroup() { + while ( true ) { + try { + System.out.print("Which group (GENERAL (G), VIP (V), VVIP (VV))? "); + String choice = nextLine(Message.END_MSG); + + if (choice.equals("")) throw new InputEmptyException(); + return choice; + + } catch (InputEmptyException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_NULL); + } catch (IllegalArgumentException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + return null; + } + } + } + + @Override + public void manage() { + while ( true ) { + 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 if (choice == 4) { + break; + } + } + } + + public void setParameter() { + while ( true ) { + String strGroup = chooseGroup(); // "V", "VIP" (string) => GroupType.VIP + if (strGroup == null) return; + + GroupType groupType; + try { + groupType = GroupType.valueOf(strGroup).replaceFullName(); + } catch (IllegalArgumentException e) { + System.out.println("\n" + Message.ERR_MSG_INVALID_INPUT_RANGE); + continue; + } + + Group grp = allGroups.find(groupType); + if (grp != null && grp.getParameter() != null) { + System.out.println("\n" + grp.getGroupType() + " group already exists."); + System.out.println("\n" + grp); + } else { + Parameter param = new Parameter(); + + while ( true ) { + int choice = chooseMenu(new String[]{ + "Minimum Spent Time", + "Minimum Total Pay", + "Back"}); + if (choice == 1) { + setParameterMinimumSpentTime(param); + } else if (choice == 2) { + setParameterMinimumTotalPay(param); + } else if (choice == 3) break; +// else System.out.println("\n" + Message.ERR_MSG_INVALID_INPUT_RANGE); + } + + if (Util.isAllNonNUll(param, param.getMinTime(), param.getMinPay())) { + Group newGrp = allGroups.find(groupType); + newGrp.setParameter(param); + allCustomers.refresh(allGroups); + System.out.println("\n" + newGrp); + } else { + System.out.println("No parameter is added. Please fill out all information."); + } + } + } + } + + public void viewParameter() { + while ( true ) { + String strGroup = chooseGroup(); + if (strGroup == null) return; + + GroupType groupType; + try { + groupType = GroupType.valueOf(strGroup).replaceFullName(); + } catch (IllegalArgumentException e) { + System.out.println("\n" + Message.ERR_MSG_INVALID_INPUT_TYPE); + continue; + } + + Group grp = allGroups.find(groupType); + System.out.println(); + System.out.println(grp); + } + } + + public void updateParameter() { + while ( true ) { + String strGroup = chooseGroup(); + if (strGroup == null) return; + + GroupType groupType; + try { + groupType = GroupType.valueOf(strGroup).replaceFullName(); + } catch (IllegalArgumentException e) { + System.out.println("\n" + Message.ERR_MSG_INVALID_INPUT_RANGE); + return; + } + + Group grp = allGroups.find(groupType); + if (grp == null || grp.getParameter() == null) { + System.out.println("\nNo parameter. Set the parameter first."); + return; + } + + System.out.println("\n" + grp); + Parameter param = grp.getParameter(); + + while ( true ) { + int choice = chooseMenu(new String[]{ + "Minimum Spent Time", + "Minimum Total Pay", + "Back"}); + if (choice == 1) { + setParameterMinimumSpentTime(param); + } else if (choice == 2) { + setParameterMinimumTotalPay(param); + } else if (choice == 3) { + break; + } + } + + allGroups.update(new Group(groupType, param)); + allCustomers.refresh(allGroups); + System.out.println("\n" + grp); + + } + } + + public void setParameterMinimumSpentTime(Parameter param) { + while ( true ) { + try { + System.out.print("\nInput Minimum Spent Time: "); + int minimumSpentTime = Integer.parseInt(nextLine(Message.END_MSG)); + if (minimumSpentTime < 0) throw new InputRangeException(); + + param.setMinTime(minimumSpentTime); + return; + } catch (InputFormatException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_TYPE); + } catch (InputRangeException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + return; + } + } + } + + + public void setParameterMinimumTotalPay(Parameter param) { + + while ( true ) { + try { + System.out.print("\nInput Minimum Total Pay: "); + int minimumTotalPay = Integer.parseInt(nextLine(Message.END_MSG)); + if (minimumTotalPay < 0) throw new InputRangeException(); + param.setMinPay(minimumTotalPay); + return; + } catch (InputFormatException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_TYPE); + } catch (InputRangeException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + return; + } + } + } +} diff --git a/src/me/smartstore/menu/MainMenu.java b/src/me/smartstore/menu/MainMenu.java new file mode 100644 index 00000000..a7768657 --- /dev/null +++ b/src/me/smartstore/menu/MainMenu.java @@ -0,0 +1,39 @@ +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(); + + // singleton + 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..4c907be4 --- /dev/null +++ b/src/me/smartstore/menu/Menu.java @@ -0,0 +1,48 @@ +package me.smartstore.menu; + +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(Message 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); + + } + } + } + + void manage(); // 각 서브메뉴들을 관리하는 함수 (각 서브메뉴의 최상위 메뉴) +} \ No newline at end of file diff --git a/src/me/smartstore/menu/SummaryMenu.java b/src/me/smartstore/menu/SummaryMenu.java new file mode 100644 index 00000000..b86abb9a --- /dev/null +++ b/src/me/smartstore/menu/SummaryMenu.java @@ -0,0 +1,27 @@ +package me.smartstore.menu; + +public class SummaryMenu implements Menu { + // 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"}); + } + } +} \ No newline at end of file diff --git a/src/me/smartstore/util/Message.java b/src/me/smartstore/util/Message.java new file mode 100644 index 00000000..25989f93 --- /dev/null +++ b/src/me/smartstore/util/Message.java @@ -0,0 +1,25 @@ +package me.smartstore.util; + + +public enum Message { + ERR_MSG_INVALID_ARR_EMPTY("No Customers. Please input one first."), + ERR_MSG_NULL_ARR_ELEMENT("Elements in Array has null. Array can't be sorted."), + ERR_MSG_INVALID_INPUT_NULL("Null Input. Please input something."), + ERR_MSG_INVALID_INPUT_EMPTY("Empty Input. Please input something."), + ERR_MSG_INVALID_INPUT_RANGE("Invalid Input. Please try again."), + ERR_MSG_INVALID_INPUT_TYPE("Invalid Type for Input. Please try again."), + ERR_MSG_INVALID_INPUT_FORMAT("Invalid Format for Input. Please try again."), + ERR_MSG_INPUT_END("END is pressed. Exit this menu."), + END_MSG("END"); + + + private final String message; + + Message(String message) { + this.message = message; + } + + public String getMessage() { + return this.message; + } +} \ No newline at end of file diff --git a/src/me/smartstore/util/Util.java b/src/me/smartstore/util/Util.java new file mode 100644 index 00000000..f3805042 --- /dev/null +++ b/src/me/smartstore/util/Util.java @@ -0,0 +1,22 @@ +package me.smartstore.util; + +import java.util.Arrays; +import java.util.Objects; + +public class Util { + public static boolean isAnyNUll(Object... objects) { + return Arrays.stream(objects).anyMatch(Objects::isNull); + } + + public static boolean isAnyNonNUll(Object... objects) { + return Arrays.stream(objects).anyMatch(Objects::nonNull); + } + + public static boolean isAllNUll(Object... objects) { + return Arrays.stream(objects).allMatch(Objects::isNull); + } + + public static boolean isAllNonNUll(Object... objects) { + return Arrays.stream(objects).allMatch(Objects::nonNull); + } +}