diff --git a/src/me/smartstore/Main.java b/src/me/smartstore/Main.java new file mode 100644 index 00000000..8cd59151 --- /dev/null +++ b/src/me/smartstore/Main.java @@ -0,0 +1,8 @@ +package me.smartstore; + +public class Main { + public static void main(String[] args) { + SmartStoreApp.getInstance().test().run(); +// SmartStoreApp.getInstance().run(); + } +} diff --git a/src/me/smartstore/SmartStoreApp.java b/src/me/smartstore/SmartStoreApp.java new file mode 100644 index 00000000..7b7f8cf3 --- /dev/null +++ b/src/me/smartstore/SmartStoreApp.java @@ -0,0 +1,60 @@ +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.GroupConditions; +import me.smartstore.menu.MainMenu; + +public class SmartStoreApp { + + private Groups allGroups; + private Customers allCustomers; + private MainMenu mainMenu; + private static SmartStoreApp smartStoreApp; + + public static SmartStoreApp getInstance() { + if (smartStoreApp == null) { + smartStoreApp = new SmartStoreApp(); + } + return smartStoreApp; + } + + private SmartStoreApp() { + this.allCustomers = Customers.getInstance(); + this.allGroups = Groups.getInstance(); + this.mainMenu = MainMenu.getInstance(); + } + + public void details() { + System.out.println("\n\n==========================================="); + System.out.println(" Title : SmartStore Customer Classification"); + System.out.println(" Release Date : 23.05.10"); + System.out.println(" Copyright 2023 taeHyoung All rights reserved."); + System.out.println("===========================================\n"); + } + + public SmartStoreApp test() { + allGroups.add(new Group(new GroupConditions(10, 100000), GroupType.GENERAL)); + allGroups.add(new Group(new GroupConditions(20, 200000), GroupType.VIP)); + allGroups.add(new Group(new GroupConditions(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() * 10) + 1) * 5, + ((int) (Math.random() * 5) + 1) * 100000)); + } + allCustomers.refresh(allGroups); + + return this; + } + + public void run() { + details(); + mainMenu.manage(); + } +} diff --git a/src/me/smartstore/arrays/Collections.java b/src/me/smartstore/arrays/Collections.java new file mode 100644 index 00000000..87a43dd1 --- /dev/null +++ b/src/me/smartstore/arrays/Collections.java @@ -0,0 +1,13 @@ +package me.smartstore.arrays; + +public interface Collections { + int size(); + T get(int index); + void set(int index, T object); + int indexOf(T object); + void add(T object); + void add(int index, T object); + T pop(); + T pop(int index); + T pop(T object); +} diff --git a/src/me/smartstore/arrays/DArray.java b/src/me/smartstore/arrays/DArray.java new file mode 100644 index 00000000..677ee36e --- /dev/null +++ b/src/me/smartstore/arrays/DArray.java @@ -0,0 +1,141 @@ +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() throws ClassCastException { + arrays = (T[]) new Object[DEFAULT]; + capacity = DEFAULT; + } + + @SuppressWarnings("unchecked") + 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; + } + + @Override + public int size() { + return size; + } + + protected int capacity() { + return capacity; + } + + @Override + public T get(int index) throws IndexOutOfBoundsException { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); + return arrays[index]; + } + + /** + * @param: ... + * @return: ... + * @throws: IndexOutOfBoundsException + * @throws: NullArgumentException + * */ + @Override + public void set(int index, T object) throws IndexOutOfBoundsException, 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(); + + for (int i = 0; i < size; i++) { + if (arrays[i] == null) continue; + if (arrays[i].equals(object)) return i; + } + throw new ElementNotFoundException(); + } + + @Override + public void add(T object) throws NullArgumentException { + if (object == null) throw new NullArgumentException(); + + 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; + arrays = java.util.Arrays.copyOf(arrays, capacity); + } + + @Override + public String toString() { + String toStr = ""; + for (int i = 0; i < size; i++) { + toStr += (arrays[i] + "\n"); + } + return toStr; + } +} diff --git a/src/me/smartstore/customer/Customer.java b/src/me/smartstore/customer/Customer.java new file mode 100644 index 00000000..82c452e4 --- /dev/null +++ b/src/me/smartstore/customer/Customer.java @@ -0,0 +1,111 @@ +package me.smartstore.customer; + +import me.smartstore.group.GroupType; + +import java.util.Objects; + +public class Customer implements Comparable { + private int cusNo; + private String cusId; + private String cusName; + private Integer cusTotalTime; + private Integer cusTotalPay; + private GroupType group; + + private static int seqNum = 1; + + public Customer() { + cusNo = seqNum; + cusId = null; + cusName = null; + cusTotalTime = 0; + cusTotalPay = 0; + group = GroupType.NONE; + seqNum++; + } + + public Customer(String cusName, String cusId, Integer cusTotalTime, Integer cusTotalPay) { + this(); + this.cusName = cusName; + this.cusId = cusId; + this.cusTotalTime = cusTotalTime; + this.cusTotalPay = cusTotalPay; + } + + public int getCusNo() { + return cusNo; + } + + public void setCusNo(int cusNo) { + this.cusNo = cusNo; + } + + 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 GroupType getGroup() { + return group; + } + + public void setGroup(GroupType 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 + + '}'; + } + + @Override + public int compareTo(Customer o) { + return o.getCusNo(); + } +} diff --git a/src/me/smartstore/customer/Customers.java b/src/me/smartstore/customer/Customers.java new file mode 100644 index 00000000..a424bbc7 --- /dev/null +++ b/src/me/smartstore/customer/Customers.java @@ -0,0 +1,158 @@ +package me.smartstore.customer; + +import me.smartstore.arrays.DArray; +import me.smartstore.group.GroupType; +import me.smartstore.group.Groups; +import java.util.Arrays; + +public class Customers extends DArray { + private static Customers allCustomers; + + public static Customers getInstance() { + if (allCustomers == null){ + allCustomers = new Customers(); + } + return allCustomers; + } + + private Customers() {} + + public void refresh(Groups groups) { + int cusMinTime; + int cusMinPay; + + int VVIPMinTime = 0, VVIPMinPay = 0, + VIPMinTime = 0, VIPMinPay = 0, + GENERALMinTime = 0, GENERALMInPay = 0; + + int cnt = 0; + + if (groups.findGroupByGroupType(GroupType.VVIP) != null) { + VVIPMinTime = groups.findGroupByGroupType(GroupType.VVIP).getParameter().getMinTime(); + VVIPMinPay = groups.findGroupByGroupType(GroupType.VVIP).getParameter().getMinPay(); + cnt += 4; + } + if (groups.findGroupByGroupType(GroupType.VIP) != null) { + VIPMinTime = groups.findGroupByGroupType(GroupType.VIP).getParameter().getMinTime(); + VIPMinPay = groups.findGroupByGroupType(GroupType.VIP).getParameter().getMinPay(); + cnt += 2; + } + if (groups.findGroupByGroupType(GroupType.GENERAL) != null) { + GENERALMinTime = groups.findGroupByGroupType(GroupType.GENERAL).getParameter().getMinTime(); + GENERALMInPay = groups.findGroupByGroupType(GroupType.GENERAL).getParameter().getMinPay(); + cnt += 1; + } + + for (int i = 0; i < allCustomers.size; i++) { + cusMinTime = allCustomers.get(i).getCusTotalTime(); + cusMinPay = allCustomers.get(i).getCusTotalPay(); + + if( cnt == 7 ) { + if (cusMinTime >= VVIPMinTime && cusMinPay >= VVIPMinPay) + allCustomers.get(i).setGroup(GroupType.VVIP); + else if (cusMinTime >= VIPMinTime && cusMinPay >= VIPMinPay) + allCustomers.get(i).setGroup(GroupType.VIP); + else if (cusMinTime >= GENERALMinTime && cusMinPay >= GENERALMInPay) + allCustomers.get(i).setGroup(GroupType.GENERAL); + else allCustomers.get(i).setGroup(GroupType.NONE); + } else if ( cnt == 6 ){ + if (cusMinTime >= VVIPMinTime && cusMinPay >= VVIPMinPay) + allCustomers.get(i).setGroup(GroupType.VVIP); + else if (cusMinTime >= VIPMinTime && cusMinPay >= VIPMinPay) + allCustomers.get(i).setGroup(GroupType.VIP); + else allCustomers.get(i).setGroup(GroupType.NONE); + } else if ( cnt == 5 ){ + if (cusMinTime >= VVIPMinTime && cusMinPay >= VVIPMinPay) + allCustomers.get(i).setGroup(GroupType.VVIP); + else if (cusMinTime >= GENERALMinTime && cusMinPay >= GENERALMInPay) + allCustomers.get(i).setGroup(GroupType.GENERAL); + else allCustomers.get(i).setGroup(GroupType.NONE); + } else if ( cnt == 4 ){ + if (cusMinTime >= VVIPMinTime && cusMinPay >= VVIPMinPay) + allCustomers.get(i).setGroup(GroupType.VVIP); + else allCustomers.get(i).setGroup(GroupType.NONE); + } else if ( cnt == 3 ){ + if (cusMinTime >= VIPMinTime && cusMinPay >= VIPMinPay) + allCustomers.get(i).setGroup(GroupType.VIP); + else if (cusMinTime >= GENERALMinTime && cusMinPay >= GENERALMInPay) + allCustomers.get(i).setGroup(GroupType.GENERAL); + else allCustomers.get(i).setGroup(GroupType.NONE); + } else if ( cnt == 2 ){ + if (cusMinTime >= VIPMinTime && cusMinPay >= VIPMinPay) + allCustomers.get(i).setGroup(GroupType.VIP); + else allCustomers.get(i).setGroup(GroupType.NONE); + } else if ( cnt == 1 ){ + if (cusMinTime >= GENERALMinTime && cusMinPay >= GENERALMInPay) + allCustomers.get(i).setGroup(GroupType.GENERAL); + else allCustomers.get(i).setGroup(GroupType.NONE); + } + } + } + + public int findCustomerDataByCusNo(int customerIdx){ + + int idx = -1; + for (int i = 0; i < allCustomers.size(); i++) { + if(allCustomers.get(i).getCusNo() == customerIdx){ + idx = allCustomers.indexOf(allCustomers.get(i)); + } + } + return idx; + } + + public void viewCustomerData(){ + for (int i = 0; i < allCustomers.size; i++){ + System.out.println("No. "+ allCustomers.get(i).getCusNo() + + " => " + allCustomers.get(i)); + } + } + + public void viewCustomerDataByGroupType(GroupType groupType) { + int num = 1; + Customer[] customers = new Customer[allCustomers.size]; + for (int i = 0; i < allCustomers.size; i++) { + customers[i] = allCustomers.get(i); + } + for (int i = 0; i < allCustomers.size; i++){ + if( allCustomers.get(i).getGroup() == groupType) { + System.out.println("No. "+ num + " => " + customers[i]); + num++; + } + } + } + + public void viewCustomerDataByGroupType(GroupType groupType, String sortingMethod, String order) { + int num = 1; + Customer[] customers = new Customer[allCustomers.size]; + for (int i = 0; i < allCustomers.size; i++) { + customers[i] = allCustomers.get(i); + } + + Arrays.sort(customers, (o1, o2) -> { + if (sortingMethod.equals("Name")){ + if (order.equals("A")) + return o1.getCusName().compareTo(o2.getCusName()); + else if (order.equals("D")) + return o2.getCusName().compareTo(o1.getCusName()); + } else if (sortingMethod.equals("SpentTime")) { + if (order.equals("A")) + return o1.getCusTotalTime() - o2.getCusTotalTime(); + else if (order.equals("D")) + return o2.getCusTotalTime() - o1.getCusTotalTime(); + } else if (sortingMethod.equals("TotalPayment")) { + if (order.equals("A")) + return o1.getCusTotalPay() - o2.getCusTotalPay(); + else if (order.equals("D")) + return o2.getCusTotalPay() - o1.getCusTotalPay(); + } + return 0; + }); + + for (int i = 0; i < allCustomers.size; i++) { + if (allCustomers.get(i).getGroup() == groupType) { + System.out.println("No. "+ num + " => " + customers[i]); + num++; + } + } + } +} diff --git a/src/me/smartstore/exception/CustomerArrayEmptyException.java b/src/me/smartstore/exception/CustomerArrayEmptyException.java new file mode 100644 index 00000000..7ad3fe6b --- /dev/null +++ b/src/me/smartstore/exception/CustomerArrayEmptyException.java @@ -0,0 +1,14 @@ +package me.smartstore.exception; + +import me.smartstore.utils.Message; + +public class CustomerArrayEmptyException extends RuntimeException { + + public CustomerArrayEmptyException() { + super(Message.ERR_MSG_INVALID_CUSTOMER_ARR_EMPTY); + } + + public CustomerArrayEmptyException(String message) { + super(message); + } +} diff --git a/src/me/smartstore/exception/ElementNotFoundException.java b/src/me/smartstore/exception/ElementNotFoundException.java new file mode 100644 index 00000000..b90fa497 --- /dev/null +++ b/src/me/smartstore/exception/ElementNotFoundException.java @@ -0,0 +1,13 @@ +package me.smartstore.exception; + +import me.smartstore.utils.Message; + +public class ElementNotFoundException extends RuntimeException{ + public ElementNotFoundException() { + super(Message.ERR_MSG_NULL_ARR_ELEMENT); + } + + public ElementNotFoundException(String message) { + super(message); + } +} diff --git a/src/me/smartstore/exception/EmptyArrayException.java b/src/me/smartstore/exception/EmptyArrayException.java new file mode 100644 index 00000000..759a8740 --- /dev/null +++ b/src/me/smartstore/exception/EmptyArrayException.java @@ -0,0 +1,13 @@ +package me.smartstore.exception; + +import me.smartstore.utils.Message; + +public class EmptyArrayException extends RuntimeException{ + public EmptyArrayException() { + super(Message.ERR_MSG_ARR_EMPTY); + } + + public EmptyArrayException(String message) { + super(message); + } +} diff --git a/src/me/smartstore/exception/InputEmptyException.java b/src/me/smartstore/exception/InputEmptyException.java new file mode 100644 index 00000000..f38777d4 --- /dev/null +++ b/src/me/smartstore/exception/InputEmptyException.java @@ -0,0 +1,14 @@ +package me.smartstore.exception; + +import me.smartstore.utils.Message; + +public class InputEmptyException extends RuntimeException{ + + public InputEmptyException() { + super(Message.ERR_MSG_INVALID_INPUT_EMPTY); + } + + public InputEmptyException(String message) { + super(message); + } +} diff --git a/src/me/smartstore/exception/InputEndException.java b/src/me/smartstore/exception/InputEndException.java new file mode 100644 index 00000000..b26a6a00 --- /dev/null +++ b/src/me/smartstore/exception/InputEndException.java @@ -0,0 +1,13 @@ +package me.smartstore.exception; + +import me.smartstore.utils.Message; + +public class InputEndException extends RuntimeException{ + public InputEndException() { + super(Message.ERR_MSG_INPUT_END); + } + + public InputEndException(String message) { + super(message); + } +} diff --git a/src/me/smartstore/exception/InputFormatException.java b/src/me/smartstore/exception/InputFormatException.java new file mode 100644 index 00000000..bae0431f --- /dev/null +++ b/src/me/smartstore/exception/InputFormatException.java @@ -0,0 +1,13 @@ +package me.smartstore.exception; + +import me.smartstore.utils.Message; + +public class InputFormatException extends RuntimeException{ + public InputFormatException() { + super(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } + + public InputFormatException(String message) { + super(message); + } +} diff --git a/src/me/smartstore/exception/InputRangeException.java b/src/me/smartstore/exception/InputRangeException.java new file mode 100644 index 00000000..a73cb789 --- /dev/null +++ b/src/me/smartstore/exception/InputRangeException.java @@ -0,0 +1,13 @@ +package me.smartstore.exception; + +import me.smartstore.utils.Message; + +public class InputRangeException extends RuntimeException{ + public InputRangeException() { + super(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + + public InputRangeException(String message) { + super(message); + } +} diff --git a/src/me/smartstore/exception/InputTypeException.java b/src/me/smartstore/exception/InputTypeException.java new file mode 100644 index 00000000..538ae57f --- /dev/null +++ b/src/me/smartstore/exception/InputTypeException.java @@ -0,0 +1,14 @@ +package me.smartstore.exception; + +import me.smartstore.utils.Message; + +public class InputTypeException extends RuntimeException{ + + public InputTypeException() { + super(Message.ERR_MSG_INVALID_INPUT_TYPE); + } + + public InputTypeException(String message) { + super(message); + } +} diff --git a/src/me/smartstore/exception/NullArgumentException.java b/src/me/smartstore/exception/NullArgumentException.java new file mode 100644 index 00000000..e420db6a --- /dev/null +++ b/src/me/smartstore/exception/NullArgumentException.java @@ -0,0 +1,11 @@ +package me.smartstore.exception; + +public class NullArgumentException extends RuntimeException{ + + public NullArgumentException() { + } + + public NullArgumentException(String message) { + super(message); + } +} diff --git a/src/me/smartstore/exception/ParameterArrEmptyException.java b/src/me/smartstore/exception/ParameterArrEmptyException.java new file mode 100644 index 00000000..b454a853 --- /dev/null +++ b/src/me/smartstore/exception/ParameterArrEmptyException.java @@ -0,0 +1,14 @@ +package me.smartstore.exception; + +import me.smartstore.utils.Message; + +public class ParameterArrEmptyException extends RuntimeException { + + public ParameterArrEmptyException() { + super(Message.ERR_MSG_INVALID_PARAMETER_ARR_EMPTY); + } + + public ParameterArrEmptyException(String message) { + super(message); + } +} diff --git a/src/me/smartstore/group/Group.java b/src/me/smartstore/group/Group.java new file mode 100644 index 00000000..29ac4879 --- /dev/null +++ b/src/me/smartstore/group/Group.java @@ -0,0 +1,51 @@ +package me.smartstore.group; + +import java.util.Objects; + +public class Group { + private GroupConditions groupConditions; + private GroupType groupType; + + public Group() { + } + + public Group(GroupConditions groupConditions, GroupType groupType) { + this.groupConditions = groupConditions; + this.groupType = groupType; + } + + public GroupConditions getParameter() { + return groupConditions; + } + + public void setParameter(GroupConditions groupConditions) { + this.groupConditions = groupConditions; + } + + 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(groupConditions, group.groupConditions) && groupType == group.groupType; + } + + @Override + public int hashCode() { + return Objects.hash(groupConditions, groupType); + } + + @Override + public String toString() { + return "GroupType=" + groupType +"\n" + + "Parameter=" + groupConditions; + } +} diff --git a/src/me/smartstore/group/GroupConditions.java b/src/me/smartstore/group/GroupConditions.java new file mode 100644 index 00000000..a0c5120a --- /dev/null +++ b/src/me/smartstore/group/GroupConditions.java @@ -0,0 +1,53 @@ +package me.smartstore.group; + +import java.util.Objects; + +public class GroupConditions { + private Integer minTime; + private Integer minPay; + + public GroupConditions() { + } + + public GroupConditions(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; + GroupConditions groupConditions = (GroupConditions) o; + return Objects.equals(minTime, groupConditions.minTime) && Objects.equals(minPay, groupConditions.minPay); + } + + @Override + public int hashCode() { + return Objects.hash(minTime, minPay); + } + + @Override + public String toString() { + return "GroupConditions{" + + "minTime=" + minTime + + ", minPay=" + minPay + + '}'; + } +} diff --git a/src/me/smartstore/group/GroupType.java b/src/me/smartstore/group/GroupType.java new file mode 100644 index 00000000..50070aad --- /dev/null +++ b/src/me/smartstore/group/GroupType.java @@ -0,0 +1,20 @@ +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/src/me/smartstore/group/Groups.java b/src/me/smartstore/group/Groups.java new file mode 100644 index 00000000..88b29359 --- /dev/null +++ b/src/me/smartstore/group/Groups.java @@ -0,0 +1,40 @@ +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 findGroupByGroupType(GroupType groupType) { + for (int i = 0; i < allGroups.size; i++) { + if (allGroups.get(i).getGroupType() == groupType) { + return allGroups.get(i); + } + } + return null; + } + + public void viewGroupTimeAndPayByGroupType(GroupType groupType){ + + if ( findGroupByGroupType(groupType) == null ) { + System.out.println("Group : " + groupType + + " ( Time : null, Pay : null )" + ); + } else { + System.out.println("Group : " + groupType + + " ( Time : " + findGroupByGroupType(groupType).getParameter().getMinTime() + ", " + + "Pay : " + findGroupByGroupType(groupType).getParameter().getMinPay() + " )" + ); + } + System.out.println(findGroupByGroupType(groupType)); + } +} diff --git a/src/me/smartstore/menu/CustomerMenu.java b/src/me/smartstore/menu/CustomerMenu.java new file mode 100644 index 00000000..e1d814e9 --- /dev/null +++ b/src/me/smartstore/menu/CustomerMenu.java @@ -0,0 +1,211 @@ +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.utils.Message; + +public class CustomerMenu implements Menu { + private static CustomerMenu customerMenu; + private Customers allCustomers; + + public static CustomerMenu getInstance() { + if(customerMenu == null) { + customerMenu = new CustomerMenu(); + } + return customerMenu; + } + + private CustomerMenu() { + this.allCustomers = Customers.getInstance(); + } + + + private final Groups allGroups = Groups.getInstance(); + + @Override + public void manage() { + while ( true ) { + int choice = chooseMenu(new String[]{ + "Add Customer Data", + "View Customer Data", + "Update Customer Data", + "Delete Customer Data", + "Back"}); + + if (choice == 1) addCustomer(); + else if (choice == 2) viewCustomer(); + else if (choice == 3) updateCustomer(); + else if (choice == 4) deleteCustomer(); + else MainMenu.getInstance().manage(); + } + } + + private void addCustomer() { + System.out.println("How many customers to input?"); + int addCustomer = Integer.parseInt(nextLine(Message.END_MSG)); + int cnt = 1; + + while ( cnt <= addCustomer ){ + + String cusName = null; + String cusId = null; + int cusTotalTime = 0; + int cusTotalPay = 0; + + Customer customer = new Customer(); + + while ( true ) { + + System.out.println("====== Customer " + cnt + " Info. ======"); + int choice = chooseMenu(new String[]{ + "Customer Name", + "Customer ID", + "Customer Spent Time", + "Customer Total Pay", + "Back"}); + + if (choice == 1) { + cusName = setCustomerName(); + customer.setCusName(cusName); + } else if (choice == 2) { + cusId = setCustomerID(); + customer.setCusId(cusId); + } else if (choice == 3) { + cusTotalTime = setCustomerSpentTime(); + customer.setCusTotalTime(cusTotalTime); + } else if (choice == 4) { + cusTotalPay = setCustomerTotalPay(); + customer.setCusTotalPay(cusTotalPay); + } else { + allCustomers.add(customer); + allCustomers.refresh(allGroups); + System.out.println(customer); + cnt++; + break; + } + } + } + } + + private String setCustomerName() { + while ( true ) { + try { + System.out.println("Input Customer`s Name: "); + return nextLine(Message.END_MSG); + } 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); + } + } + } + + private String setCustomerID() { + while ( true ) { + try { + System.out.println("Input Customer`s Id: "); + return nextLine(Message.END_MSG); + } 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); + } + } + } + + private int setCustomerSpentTime() { + while ( true ) { + try { + System.out.println("Input Customer`s Spent Time: "); + String minTime = nextLine(Message.END_MSG); + return Integer.parseInt(minTime); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + return 0; + } catch (IllegalArgumentException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + } + + private int setCustomerTotalPay() { + while ( true ) { + try { + System.out.println("Input Customer`s Total Pay: "); + String minPay = nextLine(Message.END_MSG); + return Integer.parseInt(minPay); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + return 0; + } catch (IllegalArgumentException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + } + + private void viewCustomer() { + System.out.println("======= Customer Info. ======="); + allCustomers.viewCustomerData(); + } + + private void updateCustomer() { + allCustomers.viewCustomerData(); + int customerIdx = nextLine(allCustomers.size()); + + int saveIdx = allCustomers.findCustomerDataByCusNo(customerIdx); + Customer customer = allCustomers.get(saveIdx); + + String cusName = null; + String cusId = null; + int cusTotalTime = 0; + int cusTotalPay = 0; + + while ( true ) { + + int choice = chooseMenu(new String[]{ + "Customer Name", + "Customer ID", + "Customer Spent Time", + "Customer Total Pay", + "Back"}); + + if (choice == 1) { + cusName = setCustomerName(); + customer.setCusName(cusName); + } else if (choice == 2) { + cusId = setCustomerID(); + customer.setCusId(cusId); + } else if (choice == 3) { + cusTotalTime = setCustomerSpentTime(); + customer.setCusTotalTime(cusTotalTime); + } else if (choice == 4) { + cusTotalPay = setCustomerTotalPay(); + customer.setCusTotalPay(cusTotalPay); + } else { + allCustomers.set(saveIdx, customer); + allCustomers.refresh(allGroups); + System.out.println(customer); + manage(); + } + } + } + + private void deleteCustomer() { + allCustomers.viewCustomerData(); + int customerIdx = nextLine(allCustomers.size()); + int saveIdx = allCustomers.findCustomerDataByCusNo(customerIdx); + + allCustomers.pop(saveIdx); + + for (int i = saveIdx; i < allCustomers.size(); i++) { + Customer customer = allCustomers.get(i); + System.out.println(customer); + customer.setCusNo(customer.getCusNo()-1); + allCustomers.set(i, customer); + } + } +} diff --git a/src/me/smartstore/menu/GroupMenu.java b/src/me/smartstore/menu/GroupMenu.java new file mode 100644 index 00000000..16e8e42b --- /dev/null +++ b/src/me/smartstore/menu/GroupMenu.java @@ -0,0 +1,185 @@ +package me.smartstore.menu; + +import me.smartstore.customer.Customers; +import me.smartstore.exception.InputEndException; +import me.smartstore.group.Group; +import me.smartstore.group.GroupType; +import me.smartstore.group.Groups; +import me.smartstore.group.GroupConditions; +import me.smartstore.utils.Message; + +public class GroupMenu implements Menu{ + + private static GroupMenu groupMenu; + + private final Groups allGroups; + private final Customers allCustomers; + + public static GroupMenu getInstance() { + if(groupMenu == null) { + groupMenu = new GroupMenu(); + } + return groupMenu; + } + + private GroupMenu() { + this.allGroups = Groups.getInstance(); + this.allCustomers = Customers.getInstance(); + } + + @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 MainMenu.getInstance().manage(); + } + } + + public GroupType chooseGroup() { + while ( true ) { + try { + System.out.println("Which group (GENERAL (G), VIP (V), VVIP (VV))? "); + String choice = nextLine(Message.END_MSG); + return GroupType.valueOf(choice).replaceFullName(); + } catch (InputEndException e) { + System.out.println(e.getMessage()); + 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) manage(); + else if (groupType == GroupType.NONE) { + System.out.println(Message.ERR_MSG_INPUT_NONE); + manage(); + } + + Group group = allGroups.findGroupByGroupType(groupType); + if (group != null && group.getParameter() != null) { + System.out.println("\n" + group.getGroupType() + " group already exists."); + System.out.println("\n" + group); + } else { + inputTimeAndPay(groupType); + allCustomers.refresh(allGroups); + } + } + } + + public void inputTimeAndPay(GroupType groupType) { + + Group group = null; + + int minimumTime = 0; + int minimumPay = 0; + + if (allGroups.findGroupByGroupType(groupType) != null) { + group = allGroups.findGroupByGroupType(groupType); + minimumTime = group.getParameter().getMinTime(); + minimumPay = group.getParameter().getMinPay(); + } else { + allGroups.add(new Group(new GroupConditions(null, null), groupType)); + } + + while ( true ) { + + int choice = chooseMenu(new String[]{ + "Minimum Spent Time", + "Minimum Total Pay", + "Back"}); + + if (choice == 1) minimumTime = setMinimumTime(); + else if (choice == 2) minimumPay = setMinimumPay(); + else { + System.out.println("\n" + group); + manage(); + } + + group = allGroups.findGroupByGroupType(groupType); + group.setParameter(new GroupConditions(minimumTime, minimumPay)); + allGroups.set(allGroups.indexOf(group), group); + + allCustomers.refresh(allGroups); + } + } + + public void viewParameter() { + while ( true ) { + GroupType groupType = chooseGroup(); + + if (groupType == null) manage(); + + Group group = allGroups.findGroupByGroupType(groupType); + + if (group != null && group.getParameter() != null) { + System.out.println(group); + } else if (group == null){ + System.out.println("GroupType: " + groupType); + System.out.println("Parameter: " + null); + } + } + } + + public int setMinimumTime() { + while ( true ) { + try { + System.out.println("Input Minimum Spent Time: "); + String minTime = nextLine(Message.END_MSG); + return Integer.parseInt(minTime); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + } catch (IllegalArgumentException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + } + + public int setMinimumPay() { + while ( true ) { + try { + System.out.println("Input Minimum Total Pay : "); + String minPay = nextLine(Message.END_MSG); + return Integer.parseInt(minPay); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + } catch (IllegalArgumentException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + } + + public void updateParameter() { + while ( true ) { + GroupType groupType = chooseGroup(); + + if (groupType == null) manage(); + else if (groupType == GroupType.NONE) { + System.out.println(Message.ERR_MSG_INPUT_NONE); + manage(); + } + + Group group = allGroups.findGroupByGroupType(groupType); + + if (group != null && group.getParameter() != null) { + System.out.println("\n" + group); + inputTimeAndPay(groupType); + } else { + System.out.println(Message.ERR_MSG_INVALID_PARAMETER_ARR_EMPTY); + manage(); + } + } + } +} diff --git a/src/me/smartstore/menu/MainMenu.java b/src/me/smartstore/menu/MainMenu.java new file mode 100644 index 00000000..299764dd --- /dev/null +++ b/src/me/smartstore/menu/MainMenu.java @@ -0,0 +1,42 @@ +package me.smartstore.menu; + +public class MainMenu implements Menu { + + private static MainMenu mainMenu; + + private final CustomerMenu customerMenu; + private final GroupMenu groupMenu; + private final SummaryMenu summaryMenu; + + public static MainMenu getInstance(){ + if(mainMenu == null){ + mainMenu = new MainMenu(); + } + return mainMenu; + } + + private MainMenu() { + this.customerMenu = CustomerMenu.getInstance(); + this.groupMenu = GroupMenu.getInstance(); + this.summaryMenu = SummaryMenu.getInstance(); + } + + @Override + public void manage() { + while ( true ) { + 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 { + System.out.println("Program Finished"); + break; + } + } + } +} diff --git a/src/me/smartstore/menu/Menu.java b/src/me/smartstore/menu/Menu.java new file mode 100644 index 00000000..2033c023 --- /dev/null +++ b/src/me/smartstore/menu/Menu.java @@ -0,0 +1,60 @@ +package me.smartstore.menu; + +import me.smartstore.exception.InputEndException; +import me.smartstore.exception.InputRangeException; +import me.smartstore.utils.Message; +import java.util.InputMismatchException; +import java.util.Scanner; + +public interface Menu { + Scanner scanner = new Scanner(System.in); + + default String nextLine() { + return scanner.nextLine().toUpperCase(); + } + + default String nextLine(String end) { + System.out.println("** Press 'end', if you want to exit! **"); + String str = scanner.nextLine().toUpperCase(); + if (str.equals(end)) throw new InputEndException(); + return str; + } + + default Integer nextLine(Integer size){ + while( true ) { + try { + System.out.println("Which customer ( 1 ~ " + size + " )?"); + int customerIdx = Integer.parseInt(nextLine()); + if (customerIdx > 0 && customerIdx <= size) return customerIdx; + throw new InputRangeException(); + } catch (InputMismatchException | NumberFormatException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } catch (InputRangeException e) { + System.out.println(e.getMessage()); + } + + } + } + + 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(); + } catch (InputMismatchException | NumberFormatException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } catch (InputRangeException e) { + System.out.println(e.getMessage()); + } + } + } + + void manage(); +} diff --git a/src/me/smartstore/menu/SummaryMenu.java b/src/me/smartstore/menu/SummaryMenu.java new file mode 100644 index 00000000..560ecbde --- /dev/null +++ b/src/me/smartstore/menu/SummaryMenu.java @@ -0,0 +1,132 @@ +package me.smartstore.menu; + +import me.smartstore.customer.Customers; +import me.smartstore.exception.InputEndException; +import me.smartstore.group.GroupType; +import me.smartstore.group.Groups; +import me.smartstore.utils.Message; +import java.util.Arrays; +import java.util.InputMismatchException; + +public class SummaryMenu implements Menu { + + private static SummaryMenu summaryMenu; + + private final Groups allGroups; + private final Customers allCustomers; + + public static SummaryMenu getInstance(){ + if(summaryMenu == null) { + summaryMenu = new SummaryMenu(); + } + return summaryMenu; + } + + private SummaryMenu() { + this.allGroups = Groups.getInstance(); + this.allCustomers = Customers.getInstance(); + } + + @Override + public void manage() { + 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) showSummary(); + else if (choice == 2) showSummarySort("Name"); + else if (choice == 3) showSummarySort("SpentTime"); + else if (choice == 4) showSummarySort("TotalPayment"); + else MainMenu.getInstance().manage(); + } + } + + public GroupType[] distinctGroupTypeList() { + GroupType[] groupType = GroupType.values(); + + for (int i = 0; i < GroupType.values().length; i++) { + groupType[i] = groupType[i].replaceFullName(); + } + + GroupType[] temp = new GroupType[GroupType.values().length]; + int cnt = 0; + + for (int i = 0; i < groupType.length; i++) { + boolean flag = false; + for (int j = i+1; j < groupType.length; j++){ + if (groupType[i].equals(groupType[j])) { + flag= true; + } + } + if (!flag) { + temp[cnt++] = groupType[i]; + } + } + GroupType[] result = new GroupType[cnt]; + + for (int i = 0; i < cnt; i++) { + result[i] = temp[i]; + } + + return result; + } + + private void showSummary() { + GroupType[] groupType = distinctGroupTypeList(); + System.out.println(Arrays.toString(groupType)); + + for (int i = 0; i < groupType.length; i++) { + System.out.println("==============================="); + allGroups.viewGroupTimeAndPayByGroupType(groupType[i]); + System.out.println("==============================="); + allCustomers.viewCustomerDataByGroupType(groupType[i]); + System.out.println("==============================="); + System.out.println(); + } + } + + private String getSummaryOrder() { + while ( true ){ + try { + System.out.println("Which order (ASCENDING (A), DESCENDING (D))?"); + String order = nextLine(Message.END_MSG); + if (order.equals("A") || order.equals("D")) return order; + else if (order.equals("end")) { + System.out.println(Message.ERR_MSG_INPUT_END); + return order; + } + throw new InputMismatchException(); + } catch (InputMismatchException e){ + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } + } + } + + private void showSummarySort(String sortingMethod) { + GroupType[] groupType = distinctGroupTypeList(); + System.out.println(Arrays.toString(groupType)); + + while ( true ){ + try { + String order = getSummaryOrder(); + + for (int i = 0; i < groupType.length; i++) { + System.out.println("==============================="); + allGroups.viewGroupTimeAndPayByGroupType(groupType[i]); + System.out.println("==============================="); + allCustomers.viewCustomerDataByGroupType(groupType[i], sortingMethod, order); + System.out.println("==============================="); + System.out.println(); + } + } catch (InputEndException e) { + System.out.println(e.getMessage());; + manage(); + } + } + } +} + diff --git a/src/me/smartstore/utils/Message.java b/src/me/smartstore/utils/Message.java new file mode 100644 index 00000000..68b664ff --- /dev/null +++ b/src/me/smartstore/utils/Message.java @@ -0,0 +1,16 @@ +package me.smartstore.utils; + +public interface Message { + String ERR_MSG_INVALID_CUSTOMER_ARR_EMPTY = "No Customers. Please input one first."; + String ERR_MSG_INVALID_PARAMETER_ARR_EMPTY = "No parameter. Set the parameter first."; + String ERR_MSG_NULL_ARR_ELEMENT = "Elements in Array has null. Array can't be sorted."; + String ERR_MSG_ARR_EMPTY = "Array is Empty"; + 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. \n"; + String ERR_MSG_INPUT_NONE = "None(GroupType)`s cannot be changed"; + String END_MSG = "END"; +}