From f98c3d49b537b5eeb777c60d2f4f62a0339cd876 Mon Sep 17 00:00:00 2001 From: youngjun Date: Wed, 10 May 2023 23:47:02 +0900 Subject: [PATCH] =?UTF-8?q?[1=EC=B0=A8=20VER1.0]=20Java=20ToyProject=20upl?= =?UTF-8?q?oad=20by=20SonYoungJun?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/me/smartstore/Main.java | 8 + src/me/smartstore/SmartStoreApp.java | 60 +++++ src/me/smartstore/arrays/Collections.java | 13 + src/me/smartstore/arrays/MyArray.java | 131 +++++++++ .../arrays/exception/ArrayEmptyException.java | 15 ++ .../exception/ElementNotFoundException.java | 22 ++ .../arrays/exception/EmptyArrayException.java | 22 ++ .../exception/NullArgumentException.java | 22 ++ src/me/smartstore/customer/Customer.java | 96 +++++++ src/me/smartstore/customer/Customers.java | 59 +++++ .../exception/InputEmptyException.java | 15 ++ .../exception/InputEndException.java | 14 + .../exception/InputFormatException.java | 13 + .../exception/InputRangeException.java | 14 + .../exception/InputTypeException.java | 14 + src/me/smartstore/group/Group.java | 53 ++++ src/me/smartstore/group/GroupType.java | 20 ++ src/me/smartstore/group/Groups.java | 26 ++ src/me/smartstore/group/Parameter.java | 53 ++++ src/me/smartstore/menu/CustomerMenu.java | 250 ++++++++++++++++++ src/me/smartstore/menu/GroupMenu.java | 177 +++++++++++++ src/me/smartstore/menu/MainMenu.java | 38 +++ src/me/smartstore/menu/Menu.java | 46 ++++ src/me/smartstore/menu/SummaryMenu.java | 113 ++++++++ .../smartstore/sort/SortedCustomerImpl.java | 14 + .../smartstore/sort/SortedCustomerName.java | 13 + src/me/smartstore/sort/SortedCustomerPay.java | 13 + .../smartstore/sort/SortedCustomerTime.java | 13 + src/me/smartstore/sort/SortedType.java | 13 + src/me/smartstore/util/Message.java | 11 + 30 files changed, 1371 insertions(+) create mode 100644 src/me/smartstore/Main.java create mode 100644 src/me/smartstore/SmartStoreApp.java create mode 100644 src/me/smartstore/arrays/Collections.java create mode 100644 src/me/smartstore/arrays/MyArray.java create mode 100644 src/me/smartstore/arrays/exception/ArrayEmptyException.java create mode 100644 src/me/smartstore/arrays/exception/ElementNotFoundException.java create mode 100644 src/me/smartstore/arrays/exception/EmptyArrayException.java create mode 100644 src/me/smartstore/arrays/exception/NullArgumentException.java create mode 100644 src/me/smartstore/customer/Customer.java create mode 100644 src/me/smartstore/customer/Customers.java create mode 100644 src/me/smartstore/exception/InputEmptyException.java create mode 100644 src/me/smartstore/exception/InputEndException.java create mode 100644 src/me/smartstore/exception/InputFormatException.java create mode 100644 src/me/smartstore/exception/InputRangeException.java create mode 100644 src/me/smartstore/exception/InputTypeException.java create mode 100644 src/me/smartstore/group/Group.java create mode 100644 src/me/smartstore/group/GroupType.java create mode 100644 src/me/smartstore/group/Groups.java create mode 100644 src/me/smartstore/group/Parameter.java create mode 100644 src/me/smartstore/menu/CustomerMenu.java create mode 100644 src/me/smartstore/menu/GroupMenu.java create mode 100644 src/me/smartstore/menu/MainMenu.java create mode 100644 src/me/smartstore/menu/Menu.java create mode 100644 src/me/smartstore/menu/SummaryMenu.java create mode 100644 src/me/smartstore/sort/SortedCustomerImpl.java create mode 100644 src/me/smartstore/sort/SortedCustomerName.java create mode 100644 src/me/smartstore/sort/SortedCustomerPay.java create mode 100644 src/me/smartstore/sort/SortedCustomerTime.java create mode 100644 src/me/smartstore/sort/SortedType.java create mode 100644 src/me/smartstore/util/Message.java 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..f1335db4 --- /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.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.10"); + System.out.println(" Copyright 2023 SonYoungJun 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)); + } + allCustomers.refresh(allGroups); + System.out.println("allCustomers = " + allCustomers); + System.out.println("allGroups = " + allGroups); + return this; // smartStoreApp + } + + 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/MyArray.java b/src/me/smartstore/arrays/MyArray.java new file mode 100644 index 00000000..ce896322 --- /dev/null +++ b/src/me/smartstore/arrays/MyArray.java @@ -0,0 +1,131 @@ +package me.smartstore.arrays; + +import me.smartstore.arrays.exception.ElementNotFoundException; +import me.smartstore.arrays.exception.EmptyArrayException; +import me.smartstore.arrays.exception.NullArgumentException; + +public class MyArray implements Collections { + + protected static final int DEFAULT = 10; + + protected T[] arrays; + protected int size; + protected int capacity; + + public MyArray() { + arrays = (T[]) new Object[DEFAULT]; + capacity = DEFAULT; + } + + public MyArray(int initial) { + arrays = (T[]) new Object[initial]; + capacity = initial; + } + + public MyArray(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]; + } + + @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/arrays/exception/ArrayEmptyException.java b/src/me/smartstore/arrays/exception/ArrayEmptyException.java new file mode 100644 index 00000000..d142def3 --- /dev/null +++ b/src/me/smartstore/arrays/exception/ArrayEmptyException.java @@ -0,0 +1,15 @@ +package me.smartstore.arrays.exception; + + +import me.smartstore.util.Message; + +public class ArrayEmptyException extends RuntimeException { + + public ArrayEmptyException() { + super(Message.ERR_MSG_INVALID_ARR_EMPTY); + } + + public ArrayEmptyException(String message) { + super(message); + } +} diff --git a/src/me/smartstore/arrays/exception/ElementNotFoundException.java b/src/me/smartstore/arrays/exception/ElementNotFoundException.java new file mode 100644 index 00000000..a3b1e7f7 --- /dev/null +++ b/src/me/smartstore/arrays/exception/ElementNotFoundException.java @@ -0,0 +1,22 @@ +package me.smartstore.arrays.exception; + +public class ElementNotFoundException extends RuntimeException { + public ElementNotFoundException() { + } + + public ElementNotFoundException(String message) { + super(message); + } + + public ElementNotFoundException(String message, Throwable cause) { + super(message, cause); + } + + public ElementNotFoundException(Throwable cause) { + super(cause); + } + + public ElementNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/me/smartstore/arrays/exception/EmptyArrayException.java b/src/me/smartstore/arrays/exception/EmptyArrayException.java new file mode 100644 index 00000000..e85a19a8 --- /dev/null +++ b/src/me/smartstore/arrays/exception/EmptyArrayException.java @@ -0,0 +1,22 @@ +package me.smartstore.arrays.exception; + +public class EmptyArrayException extends RuntimeException { + public EmptyArrayException() { + } + + public EmptyArrayException(String message) { + super(message); + } + + public EmptyArrayException(String message, Throwable cause) { + super(message, cause); + } + + public EmptyArrayException(Throwable cause) { + super(cause); + } + + public EmptyArrayException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/me/smartstore/arrays/exception/NullArgumentException.java b/src/me/smartstore/arrays/exception/NullArgumentException.java new file mode 100644 index 00000000..ec912d26 --- /dev/null +++ b/src/me/smartstore/arrays/exception/NullArgumentException.java @@ -0,0 +1,22 @@ +package me.smartstore.arrays.exception; + +public class NullArgumentException extends RuntimeException { + public NullArgumentException() { + } + + public NullArgumentException(String message) { + super(message); + } + + public NullArgumentException(String message, Throwable cause) { + super(message, cause); + } + + public NullArgumentException(Throwable cause) { + super(cause); + } + + public NullArgumentException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/me/smartstore/customer/Customer.java b/src/me/smartstore/customer/Customer.java new file mode 100644 index 00000000..210af9e3 --- /dev/null +++ b/src/me/smartstore/customer/Customer.java @@ -0,0 +1,96 @@ +package me.smartstore.customer; + + +import me.smartstore.group.Group; + +import java.util.Objects; + +public class Customer { + private String userId; + private String name; + private Integer spentTime; + private Integer totalPay; + private Group group; + public Customer() { + } + + public Customer(String userId) { + this.userId = userId; + } + + public Customer(String userId, String name) { + this.userId = userId; + this.name = name; + } + + public Customer(String userId, String name, Integer spentTime, Integer totalPay) { + this.userId = userId; + this.name = name; + this.spentTime = spentTime; + this.totalPay = totalPay; + } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + 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 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 + + '}'; + } +} diff --git a/src/me/smartstore/customer/Customers.java b/src/me/smartstore/customer/Customers.java new file mode 100644 index 00000000..8551fdeb --- /dev/null +++ b/src/me/smartstore/customer/Customers.java @@ -0,0 +1,59 @@ +package me.smartstore.customer; + +import me.smartstore.arrays.MyArray; +import me.smartstore.group.Groups; +import me.smartstore.group.Parameter; +import me.smartstore.sort.SortedType; + +import java.util.Arrays; + +public class Customers extends MyArray { + + private static Customers allCustomers; + public static Customers getInstance() { + if (allCustomers == null) { + allCustomers = new Customers(); + } + return allCustomers; + } + + private Customers() {} + + public void refresh(Groups groups) { + for(int i=0; i= minTime && cusPay >= minPay) + customer.setGroup(groups.get(i)); + } + } + } + + public Customer[] getCustomerArray() { + Customer[] customers = new Customer[size]; + for (int i = 0; i < size; i++) { + customers[i] = allCustomers.get(i); + } + return customers; + } + + public Customer[] sort(SortedType orderType, int type) { + Customer[] customers1 = getCustomerArray(); + if(type == -1) { + Arrays.sort(customers1, orderType.sortedCustomer.reversed()); + } else if(type == 1) { + Arrays.sort(customers1, orderType.sortedCustomer); + } else return customers1; + return customers1; + } + +} diff --git a/src/me/smartstore/exception/InputEmptyException.java b/src/me/smartstore/exception/InputEmptyException.java new file mode 100644 index 00000000..93163bc6 --- /dev/null +++ b/src/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/src/me/smartstore/exception/InputEndException.java b/src/me/smartstore/exception/InputEndException.java new file mode 100644 index 00000000..f549a39e --- /dev/null +++ b/src/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/src/me/smartstore/exception/InputFormatException.java b/src/me/smartstore/exception/InputFormatException.java new file mode 100644 index 00000000..edc44e44 --- /dev/null +++ b/src/me/smartstore/exception/InputFormatException.java @@ -0,0 +1,13 @@ +package me.smartstore.exception; + +import me.smartstore.util.Message; + +public class InputFormatException extends RuntimeException { + public InputFormatException() { + super(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } + + public InputFormatException(String message) { + super(message); + } +} diff --git a/src/me/smartstore/exception/InputRangeException.java b/src/me/smartstore/exception/InputRangeException.java new file mode 100644 index 00000000..fb2c76df --- /dev/null +++ b/src/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/src/me/smartstore/exception/InputTypeException.java b/src/me/smartstore/exception/InputTypeException.java new file mode 100644 index 00000000..d321992f --- /dev/null +++ b/src/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/src/me/smartstore/group/Group.java b/src/me/smartstore/group/Group.java new file mode 100644 index 00000000..e80f33b3 --- /dev/null +++ b/src/me/smartstore/group/Group.java @@ -0,0 +1,53 @@ +package me.smartstore.group; + +import java.util.Objects; + +public class Group { + private Parameter parameter; + private GroupType groupType; + + public Group() { + } + + public Group(Parameter parameter, GroupType groupType) { + this.parameter = parameter; + this.groupType = groupType; + } + + public Parameter getParameter() { + return parameter; + } + + public void setParameter(Parameter parameter) { + this.parameter = parameter; + } + + public GroupType getGroupType() { + return groupType; + } + + public void setGroupType(GroupType groupType) { + this.groupType = groupType; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Group group = (Group) o; + return Objects.equals(parameter, group.parameter) && groupType == group.groupType; + } + + @Override + public int hashCode() { + return Objects.hash(parameter, groupType); + } + + @Override + public String toString() { + return "Group{" + + "parameter=" + parameter + + ", groupType=" + groupType + + '}'; + } +} diff --git a/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..e7d99b71 --- /dev/null +++ b/src/me/smartstore/group/Groups.java @@ -0,0 +1,26 @@ +package me.smartstore.group; + + +import me.smartstore.arrays.MyArray; + +public class Groups extends MyArray { + 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/src/me/smartstore/group/Parameter.java b/src/me/smartstore/group/Parameter.java new file mode 100644 index 00000000..c2b0b3b0 --- /dev/null +++ b/src/me/smartstore/group/Parameter.java @@ -0,0 +1,53 @@ +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/src/me/smartstore/menu/CustomerMenu.java b/src/me/smartstore/menu/CustomerMenu.java new file mode 100644 index 00000000..c70e805f --- /dev/null +++ b/src/me/smartstore/menu/CustomerMenu.java @@ -0,0 +1,250 @@ +package me.smartstore.menu; + +import me.smartstore.customer.Customer; +import me.smartstore.customer.Customers; +import me.smartstore.exception.InputEmptyException; +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(); + private static CustomerMenu customerMenu; + + public static CustomerMenu getInstance() { + if (customerMenu == null) { + customerMenu = new CustomerMenu(); + } + return customerMenu; + } + + private CustomerMenu() {} + + public void refresh() { + allCustomers.refresh(allGroups); + } + + @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 break; + } + } + + public int addCustomerMenu() { + while(true) { + try { + System.out.println(); + System.out.println("==============================="); + System.out.println(" 1. Customer Name"); + System.out.println(" 2. Customer ID"); + System.out.println(" 3. Customer Spent Time"); + System.out.println(" 4. Customer Total Pay"); + System.out.println(" 5. Back"); + System.out.println("==============================="); + System.out.print("Choose One: "); + int choice = Integer.parseInt(nextLine()); + if (choice >= 1 && choice <= 5) + 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); + } + } + } + + public void addCustomer() { + int size = addSizeCustomer(); + for(int i=1; i<=size; i++) { + System.out.println(); + System.out.printf("====== Customer %d Info. ======", i); + Customer customer = new Customer(); + while(true) { + int choice = addCustomerMenu(); + if (choice == 1) setNameCustomer(customer); + else if (choice == 2) setIdCustomer(customer); + else if (choice == 3) setSpentTimeCustomer(customer); + else if (choice == 4) setTotalPayCustomer(customer); + else break; + } + allCustomers.add(customer); + refresh(); + } + } + + public void deleteCustomer() { + viewCustomer(); + if(allCustomers.size() != 0) { + while(true) { + try { + System.out.printf("Which customer ( 1 ~ %d )? ", allCustomers.size()); + int choice = Integer.parseInt(nextLine()); + if (choice >= 1 && choice <= allCustomers.size()) { + allCustomers.pop(choice-1); + break; + } + 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); + } + } + } + } + + public void updateCustomer() { + viewCustomer(); + if(allCustomers.size() != 0) { + while(true) { + try { + System.out.printf("Which customer ( 1 ~ %d )? ", allCustomers.size()); + int choice = Integer.parseInt(nextLine()); + if (choice >= 1 && choice <= allCustomers.size()) { + updateSetCustomer(choice); + break; + } + 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); + } + } + } + } + + public void updateSetCustomer(int num) { + Customer customer = allCustomers.get(num-1); + while(true) { + int choice = addCustomerMenu(); + if (choice == 1) setNameCustomer(customer); + else if (choice == 2) setIdCustomer(customer); + else if (choice == 3) setSpentTimeCustomer(customer); + else if (choice == 4) setTotalPayCustomer(customer); + else break; + } + refresh(); + } + + public void viewCustomer() { + if(allCustomers.size() == 0) { + System.out.println("No Customers. Please input one first."); + } + else { + System.out.println("====== Customer Info. ======"); + for(int i=0; i " + allCustomers.get(i) + "\n", i+1); + } + } + } + + + + public void setTotalPayCustomer(Customer customer) { + while(true) { + System.out.println("Input Customer's Total Pay: "); + try { + int totalPay = Integer.parseInt(nextLine(Message.END_MSG)); + customer.setTotalPay(totalPay); + break; + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + break; + } catch (InputEmptyException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_EMPTY); + } catch (NumberFormatException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } + } + } + + public void setSpentTimeCustomer(Customer customer) { + while(true) { + System.out.println("Input Customer's Spent Time: "); + try { + int spentTime = Integer.parseInt(nextLine(Message.END_MSG)); + customer.setSpentTime(spentTime); + break; + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + break; + } catch (InputEmptyException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_EMPTY); + } catch (NumberFormatException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } + } + } + + public void setIdCustomer(Customer customer) { + while(true) { + System.out.println("Input Customer's Id: "); + try { + String userId = nextLine(Message.END_MSG); + if (userId == null || userId.equals("")) + throw new InputEmptyException(); + customer.setUserId(userId); + break; + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + break; + } catch (InputEmptyException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_EMPTY); + } + } + } + + public void setNameCustomer(Customer customer) { + while(true) { + System.out.println("Input Customer's Name: "); + try { + String name = nextLine(Message.END_MSG); + if (name == null || name.equals("")) + throw new InputEmptyException(); + customer.setName(name); + break; + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + break; + } catch (InputEmptyException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_EMPTY); + } + } + } + + public int addSizeCustomer() { + while(true) { + try { + System.out.println("How many customers to input?"); + int choice = Integer.parseInt(nextLine(Message.END_MSG)); + if(choice < 0) + throw new InputRangeException(); + return choice; + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + return 0; + } catch (NumberFormatException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } catch (InputRangeException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + } + +} diff --git a/src/me/smartstore/menu/GroupMenu.java b/src/me/smartstore/menu/GroupMenu.java new file mode 100644 index 00000000..4d4d9668 --- /dev/null +++ b/src/me/smartstore/menu/GroupMenu.java @@ -0,0 +1,177 @@ +package me.smartstore.menu; + +import me.smartstore.customer.Customers; +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(); + 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.println("Which group (GENERAL (G), VIP (V), VVIP (VV))? "); + String choice = nextLine(Message.END_MSG).toUpperCase(); + 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) { + return; + } + 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(); + while(true) { + int choice = setParameterMenu(); + if(choice == 1) spentMinParameter(parameter); + else if(choice == 2) totalMinParameter(parameter); + else break; + } + allGroups.add(new Group(parameter, groupType)); + allCustomers.refresh(allGroups); + } + } + } + + public void viewParameter() { + while(true) { + GroupType groupType = chooseGroup(); + if(groupType == null) { + return; + } + Group group = allGroups.find(groupType); + if(group == null) { + System.out.println("GroupType: " + groupType); + System.out.println("Parameter: null"); + } else { + System.out.println("GroupType: " + groupType); + System.out.println("Parameter: " + group.getParameter()); + } + } + } + + public void updateParameter() { + while(true) { + GroupType groupType = chooseGroup(); + if(groupType == null) { + return; + } + Group group = allGroups.find(groupType); + if(group == null) { + System.out.println("No parameter. Set the parameter first."); + return; + } + System.out.println("GroupType: " + group.getGroupType()); + System.out.println("Parameter: " + group.getParameter()); + Parameter parameter = group.getParameter(); + while(true) { + int choice = setParameterMenu(); + if(choice == 1) spentMinParameter(parameter); + else if(choice == 2) totalMinParameter(parameter); + else break; + } + } + } + + public int setParameterMenu() { + while(true) { + try { + System.out.println("==============================="); + System.out.println(" 1. Minimum Spent Time"); + System.out.println(" 2. Minimum Total Pay"); + System.out.println(" 3. Back"); + System.out.println("==============================="); + System.out.print("Choose One: "); + int choice = Integer.parseInt(nextLine()); + if (choice >= 1 && choice <= 3) + 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); + } + } + } + + public void spentMinParameter(Parameter parameter) { + while(true) { + try { + System.out.println("Input Minimum Spent Time: "); + int minSpentTime = Integer.parseInt(nextLine(Message.END_MSG)); + parameter.setMinTime(minSpentTime); + allCustomers.refresh(allGroups); + return; + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + return; + } catch (IllegalArgumentException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + } + + public void totalMinParameter(Parameter parameter) { + while(true) { + try { + System.out.println("Input Minimum Total Pay: "); + int minTotalPay = Integer.parseInt(nextLine(Message.END_MSG)); + parameter.setMinPay(minTotalPay); + allCustomers.refresh(allGroups); + return; + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + return; + } catch (IllegalArgumentException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + } +} diff --git a/src/me/smartstore/menu/MainMenu.java b/src/me/smartstore/menu/MainMenu.java new file mode 100644 index 00000000..d7d4f2c2 --- /dev/null +++ b/src/me/smartstore/menu/MainMenu.java @@ -0,0 +1,38 @@ +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(); + + 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 = mainMenu.chooseMenu(new String[] { + "Parameter", + "Customer Data", + "Classification Summary", + "Quit"}); + + if (choice == 1) groupMenu.manage(); + else if (choice == 2) customerMenu.manage(); + else if (choice == 3) summaryMenu.manage(); + else { + System.out.println("Program Finished"); + break; + } + } + } +} diff --git a/src/me/smartstore/menu/Menu.java b/src/me/smartstore/menu/Menu.java new file mode 100644 index 00000000..1b2fdaee --- /dev/null +++ b/src/me/smartstore/menu/Menu.java @@ -0,0 +1,46 @@ +package me.smartstore.menu; + +import me.smartstore.exception.InputEndException; +import me.smartstore.exception.InputRangeException; +import me.smartstore.util.Message; + +import java.util.Scanner; + +public interface Menu { + Scanner 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(); + if (str.toUpperCase().equals(end)) + throw new InputEndException(); + return str; + } + + default int chooseMenu(String[] menus) { + while ( true ) { + try { + System.out.println("==============================="); + for (int i = 0; i < menus.length; i++) { + System.out.printf(" %d. %s\n", i + 1, menus[i]); + } + System.out.println("==============================="); + System.out.print("Choose One: "); + int choice = Integer.parseInt(nextLine()); + if (choice >= 1 && choice <= menus.length) + return choice; + throw new InputRangeException(); + } catch (NumberFormatException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_FORMAT); + } catch (InputRangeException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_RANGE); + } + } + } + + void manage(); +} diff --git a/src/me/smartstore/menu/SummaryMenu.java b/src/me/smartstore/menu/SummaryMenu.java new file mode 100644 index 00000000..c4ec4a5e --- /dev/null +++ b/src/me/smartstore/menu/SummaryMenu.java @@ -0,0 +1,113 @@ +package me.smartstore.menu; + +import me.smartstore.customer.Customer; +import me.smartstore.customer.Customers; +import me.smartstore.exception.InputEndException; +import me.smartstore.exception.InputTypeException; +import me.smartstore.group.GroupType; +import me.smartstore.group.Groups; +import me.smartstore.group.Parameter; +import me.smartstore.sort.SortedType; +import me.smartstore.util.Message; + + +public class SummaryMenu implements Menu { + + private final Groups allGroups = Groups.getInstance(); + private final Customers allCustomers = Customers.getInstance(); + private final int ASC = 1; + private final int DESC = -1; + private final int END = 0; + + private static SummaryMenu summaryMenu; + + 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(allCustomers.getCustomerArray()); + else if (choice == 2) sorted(SortedType.SORTED_NAME); + else if (choice == 3) sorted(SortedType.SORTED_TIME); + else if (choice == 4) sorted(SortedType.SORTED_PAY); + else break; + } + } + + public int sortedType() { + while(true) { + try { + System.out.println("Which order (ASCENDING (A), DESCENDING (D))?"); + String choice = nextLine(Message.END_MSG); + if (choice.equals("A")) { + return ASC; + } else if (choice.equals("D")) { + return DESC; + } else throw new InputTypeException(); + } catch (InputTypeException e) { + System.out.println(Message.ERR_MSG_INVALID_INPUT_TYPE); + } catch (InputEndException e) { + System.out.println(Message.ERR_MSG_INPUT_END); + return END; + } + } + } + + public void sorted(SortedType sortedType) { + while(true) { + int type = sortedType(); + if (type != END) { + summary(allCustomers.sort(sortedType, type)); + } else break; + } + } + + public void summary(Customer[] customers) { + GroupType[] groupType = {GroupType.NONE, GroupType.GENERAL, GroupType.VIP, GroupType.VVIP}; + for(int i=0; i " + customers[i] + "\n", ++count); + continue; + } + if(customers[i].getGroup().getGroupType() == groupType) + System.out.printf("No. %d => " + customers[i] + "\n", ++count); + } + if(count == 0) + System.out.println("Null"); + } + +} diff --git a/src/me/smartstore/sort/SortedCustomerImpl.java b/src/me/smartstore/sort/SortedCustomerImpl.java new file mode 100644 index 00000000..26b5d1a7 --- /dev/null +++ b/src/me/smartstore/sort/SortedCustomerImpl.java @@ -0,0 +1,14 @@ +package me.smartstore.sort; + +import me.smartstore.customer.Customer; + +import java.util.Comparator; + +public interface SortedCustomerImpl extends Comparator { + int compare(Customer o1, Customer o2); + + @Override + default Comparator reversed() { + return Comparator.super.reversed(); + } +} diff --git a/src/me/smartstore/sort/SortedCustomerName.java b/src/me/smartstore/sort/SortedCustomerName.java new file mode 100644 index 00000000..b59fe1d7 --- /dev/null +++ b/src/me/smartstore/sort/SortedCustomerName.java @@ -0,0 +1,13 @@ +package me.smartstore.sort; + +import me.smartstore.customer.Customer; + +public class SortedCustomerName implements SortedCustomerImpl { + @Override + public int compare(Customer o1, Customer o2) { + if(o1.getName() != null && o2.getName() != null) + return o1.getName().compareTo(o2.getName()); + else + return 0; + } +} diff --git a/src/me/smartstore/sort/SortedCustomerPay.java b/src/me/smartstore/sort/SortedCustomerPay.java new file mode 100644 index 00000000..97741624 --- /dev/null +++ b/src/me/smartstore/sort/SortedCustomerPay.java @@ -0,0 +1,13 @@ +package me.smartstore.sort; + +import me.smartstore.customer.Customer; + +public class SortedCustomerPay implements SortedCustomerImpl { + @Override + public int compare(Customer o1, Customer o2) { + if(o1.getTotalPay() != null && o2.getTotalPay() != null) + return o1.getTotalPay().compareTo(o2.getTotalPay()); + else + return 0; + } +} diff --git a/src/me/smartstore/sort/SortedCustomerTime.java b/src/me/smartstore/sort/SortedCustomerTime.java new file mode 100644 index 00000000..092198b5 --- /dev/null +++ b/src/me/smartstore/sort/SortedCustomerTime.java @@ -0,0 +1,13 @@ +package me.smartstore.sort; + +import me.smartstore.customer.Customer; + +public class SortedCustomerTime implements SortedCustomerImpl { + @Override + public int compare(Customer o1, Customer o2) { + if(o1.getSpentTime() != null && o2.getSpentTime() != null) + return o1.getSpentTime().compareTo(o2.getSpentTime()); + else + return 0; + } +} diff --git a/src/me/smartstore/sort/SortedType.java b/src/me/smartstore/sort/SortedType.java new file mode 100644 index 00000000..5cb73d59 --- /dev/null +++ b/src/me/smartstore/sort/SortedType.java @@ -0,0 +1,13 @@ +package me.smartstore.sort; + +public enum SortedType { + SORTED_NAME(new SortedCustomerName()), + SORTED_TIME(new SortedCustomerTime()), + SORTED_PAY(new SortedCustomerPay()); + + public SortedCustomerImpl sortedCustomer; + + SortedType(SortedCustomerImpl sortedCustomer) { + this.sortedCustomer = sortedCustomer; + } +} diff --git a/src/me/smartstore/util/Message.java b/src/me/smartstore/util/Message.java new file mode 100644 index 00000000..1ce12c3e --- /dev/null +++ b/src/me/smartstore/util/Message.java @@ -0,0 +1,11 @@ +package me.smartstore.util; + +public interface Message { + String ERR_MSG_INVALID_ARR_EMPTY = "No Customers. Please input one first."; + 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"; +}