diff --git a/me/smartstore/Main.java b/me/smartstore/Main.java new file mode 100644 index 00000000..291f0fa5 --- /dev/null +++ b/me/smartstore/Main.java @@ -0,0 +1,8 @@ +package me.smartstore; + +public class Main { + public static void main(String[] args) { + SmartStoreApp.getInstance().test().run(); // function chaining +// SmartStoreApp.getInstance().run(); + } +} diff --git a/me/smartstore/SmartStoreApp.java b/me/smartstore/SmartStoreApp.java new file mode 100644 index 00000000..c8ddd316 --- /dev/null +++ b/me/smartstore/SmartStoreApp.java @@ -0,0 +1,64 @@ +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.05.09"); + System.out.println(" Copyright 2023 Inyoung 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); + + allCustomers.refresh(allGroups); + + return this; + } + + public void run() { + details(); + mainMenu.manage(); + + } +} diff --git a/me/smartstore/arrays/Collections.java b/me/smartstore/arrays/Collections.java new file mode 100644 index 00000000..8f6564cc --- /dev/null +++ b/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); +} \ 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..6b273986 --- /dev/null +++ b/me/smartstore/arrays/DArray.java @@ -0,0 +1,140 @@ +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; + + @SuppressWarnings("unchecked") + public DArray() { + arrays = (T[]) new Object[DEFAULT]; + capacity = DEFAULT; + } + + @SuppressWarnings("unchecked") + public DArray(int initial) { + arrays = (T[]) new Object[initial]; + capacity = initial; + } + + public DArray(T[] arrays) { + this.arrays = arrays; + capacity = arrays.length; + size = arrays.length; + } + + @Override + public int size() { + return size; + } + + // 배열에 얼마나 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]; + } + + + @Override + public void set(int index, T object) throws IndexOutOfBoundsException, NullArgumentException { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); + if (object == null) throw new NullArgumentException(); + + arrays[index] = object; + } + + @Override + public int indexOf(T object) throws NullArgumentException, ElementNotFoundException { + if (object == null) throw new NullArgumentException(); // not found (instead of throwing exception) + + for (int i = 0; i < size; i++) { + if (arrays[i] == null) continue; + if (arrays[i].equals(object)) return i; + } + throw new ElementNotFoundException(); // not found + } + + @Override + public void add(T object) throws NullArgumentException { + if (object == null) throw new NullArgumentException(); // if argument is null, do not add null value in array + + if (size < capacity) { + arrays[size] = object; + size++; + } else { + grow(); + add(object); + } + } + + @Override + public void add(int index, T object) throws IndexOutOfBoundsException, NullArgumentException { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); + if (object == null) throw new NullArgumentException(); + + if (size < capacity) { + for (int i = size-1; i >= index ; i--) { + arrays[i+1] = arrays[i]; + } + arrays[index] = object; + size++; + } else { + grow(); + add(index, object); + } + } + + @Override + public T pop() { + return pop(size-1); + } + + @Override + public T pop(int index) throws IndexOutOfBoundsException { + if (size == 0) throw new EmptyArrayException(); + if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); + + T popElement = arrays[index]; + arrays[index] = null; // 삭제됨을 명시적으로 표현 + + for (int i = index+1; i < size; i++) { + arrays[i-1] = arrays[i]; + } + arrays[size-1] = null; + size--; + return popElement; + } + + @Override + public T pop(T object) { + return pop(indexOf(object)); + } + + protected void grow() { + capacity *= 2; // doubling + arrays = java.util.Arrays.copyOf(arrays, capacity); + + // 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/customer/Customer.java b/me/smartstore/customer/Customer.java new file mode 100644 index 00000000..87d0ba12 --- /dev/null +++ b/me/smartstore/customer/Customer.java @@ -0,0 +1,100 @@ +package me.smartstore.customer; + +import me.smartstore.group.Group; + +import java.util.Objects; + +public class Customer implements Comparable{ + 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 + + '}'; + } + + @Override + public int compareTo(Customer o) { + return this.cusName.compareTo(o.cusName); + } +} diff --git a/me/smartstore/customer/Customers.java b/me/smartstore/customer/Customers.java new file mode 100644 index 00000000..ea5e2b26 --- /dev/null +++ b/me/smartstore/customer/Customers.java @@ -0,0 +1,51 @@ +package me.smartstore.customer; + +import me.smartstore.arrays.DArray; +import me.smartstore.group.Group; +import me.smartstore.group.Groups; + +public class Customers extends DArray { + + private static Customers allCustomers; + private final Groups allGroups = Groups.getInstance(); + + public static Customers getInstance() { + if (allCustomers == null) { + allCustomers = new Customers(); + } + return allCustomers; + } + + private Customers() { + } + + public void refresh(Customer customer) { + int time = customer.getCusTotalTime(); + int pay = customer.getCusTotalPay(); + for (int i = 0; i < allGroups.size(); i++) { + if (time >= allGroups.get(i).getParameter().getMinTime() + && pay >= allGroups.get(i).getParameter().getMinPay()) { + customer.setGroup(allGroups.get(i)); + } + } + } + + public void refresh(Groups groups) { + for (int i = 0; i < allCustomers.size(); i++) { + Customer customer = allCustomers.get(i); + Group customerGroup = customer.getGroup(); + if (customerGroup == null) { + continue; + } + for (int j = 0; j < groups.size(); j++) { + Group group = groups.get(j); + if (customer.getCusTotalTime() >= group.getParameter().getMinTime() + && customer.getCusTotalPay() >= group.getParameter().getMinPay()) { + customer.setGroup(group); + break; + } + } + } + } +} + diff --git a/me/smartstore/exception/ElementNotFoundException.java b/me/smartstore/exception/ElementNotFoundException.java new file mode 100644 index 00000000..ecebe6f1 --- /dev/null +++ b/me/smartstore/exception/ElementNotFoundException.java @@ -0,0 +1,12 @@ +package me.smartstore.exception; +import me.smartstore.util.Message; +public class ElementNotFoundException extends RuntimeException { + public ElementNotFoundException(){ + super(Message.ERR_MSG_NULL_ARR_ELEMENT); + } + + public ElementNotFoundException(String message){ + super(message); + } + +} diff --git a/me/smartstore/exception/EmptyArrayException.java b/me/smartstore/exception/EmptyArrayException.java new file mode 100644 index 00000000..3f8005bc --- /dev/null +++ b/me/smartstore/exception/EmptyArrayException.java @@ -0,0 +1,12 @@ +package me.smartstore.exception; + +import me.smartstore.util.Message; + +public class EmptyArrayException extends RuntimeException { + public EmptyArrayException(){ + super(Message.ERR_MSG_INVALID_ARR_EMPTY); + } + public EmptyArrayException(String message){ + super(message); + } +} diff --git a/me/smartstore/exception/InputEmptyException.java b/me/smartstore/exception/InputEmptyException.java new file mode 100644 index 00000000..93163bc6 --- /dev/null +++ b/me/smartstore/exception/InputEmptyException.java @@ -0,0 +1,15 @@ +package me.smartstore.exception; + + +import me.smartstore.util.Message; + +public class InputEmptyException extends RuntimeException { + + public InputEmptyException() { + super(Message.ERR_MSG_INVALID_INPUT_EMPTY); + } + + public InputEmptyException(String message) { + super(message); + } +} diff --git a/me/smartstore/exception/InputEndException.java b/me/smartstore/exception/InputEndException.java new file mode 100644 index 00000000..f549a39e --- /dev/null +++ b/me/smartstore/exception/InputEndException.java @@ -0,0 +1,14 @@ +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); + } +} diff --git a/me/smartstore/exception/InputFormatException.java b/me/smartstore/exception/InputFormatException.java new file mode 100644 index 00000000..84faad3c --- /dev/null +++ b/me/smartstore/exception/InputFormatException.java @@ -0,0 +1,14 @@ +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); + } +} diff --git a/me/smartstore/exception/InputRangeException.java b/me/smartstore/exception/InputRangeException.java new file mode 100644 index 00000000..fb2c76df --- /dev/null +++ b/me/smartstore/exception/InputRangeException.java @@ -0,0 +1,14 @@ +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..d321992f --- /dev/null +++ b/me/smartstore/exception/InputTypeException.java @@ -0,0 +1,14 @@ +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..84d72f58 --- /dev/null +++ b/me/smartstore/exception/NullArgumentException.java @@ -0,0 +1,13 @@ +package me.smartstore.exception; + +import me.smartstore.util.Message; + +public class NullArgumentException extends RuntimeException { + public NullArgumentException(){ + super(Message.ERR_MSG_INVALID_INPUT_NULL); + } + + public NullArgumentException(String message){ + super(message); + } +} diff --git a/me/smartstore/group/Group.java b/me/smartstore/group/Group.java new file mode 100644 index 00000000..92182c6a --- /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(GroupType groupType) { + } + + 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..d6ccca37 --- /dev/null +++ b/me/smartstore/group/GroupType.java @@ -0,0 +1,22 @@ +package me.smartstore.group; + +public enum GroupType { + NONE("해당없음"), GENERAL("일반고객"), VIP("우수고객"), VVIP("최우수고객"), + N("해당없음"), G("일반고객"), V("우수고객"), VV("최우수고객"); + + + String groupType = ""; + + + GroupType(String groupType) { + this.groupType = groupType; + } + + public GroupType replaceFullName() { + if (this == N) return NONE; + else if (this == G) return GENERAL; + else if (this == V) return VIP; + else if (this == VV) return VVIP; + return this; + } +} diff --git a/me/smartstore/group/Groups.java b/me/smartstore/group/Groups.java new file mode 100644 index 00000000..e926efd9 --- /dev/null +++ b/me/smartstore/group/Groups.java @@ -0,0 +1,27 @@ +package me.smartstore.group; + + +import me.smartstore.arrays.DArray; + +public class Groups extends DArray { + // singleton + private static Groups allGroups; + + public static Groups getInstance() { + if (allGroups == null) { + allGroups = new Groups(); + } + return allGroups; + } + + private Groups() {} + + public Group find(GroupType groupType) { + for (int i = 0; i < allGroups.size(); i++) { + if (allGroups.get(i).getGroupType() == groupType) { + return allGroups.get(i); + } + } + return null; + } +} diff --git a/me/smartstore/group/Parameter.java b/me/smartstore/group/Parameter.java new file mode 100644 index 00000000..9b711f41 --- /dev/null +++ b/me/smartstore/group/Parameter.java @@ -0,0 +1,54 @@ +package me.smartstore.group; + +import java.util.Objects; + +public class Parameter { + private Integer minTime; + private Integer minPay; + + + public Parameter() { + } + + public Parameter(Integer minTime, Integer minPay) { + this.minTime = minTime; + this.minPay = minPay; + } + + public Integer getMinTime() { + return minTime; + } + + public void setMinTime(Integer minTime) { + this.minTime = minTime; + } + + public Integer getMinPay() { + return minPay; + } + + public void setMinPay(Integer minPay) { + this.minPay = minPay; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Parameter parameter = (Parameter) o; + return Objects.equals(minTime, parameter.minTime) && Objects.equals(minPay, parameter.minPay); + } + + @Override + public int hashCode() { + return Objects.hash(minTime, minPay); + } + + @Override + public String toString() { + return "Parameter{" + + "minTime=" + minTime + + ", minPay=" + minPay + + '}'; + } +} diff --git a/me/smartstore/menu/CustomerMenu.java b/me/smartstore/menu/CustomerMenu.java new file mode 100644 index 00000000..093e6599 --- /dev/null +++ b/me/smartstore/menu/CustomerMenu.java @@ -0,0 +1,180 @@ +package me.smartstore.menu; + +import me.smartstore.customer.Customer; +import me.smartstore.customer.Customers; +import me.smartstore.exception.EmptyArrayException; +import me.smartstore.exception.InputEndException; +import me.smartstore.exception.InputRangeException; +import me.smartstore.group.Groups; +import me.smartstore.util.Message; + + +public class CustomerMenu implements Menu { + private final Groups allGroups = Groups.getInstance(); + private final Customers allCustomers = Customers.getInstance(); + // singleton + private static CustomerMenu customerMenu; + + public static CustomerMenu getInstance() { + if (customerMenu == null) { + customerMenu = new CustomerMenu(); + } + return customerMenu; + } + + private CustomerMenu() { + } + + @Override + public void manage() { + while (true) { // 서브 메뉴 페이지를 유지하기 위한 while + int choice = chooseMenu(new String[]{ + "Add Customer", + "View Customer", + "Update Customer", + "Delete Customer", + "Back"}); + + if (choice == 1) addCustomer(); + else if (choice == 2) { + viewCustomer(); + } else if (choice == 3) { + updateCustomer(); + } else if (choice == 4) { + deleteCustomer(); + } else break; + } + } + + public void addCustomer() { + + System.out.println("Enter customer name: "); + String name = nextLine(); + System.out.println("Enter customer ID: "); + String id = nextLine(); + for (int i = 0; i < allCustomers.size(); i++) { + if (allCustomers.get(i).getCusId().equals(id)) { + System.out.println("ID already exists"); + return; + } + } + System.out.println("Enter total time"); + int time; + while (true) { + try { + time = Integer.parseInt(nextLine()); + if (time < 0) throw new InputRangeException(); + break; + } catch (NumberFormatException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } catch (InputRangeException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + + System.out.println("Enter total pay"); + int pay; + while (true) { + try { + pay = Integer.parseInt(nextLine()); + if (pay < 0) throw new InputRangeException(); + break; + } catch (NumberFormatException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } catch (InputRangeException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + + Customer customer = new Customer(name, id, time, pay); + allCustomers.add(customer); + allCustomers.refresh(customer); + System.out.println("** Add SUCCESS **" + customer); + + } + + public void viewCustomer() { + try { + for (int i = 0; i < allCustomers.size(); i++) { + System.out.println(allCustomers.get(i).toString()); + } + } catch (EmptyArrayException e) { + System.out.println(Message.ERR_MSG_NULL_ARR_ELEMENT); + } + } + + public void updateCustomer() { + Customer customer = null; + + System.out.println("Enter customer ID: "); + String id = scanner.nextLine(); + + for (int i = 0; i < allCustomers.size(); i++) { + if (id != null && allCustomers.get(i).getCusId().equals(id)) { + customer = allCustomers.get(i); + System.out.println(customer.toString()); + break; + } + } + if (customer == null) { + System.out.println("customer ID does not exist"); + return; + } + + System.out.println("***** Choose Update *****"); + while (true) { + try { + int choice = chooseMenu(new String[]{ + "Customer Name", + "Customer Total Time", + "Customer Total Pay", + "Back"}); + + if (choice == 1) { + System.out.println("Update customer Name : "); + String setName = scanner.nextLine(); + customer.setCusName(setName); + } else if (choice == 2) { + System.out.println("Update customer Total time : "); + int setTime = Integer.parseInt(scanner.nextLine()); + customer.setCusTotalTime(setTime); + } else if (choice == 3) { + System.out.println("Update customer Total Pay : "); + int setPay = Integer.parseInt(scanner.nextLine()); + customer.setCusTotalPay(setPay); + } else break; + System.out.println(customer); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + } + allCustomers.refresh(allGroups); + } + } + + + public void deleteCustomer() { + + System.out.println("Enter customer ID : "); + String id = scanner.nextLine(); + int index = -1; + try { + for (int i = 0; i < allCustomers.size(); i++) { + if (id != null && allCustomers.get(i).getCusId().equals(id)) { + index = i; + break; + } + } + } catch (EmptyArrayException e) { + System.out.println(Message.ERR_MSG_INVALID_ARR_EMPTY); + } + + if (index == -1) { + System.out.println("customer ID does not exist"); + } + try { + System.out.println("**Delete Success**" + allCustomers.pop(index)); + } catch (IndexOutOfBoundsException e) { + System.out.println(Message.ERR_MSG_INVALID_ARR_INDEX); + } + } +} diff --git a/me/smartstore/menu/GroupMenu.java b/me/smartstore/menu/GroupMenu.java new file mode 100644 index 00000000..6736e7ec --- /dev/null +++ b/me/smartstore/menu/GroupMenu.java @@ -0,0 +1,169 @@ +package me.smartstore.menu; + + +import me.smartstore.customer.Customers; +import me.smartstore.exception.EmptyArrayException; +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) { + 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.print("Which group (GENERAL (G), VIP (V), VVIP (VV))? "); + String choice = nextLine(Message.END_MSG); + + GroupType groupType = GroupType.valueOf(choice).replaceFullName(); + return groupType; + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + return null; + } catch (IllegalArgumentException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + } + + + public void setParameter() { + while (true) { + GroupType groupType = chooseGroup(); + if (groupType == null) { + break; + } + Group group = allGroups.find(groupType); + if (group != null && group.getParameter() != null) { + System.out.println("\n" + group.getGroupType() + " group already exists."); + System.out.println("\n" + group); + } else { + Parameter parameter = new Parameter(); + System.out.println("Enter min time"); + int time; + while (true) { + try { + time = Integer.parseInt(nextLine()); + if (time < 0) throw new InputRangeException(); + break; + } catch (NumberFormatException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } catch (InputRangeException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + parameter.setMinTime(time); + + System.out.println("Enter min pay"); + int pay; + while (true) { + try { + pay = Integer.parseInt(nextLine()); + if (pay < 0) throw new InputRangeException(); + break; + } catch (NumberFormatException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } catch (InputRangeException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + parameter.setMinPay(pay); + + group.setParameter(parameter); + allCustomers.refresh(allGroups); + } + } + } + + public void viewParameter() { + while (true) { + GroupType groupType = chooseGroup(); + if (groupType == null) { + break; + } + try { + Group group = allGroups.find(groupType); + System.out.println(groupType); + System.out.println(group.getParameter()); + } catch (EmptyArrayException e) { + System.out.println(Message.ERR_MSG_INVALID_ARR_EMPTY); + } + } + } + + public void updateParameter() { + while (true) { + GroupType groupType = chooseGroup(); + if (groupType == null) { + break; + } + + Group group = allGroups.find(groupType); + if (group == null || group.getParameter() == null) { + System.out.println("\n" + groupType + " group does not exist or parameter is not set."); + } else { + System.out.println("\nCurrent parameter: " + group.getParameter()); + + while (true) { + try { + System.out.print("Enter minimum time \n"); + int minTime = Integer.parseInt(nextLine(Message.END_MSG)); + + System.out.print("Enter minimum pay \n"); + int minPay = Integer.parseInt(nextLine(Message.END_MSG)); + + group.getParameter().setMinTime(minTime); + group.getParameter().setMinPay(minPay); + + allCustomers.refresh(allGroups); + + System.out.println("\nUpdated parameter: " + group.getParameter()); + break; + } catch (NumberFormatException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_TYPE); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + break; + } + } + } + allCustomers.refresh(allGroups); + } + } +} \ 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..a7768657 --- /dev/null +++ b/me/smartstore/menu/MainMenu.java @@ -0,0 +1,39 @@ +package me.smartstore.menu; + +public class MainMenu implements Menu { + + private final CustomerMenu customerMenu = CustomerMenu.getInstance(); + private final GroupMenu groupMenu = GroupMenu.getInstance(); + private final SummaryMenu summaryMenu = SummaryMenu.getInstance(); + + // singleton + private static MainMenu mainMenu; + + public static MainMenu getInstance() { + if (mainMenu == null) { + mainMenu = new MainMenu(); + } + return mainMenu; + } + + private MainMenu() {} + + @Override + public void manage() { + while ( true ) { // 프로그램 실행 while + int choice = mainMenu.chooseMenu(new String[] { + "Parameter", + "Customer", + "Classification Summary", + "Quit"}); + + if (choice == 1) groupMenu.manage(); + else if (choice == 2) customerMenu.manage(); + else if (choice == 3) summaryMenu.manage(); + else { // choice == 4 + System.out.println("Program Finished"); + break; + } + } + } +} diff --git a/me/smartstore/menu/Menu.java b/me/smartstore/menu/Menu.java new file mode 100644 index 00000000..5dc42701 --- /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.InputMismatchException; +import java.util.Scanner; + +public interface Menu { + Scanner scanner = new Scanner(System.in); + + default String nextLine() { // 하나의 프로그램 상에서 nextLine() 함수를 통해서 사용자 입력을 받음 + return scanner.nextLine().toUpperCase(); + } + + default String nextLine(String end) { + System.out.println("** Press 'end', if you want to exit! **"); + String str = scanner.nextLine().toUpperCase(); + if (str.equals(end)) throw new InputEndException(); + return str; + } + + default int chooseMenu(String[] menus) { + while ( true ) { // 예외 복구 while + try { + System.out.println("==============================="); + for (int i = 0; i < menus.length; i++) { + System.out.printf(" %d. %s\n", i + 1, menus[i]); + } + System.out.println("==============================="); + System.out.print("Choose One: "); + int choice = Integer.parseInt(nextLine()); + if (choice >= 1 && choice <= menus.length) return choice; + throw new InputRangeException(); // choice 가 범위에 벗어남 + + } catch (InputMismatchException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + + } catch (InputRangeException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + + } + } + } + + void manage(); // 각 서브메뉴들을 관리하는 함수 (각 서브메뉴의 최상위 메뉴) +} diff --git a/me/smartstore/menu/SummaryMenu.java b/me/smartstore/menu/SummaryMenu.java new file mode 100644 index 00000000..dc2802c8 --- /dev/null +++ b/me/smartstore/menu/SummaryMenu.java @@ -0,0 +1,104 @@ +package me.smartstore.menu; + +import me.smartstore.customer.Customer; +import me.smartstore.customer.Customers; +import me.smartstore.exception.EmptyArrayException; +import me.smartstore.group.Groups; +import me.smartstore.util.Message; + +import java.util.Arrays; +import java.util.Comparator; + + +public class SummaryMenu implements Menu { + 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) sortedByName(); + else if (choice == 3) sortedByTime(); + else if (choice == 4) sortedByPay(); + else break; + } + } + + private void summary() { + try { + for (int i = 0; i < allCustomers.size(); i++) { + System.out.println(allCustomers.get(i).toString()); + } + } catch (EmptyArrayException e) { + System.out.println(Message.ERR_MSG_NULL_ARR_ELEMENT); + } + } + + public void sortedByName() { + try { + Customer[] customers = new Customer[allCustomers.size()]; + for (int i = 0; i < customers.length; i++) { + customers[i] = allCustomers.get(i); + } + Arrays.sort(customers); + + for (Customer customer : customers) { + System.out.println(customer.toString()); + } + } catch (EmptyArrayException e) { + System.out.println(Message.ERR_MSG_INVALID_ARR_EMPTY); + } + } + + public void sortedByTime() { + try { + Customer[] customers = new Customer[allCustomers.size()]; + for (int i = 0; i < customers.length; i++) { + customers[i] = allCustomers.get(i); + } + Arrays.sort(customers, Comparator.comparing(Customer::getCusTotalTime)); + + for (Customer customer : customers) { + System.out.println(customer.toString()); + } + } catch (EmptyArrayException e) { + System.out.println(Message.ERR_MSG_NULL_ARR_ELEMENT); + } + } + + public void sortedByPay() { + try { + Customer[] customers = new Customer[allCustomers.size()]; + for (int i = 0; i < customers.length; i++) { + customers[i] = allCustomers.get(i); + } + Arrays.sort(customers, Comparator.comparing(Customer::getCusTotalPay)); + + for (Customer customer : customers) { + System.out.println(customer.toString()); + } + } catch (EmptyArrayException e) { + System.out.println(Message.ERR_MSG_NULL_ARR_ELEMENT); + } + } +} + + diff --git a/me/smartstore/util/Message.java b/me/smartstore/util/Message.java new file mode 100644 index 00000000..e3c04642 --- /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 END_MSG = "END"; + String ERR_MSG_INVALID_ARR_INDEX = "Invaild Input. Index out of Bounds."; +}