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 JinyoungJang #36

Open
wants to merge 6 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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions jyjang/smartstore/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package jyjang.smartstore;

public class Main {

public static void main(String[] args) {
// SmartStoreApplication.getInstance().test().run();
SmartStoreApplication.getInstance().run();
}
}
45 changes: 45 additions & 0 deletions jyjang/smartstore/SmartStoreApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package jyjang.smartstore;

import jyjang.smartstore.customer.Customers;
import jyjang.smartstore.group.Groups;
import jyjang.smartstore.menu.*;

public class SmartStoreApplication {

private static SmartStoreApplication smartStoreApplication;

public static SmartStoreApplication getInstance() {
if (smartStoreApplication == null)
smartStoreApplication = new SmartStoreApplication();
return smartStoreApplication;
}



private final Groups allGroups = Groups.getInstance();
private final Customers allCustomers = Customers.getInstance();

private final MainMenu mainMenu = MainMenu.getInstance();

public SmartStoreApplication test() {
// this.allGroups.add(new Group(GroupType.GENERAL, new Parameter(Integer.valueOf(10), Integer.valueOf(100000))));
// this.allGroups.add(new Group(GroupType.VIP, new Parameter(Integer.valueOf(20), Integer.valueOf(200000))));
// this.allGroups.add(new Group(GroupType.VVIP, new Parameter(Integer.valueOf(30), Integer.valueOf(300000))));
// for (int i = 0; i < 20; i++)
// this.allCustomers.add(new Customer(
// Character.toString((char)(97 + i)), "" + (char)(97 + i) + "123", (
//
// (int)(Math.random() * 5.0D) + 1) * 10, (
// (int)(Math.random() * 5.0D) + 1) * 100000));
// this.allCustomers.refresh(this.allGroups);
return this;
}

private SmartStoreApplication() {

}

public void run() {
mainMenu.manage();
}
}
14 changes: 14 additions & 0 deletions jyjang/smartstore/arrays/Collections.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package jyjang.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);
}
163 changes: 163 additions & 0 deletions jyjang/smartstore/arrays/DArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package jyjang.smartstore.arrays;

import jyjang.smartstore.exception.ElementNotFoundException;
import jyjang.smartstore.exception.EmptyArrayException;
import jyjang.smartstore.exception.NullArgumentException;

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

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;
}

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

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

// 배열의 cap이 부족한 경우
@Override
public void add(T object) throws NullArgumentException {
// if argument is null, do not add null value in array
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; // doubling
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;
}
}
94 changes: 94 additions & 0 deletions jyjang/smartstore/customer/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package jyjang.smartstore.customer;

import jyjang.smartstore.group.Group;

import java.util.Objects;

public class Customer {

private String cName;
private String cId;
private Integer totalTime;
private Integer totalPay;

private Group group;

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 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 +
'}';
}
}
52 changes: 52 additions & 0 deletions jyjang/smartstore/customer/Customers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package jyjang.smartstore.customer;

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

public class Customers extends DArray<Customer> {

// singleton
private static Customers allCustomers;
private final Groups allGroups;

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

private Customers() {
this.allGroups = Groups.getInstance();
}

// refresh 함수가 호출되는 경우
// 1. 분류기준 바뀔 때
// 2. 새로운 고객이 들어올 때
public void refresh() {
for (int i = 0; i < allCustomers.size(); i++) {
Customer customer = allCustomers.get(i);
customer.setGroup(findGroupByCustomer(customer));
}

}

public Group findGroupByCustomer(Customer customer) {

int totalPay = customer.getTotalPay();
int totalTime = customer.getTotalTime();

for (int i = allGroups.size() - 1; i >= 0; i--) {
Group group = allGroups.get(i);

if (totalPay >= group.getParameter().getMinimumTotalPay() && totalTime >= group.getParameter().getMinimumSpentTime()) {
return group;
}
}

return null;
}

}
Loading