diff --git a/me/smartstore/Main.java b/me/smartstore/Main.java new file mode 100644 index 00000000..2f8a3d49 --- /dev/null +++ b/me/smartstore/Main.java @@ -0,0 +1,9 @@ +package me.smartstore; + +public class Main { + public static void main(String[] args) { + SmartStoreApp.getInstance().test().run(); + //SmartStoreApp.getInstance().run(); + + } +} diff --git a/me/smartstore/SmartStoreApp.java b/me/smartstore/SmartStoreApp.java new file mode 100644 index 00000000..c488f6e7 --- /dev/null +++ b/me/smartstore/SmartStoreApp.java @@ -0,0 +1,65 @@ +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.04.27"); + System.out.println(" Copyright 2023 Gyeongmin 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(allGroups); + + return this; // smartStoreApp + } + + public void run() { + details(); + mainMenu.manage(); + + } +} \ No newline at end of file diff --git a/me/smartstore/arrays/Collections.java b/me/smartstore/arrays/Collections.java new file mode 100644 index 00000000..536be0a1 --- /dev/null +++ b/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/me/smartstore/arrays/DArray.java b/me/smartstore/arrays/DArray.java new file mode 100644 index 00000000..c9709002 --- /dev/null +++ b/me/smartstore/arrays/DArray.java @@ -0,0 +1,161 @@ +package me.smartstore.arrays; + +import me.smartstore.exception.ElementNotFoundException; +import me.smartstore.exception.EmptyArrayException; +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; //배열의 전체 크기 + + 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; + } + + + //arrays의 index 위치에 있는 값을 반환 + @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; + } + + + //object의 index를 반환 + @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); + } + } + + //원하는 index 위치로 데이터 넣기 + @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); + } + } + + + //배열의 맨 끝 원소 pop + @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); + } + + //index의 원소 pop + @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; + } + + //원하는 데이터 pop + @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; + } +} \ No newline at end of file diff --git a/me/smartstore/customer/Customer.java b/me/smartstore/customer/Customer.java new file mode 100644 index 00000000..1a30c3a5 --- /dev/null +++ b/me/smartstore/customer/Customer.java @@ -0,0 +1,94 @@ +package me.smartstore.customer; + +import me.smartstore.group.Group; +import java.util.Objects; + +public class Customer { + private String cusName; + private String cusId; + private Integer cusTotalTime; //총 이용 시간 + private Integer cusTotalPay; //총 이용 금액 + private Group group; + + public Customer(){} + + public Customer(String cusId) { + this.cusId = cusId; + } + + public Customer(String cusName, String cusId) { + this.cusName = cusName; + this.cusId = cusId; + } + + public Customer(String cusName, String cusId, Integer cusTotalTime, Integer cusTotalPay) { + this.cusName = cusName; + this.cusId = cusId; + this.cusTotalTime = cusTotalTime; + this.cusTotalPay = cusTotalPay; + } + + public String getCusName() { + return cusName; + } + + public void setCusName(String cusName) { + this.cusName = cusName; + } + + public String getCusId() { + return cusId; + } + + public void setCusId(String cusId) { + this.cusId = cusId; + } + + public Integer getCusTotalTime() { + return cusTotalTime; + } + + public void setCusTotalTime(Integer cusTotalTime) { + this.cusTotalTime = cusTotalTime; + } + + public Integer getCusTotalPay() { + return cusTotalPay; + } + + public void setCusTotalPay(Integer cusTotalPay) { + this.cusTotalPay = cusTotalPay; + } + + 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(cusId, customer.cusId); + } + + @Override + public int hashCode() { + return Objects.hash(cusId); + } + + @Override + public String toString() { + return "Customer{" + + "cusName='" + cusName + '\'' + + ", cusId='" + cusId + '\'' + + ", cusTotalTime=" + cusTotalTime + + ", cusTotalPay=" + cusTotalPay + + ", group=" + group + + '}'; + } +} diff --git a/me/smartstore/customer/Customers.java b/me/smartstore/customer/Customers.java new file mode 100644 index 00000000..46cd91d8 --- /dev/null +++ b/me/smartstore/customer/Customers.java @@ -0,0 +1,40 @@ +package me.smartstore.customer; + +import me.smartstore.arrays.DArray; +import me.smartstore.group.Groups; + +public class Customers extends DArray{ + + //singleton + private static Customers allCustomers; + + private final Groups allGroups = Groups.getInstance(); + + public static Customers getInstance(){ + if(allCustomers == null) + allCustomers = new Customers(); + return allCustomers; + } + + private Customers(){} + + //refresh 함수가 호출되는 경우 + //1. 분류기준이 바뀔 때 + //2. 새로운 고객이 들어올 때 + public void refresh(Groups groups){ + for(int i = 0; i < allCustomers.size(); i++){ + try{ + for(int j = 0; j < groups.size(); j++){ + if(allCustomers.get(i).getCusTotalTime() >= groups.get(j).getParameter().getMinTime() + && allCustomers.get(i).getCusTotalPay() >= groups.get(j).getParameter().getMinPay()){ + allCustomers.get(i).setGroup(groups.get(j)); + } + } + }catch (NullPointerException e){ + continue; + } + + } + } + +} diff --git a/me/smartstore/exception/ArrayEmptyException.java b/me/smartstore/exception/ArrayEmptyException.java new file mode 100644 index 00000000..8acc24f4 --- /dev/null +++ b/me/smartstore/exception/ArrayEmptyException.java @@ -0,0 +1,13 @@ +package me.smartstore.exception; + +import me.smartstore.util.Message; +public class ArrayEmptyException extends RuntimeException { + + public ArrayEmptyException() { + super(Message.ERR_MSG_INVALID_ARR_EMPTY); + } + + public ArrayEmptyException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/me/smartstore/exception/ElementNotFoundException.java b/me/smartstore/exception/ElementNotFoundException.java new file mode 100644 index 00000000..efe992f5 --- /dev/null +++ b/me/smartstore/exception/ElementNotFoundException.java @@ -0,0 +1,22 @@ +package me.smartstore.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); + } +} \ No newline at end of file diff --git a/me/smartstore/exception/EmptyArrayException.java b/me/smartstore/exception/EmptyArrayException.java new file mode 100644 index 00000000..af00e9f5 --- /dev/null +++ b/me/smartstore/exception/EmptyArrayException.java @@ -0,0 +1,22 @@ +package me.smartstore.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); + } +} \ No newline at end of file diff --git a/me/smartstore/exception/InputEmptyException.java b/me/smartstore/exception/InputEmptyException.java new file mode 100644 index 00000000..8ea30894 --- /dev/null +++ b/me/smartstore/exception/InputEmptyException.java @@ -0,0 +1,14 @@ +package me.smartstore.exception; + +import me.smartstore.util.Message; + +public class InputEmptyException extends RuntimeException { + + public InputEmptyException() { + super(Message.ERR_MSG_INVALID_INPUT_EMPTY); + } + + public InputEmptyException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/me/smartstore/exception/InputEndException.java b/me/smartstore/exception/InputEndException.java new file mode 100644 index 00000000..af902e83 --- /dev/null +++ b/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); + } + + public InputEndException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/me/smartstore/exception/InputFormatException.java b/me/smartstore/exception/InputFormatException.java new file mode 100644 index 00000000..49a76220 --- /dev/null +++ b/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); + } + + public InputFormatException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/me/smartstore/exception/InputRangeException.java b/me/smartstore/exception/InputRangeException.java new file mode 100644 index 00000000..10d7b011 --- /dev/null +++ b/me/smartstore/exception/InputRangeException.java @@ -0,0 +1,13 @@ +package me.smartstore.exception; + +import me.smartstore.util.Message; + +public class InputRangeException extends RuntimeException { + public InputRangeException() { + super(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + + public InputRangeException(String message) { + super(message); + } +} diff --git a/me/smartstore/exception/InputTypeException.java b/me/smartstore/exception/InputTypeException.java new file mode 100644 index 00000000..0226f9c3 --- /dev/null +++ b/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); + } + + public InputTypeException(String message) { + super(message); + } +} diff --git a/me/smartstore/exception/NullArgumentException.java b/me/smartstore/exception/NullArgumentException.java new file mode 100644 index 00000000..15f03cd5 --- /dev/null +++ b/me/smartstore/exception/NullArgumentException.java @@ -0,0 +1,22 @@ +package me.smartstore.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); + } +} \ No newline at end of file diff --git a/me/smartstore/group/Group.java b/me/smartstore/group/Group.java new file mode 100644 index 00000000..633e1cee --- /dev/null +++ b/me/smartstore/group/Group.java @@ -0,0 +1,53 @@ +package me.smartstore.group; + +import java.util.Objects; + +public class Group { + private Parameter parameter; //분류 기준 + private GroupType groupType; + + public Group(){} + + public Group(Parameter parameter, GroupType groupType) { + this.parameter = parameter; + this.groupType = groupType; + } + + + public Parameter getParameter() { + return parameter; + } + + public void setParameter(Parameter parameter) { + this.parameter = parameter; + } + + public GroupType getGroupType() { + return groupType; + } + + public void setGroupType(GroupType groupType) { + this.groupType = groupType; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Group group = (Group) o; + return Objects.equals(parameter, group.parameter) && groupType == group.groupType; + } + + @Override + public int hashCode() { + return Objects.hash(parameter, groupType); + } + + @Override + public String toString() { + return "Group{" + + "parameter=" + parameter + + ", groupType=" + groupType + + '}'; + } +} diff --git a/me/smartstore/group/GroupType.java b/me/smartstore/group/GroupType.java new file mode 100644 index 00000000..33eadabe --- /dev/null +++ b/me/smartstore/group/GroupType.java @@ -0,0 +1,25 @@ +package me.smartstore.group; + +public enum GroupType { + N("해당없음"), G("일반고객"), V("우수고객"), VV("최우수고객"), + NONE("해당없음"), GENERAL("일반고객"), VIP("우수고객"), VVIP("최우수고객"); + + String groupType = ""; + + GroupType(String groupType) { + this.groupType = groupType; + } + + public GroupType replaceFullName(){ + if(this == N) + return NONE; + else if(this == G) + return GENERAL; + else if(this == V) + return VIP; + else if(this == VV) + return VVIP; + + return this; + } +} diff --git a/me/smartstore/group/Groups.java b/me/smartstore/group/Groups.java new file mode 100644 index 00000000..6022556f --- /dev/null +++ b/me/smartstore/group/Groups.java @@ -0,0 +1,31 @@ +package me.smartstore.group; + +import me.smartstore.arrays.DArray; + +public class Groups extends DArray { + private static Groups allGroups; + + public static Groups getInstance(){ + if(allGroups == null) + allGroups = new Groups(); + return allGroups; + } + + private Groups(){} + + public Group find(GroupType groupType){ + for(int i = 0; i < allGroups.size(); i++){ + if(allGroups.get(i).getGroupType() == groupType){ + return allGroups.get(i); + } + } +// for(Group group : arrays){ +// if(group.getGroupType() == groupType){ //enum의 비교는 equals가 아님 -> 상수이기 때문 +// return group; +// } +// } + + return null; + + } +} diff --git a/me/smartstore/group/Parameter.java b/me/smartstore/group/Parameter.java new file mode 100644 index 00000000..0622a4fe --- /dev/null +++ b/me/smartstore/group/Parameter.java @@ -0,0 +1,52 @@ +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/me/smartstore/menu/CustomerMenu.java b/me/smartstore/menu/CustomerMenu.java new file mode 100644 index 00000000..b51af811 --- /dev/null +++ b/me/smartstore/menu/CustomerMenu.java @@ -0,0 +1,156 @@ +package me.smartstore.menu; + +import me.smartstore.customer.Customer; +import me.smartstore.customer.Customers; +import me.smartstore.exception.InputEndException; +import me.smartstore.group.Groups; +import me.smartstore.util.Message; + +import java.util.Scanner; + +public class CustomerMenu implements Menu{ + + //singleton + private static CustomerMenu customerMenu; + + private final Customers allCustomers = Customers.getInstance(); + private final Groups allGroups = Groups.getInstance(); + + Scanner sc = new Scanner(System.in); + + 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; // choice == 5 + } + } + + + public void addCustomer() { + while(true){ + try{ + System.out.println("How many customers to input?"); + int n = Integer.parseInt(nextLine(Message.END_MSG)); + + for(int i = 0; i < n; i++){ + System.out.println("==============="); + System.out.println("User" + (i + 1) + " info"); + System.out.println("==============="); + Customer customer = new Customer(); + chooseCustomerInfo(customer); + allCustomers.add(customer); + + } + + allCustomers.refresh(allGroups); + break; + + }catch (InputEndException e){ + System.out.println(Message.ERR_MSG_INPUT_END); + break; + }catch (NumberFormatException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } + } + + + } + + public void viewCustomer(){ + System.out.println(allCustomers); + } + + public void updateCustomer(){ + chooseCustomerInfo(allCustomers.get(pickCustomer())); + + allCustomers.refresh(allGroups); + } + + private void deleteCustomer() { + Customer customer = allCustomers.pop(pickCustomer()); + } + + public int pickCustomer() { + for (int i = 0; i < allCustomers.size(); i++) { + System.out.println("No. " + (i + 1) + " => " + allCustomers.get(i)); + } + while(true){ + try { + System.out.print("Which customer ( 1 ~ " + allCustomers.size() + " )? "); + int n = Integer.parseInt(nextLine()); + + if(n > allCustomers.size()){ + System.out.println("Index Out of Bounds. Please try again."); + continue; + } + + return n; + } catch (NumberFormatException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } + } + + } + + public void chooseCustomerInfo(Customer customer){ + while( true ){ + try{ + int choice = chooseMenu(new String[]{ + "Customer Name", + "Customer ID", + "Customer Spent Time", + "Customer Total Pay", + "Back"}); + + if(choice == 1){ + System.out.println("Input Customer's name:"); + String name = nextLine(Message.END_MSG); + customer.setCusName(name); + } + else if(choice == 2){ + System.out.println("'Input Customer's ID:"); + String id = nextLine(Message.END_MSG); + customer.setCusId(id); + } + else if(choice == 3){ + System.out.println("'Input Customer's Spent Time:"); + int spentTime = Integer.parseInt(nextLine(Message.END_MSG)); + customer.setCusTotalTime(spentTime); + } + else if(choice == 4){ + System.out.println("'Input Customer's Total Pay:"); + int totalPay = Integer.parseInt(nextLine(Message.END_MSG)); + customer.setCusTotalPay(totalPay); + } + else break; // choice == 5 + }catch (InputEndException e){ + System.out.println(Message.ERR_MSG_INPUT_END); + } + + } + + } +} diff --git a/me/smartstore/menu/GroupMenu.java b/me/smartstore/menu/GroupMenu.java new file mode 100644 index 00000000..3c055bc7 --- /dev/null +++ b/me/smartstore/menu/GroupMenu.java @@ -0,0 +1,171 @@ +package me.smartstore.menu; + +import me.smartstore.customer.Customers; +import me.smartstore.exception.InputEndException; +import me.smartstore.exception.InputTypeException; +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) { + 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); + + GroupType groupType = GroupType.valueOf(choice).replaceFullName(); + return groupType; + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + break; + } catch (IllegalArgumentException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + return null; + } + + public void setParameter() { + while (true) { + GroupType groupType = chooseGroup(); + + Group group = allGroups.find(groupType); + if (group != null && group.getGroupType() != null) { + System.out.println("\n" + group.getGroupType() + "group already exists."); + System.out.println("\n" + group); + } else if(group == null){ + return; + }else { + Parameter parameter = new Parameter(); + //time, pay 사용자 입력받은 후, 설정 필요 + + group.setParameter(parameter); + allCustomers.refresh(allGroups); + } + } + } + + public void viewParameter() { + while (true) { + GroupType groupType = chooseGroup(); + + Group group = allGroups.find(groupType); + if (group == null || group.getGroupType() == null) + break; + else { + System.out.println("\n" + group.getGroupType()); + System.out.println("\n" + group); + } + } + } + + public void updateParameter() { + while (true) { + GroupType groupType = chooseGroup(); + + Group group = allGroups.find(groupType); + if (group == null || group.getGroupType() == null) + return; + else { + chooseParameter(group); + } + } + + } + + public void chooseParameter(Group group){ + while( true ){ + int choice = chooseMenu(new String[]{ + "Minimum Spent Time", + "Minimum Total Pay", + "Back"}); + try{ + if (choice == 1) { + inputTime(group); + allCustomers.refresh(allGroups); + }else if ( choice == 2){ + inputPay(group); + allCustomers.refresh(allGroups); + }else { + System.out.println(group.toString()); + break; //choice == 3 + } + }catch (InputTypeException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_TYPE); + } + } + } + + public int inputTime(Group group){ + int minTime = 0; + while(true){ + try{ + System.out.println("Input Minimum Spent Time: "); + minTime = Integer.parseInt(nextLine(Message.END_MSG)); + }catch (InputTypeException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_TYPE); + }catch (NumberFormatException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + }catch (InputEndException e){ + System.out.println(Message.ERR_MSG_INPUT_END); + break; + } + } + return minTime; + } + + public int inputPay(Group group){ + int totalPay = 0; + while(true){ + try{ + System.out.println("Input Minimum Total pay: "); + totalPay = Integer.parseInt(nextLine(Message.END_MSG)); + }catch (InputTypeException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_TYPE); + }catch (NumberFormatException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + }catch (InputEndException e){ + System.out.println(Message.ERR_MSG_INPUT_END); + break; + } + } + return totalPay; + } +} \ No newline at end of file diff --git a/me/smartstore/menu/MainMenu.java b/me/smartstore/menu/MainMenu.java new file mode 100644 index 00000000..952ead3c --- /dev/null +++ b/me/smartstore/menu/MainMenu.java @@ -0,0 +1,41 @@ +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 ){ + int choice = 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{ + System.out.println("Program Finished"); + break; + } + } + } +} diff --git a/me/smartstore/menu/Menu.java b/me/smartstore/menu/Menu.java new file mode 100644 index 00000000..b5805ddd --- /dev/null +++ b/me/smartstore/menu/Menu.java @@ -0,0 +1,49 @@ +package me.smartstore.menu; + +import me.smartstore.exception.InputEndException; +import me.smartstore.exception.InputRangeException; +import me.smartstore.util.Message; + +import java.util.Scanner; + +public interface Menu { + Scanner sc = new Scanner(System.in); + + default String nextLine(){ + return sc.nextLine().toUpperCase(); + + } + + default String nextLine(String end){ + System.out.println("** Press 'end', if you want to exit! **"); + String str = nextLine().toUpperCase(); + if(str.equals(end)) throw new InputEndException(); + return str; + + } + + default int chooseMenu(String[] menus){ + while( true ){ + 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 (InputRangeException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } catch (NumberFormatException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } + } + } + + //각 서브 메뉴들을 관리하는 메소드 + void manage(); +} diff --git a/me/smartstore/menu/SummaryMenu.java b/me/smartstore/menu/SummaryMenu.java new file mode 100644 index 00000000..100f9584 --- /dev/null +++ b/me/smartstore/menu/SummaryMenu.java @@ -0,0 +1,257 @@ +package me.smartstore.menu; + +import me.smartstore.customer.Customer; +import me.smartstore.customer.Customers; +import me.smartstore.exception.InputEndException; +import me.smartstore.exception.InputTypeException; +import me.smartstore.group.Group; +import me.smartstore.group.Groups; +import me.smartstore.util.Message; + +import java.util.Arrays; +import java.util.Comparator; + +public class SummaryMenu implements Menu { + + //singleton + private static SummaryMenu summaryMenu; + + private final Groups allGroups = Groups.getInstance(); + private final Customers allCustomers = Customers.getInstance(); + + public static SummaryMenu getInstance(){ + if(summaryMenu == null) + summaryMenu = new SummaryMenu(); + return summaryMenu; + } + + private SummaryMenu(){} + + @Override + public void manage() { + while ( true ){ + int choice = chooseMenu(new String[]{ + "Summary", + "Summary (Sorted By Name)", + "Summary (Sorted By Time)", + "Summary (Sorted By Pay)", + "Back"}); + + if(choice == 1) + summary(); + else if(choice == 2) + summaryByName(); + else if(choice == 3) + summaryByTime(); + else if(choice == 4) + summaryByPay(); + else break; // choice == 5 + } + } + + public void summary(){ + Customer[] customers = toArray(); + for(int i = 0; i < allGroups.size(); i++){ + Group group = allGroups.get(i); + System.out.println("=============================="); + System.out.println("Group : " + group.getGroupType() + + " ( Time : " + group.getParameter().getMinTime() + ", Pay : " + + group.getParameter().getMinPay() + " )"); + System.out.println("=============================="); + + for(int j = 0; j < customers.length; j++){ + if(allGroups.get(i).getGroupType().equals(customers[j].getGroup().getGroupType())){ + System.out.println("No. " + (j + 1) + " => " + customers[j]); + } + } + } + } + + public void summaryByName(){ + Customer[] customers = toArray(); + String order = ""; + while(true){ + System.out.println("Which order (ASCENDING (A), DESCENDING (D))?"); + try{ + order = nextLine(Message.END_MSG); + + if(order.equals("A")){ + Arrays.sort(customers, new Comparator() { + @Override + public int compare(Customer o1, Customer o2) { + return o1.getCusName().compareTo(o2.getCusName()); + } + }); + + for(int i = 0; i < allGroups.size(); i++){ + Group group = allGroups.get(i); + System.out.println("=============================="); + System.out.println("Group : " + group.getGroupType() + + " ( Time : " + group.getParameter().getMinTime() + ", Pay : " + + group.getParameter().getMinPay() + " )"); + System.out.println("=============================="); + + for(int j = 0; j < customers.length; j++){ + if(allGroups.get(i).getGroupType().equals(customers[j].getGroup().getGroupType())){ + System.out.println("No. " + (j + 1) + " => " + customers[j]); + } + } + } + } + + if(order.equals("D")){ + Arrays.sort(customers, new Comparator() { + @Override + public int compare(Customer o1, Customer o2) { + return o1.getCusName().compareTo(o2.getCusName()) * -1; + } + }); + + for(int i = 0; i < allGroups.size(); i++){ + Group group = allGroups.get(i); + System.out.println("=============================="); + System.out.println("Group : " + group.getGroupType() + + " ( Time : " + group.getParameter().getMinTime() + ", Pay : " + + group.getParameter().getMinPay() + " )"); + System.out.println("=============================="); + + for(int j = 0; j < customers.length; j++){ + if(allGroups.get(i).getGroupType().equals(customers[j].getGroup().getGroupType())){ + System.out.println("No. " + (j + 1) + " => " + customers[j]); + } + } + } + } + + }catch (InputEndException e){ + System.out.println(Message.ERR_MSG_INPUT_END); + break; + } + } + + + + } + + private void summaryByTime() { + Customer[] customers = toArray(); + String order = ""; + while(true){ + System.out.println("Which order (ASCENDING (A), DESCENDING (D))?"); + try{ + order = nextLine(Message.END_MSG); + + if(order.equals("A")){ + Arrays.sort(customers, new Comparator() { + @Override + public int compare(Customer o1, Customer o2) { + return o1.getCusTotalTime().compareTo(o2.getCusTotalTime()); + } + }); + + for(int i = 0; i < allGroups.size(); i++){ + Group group = allGroups.get(i); + System.out.println("=============================="); + System.out.println("Group : " + group.getGroupType() + + " ( Time : " + group.getParameter().getMinTime() + ", Pay : " + + group.getParameter().getMinPay() + " )"); + System.out.println("=============================="); + + for(int j = 0; j < customers.length; j++){ + if(allGroups.get(i).getGroupType().equals(customers[j].getGroup().getGroupType())){ + System.out.println("No. " + (j + 1) + " => " + customers[j]); + } + } + } + } + + if(order.equals("D")) { + Arrays.sort(customers, new Comparator() { + @Override + public int compare(Customer o1, Customer o2) { + return o1.getCusTotalTime().compareTo(o2.getCusTotalTime()) * -1; + } + }); + + for(int i = 0; i < allGroups.size(); i++){ + Group group = allGroups.get(i); + System.out.println("=============================="); + System.out.println("Group : " + group.getGroupType() + + " ( Time : " + group.getParameter().getMinTime() + ", Pay : " + + group.getParameter().getMinPay() + " )"); + System.out.println("=============================="); + + for(int j = 0; j < customers.length; j++){ + if(allGroups.get(i).getGroupType().equals(customers[j].getGroup().getGroupType())){ + System.out.println("No. " + (j + 1) + " => " + customers[j]); + } + } + } + } + + }catch (InputEndException e){ + System.out.println(Message.ERR_MSG_INPUT_END); + break; + } + + + } + + } + + public void summaryByPay(){ + Customer[] customers = toArray(); + String order = ""; + + while(true){ + System.out.println("Which order (ASCENDING (A), DESCENDING (D))?"); + try{ + order = nextLine(Message.END_MSG); + + if(order.equals("A")) + Arrays.sort(customers, new Comparator() { + @Override + public int compare(Customer o1, Customer o2) { + return o1.getCusTotalPay().compareTo(o2.getCusTotalPay()); + } + }); + if(order.equals("D")) + Arrays.sort(customers, new Comparator() { + @Override + public int compare(Customer o1, Customer o2) { + return o1.getCusTotalPay().compareTo(o2.getCusTotalPay()) * -1; + } + }); + + for(int i = 0; i < allGroups.size(); i++){ + Group group = allGroups.get(i); + System.out.println("=============================="); + System.out.println("Group : " + group.getGroupType() + + " ( Time : " + group.getParameter().getMinTime() + ", Pay : " + + group.getParameter().getMinPay() + " )"); + System.out.println("=============================="); + + for(int j = 0; j < customers.length; j++){ + if(allGroups.get(i).getGroupType().equals(customers[j].getGroup().getGroupType())){ + System.out.println("No. " + (j + 1) + " => " + customers[j]); + } + } + } + }catch (InputEndException e){ + System.out.println(Message.ERR_MSG_INPUT_END); + break; + } + } + + + } + public Customer[] toArray(){ + Customer[] customers = new Customer[allCustomers.size()]; + for(int i = 0; i < allCustomers.size(); i++){ + customers[i] = allCustomers.get(i); + } + + return customers; + } + +} diff --git a/me/smartstore/util/Message.java b/me/smartstore/util/Message.java new file mode 100644 index 00000000..328958cc --- /dev/null +++ b/me/smartstore/util/Message.java @@ -0,0 +1,14 @@ +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 ERR_MSG_INVALID_INDEX_BOUND = "Index Out of Bounds. Please try again."; + String END_MSG = "END"; +}