Skip to content

[1차 VER1.0] Java ToyProject upload by JeonghoSong #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions ToyProject/src/me/smartstore/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.smartstore;

public class Main {
public static void main(String[] args) {
SmartStoreApp.getInstance().run(); // function chaining
}
}
87 changes: 87 additions & 0 deletions ToyProject/src/me/smartstore/SmartStoreApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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();

private static SmartStoreApp smartStoreApp;

// singletone pattern
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.00");
System.out.println(" Copyright 2023 JeonghoSong All rights reserved.");
System.out.println("===========================================\n");
}

public SmartStoreApp test() {
allGroups.add(new Group(new Parameter(0, 0), GroupType.NONE));
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++) {
int spentTime = ((int) (Math.random() * 5) + 1) * 10;
int totalPay = ((int) (Math.random() * 5) + 1) * 100000;

// Group customerGroup = allGroups.getGroupByParameter(spentTime, totalPay);

// 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.add(new Customer(
// Character.toString(
// (char) ('a' + i) + "123",
// spentTime,
// totalPay,
// customerGroup
// )
// ));

// // 마지막 사용하던 코드
// allCustomers.add(new Customer(
// Character.toString(
// (char) ('a' + i)),
// (char) ('a' + i) + "123",
// spentTime,
// totalPay,
// customerGroup));
}

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

return this;
}

public void run() {
details();
mainMenu.manage();
}

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

import me.smartstore.exception.EmptyArrayException;
import me.smartstore.group.Group;
import me.smartstore.util.Message;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Objects;
import java.util.stream.IntStream;

import static java.util.Arrays.copyOf;

public class DArray<T> implements Collections<T> {

protected static final int DEFAULT = 10;

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

/**
* ClassCastException 예외 던지는 이유
* DArray Constructor에서 T타입이 Object 타입으로 캐스팅되는 과정에서 발생 가능
*
* 즉, T타입이 Object 타입과 호환되지 않아서 ClassCastException 예외 발생 가능
*/
public DArray() throws ClassCastException {
arrays = (T[]) new Object[DEFAULT];
capacity = DEFAULT;
}

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

/**
* ClassCastException 안 던지는 이유
* T[] 배열을 이미 파라미터로 직접 전달 받음 > 예외 발생 가능성 없음
*/
public DArray(T[] arrays) {
this.arrays = arrays;
capacity = arrays.length;
size = arrays.length;
}

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

/**
*
* @param index
* @return
* @throws IndexOutOfBoundsException
*
* 예외 처리를 두 번 해주는 이유
* 메서드 호출자가 index value를 잘못 전달할 경우 걸러지지 않음.
* if문 안에서는 인덱스 값이 유효하지 않은 경우에 대한 예외 처리 담당
*/
@Override
public T get(int index) throws IndexOutOfBoundsException {
Copy link
Member

Choose a reason for hiding this comment

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

꼼꼼한 예외처리 좋습니다!!!

어떤 경우 throw로 터트리는게 좋고, 또 어떤 경우 throws로 넘길까 고민해보는거 되게 좋은 것 같아요 👍

if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
return arrays[index];
}

@Override
public void set(int index, T object) {

}

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

@Override
public void add(T object) {
// if (object == null) {
// throw new
// }
if (size < capacity) {
arrays[size] = object;
size++;
} else {
extend();
add(object);
}
}

@Override
public void add(int index, T object) {

}

@Override
public T pop() {
return null;
}

@Override
public T pop(int index) throws EmptyArrayException, IndexOutOfBoundsException {
if (size==0) throw new EmptyArrayException(Message.ERR_MSG_ARR_EMPTY);
if (index < 0 || index > size) throw new IndexOutOfBoundsException(Message.ERR_MSG_ARR_OUT_OF_BOUNDARY);
T item = arrays[index];
for (int i = index; i < size; i++) {
arrays[i] = arrays[i+1];
}
size--;
return item;
}

// public boolean isEmpty() {
//
// }

@Override
public T pop(T object) throws EmptyArrayException {
return pop(size-1);
}

/**
* 목적 : 리스트의 capacity가 모자랄 경우, *2만큼 증분하여 리스트 길이를 증가
*/
@Override
public void extend() {
capacity *= 2;
arrays = copyOf(arrays, capacity);
}


// @Override
// public Iterator<T> iterator() {
// return IntStream.range(0, size)
// .mapToObj(i -> arrays[i])
// .iterator();
// }


}
93 changes: 93 additions & 0 deletions ToyProject/src/me/smartstore/customer/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package me.smartstore.customer;

import me.smartstore.group.Group;
import me.smartstore.group.Groups;

public class Customer {
private String customerName;
private String customerId;
private Integer customerTotalTime;
private Integer customerTotalPay;
private Group group; // 분류 기준에 따라 고객을 분류한 결과

public Customer() {
}

public Customer(String customerId) {
this.customerId = customerId;
}

public Customer(String customerName, String customerId) {
this.customerName = customerName;
this.customerId = customerId;
}

public Customer(String customerName, String customerId, Integer customerTotalTime, Integer customerTotalPay) {
this.customerName = customerName;
this.customerId = customerId;
this.customerTotalTime = customerTotalTime;
this.customerTotalPay = customerTotalPay;
}

public Customer(String customerName, String customerId, Integer customerTotalTime, Integer customerTotalPay, Group group) {
this.customerName = customerName;
this.customerId = customerId;
this.customerTotalTime = customerTotalTime;
this.customerTotalPay = customerTotalPay;
}

public void getCustomerInfo() {
Copy link
Member

Choose a reason for hiding this comment

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

오잉?


}

public String getCustomerName() {
return customerName;
}

public void setCustomerName(String customerName) {
this.customerName = customerName;
}

public String getCustomerId() {
return customerId;
}

public void setCustomerId(String customerId) {
this.customerId = customerId;
}

public Integer getCustomerTotalTime() {
return customerTotalTime;
}

public void setCustomerTotalTime(Integer customerTotalTime) {
this.customerTotalTime = customerTotalTime;
}

public Integer getCustomerTotalPay() {
return customerTotalPay;
}

public void setCustomerTotalPay(Integer customerTotalPay) {
this.customerTotalPay = customerTotalPay;
}

public Group getGroup() {
return group;
}

public void setGroup(Group group) {
this.group = group;
}

@Override
public String toString() {
return "Customer{" +
"customerName='" + customerName + '\'' +
", customerId='" + customerId + '\'' +
", customerTotalTime=" + customerTotalTime +
", customerTotalPay=" + customerTotalPay +
", group=" + group +
'}';
}
}
Loading