diff --git a/me.smartstore/src/Main.java b/me.smartstore/src/Main.java new file mode 100644 index 00000000..8ef738b9 --- /dev/null +++ b/me.smartstore/src/Main.java @@ -0,0 +1,8 @@ +import customer.Customers; + +public class Main { + public static void main(String[] args) { + SmartStoreApp.getInstance().test().run(); + //SmartStoreApp.getInstance().run(); + } +} diff --git a/me.smartstore/src/SmartStoreApp.java b/me.smartstore/src/SmartStoreApp.java new file mode 100644 index 00000000..8bcfb57c --- /dev/null +++ b/me.smartstore/src/SmartStoreApp.java @@ -0,0 +1,57 @@ +import customer.Customer; +import customer.Customers; +import group.Group; +import group.GroupType; +import group.Groups; +import group.Parameter; +import menu.*; + + +public class SmartStoreApp { + private final Groups allGroups = Groups.getInstance(); + private final Customers allCustomers = Customers.getInstance(); + private final MainMenu mainMenu = MainMenu.getInstance(); + 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)); + } + + System.out.println("allCustomers = " + allCustomers); + System.out.println("allGroups = " + allGroups); + + // @TODO: refresh do not implemented yet. + allCustomers.refresh(); + + return this; // smartStoreApp + } + public void run() { + details(); + mainMenu.manage(); + } + +} diff --git a/me.smartstore/src/arrays/Collections.java b/me.smartstore/src/arrays/Collections.java new file mode 100644 index 00000000..e8d0c436 --- /dev/null +++ b/me.smartstore/src/arrays/Collections.java @@ -0,0 +1,16 @@ +package arrays; + +public interface Collections { + // 데이터를 가지고 있는 객체가 아님 + // 구현 해야하는 메소드의 정보만 가지고 있음 (인터페이스) + + int size(); + T get(int index); + void set(int index, T object); + int indexOf(T object); + void add(T object); + void add(int index, T object); + T pop(); + T pop(int index); + T pop(T object); +} diff --git a/me.smartstore/src/arrays/DArray.java b/me.smartstore/src/arrays/DArray.java new file mode 100644 index 00000000..c6726a5c --- /dev/null +++ b/me.smartstore/src/arrays/DArray.java @@ -0,0 +1,153 @@ +package arrays; + +import exception.ElementNotFoundException; +import exception.EmptyArrayException; +import exception.NullArgumentException; + +public class DArray implements Collections { // Dynamic Array + + 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; + } + + // 배열에 얼마나 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/me.smartstore/src/customer/Customer.java b/me.smartstore/src/customer/Customer.java new file mode 100644 index 00000000..a350d81e --- /dev/null +++ b/me.smartstore/src/customer/Customer.java @@ -0,0 +1,109 @@ +package customer; + +import group.Group; +import jdk.jfr.Description; + +import java.util.Objects; + +public class Customer implements Comparable { + private String cName; + private String cId; + private Integer totalTime; + private Integer totalPay; + + + private Group group; + //refresh 함수가 호출되는 경우 -> 분류기준이 바뀔때, 새로운 고객이 들어올때 + + public Customer(){ + + } + public Customer(String cName, String cId) { + this.cName = cName; + this.cId = cId; + } + + public Customer(String cName, String cId, Integer totalTime, Integer totalPay) { + this.cName = cName; + this.cId = cId; + this.totalTime = totalTime; + this.totalPay = totalPay; + } + public Customer(String cName, String cId, Integer totalTime, Integer totalPay, Group group) { + this.cName = cName; + this.cId = cId; + this.totalTime = totalTime; + this.totalPay = totalPay; + this.group = group; + } + + public String getcName() { + return cName; + } + + public void setcName(String cName) { + this.cName = cName; + } + + public String getcId() { + return cId; + } + + public void setcId(String cId) { + this.cId = cId; + } + + 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; + } + 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(cId, customer.cId); + } + + @Override + public int hashCode() { + return Objects.hash(cId); + } + + @Override + public String toString() { + return "Customer{" + + "cName='" + cName + '\'' + + ", cId='" + cId + '\'' + + ", totalTime=" + totalTime + + ", totalPay=" + totalPay + + ", group=" + group+ + '}'; + } + /** + * 정렬을 위한 메소드 + */ + @Override + public int compareTo(Customer o) { + return this.cName.compareTo(o.cName); + } +} diff --git a/me.smartstore/src/customer/Customers.java b/me.smartstore/src/customer/Customers.java new file mode 100644 index 00000000..79a22fe2 --- /dev/null +++ b/me.smartstore/src/customer/Customers.java @@ -0,0 +1,51 @@ +package customer; + +import arrays.DArray; +import group.Group; +import group.GroupType; +import group.Groups; + +public class Customers extends DArray{ + private final Groups allGroups = Groups.getInstance(); + private static Customers allCustomers; + + public static Customers getInstance() { + if (allCustomers == null) { + allCustomers = new Customers(); + } + return allCustomers; + } + + private Customers() {} + + // refresh 함수가 호출되는 경우 + // 1. 분류기준 바뀔 때 + // 2. 새로운 고객이 들어올 때 + public void refresh() { + for(int i=0; i< allCustomers.size(); i++){ + Customer customer = allCustomers.get(i); + Group group = findGroup(customer.getTotalTime(), customer.getTotalPay()); + customer.setGroup(group); + } + } + + /** + * 시간과 돈을 매개변수로 받아 해당하는 등급을 반환 + * */ + public Group findGroup(int totalTime, int totalPay){ + try { + if(totalTime >= allGroups.find(GroupType.VVIP).getParameter().getMinTime() && totalPay>=allGroups.find(GroupType.VVIP).getParameter().getMinPay()){ + return allGroups.find(GroupType.VVIP); + } else if (totalTime >= allGroups.find(GroupType.VIP).getParameter().getMinTime() && totalPay>=allGroups.find(GroupType.VIP).getParameter().getMinPay()) { + return allGroups.find(GroupType.VIP); + } else if (totalTime >= allGroups.find(GroupType.GENERAL).getParameter().getMinTime() && totalPay>=allGroups.find(GroupType.GENERAL).getParameter().getMinPay()) { + return allGroups.find(GroupType.GENERAL); + } else{ + return allGroups.find(GroupType.NONE); + } + } catch (NullPointerException e){ + return null; + } + } + +} diff --git a/me.smartstore/src/exception/ArrayEmptyException.java b/me.smartstore/src/exception/ArrayEmptyException.java new file mode 100644 index 00000000..1a0538a2 --- /dev/null +++ b/me.smartstore/src/exception/ArrayEmptyException.java @@ -0,0 +1,15 @@ +package exception; + + +import util.Message; + +public class ArrayEmptyException extends RuntimeException { + + public ArrayEmptyException() { + super(Message.ERR_MSG_INVALID_ARR_EMPTY); + } + + public ArrayEmptyException(String message) { + super(message); + } +} diff --git a/me.smartstore/src/exception/ElementNotFoundException.java b/me.smartstore/src/exception/ElementNotFoundException.java new file mode 100644 index 00000000..f163a3e8 --- /dev/null +++ b/me.smartstore/src/exception/ElementNotFoundException.java @@ -0,0 +1,22 @@ +package exception; + +public class ElementNotFoundException extends RuntimeException { + public ElementNotFoundException() { + } + + public ElementNotFoundException(String message) { + super(message); + } + + public ElementNotFoundException(String message, Throwable cause) { + super(message, cause); + } + + public ElementNotFoundException(Throwable cause) { + super(cause); + } + + public ElementNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/me.smartstore/src/exception/EmptyArrayException.java b/me.smartstore/src/exception/EmptyArrayException.java new file mode 100644 index 00000000..4ece8b3b --- /dev/null +++ b/me.smartstore/src/exception/EmptyArrayException.java @@ -0,0 +1,22 @@ +package exception; + +public class EmptyArrayException extends RuntimeException { + public EmptyArrayException() { + } + + public EmptyArrayException(String message) { + super(message); + } + + public EmptyArrayException(String message, Throwable cause) { + super(message, cause); + } + + public EmptyArrayException(Throwable cause) { + super(cause); + } + + public EmptyArrayException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/me.smartstore/src/exception/InputEmptyException.java b/me.smartstore/src/exception/InputEmptyException.java new file mode 100644 index 00000000..2327adb2 --- /dev/null +++ b/me.smartstore/src/exception/InputEmptyException.java @@ -0,0 +1,15 @@ +package exception; + + +import 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/me.smartstore/src/exception/InputEndException.java b/me.smartstore/src/exception/InputEndException.java new file mode 100644 index 00000000..8af309b5 --- /dev/null +++ b/me.smartstore/src/exception/InputEndException.java @@ -0,0 +1,14 @@ +package exception; + + +import util.Message; + +public class InputEndException extends RuntimeException { + public InputEndException() { + super(Message.ERR_MSG_INPUT_END); + } + + public InputEndException(String message) { + super(message); + } +} diff --git a/me.smartstore/src/exception/InputFormatException.java b/me.smartstore/src/exception/InputFormatException.java new file mode 100644 index 00000000..1ed77d99 --- /dev/null +++ b/me.smartstore/src/exception/InputFormatException.java @@ -0,0 +1,14 @@ +package exception; + + +import util.Message; + +public class InputFormatException extends RuntimeException { + public InputFormatException() { + super(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } + + public InputFormatException(String message) { + super(message); + } +} diff --git a/me.smartstore/src/exception/InputRangeException.java b/me.smartstore/src/exception/InputRangeException.java new file mode 100644 index 00000000..35b0f4ad --- /dev/null +++ b/me.smartstore/src/exception/InputRangeException.java @@ -0,0 +1,14 @@ +package exception; + + +import 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/me.smartstore/src/exception/InputTypeException.java b/me.smartstore/src/exception/InputTypeException.java new file mode 100644 index 00000000..58b6a149 --- /dev/null +++ b/me.smartstore/src/exception/InputTypeException.java @@ -0,0 +1,14 @@ +package exception; + + +import util.Message; + +public class InputTypeException extends RuntimeException { + public InputTypeException() { + super(Message.ERR_MSG_INVALID_INPUT_TYPE); + } + + public InputTypeException(String message) { + super(message); + } +} diff --git a/me.smartstore/src/exception/NullArgumentException.java b/me.smartstore/src/exception/NullArgumentException.java new file mode 100644 index 00000000..14349091 --- /dev/null +++ b/me.smartstore/src/exception/NullArgumentException.java @@ -0,0 +1,22 @@ +package exception; + +public class NullArgumentException extends RuntimeException { + public NullArgumentException() { + } + + public NullArgumentException(String message) { + super(message); + } + + public NullArgumentException(String message, Throwable cause) { + super(message, cause); + } + + public NullArgumentException(Throwable cause) { + super(cause); + } + + public NullArgumentException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/me.smartstore/src/group/Group.java b/me.smartstore/src/group/Group.java new file mode 100644 index 00000000..a47dab3b --- /dev/null +++ b/me.smartstore/src/group/Group.java @@ -0,0 +1,53 @@ +package group; + +import java.util.Objects; + +public class Group { + private Parameter parameter; //분류기준 + private GroupType groupType; //분류한 그룹 타입 + + public Group() { + } + + public Group(Parameter parameter, GroupType groupType) { + this.parameter = parameter; + this.groupType = groupType; + } + + public Parameter getParameter() { + return parameter; + } + + public void setParameter(Parameter parameter) { + this.parameter = parameter; + } + + public GroupType getGroupType() { + return groupType; + } + + public void setGroupType(GroupType groupType) { + this.groupType = groupType; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Group group = (Group) o; + return Objects.equals(parameter, group.parameter) && groupType == group.groupType; + } + + @Override + public int hashCode() { + return Objects.hash(parameter, groupType); + } + + @Override + public String toString() { + return "Group{" + + "parameter=" + parameter + + ", groupType=" + groupType + + '}'; + } +} diff --git a/me.smartstore/src/group/GroupType.java b/me.smartstore/src/group/GroupType.java new file mode 100644 index 00000000..d28d52f3 --- /dev/null +++ b/me.smartstore/src/group/GroupType.java @@ -0,0 +1,19 @@ +package group; + +public enum GroupType { + N("해당없음"), G("일반고객"), V("우수고객"), VV("최우수고객"), + NONE("해당없음"), GENERAL("일반고객"), VIP("우수고객"), VVIP("최우수고객"); + String groupType = ""; + GroupType(String groupType) { + this.groupType = groupType; + } + + + public GroupType replaceFullName(){ + if(this == N) return NONE; + else if (this == G) return GENERAL; + else if (this == V) return VIP; + else if (this == VV) return VVIP; + return this; + } +} diff --git a/me.smartstore/src/group/Groups.java b/me.smartstore/src/group/Groups.java new file mode 100644 index 00000000..6c395b45 --- /dev/null +++ b/me.smartstore/src/group/Groups.java @@ -0,0 +1,26 @@ +package group; + +import arrays.DArray; + +public class Groups extends DArray { + private static Groups allGroups; + + public static Groups getInstance(){ + if(allGroups == null){ + allGroups = new Groups(); + allGroups.add(new Group(new Parameter(), GroupType.NONE)); + } + return allGroups; + } + private Groups(){} + + public Group find(GroupType groupType){ + for (int i = 0; i < allGroups.size; i++) { + if (allGroups.get(i).getGroupType() == groupType) { + return allGroups.get(i); + } + } + return null; + } + +} diff --git a/me.smartstore/src/group/Parameter.java b/me.smartstore/src/group/Parameter.java new file mode 100644 index 00000000..537dfff0 --- /dev/null +++ b/me.smartstore/src/group/Parameter.java @@ -0,0 +1,53 @@ +package group; + +import java.util.Objects; + +public class Parameter { + private Integer minTime; + private Integer minPay; + + public Parameter() { + } + + public Parameter(Integer minTime, Integer minPay) { + this.minTime = minTime; + this.minPay = minPay; + } + + public Integer getMinTime() { + return minTime; + } + + public void setMinTime(Integer minTime) { + this.minTime = minTime; + } + + public Integer getMinPay() { + return minPay; + } + + public void setMinPay(Integer minPay) { + this.minPay = minPay; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Parameter parameter = (Parameter) o; + return 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/me.smartstore/src/menu/CustomerMenu.java b/me.smartstore/src/menu/CustomerMenu.java new file mode 100644 index 00000000..55acac1c --- /dev/null +++ b/me.smartstore/src/menu/CustomerMenu.java @@ -0,0 +1,176 @@ +package menu; + +import customer.Customer; +import customer.Customers; +import exception.InputEndException; +import exception.InputRangeException; +import group.Groups; +import util.Message; + +import static util.Message.END_MSG; + +public class CustomerMenu implements Menu{ + private final Customers allCustomers = Customers.getInstance(); + private final Groups allGroups = Groups.getInstance(); + private static CustomerMenu customerMenu; + + public static CustomerMenu getInstance(){ + if(customerMenu == null){ + customerMenu = new CustomerMenu(); + } + return customerMenu; + } + private CustomerMenu(){} + + @Override + public void manage() { + while (true) { + 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; + } + + } + + + /** + * 고객 추가 메소드 end 입력시 종료 + * */ + public void addCustomer(){ + while(true){ + System.out.println("How many customers to input?"); + try{ + String num = nextLine(Message.END_MSG); + for(int i=0; i ", i+1)+ customer); + } + } + /** + * 고객을 선택하여 정보를 업데이트 + * */ + private void updateCustomer() { + try { + viewCustomer(); + System.out.printf("Which customer ( 1 ~ %d )? ", allCustomers.size()); + Integer index = scanner.nextInt(); + scanner.nextLine(); //버퍼에 남아있는 엔터값 처리 + if(index <= 0 || index > allCustomers.size()){ + throw new InputRangeException(); + } + Customer customer = allCustomers.get(index - 1); + addCustomerManage(customer); + allCustomers.refresh(); + } catch (InputRangeException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + + /** + * 고객 정보 삭제 + * */ + private void deleteCustomer() { + try { + viewCustomer(); + System.out.printf("Which customer ( 1 ~ %d )? ", allCustomers.size()); + Integer index = scanner.nextInt(); + scanner.nextLine(); //버퍼에 남아있는 엔터값 처리 + if(index <= 0 || index > allCustomers.size()){ + throw new InputRangeException(); + } + System.out.println(allCustomers.get(index - 1)); + allCustomers.pop(index - 1); + viewCustomer(); + } catch (InputRangeException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + +} diff --git a/me.smartstore/src/menu/GroupMenu.java b/me.smartstore/src/menu/GroupMenu.java new file mode 100644 index 00000000..382ba449 --- /dev/null +++ b/me.smartstore/src/menu/GroupMenu.java @@ -0,0 +1,182 @@ +package menu; + +import customer.Customers; +import exception.InputEndException; +import group.Group; +import group.GroupType; +import group.Groups; +import group.Parameter; +import util.Message; + + +public class GroupMenu implements Menu{ + private static GroupMenu groupMenu; + private final Customers allCustomers = Customers.getInstance(); + private final Groups allGroups = Groups.getInstance(); + + public static GroupMenu getInstance(){ + if(groupMenu == null){ + groupMenu = new GroupMenu(); + } + return groupMenu; + } + private GroupMenu(){} + + @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 break; + } + } + 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(); // String -> enum + 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); + } + } + } + + /** + * groupType의 시간과 돈울 입력받은 값으로 설정해준다. + * */ + public void setParameter(){ + while (true){ + GroupType groupType = chooseGroup(); + if(groupType == null){ + System.out.println("break란다"); + 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 { + group = new Group(new Parameter(), groupType); + allGroups.add(group); + Setmanage(group); + allCustomers.refresh(); // 파라미터가 변경되었거나 추가되는 경우, 고객 분류를 다시 해야함 + } + } + } + /** + * 어떤 파라미터를 추가할건지 선택 + * */ + public void Setmanage(Group group) { + while (true) { + int choice = chooseMenu(new String[]{ + "Minimum Spent Time", + "Minimum Total Pay", + "Back" + }); + if(choice == 1) setMinTime(group); + else if(choice == 2) setMinPay(group); + else break; + } + } + + /** + * groupType의 해당하는 파라미터값을 출력함 + * */ + public void viewParameter(){ + while(true){ + GroupType groupType = chooseGroup(); + if(groupType == null){ + break; + } + //GroupType에 해당하는 group객체를 찾아야함 + Group group = allGroups.find(groupType); + System.out.println(groupType); + System.out.println(group.getParameter()); + } + } + + /** + * groupType의 파라미터값을 수정함 + * */ + public void updateParameter(){ + while(true){ + GroupType groupType = chooseGroup(); + if(groupType == null){ + break; + } + //GroupType에 해당하는 group객체를 찾아야함 + Group group = allGroups.find(groupType); + System.out.println(groupType); + System.out.println(group.getParameter()); + updateParameterManage(group); + } + } + /** + * 어떤 파라미터 값을 수정할 것인지 선택 + * */ + public void updateParameterManage(Group group){ + while (true) { + int choice = chooseMenu(new String[]{ + "Minimum Spent Time", + "Minimum Total Pay", + "Back" + }); + if(choice == 1) setMinTime(group); + else if(choice == 2) setMinPay(group); + else break; + } + } + + // 시간을 수정 end입력시 종료 + public void setMinTime(Group group){ + while (true){ + try { + System.out.printf("Input Minimum Spent Time:"); + String spentTime = nextLine(Message.END_MSG); + Parameter parameter = group.getParameter(); + parameter.setMinTime(Integer.parseInt(spentTime)); + group.setParameter(parameter); + System.out.println(group.getParameter()); + allCustomers.refresh();// 파라미터가 변경되었거나 추가되는 경우, 고객 분류를 다시 해야함 + break; + } catch (InputEndException e){ + System.out.println(Message.ERR_MSG_INPUT_END); + break; + } + } + } + //돈을 수정 end입력시 종료 + public void setMinPay(Group group){ + while (true){ + try { + System.out.printf("Input Minimum Total Pay:"); + String totalPay = nextLine(Message.END_MSG); + Parameter parameter = group.getParameter(); + parameter.setMinPay(Integer.parseInt(totalPay)); + group.setParameter(parameter); + System.out.println(group.getParameter()); + allCustomers.refresh();// 파라미터가 변경되었거나 추가되는 경우, 고객 분류를 다시 해야함 + break; + }catch (InputEndException e){ + System.out.println(Message.ERR_MSG_INPUT_END); + break; + } + } + } +} + diff --git a/me.smartstore/src/menu/MainMenu.java b/me.smartstore/src/menu/MainMenu.java new file mode 100644 index 00000000..a5ea845d --- /dev/null +++ b/me.smartstore/src/menu/MainMenu.java @@ -0,0 +1,34 @@ +package 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 Data", + "Classification Summary", + "Quit" + }); + if(choice == 1) groupMenu.manage(); + else if(choice == 2) customerMenu.manage(); + else if(choice == 3) summaryMenu.manage(); + else {System.out.println("Program Finish"); break;} + } + } + + +} diff --git a/me.smartstore/src/menu/Menu.java b/me.smartstore/src/menu/Menu.java new file mode 100644 index 00000000..8af64754 --- /dev/null +++ b/me.smartstore/src/menu/Menu.java @@ -0,0 +1,48 @@ +package menu; + +import exception.InputEndException; +import exception.InputRangeException; +import group.Groups; +import util.Message; + +import java.util.Locale; +import java.util.Scanner; + +public interface Menu { + public Scanner scanner = new Scanner(System.in); + + default String nextLine(){ //String + 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; + } + /**public String nextLine(){ //Integer + String str = scanner.nextLine().toUpperCase(); //대소문자 구분 없애기 위해 + 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 (NumberFormatException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } catch (InputRangeException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + } + void manage(); //각 서브메뉴들을 관리하는 함수 + +} diff --git a/me.smartstore/src/menu/SummaryMenu.java b/me.smartstore/src/menu/SummaryMenu.java new file mode 100644 index 00000000..cf0b7ab1 --- /dev/null +++ b/me.smartstore/src/menu/SummaryMenu.java @@ -0,0 +1,236 @@ +package menu; + +import customer.Customer; +import customer.Customers; +import exception.InputEndException; +import exception.InputRangeException; +import group.Group; +import group.GroupType; +import group.Groups; +import group.Parameter; +import util.Message; + +import java.lang.reflect.Array; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; + +import static group.GroupType.*; + +public class SummaryMenu implements Menu{ + private static SummaryMenu summaryMenu; + private final Customers allCustomers = Customers.getInstance(); + private final Groups allGroups = Groups.getInstance(); + + 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 Spent Time)", + "Summary (Sorted By Total Payment)", + "Back" + }); + if(choice == 1) summary(getCustomer()); + else if(choice == 2) summarySortByName(getCustomer()); + else if(choice == 3) summarySortBySpentTime(getCustomer()); + else if(choice == 4) summarySortByTotalPay(getCustomer()); + else break; + } + } + /** + * groupType별로 출력 + * */ + public void summary(Customer[] customers){ + Parameter parameter = null; + Group group = null; + GroupType[] groupType = {NONE, GENERAL, VIP, VVIP}; + for(GroupType type: groupType){ + for(int i=0; i ", i + 1) + customers[i]); + } + } + } + else{ + System.out.println("Null."); + } + System.out.println("=============================="); + System.out.println(""); + } + } + + /** + * 이름순으로 정렬 내림차순 올림차순은 사용자가 설정 + * */ + public void summarySortByName(Customer[] customers){ + while(true){ + try { + System.out.println("Which order (ASCENDING (A), DESCENDING (D))?"); + String choice = nextLine(Message.END_MSG); + int flag = 0; + if(!choice.equals("A") && !choice.equals("D")){ + throw new InputRangeException(); + } + if(choice.equals("A")){ + flag = 1; //오름차순 정렬 + } + else { + flag = -1; //내림차순 정렬 + } + summary(sortCustomerByName(flag, customers)); //정렬된것 바탕으로 출력 + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + break; + } catch (InputRangeException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + } + //오름차순 혹은 내림차순으로 정렬 + public Customer[] sortCustomerByName(int flag, Customer[] customers){ + if(flag == 1){ + Arrays.sort(customers); + } + else{ + Arrays.sort(customers, Collections.reverseOrder()); + } + return customers; + } + + //customer을 배열로 반환 + public Customer[] getCustomer(){ + Customer[] customers = new Customer[allCustomers.size()]; + for (int i=0; i< allCustomers.size(); i++){ + customers[i] = allCustomers.get(i); + } + return customers; + } + /** + * 시간으로 정렬 + * */ + public void summarySortBySpentTime(Customer[] customers){ + while(true){ + try { + System.out.println("Which order (ASCENDING (A), DESCENDING (D))?"); + String choice = nextLine(Message.END_MSG); + int flag = 0; + if(!choice.equals("A") && !choice.equals("D")){ + throw new InputRangeException(); + } + if(choice.equals("A")){ + flag = 1; + } + else { + flag = -1; + } + summary(sortCustomerByTime(flag, customers)); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + break; + } catch (InputRangeException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + } + + //시간으로 오름차순 혹은 내림차순으로 정렬 + public Customer[] sortCustomerByTime(int flag, Customer[] customers){ + if(flag == 1){ + Arrays.sort(customers, new Comparator() { + @Override + public int compare(Customer o1, Customer o2) { + return o1.getTotalTime() - o2.getTotalTime(); + } + }); + } + else{ + Arrays.sort(customers, new Comparator() { + @Override + public int compare(Customer o1, Customer o2) { + return o2.getTotalTime() - o1.getTotalTime(); + } + }); + } + return customers; + } + + /** + * 페이순으로 정렬 + * */ + public void summarySortByTotalPay(Customer[] customers){ + while(true){ + try { + System.out.println("Which order (ASCENDING (A), DESCENDING (D))?"); + String choice = nextLine(Message.END_MSG); + int flag = 0; + if(!choice.equals("A") && !choice.equals("D")){ + throw new InputRangeException(); + } + if(choice.equals("A")){ + flag = 1; + } + else { + flag = -1; + } + summary(sortCustomerByPay(flag, customers)); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + break; + } catch (InputRangeException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + } + //페이를 오름차순 혹은 내림차순으로 정렬 + public Customer[] sortCustomerByPay(int flag, Customer[] customers){ + if(flag == 1){ + Arrays.sort(customers, new Comparator() { + @Override + public int compare(Customer o1, Customer o2) { + return o1.getTotalPay() - o2.getTotalPay(); + } + }); + } + else{ + Arrays.sort(customers, new Comparator() { + @Override + public int compare(Customer o1, Customer o2) { + return o2.getTotalPay() - o1.getTotalPay(); + } + }); + } + return customers; + } + + + + +} diff --git a/me.smartstore/src/util/Message.java b/me.smartstore/src/util/Message.java new file mode 100644 index 00000000..7d33cb17 --- /dev/null +++ b/me.smartstore/src/util/Message.java @@ -0,0 +1,13 @@ +package util; + +public interface Message { + String ERR_MSG_INVALID_ARR_EMPTY = "No Customers. Please input one first."; + String ERR_MSG_NULL_ARR_ELEMENT = "Elements in Array has null. Array can't be sorted."; + String ERR_MSG_INVALID_INPUT_NULL = "Null Input. Please input something."; + String ERR_MSG_INVALID_INPUT_EMPTY = "Empty Input. Please input something."; + String ERR_MSG_INVALID_INPUT_RANGE = "Invalid Input. Please try again."; + String ERR_MSG_INVALID_INPUT_TYPE = "Invalid Type for Input. Please try again."; + String ERR_MSG_INVALID_INPUT_FORMAT = "Invalid Format for Input. Please try again."; + String ERR_MSG_INPUT_END = "END is pressed. Exit this menu."; + String END_MSG = "END"; +}