diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..13566b81 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/out/production/KDTBE5_Java_ToyProject/me/smartstore/Main.class b/out/production/KDTBE5_Java_ToyProject/me/smartstore/Main.class new file mode 100644 index 00000000..512de613 Binary files /dev/null and b/out/production/KDTBE5_Java_ToyProject/me/smartstore/Main.class differ diff --git a/out/production/KDTBE5_Java_ToyProject/me/smartstore/SmartStoreApp.class b/out/production/KDTBE5_Java_ToyProject/me/smartstore/SmartStoreApp.class new file mode 100644 index 00000000..91243ea7 Binary files /dev/null and b/out/production/KDTBE5_Java_ToyProject/me/smartstore/SmartStoreApp.class differ diff --git a/out/production/KDTBE5_Java_ToyProject/me/smartstore/arrays/Collections.class b/out/production/KDTBE5_Java_ToyProject/me/smartstore/arrays/Collections.class new file mode 100644 index 00000000..c3eded02 Binary files /dev/null and b/out/production/KDTBE5_Java_ToyProject/me/smartstore/arrays/Collections.class differ diff --git a/out/production/KDTBE5_Java_ToyProject/me/smartstore/arrays/DArray.class b/out/production/KDTBE5_Java_ToyProject/me/smartstore/arrays/DArray.class new file mode 100644 index 00000000..45bfe470 Binary files /dev/null and b/out/production/KDTBE5_Java_ToyProject/me/smartstore/arrays/DArray.class differ diff --git a/out/production/KDTBE5_Java_ToyProject/me/smartstore/customer/Customer.class b/out/production/KDTBE5_Java_ToyProject/me/smartstore/customer/Customer.class new file mode 100644 index 00000000..3b81409c Binary files /dev/null and b/out/production/KDTBE5_Java_ToyProject/me/smartstore/customer/Customer.class differ diff --git a/out/production/KDTBE5_Java_ToyProject/me/smartstore/customer/Customers.class b/out/production/KDTBE5_Java_ToyProject/me/smartstore/customer/Customers.class new file mode 100644 index 00000000..b162c7df Binary files /dev/null and b/out/production/KDTBE5_Java_ToyProject/me/smartstore/customer/Customers.class differ diff --git a/out/production/KDTBE5_Java_ToyProject/me/smartstore/exception/ElementNotFoundException.class b/out/production/KDTBE5_Java_ToyProject/me/smartstore/exception/ElementNotFoundException.class new file mode 100644 index 00000000..d5d73535 Binary files /dev/null and b/out/production/KDTBE5_Java_ToyProject/me/smartstore/exception/ElementNotFoundException.class differ diff --git a/out/production/KDTBE5_Java_ToyProject/me/smartstore/exception/EmptyArrayException.class b/out/production/KDTBE5_Java_ToyProject/me/smartstore/exception/EmptyArrayException.class new file mode 100644 index 00000000..246e2e5b Binary files /dev/null and b/out/production/KDTBE5_Java_ToyProject/me/smartstore/exception/EmptyArrayException.class differ diff --git a/out/production/KDTBE5_Java_ToyProject/me/smartstore/exception/InputEndException.class b/out/production/KDTBE5_Java_ToyProject/me/smartstore/exception/InputEndException.class new file mode 100644 index 00000000..4bcaee2b Binary files /dev/null and b/out/production/KDTBE5_Java_ToyProject/me/smartstore/exception/InputEndException.class differ diff --git a/out/production/KDTBE5_Java_ToyProject/me/smartstore/exception/InputRangeException.class b/out/production/KDTBE5_Java_ToyProject/me/smartstore/exception/InputRangeException.class new file mode 100644 index 00000000..268b8484 Binary files /dev/null and b/out/production/KDTBE5_Java_ToyProject/me/smartstore/exception/InputRangeException.class differ diff --git a/out/production/KDTBE5_Java_ToyProject/me/smartstore/exception/NullArgumentException.class b/out/production/KDTBE5_Java_ToyProject/me/smartstore/exception/NullArgumentException.class new file mode 100644 index 00000000..ab7c5d0e Binary files /dev/null and b/out/production/KDTBE5_Java_ToyProject/me/smartstore/exception/NullArgumentException.class differ diff --git a/out/production/KDTBE5_Java_ToyProject/me/smartstore/group/Group.class b/out/production/KDTBE5_Java_ToyProject/me/smartstore/group/Group.class new file mode 100644 index 00000000..cf2374a5 Binary files /dev/null and b/out/production/KDTBE5_Java_ToyProject/me/smartstore/group/Group.class differ diff --git a/out/production/KDTBE5_Java_ToyProject/me/smartstore/group/GroupType.class b/out/production/KDTBE5_Java_ToyProject/me/smartstore/group/GroupType.class new file mode 100644 index 00000000..c9534c81 Binary files /dev/null and b/out/production/KDTBE5_Java_ToyProject/me/smartstore/group/GroupType.class differ diff --git a/out/production/KDTBE5_Java_ToyProject/me/smartstore/group/Groups.class b/out/production/KDTBE5_Java_ToyProject/me/smartstore/group/Groups.class new file mode 100644 index 00000000..2d2b245d Binary files /dev/null and b/out/production/KDTBE5_Java_ToyProject/me/smartstore/group/Groups.class differ diff --git a/out/production/KDTBE5_Java_ToyProject/me/smartstore/group/Parameter.class b/out/production/KDTBE5_Java_ToyProject/me/smartstore/group/Parameter.class new file mode 100644 index 00000000..c6517b58 Binary files /dev/null and b/out/production/KDTBE5_Java_ToyProject/me/smartstore/group/Parameter.class differ diff --git a/out/production/KDTBE5_Java_ToyProject/me/smartstore/menu/GroupMenu.class b/out/production/KDTBE5_Java_ToyProject/me/smartstore/menu/GroupMenu.class new file mode 100644 index 00000000..1d92cfe0 Binary files /dev/null and b/out/production/KDTBE5_Java_ToyProject/me/smartstore/menu/GroupMenu.class differ diff --git a/out/production/KDTBE5_Java_ToyProject/me/smartstore/menu/MainMenu.class b/out/production/KDTBE5_Java_ToyProject/me/smartstore/menu/MainMenu.class new file mode 100644 index 00000000..bb71cdd5 Binary files /dev/null and b/out/production/KDTBE5_Java_ToyProject/me/smartstore/menu/MainMenu.class differ diff --git a/out/production/KDTBE5_Java_ToyProject/me/smartstore/menu/Menu.class b/out/production/KDTBE5_Java_ToyProject/me/smartstore/menu/Menu.class new file mode 100644 index 00000000..101b4ce1 Binary files /dev/null and b/out/production/KDTBE5_Java_ToyProject/me/smartstore/menu/Menu.class differ diff --git a/out/production/KDTBE5_Java_ToyProject/me/smartstore/menu/SummaryMenu.class b/out/production/KDTBE5_Java_ToyProject/me/smartstore/menu/SummaryMenu.class new file mode 100644 index 00000000..d3714259 Binary files /dev/null and b/out/production/KDTBE5_Java_ToyProject/me/smartstore/menu/SummaryMenu.class differ diff --git a/out/production/KDTBE5_Java_ToyProject/me/smartstore/util/Message.class b/out/production/KDTBE5_Java_ToyProject/me/smartstore/util/Message.class new file mode 100644 index 00000000..8e8789db Binary files /dev/null and b/out/production/KDTBE5_Java_ToyProject/me/smartstore/util/Message.class differ diff --git a/src/me/smartstore/Main.java b/src/me/smartstore/Main.java new file mode 100644 index 00000000..e254be45 --- /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().test() .run(); + SmartStoreApp.getInstance().run(); + } +} diff --git a/src/me/smartstore/SmartStoreApp.java b/src/me/smartstore/SmartStoreApp.java new file mode 100644 index 00000000..ff589c03 --- /dev/null +++ b/src/me/smartstore/SmartStoreApp.java @@ -0,0 +1,70 @@ +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.MainMenu; + + +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.04.27"); + System.out.println(" Copyright 2023 Eunbin All rights reserved."); + System.out.println("===========================================\n"); + } + + + public SmartStoreApp test() { + 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++) { + allCustomers.add(new Customer( + Character.toString( + (char) ('a' + i)), + (char) ('a' + i) + "123", + ((int) (Math.random() * 5) + 1) * 10, + ((int) (Math.random() * 5) + 1) * 100000)); + } + + allCustomers.add(new Customer("test1","test1",9,90000)); + allCustomers.add(new Customer("test2","test2",9,3100000)); + allCustomers.refresh(); + + System.out.println("allCustomers = " + allCustomers); + System.out.println("allGroups = " + allGroups); + + + return this; // smartStoreApp + } + + 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..264adcd3 --- /dev/null +++ b/src/me/smartstore/arrays/Collections.java @@ -0,0 +1,15 @@ +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..9e6b51ff --- /dev/null +++ b/src/me/smartstore/arrays/DArray.java @@ -0,0 +1,150 @@ +package me.smartstore.arrays; + +import me.smartstore.exception.ElementNotFoundException; +import me.smartstore.exception.EmptyArrayException; +import me.smartstore.exception.NullArgumentException; + +public class DArray implements Collections{ + + protected static final int DEFAULT = 10; + + protected T[] arrays; + 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; + } + + 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, NullPointerException{ + if (index<0 || index>=size) throw new IndexOutOfBoundsException(); + if (object == null) throw new NullPointerException(); + + arrays[index] = object; + } + + @Override + public int indexOf(T object) throws NullPointerException, ElementNotFoundException { + if (object == null) throw new NullPointerException(); + + for (int i = 0; i < size; i++) { + if (arrays[i] == null) continue; + if (arrays[i].equals(object)) return i; + } + throw new ElementNotFoundException(); + } + + @Override + public void add(T object) throws NullPointerException{ + if(object == null) throw new NullPointerException(); + + 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..7947f94d --- /dev/null +++ b/src/me/smartstore/customer/Customer.java @@ -0,0 +1,98 @@ +package me.smartstore.customer; + +import me.smartstore.group.Group; + +import java.util.Objects; + +public class Customer{ + + private String customerName; + private String customerId; + private int customerTotalTime; + private int customerTotalPay; + private Group group; + + public Customer(){ + + } + + public Customer(String customerName) { + this.customerName = customerName; + } + + public Customer(String customerName, String customerId) { + this.customerName = customerName; + this.customerId = customerId; + } + + public Customer(String customerName, String customerId, int customerTotalTime, int customerTotalPay) { + this.customerName = customerName; + this.customerId = customerId; + this.customerTotalTime = customerTotalTime; + this.customerTotalPay = customerTotalPay; + } + + 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 int getCustomerTotalTime() { + return customerTotalTime; + } + + public void setCustomerTotalTime(int customerTotalTime) { + this.customerTotalTime = customerTotalTime; + } + + public int getCustomerTotalPay() { + return customerTotalPay; + } + + public void setCustomerTotalPay(int customerTotalPay) { + this.customerTotalPay = customerTotalPay; + } + + 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 Objects.equals(customerId, customer.customerId); + } + + @Override + public int hashCode() { + return Objects.hash(customerId); + } + + @Override + public String toString() { + return "Customer{" + + "customerName='" + customerName + '\'' + + ", customerId='" + customerId + '\'' + + ", customerTotalTime=" + customerTotalTime + + ", customerTotalPay=" + customerTotalPay + + ", group=GroupType: " + group + + '}'; + } +} diff --git a/src/me/smartstore/customer/Customers.java b/src/me/smartstore/customer/Customers.java new file mode 100644 index 00000000..ebb038b5 --- /dev/null +++ b/src/me/smartstore/customer/Customers.java @@ -0,0 +1,75 @@ +package me.smartstore.customer; + +import me.smartstore.arrays.DArray; +import me.smartstore.group.Group; +import me.smartstore.group.GroupType; +import me.smartstore.group.Groups; + +import java.lang.reflect.Parameter; + +public class Customers extends DArray { + private final Groups allGroups = Groups.getInstance(); + // singleton + private static Customers allCustomers; + private Customers(){} + + public static Customers getInstance(){ + if(allCustomers == null){ + allCustomers = new Customers(); + } + return allCustomers; + } + + + public Customer[] getCustomers(){ + Customer[] customers = new Customer[allCustomers.size()]; + for (int i = 0; i < allCustomers.size(); i++) { + customers[i] = allCustomers.get(i); + } + return customers; + } + + + + + + public void refresh(){ + if (allGroups.size() != 4){ + System.out.println("you need to set all Parameter.\n"+ (4-allGroups.size())+" more left"); + } + int noneNum = 0; + int generalNum = 0; + int vipNum = 0; + int vvipNum = 0; + + for (int i = 0; i < allGroups.size(); i++) { + if (allGroups.get(i).getGroupType().equals(GroupType.GENERAL)){ + generalNum = i; + } else if (allGroups.get(i).getGroupType().equals(GroupType.VIP)) { + vipNum = i; + } else if (allGroups.get(i).getGroupType().equals(GroupType.VVIP)) { + vvipNum = i; + }else{ + noneNum = i; + } + } + + + for (int i = 0; i < allCustomers.size; i++) { + if ((allCustomers.get(i).getCustomerTotalTime() { + //singleton + private static Groups allGroups; + + private Groups() { + } + + public static Groups getInstance() { + if (allGroups == null) { + allGroups = new Groups(); + } + return allGroups; + } + + public Group find(GroupType groupType) { + for (int i = 0; i < this.size; i++) { + if (this.get(i).getGroupType() == groupType) { + return this.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..4aab14c7 --- /dev/null +++ b/src/me/smartstore/group/Parameter.java @@ -0,0 +1,55 @@ +package me.smartstore.group; + +import java.security.PrivilegedAction; +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/GroupMenu.java b/src/me/smartstore/menu/GroupMenu.java new file mode 100644 index 00000000..368abb8f --- /dev/null +++ b/src/me/smartstore/menu/GroupMenu.java @@ -0,0 +1,202 @@ +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() {} + + @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.println("Which group (GENERAL (G), VIP (V), VVIP (VV))? "); + String choice = nextLine(Message.END_MSG); + // group (str) -> GroupType (enum) + // "VIP" -> GroupType.VIP + + 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){ //chooseGroup에서 end입력되면 null리턴함 + allCustomers.refresh(); // 파라미터가 변경되었거나 추가되는 경우, 고객 분류를 다시 해야함 + 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(); + // time, pay 사용자 입력받은 후, 설정 필요 + while( true ){ + int choice = chooseMenu(new String[]{ + "Minimum Spent Time", + "Minimum Total Pay", + "Back"}); + if (choice == 1) { + parameter.setMinTime(inputMinimumSpentTime()); + } + else if (choice == 2) { + parameter.setMinPay(inputMinimumTotalPay()); + } + else{ + allGroups.add(new Group(parameter, groupType)); + group = allGroups.find(groupType); + System.out.println("\nGroupType: " + group.getGroupType()); + System.out.println("Parameter: " + group.getParameter()); + allCustomers.refresh(); + break; + } + } + + } + } + } + + public void viewParameter() { + while ( true ){ + try { + GroupType groupType = chooseGroup(); + if (groupType == null) break; + + Group group = allGroups.find(groupType); + System.out.println("GroupType: " + group.getGroupType()); + System.out.println("Parameter: " + group.getParameter()); + } catch (NullPointerException e){ + System.out.println("There is no Group Parmeter.\nPlease set a Parameter."); + break; + } + + } + } + + public void updateParameter(){ + while( true ) { + try { + GroupType groupType = chooseGroup(); + if (groupType == null){ //chooseGroup에서 end입력되면 null리턴함 + allCustomers.refresh(); + break; + } + + Group group = allGroups.find(groupType); + System.out.println("\nGroupType: " + group.getGroupType()); + System.out.println("Parameter: " + group.getParameter()); + + while ( true ){ + int choice = chooseMenu(new String[]{ + "Minimum Spent Time", + "Minimum Total Pay", + "Back"}); + + if (choice == 1) { + Integer minimumSpentTime = inputMinimumSpentTime(); + if (minimumSpentTime == null) continue; + else group.getParameter().setMinTime(minimumSpentTime); + } + else if (choice == 2) { + Integer minimumTotalPay = inputMinimumTotalPay(); + if (minimumTotalPay == null) continue; + else group.getParameter().setMinPay(minimumTotalPay); + } + else{ + System.out.println("\nGroupType: " + group.getGroupType()); + System.out.println("Parameter: " + group.getParameter()); + allCustomers.refresh(); + break; + } + } + } catch (NullPointerException e){ + System.out.println("There is no Group Parmeter.\nPlease set a Parameter."); + break; + } + } + } + + public Integer inputMinimumSpentTime(){ + while (true){ + try{ + System.out.println("\nInput Minimum Spent Time: "); + Integer minimumSpentTime = Integer.parseInt(nextLine(Message.END_MSG)); + if(minimumSpentTime<0) throw new InputRangeException(); + return minimumSpentTime; + } catch (InputEndException e){ + System.out.println(Message.ERR_MSG_INPUT_END); + return null; + } catch (InputRangeException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } catch (NumberFormatException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_TYPE); + } + } + } + + public Integer inputMinimumTotalPay(){ + while (true){ + try { + System.out.println("\nInput Minimum Total Pay: "); + Integer minimumTotalPay = Integer.parseInt(nextLine(Message.END_MSG)); + if(minimumTotalPay<0) throw new InputRangeException(); + return minimumTotalPay; + } catch (InputEndException e){ + System.out.println(Message.ERR_MSG_INPUT_END); + return null; + } catch (InputRangeException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } catch (NumberFormatException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_TYPE); + } + } + } +} diff --git a/src/me/smartstore/menu/MainMenu.java b/src/me/smartstore/menu/MainMenu.java new file mode 100644 index 00000000..d116c583 --- /dev/null +++ b/src/me/smartstore/menu/MainMenu.java @@ -0,0 +1,42 @@ +package me.smartstore.menu; + +import me.smartstore.menu.customer.CustomerMenu; + +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..82d3915a --- /dev/null +++ b/src/me/smartstore/menu/Menu.java @@ -0,0 +1,56 @@ +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(){ + 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 String nextLineNotUpperCase(String end){ + System.out.println("** Press 'end', if you want to exit! **"); + String str = scanner.nextLine(); + if (str.equals("end") || str.equals("END")) throw new InputEndException(); + return str; + } + + default int chooseMenu(String[] menus) { + while ( true ) { // 예외 복구 while + try { + System.out.println("\n==============================="); + 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 (NumberFormatException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_TYPE); + } + } + } + + void manage(); // 각 서브메뉴들을 관리하는 함수 (각 서브메뉴의 최상위 메뉴) + +} diff --git a/src/me/smartstore/menu/SummaryMenu.java b/src/me/smartstore/menu/SummaryMenu.java new file mode 100644 index 00000000..48f714f0 --- /dev/null +++ b/src/me/smartstore/menu/SummaryMenu.java @@ -0,0 +1,173 @@ +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.util.Message; + +import java.util.*; + +import static me.smartstore.util.Message.ERR_MSG_INPUT_END; +import static me.smartstore.util.Message.ERR_MSG_INVALID_INPUT_RANGE; + +public class SummaryMenu implements Menu{ + private final Customers allCustomers = Customers.getInstance(); + private final Groups allGroups = Groups.getInstance(); + + + + // 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) sortByGroup(allCustomers.getCustomers()); + else if (choice == 2) sortByName(allCustomers.getCustomers()); + else if (choice == 3) sortByTime(allCustomers.getCustomers()); + else if (choice == 4) sortByPay(allCustomers.getCustomers()); + else break; + } + } + public void printGroupInfo(Group group){ + System.out.println("\n=============================="); + System.out.printf("Group: %s ( Time : %d, Pay : %d )", group.getGroupType(), group.getParameter().getMinTime(), group.getParameter().getMinPay()); + System.out.println("\n=============================="); + } + + public void printCustomerInfo(Customer customer){ + System.out.printf("Customer{userId='%s', name='%s', spentTime=%d, totalPay=%d, " + ,customer.getCustomerId(),customer.getCustomerName(),customer.getCustomerTotalTime(),customer.getCustomerTotalPay()); + if (customer.getGroup().getGroupType() == GroupType.NONE){ + System.out.println("group=null}"); + } + else{ + System.out.printf("group=GroupType: %s\nParameter: Parameter{minimumSpentTime=%d, minimumTotalPay=%d}}\n" + , customer.getGroup().getGroupType(), customer.getGroup().getParameter().getMinTime(), customer.getGroup().getParameter().getMinPay()); + } + } + + + public int forSortbyGroup(int i, int count, Customer[] customers){ + printGroupInfo(allGroups.get(i)); + for (int j = 0; j < customers.length; j++) { + if (customers[j].getGroup().getGroupType() == allGroups.get(i).getGroupType()){ + System.out.printf("No. %d => ", count); + printCustomerInfo(customers[j]); + count++; + } + } + System.out.println("===============================\n"); + + return count; + } + + + + public void sortByGroup(Customer[] customers){ + GroupType[] groupTypes = new GroupType[]{GroupType.NONE, GroupType.GENERAL, GroupType.VIP, GroupType.VVIP}; + for (int j = 0; j < groupTypes.length; j++) { + for (int i = 0; i < allGroups.size(); i++) { + int count = 1; + if (allGroups.get(i).getGroupType() == groupTypes[j]) { + count = forSortbyGroup(i, count, customers); + } + } + + } + } + + public void sortByName(Customer[] customers){ + while (true){ + try { + String standard = selectOrderStandard(); + Customer[] result = new Customer[]{}; + if(standard.equals("A")){ // 오름차순 + result = Arrays.stream(customers).sorted(Comparator.comparing(Customer::getCustomerName)).toArray(Customer[]::new); + } else if (standard.equals("D")) { // 내림차순 + result = Arrays.stream(customers).sorted(Comparator.comparing(Customer::getCustomerName).reversed()).toArray(Customer[]::new); + } + sortByGroup(result); + } catch (InputEndException e){ + System.out.println(ERR_MSG_INPUT_END); + break; + } + + } + } + + public void sortByTime(Customer[] customers){ + while (true){ + try { + String standard = selectOrderStandard(); + Customer[] result = new Customer[]{}; + if(standard.equals("A")){ // 오름차순 + result = Arrays.stream(customers).sorted(Comparator.comparing(Customer::getCustomerTotalTime)).toArray(Customer[]::new); + } else if (standard.equals("D")) { // 내림차순 + result = Arrays.stream(customers).sorted(Comparator.comparing(Customer::getCustomerTotalTime).reversed()).toArray(Customer[]::new); + } + sortByGroup(result); + } catch (InputEndException e){ + System.out.println(ERR_MSG_INPUT_END); + break; + } + + } + + } + + public void sortByPay(Customer[] customers){ + while (true){ + try { + String standard = selectOrderStandard(); + Customer[] result = new Customer[]{}; + if(standard.equals("A")){ // 오름차순 + result = Arrays.stream(customers).sorted(Comparator.comparing(Customer::getCustomerTotalPay)).toArray(Customer[]::new); + } else if (standard.equals("D")) { // 내림차순 + result = Arrays.stream(customers).sorted(Comparator.comparing(Customer::getCustomerTotalPay).reversed()).toArray(Customer[]::new); + } + sortByGroup(result); + } catch (InputEndException e){ + System.out.println(ERR_MSG_INPUT_END); + break; + } + + } + + } + + public String selectOrderStandard() { + String standard; + while(true){ + try { + System.out.println("\nWhich order (ASCENDING (A), DESCENDING (D))?"); + standard = nextLine(Message.END_MSG); + if (standard.length() > 1) throw new InputMismatchException(); + else if (standard.equals("A") || standard.equals("D")) + return standard; + } catch (InputMismatchException e) { + System.out.println(ERR_MSG_INVALID_INPUT_RANGE); + } + } + } + + +} diff --git a/src/me/smartstore/menu/customer/AddCustomer.java b/src/me/smartstore/menu/customer/AddCustomer.java new file mode 100644 index 00000000..56b3dd2b --- /dev/null +++ b/src/me/smartstore/menu/customer/AddCustomer.java @@ -0,0 +1,148 @@ +package me.smartstore.menu.customer; + +import me.smartstore.customer.Customer; +import me.smartstore.customer.Customers; +import me.smartstore.exception.InputEndException; +import me.smartstore.group.Groups; +import me.smartstore.menu.Menu; +import me.smartstore.util.Message; + +import java.awt.*; + +public class AddCustomer implements Menu { + private final Customers allCustomers = Customers.getInstance(); + private final Groups allGroups = Groups.getInstance(); + + //singleton + private static AddCustomer addCustomer; + + public static AddCustomer getInstance(){ + if (addCustomer == null){ + addCustomer = new AddCustomer(); + } + return addCustomer; + } + + private AddCustomer(){} + + // + + public void addCustomer(){ + while(true){ + try { + System.out.println("How many customer to input?"); + int n = Integer.parseInt(nextLineNotUpperCase(Message.END_MSG)); + + for (int i = 0; i < n; i++) { + System.out.println("====== Customer "+ (i+1) + " Info. ======"); + allCustomers.add(inputCustomerInfo(null)); + // 고객정보 입력받는 함수 추가됨 + } + allCustomers.refresh(); + break; + }catch (InputEndException e){ + System.out.println(Message.ERR_MSG_INPUT_END); + break; + }catch (IllegalArgumentException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + } + + + public Customer inputCustomerInfo(Customer customer){ + //cusotmer이 null일 때 -> 새로운 고객객체를 생성할 때 + //cusotmer이 null이 아닐때 -> 기존 고객정보를 수정할 때 + String name = ""; + String id = ""; + Integer spentTime = 0; + Integer totalPay = 0; + if (customer != null){ // updateCustomer일 때 기존정보를 불러옴 + name = customer.getCustomerName(); + id = customer.getCustomerId(); + spentTime = customer.getCustomerTotalTime(); + totalPay = customer.getCustomerTotalPay(); + } + + while (true){ + int choice = chooseMenu(new String[]{ + "Customer Name", + "Customer ID", + "Customer Spent Time", + "Customer Total Pay", + "Back"}); + + if (choice == 1) { + name = inputCustomerName(name); + } else if (choice == 2) { + id = inputCustomerID(id); + } else if (choice == 3) { + spentTime = inputSpentTime(spentTime); + } else if (choice == 4) { + totalPay = inputTotalPay(totalPay); + } else { + return new Customer(name, id, spentTime, totalPay); + } + + } + + } + + public String inputCustomerName(String originName){ + try { + System.out.println("\nInput Customer's Name: "); + String name = nextLineNotUpperCase(Message.END_MSG); + + return name; + } catch (InputEndException e){ + System.out.println(Message.ERR_MSG_INPUT_END); + return originName; + } + + } + + public String inputCustomerID(String originID){ + try{ + System.out.println("\nInput Customer's ID: "); + String id = nextLineNotUpperCase(Message.END_MSG); + return id; + } catch (InputEndException e){ + System.out.println(Message.ERR_MSG_INPUT_END); + return originID; + } + } + + public Integer inputSpentTime(Integer originSpentTime){ + try{ + System.out.println("\nInput Customer's Spent Time: "); + Integer spentTime = Integer.parseInt(nextLineNotUpperCase(Message.END_MSG)); + return spentTime; + } catch (InputEndException e){ + System.out.println(Message.ERR_MSG_INPUT_END); + return originSpentTime; + } catch (NumberFormatException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + return originSpentTime; + } + } + + public Integer inputTotalPay(Integer originTotalPay){ + try{ + System.out.println("\nInput Customer's Total Payment: "); + Integer totalPay = Integer.parseInt(nextLineNotUpperCase(Message.END_MSG)); + return totalPay; + } catch (InputEndException e){ + System.out.println(Message.ERR_MSG_INPUT_END); + return originTotalPay; + } catch (NumberFormatException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + return originTotalPay; + } + } + + + @Override + public void manage() { + + } +} diff --git a/src/me/smartstore/menu/customer/CustomerMenu.java b/src/me/smartstore/menu/customer/CustomerMenu.java new file mode 100644 index 00000000..5aa5e758 --- /dev/null +++ b/src/me/smartstore/menu/customer/CustomerMenu.java @@ -0,0 +1,111 @@ +package me.smartstore.menu.customer; + +import me.smartstore.customer.Customer; +import me.smartstore.customer.Customers; +import me.smartstore.exception.InputEndException; +import me.smartstore.group.Groups; +import me.smartstore.menu.Menu; +import me.smartstore.util.Message; + +import java.util.InputMismatchException; +import java.util.Scanner; + +public class CustomerMenu implements Menu { + private final Customers allCustomers = Customers.getInstance(); + private final AddCustomer addCustomer = AddCustomer.getInstance(); + private final Groups allGroups = Groups.getInstance(); + Scanner scanner = new Scanner(System.in); + + // 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.addCustomer(); + else if (choice == 2) viewCustomer(); + else if (choice == 3) updateCustomer(); + else if (choice == 4) deleteCustomer(); + else break; + } + } + + + public void viewCustomer(){ + if(allCustomers.size() == 0){ + System.out.println("No Customers. Please input one first."); + } + else { + System.out.println("======= Customer Info. ======="); + for (int i = 0; i < allCustomers.size(); i++) { + + System.out.printf("No. %d => Customer{userId='%s', name='%s', spentTime=%d, totalPay=%d, " + , i+1,allCustomers.get(i).getCustomerId(),allCustomers.get(i).getCustomerName(),allCustomers.get(i).getCustomerTotalTime(),allCustomers.get(i).getCustomerTotalPay()); + if (allCustomers.get(i).getGroup() == null){ + System.out.println("group=GroupType: null"); + } + else { + System.out.println("group=GroupType: " + allCustomers.get(i).getGroup().getGroupType()); + } + } + } + } + + public void updateCustomer(){ + // 고객객체를 업데이트 할 때 기존 정보를 불려와야함. + viewCustomer(); + while (true){ + if(allCustomers.size() == 0) break; + System.out.printf("\nWhich customer (1 ~ %d) ?", allCustomers.size()); + try { + int num = scanner.nextInt(); + scanner.nextLine(); + if (num > allCustomers.size() || 0 > allCustomers.size()) throw new InputMismatchException(); + allCustomers.set(num-1, addCustomer.inputCustomerInfo(allCustomers.get(num-1))); + allCustomers.refresh(); + break; + }catch (InputMismatchException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + scanner.nextLine(); + } + } + + + } + + public void deleteCustomer(){ + viewCustomer(); + while (true){ + if(allCustomers.size() == 0) break; + System.out.printf("\nWhich customer (1 ~ %d)? ", allCustomers.size()); + try { + int num = scanner.nextInt(); + scanner.nextLine(); + if (num > allCustomers.size()) throw new InputMismatchException(); + allCustomers.pop(num-1); + break; + }catch (InputMismatchException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + scanner.nextLine(); + } + } + + + + } +} \ 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..1c67d21e --- /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"; +}