Skip to content

Java ToyProject upload by JoonheeJeong #13

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 61 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
f8776e9
feat: declare the application object
JoonheeJeong Apr 28, 2023
2bfe7f6
feat: main method
JoonheeJeong Apr 28, 2023
ffbc374
feat: add interface `Menu`
JoonheeJeong Apr 29, 2023
b496cae
feat: add class `StartMenu`
JoonheeJeong Apr 29, 2023
1ce8ffd
feat: add class `CustomerMenu`
JoonheeJeong Apr 29, 2023
a3ab784
feat: add class `ClassificationSummary`
JoonheeJeong Apr 29, 2023
86c4e3c
feat: add class `GroupMenu`
JoonheeJeong Apr 29, 2023
323a240
feat: add class `EndMenu`
JoonheeJeong Apr 29, 2023
5811b4a
feat: run() of `SmartStoreApp`
JoonheeJeong Apr 29, 2023
415dc40
feat: add constant `OUTPUT` of class `StartMenu`
JoonheeJeong May 1, 2023
ed6f25e
feat: add field `nextMenu` of class `StartMenu`
JoonheeJeong May 1, 2023
d40e2ac
feat: add skeleton of `printAndInputAndGetNextMenu()` of `StartMenu`
JoonheeJeong May 1, 2023
de6d2bd
feat: add class `Printer`
JoonheeJeong May 1, 2023
d9cfd09
feat: add `print()` in `StartMenu`
JoonheeJeong May 1, 2023
c6d704c
feat: add class `Reader`
JoonheeJeong May 1, 2023
4c4b02b
feat: implement `input()` of `StartMenu`
JoonheeJeong May 1, 2023
a5a454c
refactor: modify the exeption handling logic of `printAndInputAndGetN…
JoonheeJeong May 1, 2023
7cbae1b
feat: add class `InvalidMenuException`
JoonheeJeong May 1, 2023
9146a5f
refactor: use `InvalidMenuException` in `StartMenu`
JoonheeJeong May 1, 2023
799d7b3
refactor: move the messege for `InputMismatchException` from `StartMe…
JoonheeJeong May 1, 2023
886d3f1
feat: implement `printAndInputAndGetNextMenu()` of `EndMenu`
JoonheeJeong May 1, 2023
99f584a
feat: implement the skeleton of `GroupMenu`
JoonheeJeong May 1, 2023
b3c325a
feat: implement class `GroupMenu`
JoonheeJeong May 1, 2023
48da62a
feat: add classes `GroupType` and `GroupTypeParameter`
JoonheeJeong May 1, 2023
cdaab8e
feat: add `inputString()` in `Reader`
JoonheeJeong May 1, 2023
3981b3b
fix: `inputInteger()` of `Reader` throws `NumberFormatException` -> `…
JoonheeJeong May 1, 2023
290fb30
feat: new class `BackMenuException`
JoonheeJeong May 1, 2023
7addb20
feat: implement class `ViewParameterMenu`
JoonheeJeong May 1, 2023
35c6bc2
refactor: new class 'InvalidGroupTypeException'
JoonheeJeong May 1, 2023
d5a167e
refactor: extract method `getGroupTypeByString(String)` from `printAn…
JoonheeJeong May 1, 2023
797fb6e
====== 이제부터 `merge --squash`를 활용합니다=====
JoonheeJeong May 3, 2023
ef80e79
feat: implement class `SetParameterMenu`
JoonheeJeong May 3, 2023
9e9ca4e
feat: implement `CustomerMenu`
JoonheeJeong May 4, 2023
ca5fc49
refactor: modify interface `Menu` to abstract class
JoonheeJeong May 4, 2023
6a67c7e
refactor: modify singleton method
JoonheeJeong May 4, 2023
5bc836a
refactor: add abstract class `TopicIntroMenu`
JoonheeJeong May 4, 2023
387b78e
refactor: extends more specific Exception
JoonheeJeong May 4, 2023
3689d5c
refactor: extract common codes of the Group menus
JoonheeJeong May 4, 2023
2f5dbc0
refactor: rename some classes, constants, methods
JoonheeJeong May 4, 2023
8a07b7b
chore: code formatting
JoonheeJeong May 4, 2023
8292a8d
refactor: add packages for each subject in menu
JoonheeJeong May 4, 2023
b8dc537
feat: implment `ViewCustomerMenu`
JoonheeJeong May 6, 2023
a8fa411
feat: implement `AddCustomerMenu`
JoonheeJeong May 6, 2023
ee6023a
refactor: add abstract method `setNextMenus`
JoonheeJeong May 6, 2023
22f4776
feat: implement `InputCustomerIdMenu`
JoonheeJeong May 6, 2023
75d0e79
feat: implement `InputCustomerNameMenu`
JoonheeJeong May 6, 2023
193872a
refactor: add `InputCustomerPropertyMenu`
JoonheeJeong May 6, 2023
e1adfe4
feat: implement `InputCustomerSpentHoursMenu`
JoonheeJeong May 6, 2023
cf3fae0
feat: implemenet `InputcustomerTotalAmountPaid`
JoonheeJeong May 6, 2023
fa5f521
feat: implement `AddCustomerConfirmMenu`
JoonheeJeong May 7, 2023
056d0bd
feat: implement `DeleteCustomerMenu`
JoonheeJeong May 7, 2023
a88f4ae
feat: implement `UpdateCustomerMenu`
JoonheeJeong May 7, 2023
34b65e8
refacotr: modify the array range (1 -> 0 begin)
JoonheeJeong May 8, 2023
9c2f4fc
refactor: formatting in `Menu`
JoonheeJeong May 8, 2023
96667ec
refactor: move directory
JoonheeJeong May 8, 2023
d1de8bd
feat: implemenet `ClassificationMenu`
JoonheeJeong May 9, 2023
0867f52
refactor: rename `totalAmountPaid`
JoonheeJeong May 9, 2023
25fb57c
feat: add `updateGroup()` of `Customer`
JoonheeJeong May 9, 2023
f093a62
feat: delete `SetParameterMenu`
JoonheeJeong May 9, 2023
96a0a0e
fix: fix the architecture of `UpdateParameterMenu`
JoonheeJeong May 10, 2023
fc7a869
refactor: rename, extract, move some classes
JoonheeJeong May 11, 2023
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 src/me/smartstore/AppException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.smartstore;

