diff --git a/me.smartstore/src/Main.java b/me.smartstore/src/Main.java new file mode 100644 index 00000000..cc13b8be --- /dev/null +++ b/me.smartstore/src/Main.java @@ -0,0 +1,8 @@ +public class Main { + public static void main(String[] args) { + + SmartStoreApplication.getInstance().test().run(); + //SmartStoreApplication.getInstance().run(); + + } +} \ No newline at end of file diff --git a/me.smartstore/src/SmartStoreApplication.java b/me.smartstore/src/SmartStoreApplication.java new file mode 100644 index 00000000..4587a5a5 --- /dev/null +++ b/me.smartstore/src/SmartStoreApplication.java @@ -0,0 +1,52 @@ +import customers.Customer; +import customers.Customers; +import groups.*; +import menu.*; + +public class SmartStoreApplication { + + private static SmartStoreApplication smartStoreApp; + + public static SmartStoreApplication getInstance() { + if (smartStoreApp == null) { + smartStoreApp = new SmartStoreApplication(); + } + return smartStoreApp; + } + + private final Groups allGroups = Groups.getInstance(); + private final Customers allCustomers = Customers.getInstance(); + private final MainMenu mainMenu = MainMenu.getInstance(); + + + public SmartStoreApplication test() { + allGroups.add(new Group( GroupType.GENERAL, new Parameter(10, 100000)) ); + allGroups.add(new Group( GroupType.VIP, new Parameter(20, 200000)) ); + allGroups.add(new Group( GroupType.VVIP, new Parameter(30, 300000)) ); + + for (int i = 0; i < 20; ++i) { + allCustomers.add(new Customer( + Character.toString( + (char) ('a' + i)), + (char) ('a' + i) + "123", + ((int) (Math.random() * 5) + 1) * 10, + ((int) (Math.random() * 5) + 1) * 100000)); + } + + allCustomers.refresh(allGroups); + return this; + } + + private void details() { + System.out.println("\n==========================================="); + System.out.println(" Title : SmartStore Customer Segmentation"); + System.out.println(" Refer to EunBinChoi's GitHub."); + System.out.println("===========================================\n"); + } + + public void run() { + details(); + + mainMenu.manage(); + } +} diff --git a/me.smartstore/src/customers/ClassifiedCustomers.java b/me.smartstore/src/customers/ClassifiedCustomers.java new file mode 100644 index 00000000..81529510 --- /dev/null +++ b/me.smartstore/src/customers/ClassifiedCustomers.java @@ -0,0 +1,48 @@ +package customers; + +import groups.Group; + +import java.util.Arrays; +import java.util.Objects; + +public class ClassifiedCustomers extends Customers { + private Group group; + + public ClassifiedCustomers() {} + + public ClassifiedCustomers(Group group) { + this.group = group; + } + + + 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;} + ClassifiedCustomers that = (ClassifiedCustomers) o; + return Objects.equals(group, that.group); + } + + @Override + public int hashCode() { + return Objects.hash(group); + } + + @Override + public String toString() { + return "ClassifiedCustomers{" + + "group=" + group + + ", size=" + size + + ", customers=" + Arrays.toString(customers) + + '}'; + } +} \ No newline at end of file diff --git a/me.smartstore/src/customers/ClassifiedCustomersGroup.java b/me.smartstore/src/customers/ClassifiedCustomersGroup.java new file mode 100644 index 00000000..d5c0c488 --- /dev/null +++ b/me.smartstore/src/customers/ClassifiedCustomersGroup.java @@ -0,0 +1,106 @@ +package customers; + +import groups.GroupType; +import groups.Parameter; +import util.Message; + +import java.util.Arrays; +import java.util.Comparator; + +public class ClassifiedCustomersGroup { + + private static ClassifiedCustomersGroup classifiedCustomersGroup; + + public static ClassifiedCustomersGroup getInstance() { + if (classifiedCustomersGroup == null) { + classifiedCustomersGroup = new ClassifiedCustomersGroup(); + } + return classifiedCustomersGroup; + } + + private ClassifiedCustomers[] classifiedCustomers; + + public ClassifiedCustomersGroup() { + classifiedCustomers = new ClassifiedCustomers[GroupType.size()]; + + for (int i = 0; i < GroupType.size(); i++) { + classifiedCustomers[i] = new ClassifiedCustomers(); + } + } + + + public ClassifiedCustomers get(int i) { + return classifiedCustomers[i]; + } + + public void set(int i, ClassifiedCustomers customers) { + classifiedCustomers[i] = customers; + } + + public int size() { + return GroupType.size(); + } + + public void print() { + for (int i = 0; i < classifiedCustomers.length; i++) { + System.out.println("\n=============================="); + + if (classifiedCustomers[i] == null) return; + + GroupType groupType = classifiedCustomers[i].getGroup().getType(); + Parameter parameter = classifiedCustomers[i].getGroup().getParam(); + + System.out.printf("Group : %s ( Time : %d, Pay : %d )\n", + groupType, + parameter != null ? parameter.getMinimumSpentTime() : null, + parameter != null ? parameter.getMinimumTotalPay() : null); + System.out.println("=============================="); + + if (classifiedCustomers[i] == null || classifiedCustomers[i].isEmpty()) { + System.out.println("Null."); + continue; + } + classifiedCustomers[i].print(); + System.out.println("==============================\n"); + } + + } + + public void sort(Comparator comparator) { + + for (int i = 0; i < classifiedCustomersGroup.size(); i++) { + Customer[] customers = classifiedCustomersGroup.get(i).getCustomers(); + + try { + if (comparator == null) {Arrays.sort(customers);} + else {Arrays.sort(customers, comparator);} + + classifiedCustomersGroup.get(i).setCustomers(customers); + } + catch (NullPointerException e) { + System.out.println(Message.ERR_MSG_NULL_ARR_ELEMENT); + } + } + } + + @Override + public boolean equals(Object o) { + if (this == o) {return true;} + if (o == null || getClass() != o.getClass()) {return false;} + ClassifiedCustomersGroup that = (ClassifiedCustomersGroup) o; + return Arrays.equals(classifiedCustomers, that.classifiedCustomers); + } + + @Override + public int hashCode() { + return Arrays.hashCode(classifiedCustomers); + } + + @Override + public String toString() { + return "ClassifiedCustomersGroup{" + + "classifiedCustomers=" + + Arrays.toString(classifiedCustomers) + + '}'; + } +} \ No newline at end of file diff --git a/me.smartstore/src/customers/Customer.java b/me.smartstore/src/customers/Customer.java new file mode 100644 index 00000000..b82bcccb --- /dev/null +++ b/me.smartstore/src/customers/Customer.java @@ -0,0 +1,96 @@ +package customers; + +import groups.Group; + +import java.util.Objects; + +public class Customer implements Comparable { + + private String userId; + private String name; + private Integer spentTime; + private Integer totalPay; + private Group group; + + public Customer() { + } + + public Customer(String name, String userId, int spentTime, int totalPay) { + this.name = name; + this.userId = userId; + this.spentTime = spentTime; + this.totalPay = totalPay; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public Integer getSpentTime() { + return spentTime; + } + + public void setSpentTime(Integer spentTime) { + this.spentTime = spentTime; + } + + 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 int compareTo(Customer o) { + if (name.compareTo(o.name) < 0) {return -1;} + else if (name.compareTo(o.name) == 0) {return userId.compareTo(o.userId);} + else return 1; + + } + + @Override + public boolean equals(Object o) { + if (this == o) {return true;} + if (o == null || getClass() != o.getClass()) {return false;} + Customer customer = (Customer) o; + return Objects.equals(userId, customer.userId); + } + + @Override + public int hashCode() { + return Objects.hash(userId); + } + + @Override + public String toString() { + return "Customer{" + + "userId='" + userId + '\'' + + ", name='" + name + '\'' + + ", spentTime=" + spentTime + + ", totalPay=" + totalPay + + ", group=" + group + + '}'; + } +} \ No newline at end of file diff --git a/me.smartstore/src/customers/Customers.java b/me.smartstore/src/customers/Customers.java new file mode 100644 index 00000000..1d342ffb --- /dev/null +++ b/me.smartstore/src/customers/Customers.java @@ -0,0 +1,282 @@ +package customers; + +import groups.Group; +import groups.GroupType; +import groups.Groups; + +import java.util.Arrays; + +public class Customers { + + private static Customers allCustomers; + + public static Customers getInstance() { + if (allCustomers == null) { + allCustomers = new Customers(); + } + return allCustomers; + } + + + + private final Groups allGroups = Groups.getInstance(); + + protected static int DEFAULT_SIZE = 10; + protected int capacity; + protected int size; + protected Customer[] customers; + + + public Customers() { + customers = new Customer[DEFAULT_SIZE]; + capacity = DEFAULT_SIZE; + } + + public Customers(int initialCapacity) { + customers = new Customer[initialCapacity]; + capacity = initialCapacity; + } + + public Customers(Customer[] customers) { + this.customers = customers; + capacity = customers.length; + size = customers.length; + } + + public void setCustomers(Customer[] customers) { + this.customers = customers; + } + + public Customer[] getCustomers() { + return customers; + } + + public Customer[] getRealCustomers() { + int real = 0; + for (int i = 0; i < size; i++) { + if (customers[i] != null) real++; + } + size = real; + return Arrays.copyOf(customers, real); + } + + public int getCapacity() { + return capacity; + } + + public void setCapacity(int capacity) { + this.capacity = capacity; + } + + public int getSize() { + return size; + } + + public void setSize(int size) { + this.size = size; + } + + public int capacity() { + return capacity; + } + + public int size() { + return size; + } + + private boolean isNull() { + return customers == null; + } + + public boolean isEmpty() { + return size == 0; + } + + public void set(int index, Customer customer) { + + if (!(index >= 0 && index < size)) {return;} + if (customer == null) {return;} + + customers[index] = customer; + } + + public Customer get(int index) { + + if (!(index >= 0 && index < size)) {return null;} + + return customers[index]; + } + + public int indexOf(Customer customer) { + if (customer == null) {return -1;} + + for (int i = 0; i < size; i++) { + if (customers[i] == null) {continue;} + if (customers[i].equals(customer)) {return i;} + } + return -1; + } + + public void add(Customer customer) { + if (customer == null) {return;} + + if (size < capacity) { + customers[size] = customer; + size++; + } + else { + grow(); + add(customer); + } + } + + public void add(int index, Customer customer) { + if (!(index >= 0 && index <= size)) {return;} + if (customer == null) {return;} + + if (size < capacity) { + + for (int i = customers.length - 1; i >= index; i--) { + customers[i + 1] = customers[i]; + } + + customers[index] = customer; + size++; + } + else { + grow(); + add(index, customer); + } + } + + public void grow() { + Customer[] copy = Arrays.copyOf(customers, customers.length); + capacity *= 2; + customers = new Customer[capacity]; + + System.arraycopy(copy, 0, customers, 0, copy.length); + + size = copy.length; + + } + + public void pop(int index) { + if (size == 0) {return;} + if (!(index >= 0 && index < size)) {return;} + + customers[index] = null; //형식임. + + for (int j = index + 1; j < size; j++) { + customers[j - 1] = customers[j]; + } + + customers[size - 1] = null; + size--; + } + + public void pop() { + if (size == 0) {return;} + + customers[size - 1] = null; + size--; + } + + public void pop(Customer customer) { + if (size == 0) {return;} + if (customer == null) {return;} + + pop(indexOf(customer)); + } + + public Customers trimToSize() { + Customer[] newCustomers = new Customer[size]; + System.arraycopy(customers, 0, newCustomers, 0, size); + + customers = newCustomers; + capacity = size; + + return new Customers(newCustomers); + } + + public Customers findCustomers(GroupType type) { + Customers custs = new Customers(); + + for(int i = 0; i < size; ++i) { + Customer cust = get(i); + if (cust == null) return null; + + Group grp = cust.getGroup(); + if (type == GroupType.NONE) { + if (grp == null || grp.getType() == null || grp.getType() == GroupType.NONE) { + custs.add(cust); + } + } else if (grp != null && grp.getType() == type) { + custs.add(cust); + } + } + + return custs; + } + + public Customers findCustomers(Group grp) { + if (grp != null) { + if (grp.getType() != null) { + return findCustomers(grp.getType()); + } else { + System.out.println("Customers.findCustomers() Error : No group type."); + return null; + } + } else { + System.out.println("Customers.findCustomers() Error : No group."); + return null; + } + } + + public void refresh(Groups groups) { + if (groups == null) {return;} + + for (int i = 0; i < size; i++) { + Customer cust = customers[i]; + cust.setGroup(groups.findGroupFor(cust)); + } + + } + + public void print() { + for (int i = 0; i < size; i++) { + if (customers[i] != null) { + System.out.printf("No. %4d => %s\n", (i + 1), customers[i]); + } + } + } + + public ClassifiedCustomersGroup classified() { + ClassifiedCustomersGroup classifiedCusGroup = ClassifiedCustomersGroup.getInstance(); + + for (int i = 0; i < allGroups.size(); i++) { + Group grp = allGroups.get(i); + + Customer[] customers = grp.getCustomers(allCustomers).trimToSize().getCustomers(); + Customer[] copy = Arrays.copyOf(customers, customers.length); + + ClassifiedCustomers classifiedCustomers = new ClassifiedCustomers(); + classifiedCustomers.setGroup(grp); + classifiedCustomers.setSize(copy.length); + classifiedCustomers.setCustomers(copy); + + classifiedCusGroup.set(i, classifiedCustomers); + } + return classifiedCusGroup; + } + + @Override + public String toString() { + String str = ""; + + for (int i = 0; i < size; i++) { + str = str + customers[i].toString() + "\n"; + } + + return str; + } +} \ No newline at end of file diff --git a/me.smartstore/src/exception/ArrayEmptyException.java b/me.smartstore/src/exception/ArrayEmptyException.java new file mode 100644 index 00000000..43f408ad --- /dev/null +++ b/me.smartstore/src/exception/ArrayEmptyException.java @@ -0,0 +1,14 @@ +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); + } +} \ No newline at end of file diff --git a/me.smartstore/src/exception/InputEmptyException.java b/me.smartstore/src/exception/InputEmptyException.java new file mode 100644 index 00000000..e081129f --- /dev/null +++ b/me.smartstore/src/exception/InputEmptyException.java @@ -0,0 +1,14 @@ +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); + } +} \ No newline at end of file diff --git a/me.smartstore/src/exception/InputEndException.java b/me.smartstore/src/exception/InputEndException.java new file mode 100644 index 00000000..ca0cb5f1 --- /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); + } +} \ No newline at end of file diff --git a/me.smartstore/src/exception/InputFormatException.java b/me.smartstore/src/exception/InputFormatException.java new file mode 100644 index 00000000..b0d11cd6 --- /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); + } +} \ No newline at end of file diff --git a/me.smartstore/src/exception/InputRangeException.java b/me.smartstore/src/exception/InputRangeException.java new file mode 100644 index 00000000..8cbb7c12 --- /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); + } +} \ No newline at end of file diff --git a/me.smartstore/src/exception/InputTypeException.java b/me.smartstore/src/exception/InputTypeException.java new file mode 100644 index 00000000..544fbb35 --- /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); + } +} \ No newline at end of file diff --git a/me.smartstore/src/groups/Group.java b/me.smartstore/src/groups/Group.java new file mode 100644 index 00000000..8a5ceebc --- /dev/null +++ b/me.smartstore/src/groups/Group.java @@ -0,0 +1,65 @@ +package groups; + +import customers.Customers; + +import java.util.Objects; + +public class Group { + + private GroupType type; + private Parameter param; + + public Group() { + this(null, null); + } + + public Group(GroupType type, Parameter param) { + this.type = type; + this.param = param; + } + + public GroupType getType() { + return type; + } + + public void setType(GroupType type) { + this.type = type; + } + + public Parameter getParam() { + return param; + } + + public void setParam(Parameter param) { + this.param = param; + } + + public Customers getCustomers(Customers allCustomers) { + return allCustomers.findCustomers(this); + } + + public void update(GroupType type, Parameter param) { + this.type = type; + this.param = param; + } + + @Override + public boolean equals(Object o) { + if (this == o) {return true;} + if (o == null || getClass() != o.getClass()) {return false;} + Group group = (Group) o; + return type == group.type && Objects.equals(param, group.param); + } + + @Override + public int hashCode() { + return Objects.hash(type, param); + } + + @Override + public String toString() { + if (type == null) {return "No group.";} + else if (param == null) {return "GroupType: " + type + "\nParameter: null";} + else {return "GroupType: " + type + "\nParameter: " + param;} + } +} \ No newline at end of file diff --git a/me.smartstore/src/groups/GroupType.java b/me.smartstore/src/groups/GroupType.java new file mode 100644 index 00000000..72e6b849 --- /dev/null +++ b/me.smartstore/src/groups/GroupType.java @@ -0,0 +1,32 @@ +package groups; + +public enum GroupType { + NONE("해당없음"), GENERAL("일반고객"), VIP("우수고객"), VVIP("최우수고객"), + N("해당없음"), G("일반고객"), V("우수고객"), VV("최우수고객"); + + String groupType = ""; + + GroupType(String groupType) { + this.groupType = groupType; + } + + public String getGroupType() { + return this.groupType; + } + + public void setGroupType(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; + } + + public static int size() { + return GroupType.values().length / 2; + } +} \ No newline at end of file diff --git a/me.smartstore/src/groups/Groups.java b/me.smartstore/src/groups/Groups.java new file mode 100644 index 00000000..1fb97363 --- /dev/null +++ b/me.smartstore/src/groups/Groups.java @@ -0,0 +1,119 @@ +package groups; + +import customers.Customer; +import util.UtilMethod; + + +public class Groups { + private static Groups allGroups; + + public static Groups getInstance() { + if (allGroups == null) {allGroups = new Groups();} + return allGroups; + } + + private int size; + private int capacity; + private Group[] groups; + + + public Groups() { + this.groups = new Group[GroupType.size()]; + initialize(); + } + + public Group[] getGroups() { + return groups; + } + + public void setGroups(Group[] groups) { + this.groups = groups; + } + + public int size() { + return size; + } + + public int length() { + return this.groups.length; + } + + private boolean isNull() { + return groups == null; + } + + public boolean isEmpty() { + return size == 0; + } + + public void initialize() { + for (int i = 0; i < GroupType.size(); i++) { + groups[i] = new Group(GroupType.values()[i], null); + size++; + } + } + + public void add(Group group) { + Group grp = find(group.getType()); + + if (grp != null) {update(group);} + else { + groups[size] = group; + size++; + } + } + + public Group get(int i) { + return groups[i]; + } + + public void update(Group group) { + Group grp = find(group.getType()); + if (grp != null) {grp.setParam(group.getParam());} + } + + public void print() { + for (int i = 0; i < size; i++) { + if (groups[i] != null) {System.out.println(groups[i]);} + } + } + + public Group find(GroupType groupType) { + for (Group grp : groups) { + if (grp.getType() == groupType) return grp; + }return null; + } + + public Group findGroupFor(Customer customer) { + if (groups == null) return null; + if (UtilMethod.isAnyNUll(customer, customer.getSpentTime(), customer.getTotalPay())) return null; + + for (int i = size - 1; i >= 0; i--) { + if (UtilMethod.isAnyNUll(groups[i], groups[i].getParam())) { + continue; + } + + Parameter param = groups[i].getParam(); + + if (UtilMethod.isAnyNUll(param, param.getMinimumSpentTime(), param.getMinimumTotalPay())) { + continue; + } + + if (customer.getSpentTime() >= param.getMinimumSpentTime() && customer.getTotalPay() >= param.getMinimumTotalPay()) { + return groups[i]; + } + } + return null; + } + + + @Override + public String toString() { + String str = ""; + + for(int i = 0; i < size; ++i) { + str = str + " " + groups[i] + "\n"; + } + return str; + } +} \ No newline at end of file diff --git a/me.smartstore/src/groups/Parameter.java b/me.smartstore/src/groups/Parameter.java new file mode 100644 index 00000000..b4fafa08 --- /dev/null +++ b/me.smartstore/src/groups/Parameter.java @@ -0,0 +1,54 @@ +package groups; + +import java.util.Objects; + +public class Parameter { + + private Integer minimumSpentTime; + private Integer minimumTotalPay; + + public Parameter() { + } + + public Parameter(Integer minimumSpentTime, Integer minimumTotalPay) { + this.minimumSpentTime = minimumSpentTime; + this.minimumTotalPay = minimumTotalPay; + } + + public Integer getMinimumSpentTime() { + return minimumSpentTime; + } + + public void setMinimumSpentTime(Integer minimumSpentTime) { + this.minimumSpentTime = minimumSpentTime; + } + + public Integer getMinimumTotalPay() { + return minimumTotalPay; + } + + public void setMinimumTotalPay(Integer minimumTotalPay) { + this.minimumTotalPay = minimumTotalPay; + } + + @Override + public boolean equals(Object o) { + if (this == o) {return true;} + if (o == null || getClass() != o.getClass()) {return false;} + Parameter parameter = (Parameter) o; + return minimumSpentTime == parameter.minimumSpentTime && minimumTotalPay == parameter.minimumTotalPay; + } + + @Override + public int hashCode() { + return Objects.hash(minimumSpentTime, minimumTotalPay); + } + + @Override + public String toString() { + return "Parameter{" + + "minimumSpentTime=" + minimumSpentTime + + ", minimumTotalPay=" + minimumTotalPay + + '}'; + } +} \ No newline at end of file diff --git a/me.smartstore/src/menu/CustomerMenu.java b/me.smartstore/src/menu/CustomerMenu.java new file mode 100644 index 00000000..071548aa --- /dev/null +++ b/me.smartstore/src/menu/CustomerMenu.java @@ -0,0 +1,272 @@ +package menu; + +import customers.Customer; +import customers.Customers; +import exception.ArrayEmptyException; +import exception.InputEmptyException; +import exception.InputEndException; +import exception.InputRangeException; +import groups.Group; +import groups.Groups; +import util.Message; + +public class CustomerMenu implements Menu { + + private static CustomerMenu customerMenu; + + public static CustomerMenu getInstance() { + if (customerMenu == null) { + customerMenu = new CustomerMenu(); + } + return customerMenu; + } + + + private Groups allGroups = Groups.getInstance(); + private Customers allCustomers = Customers.getInstance(); + + @Override + public void manage() { + while ( true ) { + int choice = chooseMenu( + new String[]{"Set Customer Data", "View Customer Data", + "Update Customer Data", "Delete Customer Data", "Back"}); + + if (choice == 1) { + int size = 0; + size = getCustomerSizeToAdd(); + setCustomerData(size); + } + else if (choice == 2) {viewCustomerData();} + else if (choice == 3) {updateCustomerData();} + else if (choice == 4) {deleteCustomerData();} + else if (choice == 5) {return;} + } + } + + public void setCustomerData(int size) { + for (int i = 0; i < size; ++i) { + Customer customer = new Customer(); + System.out.println("\n====== Customer " + (i + 1) + " Info. ======"); + + while ( true ) { + int choice = chooseMenu( + new String[]{"Customer Name", + "Customer ID", + "Customer Spent Time", + "Customer Total Pay", + "Back"}); + + if (choice == 1) setCustomerName(customer); + else if (choice == 2) setCustomerUserId(customer); + else if (choice == 3) setCustomerSpentTime(customer); + else if (choice == 4) setCustomerTotalPay(customer); + else if (choice == 5) break; + + } + + Group grp = allGroups.findGroupFor(customer); + if (grp == null) {customer.setGroup(null);} + else if (!grp.equals(customer.getGroup())) {customer.setGroup(grp);} + + allCustomers.add(customer); + } + } + + + public void viewCustomerData() { + if (allCustomers.size() == 0) { + System.out.println(Message.ERR_MSG_INVALID_ARR_EMPTY); + return; + } + + System.out.println("\n======= Customer Info. ======="); + + for (int i = 0; i < allCustomers.size(); ++i) { + Customer cust = allCustomers.get(i); + if (cust != null) System.out.println("No. " + (i + 1) + " => " + cust); + else System.out.println("null"); + } + + } + + public void updateCustomerData() { + viewCustomerData(); + int custNo = 0; + + try { + custNo = findCustomer(); + } catch (ArrayEmptyException | InputEndException e) { + return; + } + + + Customer customer = allCustomers.get(custNo - 1); + if (customer == null) {return;} + + while ( true ) { + int choice = chooseMenu( + new String[]{"Customer Name", + "Customer ID", + "Customer Spent Time", + "Customer Total Pay", + "Back"}); + + if (choice == 1) {setCustomerName(customer);} + else if (choice == 2) {setCustomerUserId(customer);} + else if (choice == 3) {setCustomerSpentTime(customer);} + else if (choice == 4) {setCustomerTotalPay(customer);} + else if (choice == 5) {break;} + } + + Group grp = allGroups.findGroupFor(customer); + if (grp == null) {customer.setGroup(null);} + else if (!grp.equals(customer.getGroup())) { + customer.setGroup(grp); + } + } + + public void deleteCustomerData() { + viewCustomerData(); + int custNo = 0; + + try { + custNo = findCustomer(); + } catch (ArrayEmptyException | InputEndException e) { + return; + } + + + Customer customer = allCustomers.get(custNo - 1); + System.out.println(customer); + + allCustomers.pop(custNo - 1); + viewCustomerData(); + + } + + private int getCustomerSizeToAdd() { + while ( true ) { + try { + System.out.print("How many customers to input? "); + int size = Integer.parseInt(nextLine(Message.END_MSG)); + if (size < 0) { + throw new InputRangeException(); + } + return size; + } catch (NumberFormatException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } catch (InputRangeException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + return -1; + } + } + } + + public void setCustomerName(Customer customer) { + while ( true ) { + try { + System.out.print("\nInput Customer's Name: "); + String name = nextLine(Message.END_MSG); + if (name == null || name.equals("")) { + throw new InputEmptyException(); + } + customer.setName(name); + return; + + } catch (InputEmptyException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_EMPTY); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + return; + } + } + } + + public void setCustomerUserId(Customer customer) { + while ( true ) { + try { + System.out.print("\nInput Customer's ID: "); + String userId = nextLine(Message.END_MSG); + if (userId == null || userId.equals("")) { + throw new InputEmptyException(); + } + customer.setUserId(userId); + return; + + } catch (InputEmptyException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_EMPTY); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + return; + } + } + } + + public void setCustomerSpentTime(Customer customer) { + while ( true ) { + try { + System.out.print("\nInput Customer's Spent Time: "); + int spentTime = Integer.parseInt(nextLine(Message.END_MSG)); + if (spentTime < 0) { + throw new InputRangeException(); + } + customer.setSpentTime(spentTime); + return; + + } catch (InputRangeException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + return; + } + } + } + + public void setCustomerTotalPay(Customer customer) { + while ( true ) { + try { + System.out.print("\nInput Customer's Total Payment: "); + int totalPay = Integer.parseInt(nextLine(Message.END_MSG)); + if (totalPay < 0) { + throw new InputRangeException(); + } + customer.setTotalPay(totalPay); + return; + + } catch (NumberFormatException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } catch (InputRangeException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + return; + } + } + } + + + public int findCustomer() throws ArrayEmptyException, InputEndException { + int size = allCustomers.size(); + if (size == 0) { + throw new ArrayEmptyException(); + } + + while ( true ) { + try { + System.out.print("\nWhich customer ( 1 ~ " + size + " )? "); + int custNo = Integer.parseInt(nextLine()); + if (!(custNo >= 1 && custNo <= size)) { + throw new InputRangeException(); + } + return custNo; + } catch (NumberFormatException e) { + System.out.println("\n" + Message.ERR_MSG_INVALID_INPUT_FORMAT); + } catch (InputRangeException e) { + System.out.println("\n" + Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + } +} \ No newline at end of file diff --git a/me.smartstore/src/menu/GroupMenu.java b/me.smartstore/src/menu/GroupMenu.java new file mode 100644 index 00000000..1fb214e7 --- /dev/null +++ b/me.smartstore/src/menu/GroupMenu.java @@ -0,0 +1,201 @@ +package menu; + +import customers.Customers; +import exception.InputEmptyException; +import exception.InputEndException; +import exception.InputRangeException; +import groups.Group; +import groups.GroupType; +import groups.Groups; +import groups.Parameter; +import util.Message; + +public class GroupMenu implements Menu { + + private static GroupMenu groupMenu; + + public static GroupMenu getInstance() { + if (groupMenu == null) { + groupMenu = new GroupMenu(); + } + return groupMenu; + } + + + Groups allGroups = Groups.getInstance(); + Customers allCustomers = Customers.getInstance(); + + public String chooseGroup() { + while ( true ) { + try { + System.out.print("Which group (GENERAL (G), VIP (V), VVIP (VV))? "); + String choice = nextLine(Message.END_MSG); + + if (choice.equals("")) { + throw new InputEmptyException(); + } + return choice; + + } catch (InputEmptyException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_NULL); + } catch (IllegalArgumentException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + return null; + } + } + } + + @Override + public void manage() { + while ( true ) { + int choice = chooseMenu( + new String[]{"Set Parameter", "View Parameter", "Update Parameter", "Back"}); + + if (choice == 1) {setParameter();} + else if (choice == 2) {viewParameter();} + else if (choice == 3) {updateParameter();} + else if (choice == 4) {break;} + } + } + + public void setParameter() { + while ( true ) { + String strGroup = chooseGroup(); + if (strGroup == null) {return;} + + GroupType groupType; + try { + groupType = GroupType.valueOf(strGroup).replaceFullName(); + } catch (IllegalArgumentException e) { + System.out.println("\n" + Message.ERR_MSG_INVALID_INPUT_RANGE); + continue; + } + + Group grp = allGroups.find(groupType); + if (grp != null && grp.getParam() != null) { + System.out.println("\n" + grp.getType() + " group already exists."); + System.out.println("\n" + grp); + } else { + Parameter param = new Parameter(); + + while ( true ) { + int choice = chooseMenu(new String[]{"Minimum Spent Time", "Minimum Total Pay", "Back"}); + + if (choice == 1) {setParameterMinimumSpentTime(param);} + else if (choice == 2) {setParameterMinimumTotalPay(param);} + else if (choice == 3) {break;} + else System.out.println("\n" + Message.ERR_MSG_INVALID_INPUT_RANGE); + } + + allGroups.add(new Group(groupType, param)); + allCustomers.refresh(allGroups); + System.out.println("\n" + grp); + } + } + } + + public void viewParameter() { + while ( true ) { + String strGroup = chooseGroup(); + if (strGroup == null) {return;} + + GroupType groupType; + try { + groupType = GroupType.valueOf(strGroup).replaceFullName(); + } catch (IllegalArgumentException e) { + System.out.println("\n" + Message.ERR_MSG_INVALID_INPUT_TYPE); + continue; + } + + Group grp = allGroups.find(groupType); + System.out.println(); + System.out.println(grp); + } + } + + public void updateParameter() { + while ( true ) { + String strGroup = chooseGroup(); + if (strGroup == null) {return;} + + GroupType groupType; + try { + groupType = GroupType.valueOf(strGroup).replaceFullName(); + } catch (IllegalArgumentException e) { + System.out.println("\n" + Message.ERR_MSG_INVALID_INPUT_RANGE); + return; + } + + Group grp = allGroups.find(groupType); + if (grp.getParam() == null) { + System.out.println("\nNo parameter. Set the parameter first."); + return; + } + + System.out.println("\n" + grp); + Parameter param = grp.getParam(); + + while ( true ) { + int choice = chooseMenu(new String[]{"Minimum Spent Time", "Minimum Total Pay", "Back"}); + + if (choice == 1) {setParameterMinimumSpentTime(param);} + else if (choice == 2) {setParameterMinimumTotalPay(param);} + else if (choice == 3) {break;} + } + + allGroups.update(new Group(groupType, param)); + allCustomers.refresh(allGroups); + System.out.println("\n" + grp); + + } + } + + public void setParameterMinimumSpentTime(Parameter param) { + while ( true ) { + try { + System.out.print("\nInput Minimum Spent Time: "); + int minimumSpentTime = Integer.parseInt(nextLine(Message.END_MSG)); + + if (minimumSpentTime < 0) { + throw new InputRangeException(); + } + + param.setMinimumSpentTime(minimumSpentTime); + return; + } catch (NumberFormatException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_TYPE); + } catch (InputRangeException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + return; + } + } + } + + + public void setParameterMinimumTotalPay(Parameter param) { + + while ( true ) { + try { + System.out.print("\nInput Minimum Total Pay: "); + int minimumTotalPay = Integer.parseInt(nextLine(Message.END_MSG)); + + if (minimumTotalPay < 0) { + throw new InputRangeException(); + } + param.setMinimumTotalPay(minimumTotalPay); + return; + } catch (NumberFormatException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_TYPE); + } catch (InputRangeException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + return; + } + } + } +} \ No newline at end of file diff --git a/me.smartstore/src/menu/MainMenu.java b/me.smartstore/src/menu/MainMenu.java new file mode 100644 index 00000000..32e65dc9 --- /dev/null +++ b/me.smartstore/src/menu/MainMenu.java @@ -0,0 +1,40 @@ +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 ) { + 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/src/menu/Menu.java b/me.smartstore/src/menu/Menu.java new file mode 100644 index 00000000..26e63ab9 --- /dev/null +++ b/me.smartstore/src/menu/Menu.java @@ -0,0 +1,56 @@ +package menu; + +import exception.InputEndException; +import exception.InputRangeException; +import util.Message; + +import java.util.Scanner; + +public interface Menu { + Scanner scanner = new Scanner(System.in); + + default String nextLine() { + String str = scanner.nextLine().toUpperCase(); + String[] strings = str.split("\\s"); + return (strings.length > 1) ? "" : str; + } + + default String nextLine(String messageForEnd) throws InputEndException { + System.out.println("\n** Press 'end', if you want to exit! **"); + + String str = scanner.nextLine().toUpperCase(); + if (str.equals(messageForEnd)) {throw new InputEndException();} + + String[] strings = str.split("\\s"); + return (strings.length > 1) ? "" : str; + } + + default int chooseMenu(String[] menus) { + while ( true ) { + try { + System.out.println("\n=============================="); + + for (int i = 0; i < menus.length; i++) { + System.out.printf(" %d. %s\n", i + 1, menus[i]); + } + + System.out.println("=============================="); + System.out.print("Choose One: "); + + int choice = Integer.parseInt(nextLine()); + if (choice >= 1 && choice <= menus.length) { + return choice; + } + throw new InputRangeException(); + + } 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(); //서브메뉴 관리 +} \ No newline at end of file diff --git a/me.smartstore/src/menu/OrderType.java b/me.smartstore/src/menu/OrderType.java new file mode 100644 index 00000000..b5a9b20b --- /dev/null +++ b/me.smartstore/src/menu/OrderType.java @@ -0,0 +1,32 @@ +package menu; + +public enum OrderType { + ASCENDING("오름차순"), + DESCENDING("내림차순"), + A("오름차순"), + D("내림차순"); + + String sortType = ""; + + OrderType(String sortType) { + this.sortType = sortType; + } + + public String getSortType() { + return this.sortType; + } + + public void setSortType(String sortType) { + this.sortType = sortType; + } + + public OrderType replaceFullName() { + if (this == A) {return ASCENDING;} + else if (this == D) {return DESCENDING;} + return this; + } + + public static int size() { + return OrderType.values().length / 2; + } +} \ No newline at end of file diff --git a/me.smartstore/src/menu/SummaryMenu.java b/me.smartstore/src/menu/SummaryMenu.java new file mode 100644 index 00000000..cd33755f --- /dev/null +++ b/me.smartstore/src/menu/SummaryMenu.java @@ -0,0 +1,121 @@ +package menu; + +import customers.ClassifiedCustomersGroup; +import customers.Customer; +import customers.Customers; +import exception.InputEmptyException; +import exception.InputEndException; +import exception.InputRangeException; +import util.Message; + +import java.util.Comparator; + +public class SummaryMenu implements Menu { + + private static SummaryMenu summarizedMenu; + + public static SummaryMenu getInstance() { + if (summarizedMenu == null) { + summarizedMenu = new SummaryMenu(); + } + return summarizedMenu; + } + + private Customers allCustomers = Customers.getInstance(); + + private ClassifiedCustomersGroup classifiedCusGroup = ClassifiedCustomersGroup.getInstance(); + + + @Override + public void manage() { + classifiedCusGroup = allCustomers.classified(); + + while ( true ) { + int choice = chooseMenu( + new String[]{"Summary", + "Summary (Sorted By Name)", + "Summary (Sorted By Spent Time)", + "Summary (Sorted By Total Payment)", + "Back"}); + + if (choice == 1) {dispSummary();} + else if (choice == 2) { + manageSort(Comparator + .comparing(Customer::getName) + .thenComparing(Customer::getUserId));} + else if (choice == 3) { + manageSort(Comparator + .comparing(Customer::getSpentTime) + .thenComparing(Customer::getName));} + else if (choice == 4) { + manageSort(Comparator + .comparing(Customer::getTotalPay) + .thenComparing(Customer::getName));} + else if (choice == 5) {return;} + + else System.out.println("\n" + Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + + + public void dispSummary() { + if (classifiedCusGroup == null) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_NULL); + return; + } + classifiedCusGroup.print(); + } + + public void manageSort(Comparator comparator) { + while ( true ) { + String strOrder = chooseSortOrder(); + if (strOrder == null) {return;} + if (strOrder.equals(Message.END_MSG)) {return;} + + try { + OrderType orderType = OrderType.valueOf(strOrder).replaceFullName(); + + if (orderType == OrderType.ASCENDING) {classifiedCusGroup.sort(comparator);} + else if (orderType == OrderType.DESCENDING) {classifiedCusGroup.sort(comparator.reversed());} + else {throw new InputRangeException();} + + } catch (IllegalArgumentException | InputRangeException e) { + System.out.println("\n" + Message.ERR_MSG_INVALID_INPUT_RANGE); + } + + dispSummary(); + } + } + + public String chooseSortOrder() { + while ( true ) { + try { + System.out.print("Which order (ASCENDING (A), DESCENDING (D))? "); + String choice = nextLine(Message.END_MSG); + + if (choice.equals("")) { + throw new InputEmptyException(); + } + + try { + OrderType orderType = OrderType.valueOf(choice).replaceFullName(); + for (int i = 0; i < OrderType.size(); i++) { + if (orderType == OrderType.values()[i]) { + return choice; + } + } + throw new InputRangeException(); + } catch (IllegalArgumentException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_TYPE); + } + } catch (InputEmptyException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_NULL); + } catch (InputRangeException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + return null; + } + } + } +} \ No newline at end of file diff --git a/me.smartstore/src/util/Message.java b/me.smartstore/src/util/Message.java new file mode 100644 index 00000000..44a1f10c --- /dev/null +++ b/me.smartstore/src/util/Message.java @@ -0,0 +1,14 @@ +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"; + +} \ No newline at end of file diff --git a/me.smartstore/src/util/UtilMethod.java b/me.smartstore/src/util/UtilMethod.java new file mode 100644 index 00000000..2ceb0f58 --- /dev/null +++ b/me.smartstore/src/util/UtilMethod.java @@ -0,0 +1,14 @@ +package util; + +import java.util.Arrays; +import java.util.Objects; + +public class UtilMethod { + public static boolean isAnyNUll(Object... objects) { + return Arrays.stream(objects).anyMatch(Objects :: isNull); + } + + public static boolean isAllNUll(Object... objects) { + return Arrays.stream(objects).allMatch(Objects :: isNull); + } +} \ No newline at end of file