Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions me.smartstore/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import customer.Customers;

public class Main {
public static void main(String[] args) {
SmartStoreApp.getInstance().test().run();
//SmartStoreApp.getInstance().run();
}
}
57 changes: 57 additions & 0 deletions me.smartstore/src/SmartStoreApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import customer.Customer;
import customer.Customers;
import group.Group;
import group.GroupType;
import group.Groups;
import group.Parameter;
import menu.*;


public class SmartStoreApp {
private final Groups allGroups = Groups.getInstance();
private final Customers allCustomers = Customers.getInstance();
private final MainMenu mainMenu = MainMenu.getInstance();
private static SmartStoreApp smartStoreApp;

public static SmartStoreApp getInstance(){
if(smartStoreApp == null){
smartStoreApp = new SmartStoreApp();
}
return smartStoreApp;
}
private SmartStoreApp(){}
public void details(){
System.out.println("\n\n===========================================");
System.out.println(" Title : SmartStore Customer Classification");
System.out.println(" Release Date : 23.04.27");
System.out.println(" Copyright 2023 Eunbin All rights reserved.");
System.out.println("===========================================\n");
}
public SmartStoreApp test(){
allGroups.add(new Group(new Parameter(10, 100000), GroupType.GENERAL));
allGroups.add(new Group(new Parameter(20, 200000), GroupType.VIP));
allGroups.add(new Group(new Parameter(30, 300000), GroupType.VVIP));

for (int i = 0; i < 26; i++) {
allCustomers.add(new Customer(
Character.toString(
(char) ('a' + i)),
(char) ('a' + i) + "123",
((int) (Math.random() * 5) + 1) * 10,
((int) (Math.random() * 5) + 1) * 100000));
}

System.out.println("allCustomers = " + allCustomers);
System.out.println("allGroups = " + allGroups);

// @TODO: refresh do not implemented yet.
allCustomers.refresh();

return this; // smartStoreApp
}
public void run() {
details();
mainMenu.manage();
}

}
16 changes: 16 additions & 0 deletions me.smartstore/src/arrays/Collections.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package arrays;

public interface Collections<T> {
// 데이터를 가지고 있는 객체가 아님
// 구현 해야하는 메소드의 정보만 가지고 있음 (인터페이스)

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);
}
153 changes: 153 additions & 0 deletions me.smartstore/src/arrays/DArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package arrays;

import exception.ElementNotFoundException;
import exception.EmptyArrayException;
import exception.NullArgumentException;

public class DArray<T> implements Collections<T> { // Dynamic Array

protected static final int DEFAULT = 10;

protected T[] arrays;
protected int size;
protected int capacity;

public DArray() throws ClassCastException {
arrays = (T[]) new Object[DEFAULT];
capacity = DEFAULT;
}

public DArray(int initial) throws ClassCastException {
arrays = (T[]) new Object[initial];
capacity = initial;
}

public DArray(T[] arrays) {
this.arrays = arrays;
capacity = arrays.length;
size = arrays.length;
}

/////////////////////////////////////////
// add, set, get, pop, indexOf, size, capacity (for dynamic-sized array)

@Override
public int size() {
return size;
}

// 배열에 얼마나 capacity 남아있는지 외부에 알려줄 필요가 없기 때문에 <protected>으로 정의
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(); // not found (instead of throwing exception)

for (int i = 0; i < size; i++) {
if (arrays[i] == null) continue;
if (arrays[i].equals(object)) return i;
}
throw new ElementNotFoundException(); // not found
}

// 배열의 cap이 부족한 경우
@Override
public void add(T object) throws NullArgumentException {
if (object == null) throw new NullArgumentException(); // if argument is null, do not add null value in array

if (size < capacity) {
arrays[size] = object;
size++;
} else {
grow();
add(object);
}
}

@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() {
// if (size == 0) return null;
//
// T popElement = arrays[size-1];
// arrays[size-1] = null;
// size--;
// return popElement;
return pop(size-1);
}

@Override
public T pop(int index) throws IndexOutOfBoundsException {
if (size == 0) throw new EmptyArrayException();
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();

T popElement = arrays[index];
arrays[index] = null; // 삭제됨을 명시적으로 표현

for (int i = index+1; i < size; i++) {
arrays[i-1] = arrays[i];
}
arrays[size-1] = null;
size--;
return popElement;
}

