Skip to content
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

[1차 VER1.0] Java ToyProject upload by HaesolChoi #40

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

public class Main {
public static void main(String[] args) {
SmartStoreApp.getInstance().run();
}
}

43 changes: 43 additions & 0 deletions src/me/smartstore/SmartStoreApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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.*;

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.15");
System.out.println(" Copyright 2023 HaesolChoi All rights reserved.");
System.out.println("===========================================\n");
}


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

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

import me.smartstore.exception.EmptyArrayException;
import me.smartstore.exception.ElementNotFoundException;
import me.smartstore.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;

@SuppressWarnings("unchecked")
public DArray() {
arrays = (T[]) new Object[DEFAULT];
capacity = DEFAULT;
}

@SuppressWarnings("unchecked")
public DArray(int initial) {
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;
}
}
96 changes: 96 additions & 0 deletions src/me/smartstore/customer/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package me.smartstore.customer;

import me.smartstore.group.Group;
import java.util.Objects;


public class Customer implements Comparable<Customer> {
private String userId;
private String name;
private Integer spentTime;
private Integer totalPay;
private Group group;

public Customer() {
}

public Customer(String name, String userId, int spentTime, int totalPay) {
this.name = name;
this.userId = userId;
this.spentTime = Integer.valueOf(spentTime);
this.totalPay = Integer.valueOf(totalPay);
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public String getUserId() {
return this.userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public Integer getSpentTime() {
return this.spentTime;
}

public void setSpentTime(Integer spentTime) {
this.spentTime = spentTime;
}

public Integer getTotalPay() {
return this.totalPay;
}

public void setTotalPay(Integer totalPay) {
this.totalPay = totalPay;
}

public Group getGroup() {
return this.group;
}

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

@Override
public int compareTo(Customer o) {
if (this.name.compareTo(o.name) < 0)
return -1;
if (this.name.compareTo(o.name) == 0)
return this.userId.compareTo(o.userId);
return 1;
}

@Override
public boolean equals(Object o) {
if (this == o) {return true;}
if (o == null || getClass() != o.getClass()) {return false;}
Customer customer = (Customer) o;
return Objects.equals(userId, customer.userId);
}

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

@Override
public String toString() {
return "Customer{" +
"userId='" + userId + '\'' +
", name='" + name + '\'' +
", spentTime=" + spentTime +
", totalPay=" + totalPay +
", group=" + group +
'}';
}
}
Loading