public class AppException extends Exception {
public AppException(String msg) { super(msg); }
public AppException(Exception e) { super(e); }
public AppException(String msg, Exception e) { super(msg, e); }
}
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().test().run();
// SmartStoreApp.getInstance().run();
}
}
60 changes: 60 additions & 0 deletions src/me/smartstore/SmartStoreApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package me.smartstore;

import me.smartstore.customer.CustomerRepository;
import me.smartstore.group.Group;
import me.smartstore.group.GroupParameter;
import me.smartstore.menu.Menu;
import me.smartstore.menu.topic.StartMenu;

public class SmartStoreApp {

private static final SmartStoreApp INSTANCE = new SmartStoreApp();
private SmartStoreApp() {}
public static SmartStoreApp getInstance() { return INSTANCE; }

public SmartStoreApp test() {
Integer[][] groupParameters = new Integer[][] {
{10, 100_000},
{20, 200_000},
{30, 300_000}
};
CustomerRepository repository = CustomerRepository.getInstance();

Group[] groups = Group.getUsedGroups();
for (int i = 0; i < groups.length; i++) {
Group group = groups[i];
Integer[] param = groupParameters[i];
group.setGroupParameter(param);
for (int j = 0; j < 10; j++) {
repository.resetTempCustomer();

String id = String.format("%s_%02d", group.name(), j);
repository.setTempId(id);

String name = (char) ('a'+j) + "zzz";
repository.setTempName(name);

Integer spentHours = (int) (Math.random() * 10) + param[0];
repository.setTempSpentHours(spentHours);

Integer totalPaidAmount = (int) (Math.random() * 100_000) + param[1];
repository.setTempTotalPaidAmount(totalPaidAmount);

repository.updateGroupOfTempCustomer();
repository.addTempIntoRepository();
}
}
return this;
}

public void run() {
Menu menu = StartMenu.getInstance();
do {
Menu nextMenu = menu.printAndInputAndGetNextMenu();
if (nextMenu == null)
return;
nextMenu.setPrevMenu(menu);
menu = nextMenu;
} while (true);
}
}
107 changes: 107 additions & 0 deletions src/me/smartstore/customer/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package me.smartstore.customer;

import me.smartstore.customer.exception.InvalidCustomerIdException;
import me.smartstore.customer.exception.InvalidCustomerNameException;
import me.smartstore.group.Group;