@Override
public T pop(T object) {
return pop(indexOf(object));
}

protected void grow() {
capacity *= 2; // doubling
arrays = java.util.Arrays.copyOf(arrays, capacity);

// size는 그대로
}

@Override
public String toString() {
String toStr = "";
for (int i = 0; i < size; i++) {
toStr += (arrays[i] + "\n");
}
return toStr;
}
}
109 changes: 109 additions & 0 deletions me.smartstore/src/customer/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package customer;

import group.Group;
import jdk.jfr.Description;

import java.util.Objects;

public class Customer implements Comparable<Customer> {
private String cName;
private String cId;
private Integer totalTime;
private Integer totalPay;


private Group group;
//refresh 함수가 호출되는 경우 -> 분류기준이 바뀔때, 새로운 고객이 들어올때

public Customer(){

}
public Customer(String cName, String cId) {
this.cName = cName;
this.cId = cId;
}

public Customer(String cName, String cId, Integer totalTime, Integer totalPay) {
this.cName = cName;
this.cId = cId;
this.totalTime = totalTime;
this.totalPay = totalPay;
}
public Customer(String cName, String cId, Integer totalTime, Integer totalPay, Group group) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 생성자를 통해 기준에 맞지 않는 그룹을 넣을 수 있지 않을까라는 생각이 듭니다!

this.cName = cName;
this.cId = cId;
this.totalTime = totalTime;
this.totalPay = totalPay;
this.group = group;
}

public String getcName() {
return cName;
}

public void setcName(String cName) {
this.cName = cName;
}

public String getcId() {
return cId;
}

public void setcId(String cId) {
this.cId = cId;
}

public Integer getTotalTime() {
return totalTime;
}

public void setTotalTime(Integer totalTime) {
this.totalTime = totalTime;
}

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(cId, customer.cId);
}

@Override
public int hashCode() {
return Objects.hash(cId);
}

@Override
public String toString() {
return "Customer{" +
"cName='" + cName + '\'' +
", cId='" + cId + '\'' +
", totalTime=" + totalTime +
", totalPay=" + totalPay +
", group=" + group+
'}';
}
/**
* 정렬을 위한 메소드
*/
@Override
public int compareTo(Customer o) {
return this.cName.compareTo(o.cName);
}
}
51 changes: 51 additions & 0 deletions me.smartstore/src/customer/Customers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package customer;

import arrays.DArray;
import group.Group;
import group.GroupType;
import group.Groups;

public class Customers extends DArray<Customer>{
private final Groups allGroups = Groups.getInstance();
private static Customers allCustomers;

public static Customers getInstance() {
if (allCustomers == null) {
allCustomers = new Customers();
}
return allCustomers;
}

private Customers() {}

// refresh 함수가 호출되는 경우
// 1. 분류기준 바뀔 때
// 2. 새로운 고객이 들어올 때
public void refresh() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refresh 메서드를 오버로딩해서 한 고객에 대해서만 업데이트하는 메서드가 있으면 좋을 것 같습니다!
고객 한 명을 추가했는데 모든 고객을 refresh한다면 비효율적이니까요 😀

for(int i=0; i< allCustomers.size(); i++){
Customer customer = allCustomers.get(i);
Group group = findGroup(customer.getTotalTime(), customer.getTotalPay());
customer.setGroup(group);
}
}

/**
* 시간과 돈을 매개변수로 받아 해당하는 등급을 반환
* */
public Group findGroup(int totalTime, int totalPay){
try {
if(totalTime >= allGroups.find(GroupType.VVIP).getParameter().getMinTime() && totalPay>=allGroups.find(GroupType.VVIP).getParameter().getMinPay()){
return allGroups.find(GroupType.VVIP);
} else if (totalTime >= allGroups.find(GroupType.VIP).getParameter().getMinTime() && totalPay>=allGroups.find(GroupType.VIP).getParameter().getMinPay()) {
return allGroups.find(GroupType.VIP);
} else if (totalTime >= allGroups.find(GroupType.GENERAL).getParameter().getMinTime() && totalPay>=allGroups.find(GroupType.GENERAL).getParameter().getMinPay()) {
return allGroups.find(GroupType.GENERAL);
} else{
return allGroups.find(GroupType.NONE);
}
} catch (NullPointerException e){
return null;
}
}

}
Loading