public class Customer {

public static final String ID_FORMAT =
"ID Format: 4~16 letters consisting of alphabets, digits, underscore(_)";
public static final String NAME_FORMAT =
"Name Format: [1,4] space separated chuck(s) of alphabets of [2,32] lengths.";
private static final String ID_PATTERN = "^[0-9a-zA-Z_]{4,16}$";
private static final String NAME_CHUCK = "[a-z]{2,32}";
private static final String NAME_PATTERN = '^' + NAME_CHUCK + "( " + NAME_CHUCK + "){0,3}$";
private static final int DEFAULT_SPENT_HOURS = 0;
private static final int DEFAULT_TOTAL_PAID_AMOUNT = 0;
private static final Group DEFAULT_GROUP = Group.NONE;

private String id;
private String name;
private Integer spentHours;
private Integer totalPaidAmount;
private Group group;

public Customer(String id, String name) {
this(id, name, DEFAULT_SPENT_HOURS, DEFAULT_TOTAL_PAID_AMOUNT, DEFAULT_GROUP);
}

public Customer(String id, String name, Integer spentHours, Integer totalPaidAmount, Group group) {
this.id = id;
this.name = name;
this.spentHours = spentHours;
this.totalPaidAmount = totalPaidAmount;
this.group = group;
}

public Customer(Customer e) { copy(e); }

public void copy(Customer e) {
id = e.id;
name = e.name;
spentHours = e.spentHours;
totalPaidAmount = e.totalPaidAmount;
group = e.group;
}

public String getId() { return id; }

public void setId(String id, boolean checked) throws InvalidCustomerIdException {
if (!checked)
checkIfIdIsValid(id);
this.id = id;
}

public static void checkIfIdIsValid(String id) throws InvalidCustomerIdException {
if (!isValidId(id))
throw new InvalidCustomerIdException("Invalid ID input.\n");
}

private static boolean isValidId(String id) {
throwIfNull(id, "ID");
return id.matches(ID_PATTERN);
}

public String getName() { return name; }

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

public static void checkIfNameIsValid(String name) throws InvalidCustomerNameException {
if (!isValidName(name))
throw new InvalidCustomerNameException("Invalid Name input.\n");
}

private static boolean isValidName(String name) {
throwIfNull(name, "Name");
return name.matches(NAME_PATTERN);
}

private static void throwIfNull(Object o, String title) {
if (o == null)
throw new IllegalArgumentException(title + " cannot be null.\n");
}

public Integer getSpentHours() { return spentHours; }

public void setSpentHours(Integer spentHours) { this.spentHours = spentHours; }

public Integer getTotalPaidAmount() { return totalPaidAmount; }

public void setTotalPaidAmount(Integer totalPaidAmount) { this.totalPaidAmount = totalPaidAmount; }

public Group getGroup() { return group; }

public void updateGroup() {
group = Group.getGroupByParameter(spentHours, totalPaidAmount);
}

@Override
public String toString() {
return String.format("Customer{id='%s', name='%s', spentHours=%d, totalAmountPaid=%d, group=%s}",
id, name, spentHours, totalPaidAmount, group.name());
}
}
109 changes: 109 additions & 0 deletions src/me/smartstore/customer/CustomerRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package me.smartstore.customer;

import me.smartstore.customer.exception.DuplicateCustomerIdException;
import me.smartstore.customer.exception.InvalidCustomerIdException;
import me.smartstore.customer.exception.InvalidCustomerNameException;
import me.smartstore.group.Group;

public class CustomerRepository extends List<Customer> {

private static class InstanceHolder {
private static final CustomerRepository INSTANCE = new CustomerRepository();
}
public static CustomerRepository getInstance() { return InstanceHolder.INSTANCE; }
private CustomerRepository() {}

private Customer tempCustomer;
private int tempIdx;

public void resetTempCustomer() { tempCustomer = new Customer(null, null); }

public void checkIfHasNoDuplicate(String id) throws DuplicateCustomerIdException {
if (hasCustomerWithId(id))
throw new DuplicateCustomerIdException("The customer already exists which has same id.");
}

private boolean hasCustomerWithId(String id) {
assert id != null;
for (Customer customer : this)
if (id.equals(customer.getId()))
return true;
return false;
}

public void setTempId(String id) throws InvalidCustomerIdException, DuplicateCustomerIdException {
Customer.checkIfIdIsValid(id);
checkIfHasNoDuplicate(id);
tempCustomer.setId(id, true);
}

public void setTempName(String name) throws InvalidCustomerNameException {
tempCustomer.setName(name);
}

public void setTempSpentHours(Integer spentHours) { tempCustomer.setSpentHours(spentHours); }

public void setTempTotalPaidAmount(Integer totalPaidAmount) { tempCustomer.setTotalPaidAmount(totalPaidAmount); }

public String getTempInfo() {
updateGroupOfTempCustomer();
return tempCustomer.toString();
}

public void addTempIntoRepository() {
// app logic에 따라 다음 두 구문이 true 임을 보장합니다.
assert tempCustomer != null;
assert !isReachedMaxCapacity();

add(tempCustomer);
tempCustomer = null;
}

public boolean isTempIdNull() {return tempCustomer.getId() == null; }

public String deleteAndGetInfoOf(int num) {
int idx = num - 1;
return remove(idx).toString();
}

public String setTempAndGetInfoOf(int num) {
int idx = num - 1;
Customer updatingCustomer = get(idx);
tempCustomer = new Customer(updatingCustomer);
tempIdx = idx;
return updatingCustomer.toString();
}

public void updateTempInRepository() { get(tempIdx).copy(tempCustomer); }

public String getUpdateBeforeAndAfterInfo() {
Customer before = get(tempIdx);
Customer after = tempCustomer;
updateGroupOfTempCustomer();
return String.format("\nNo. %2d\n", tempIdx) +
"Before: " + before + '\n'+
"After : " + after;
}

public void updateGroupOfTempCustomer() { tempCustomer.updateGroup(); }

public void updateGroupIn(Group group) {
Group[] groups = Group.values();
int idx = 0;
while (groups[idx] != group)
idx++;
assert idx > 0;
Group rightLowGroup = groups[idx - 1];
for (Customer customer : this) {
Group g = customer.getGroup();
if (g == group || g == rightLowGroup)
customer.updateGroup();
}
}

@Override
public String toString() {
if (isEmpty()) return "No Customers." + " Please input one first.\n";
return super.toString();
}
}
Loading