-
Notifications
You must be signed in to change notification settings - Fork 166
[1기-엄진환] [3주차][W1D2~W1D5] Command-line Application #8
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
Changes from all commits
2d851cf
e10cad4
5898f08
99a878d
215a432
0567649
66f4dc2
ff07df1
2eb3c88
027209c
c0de96e
68b0fbc
9e3c980
1a895e5
be5206f
de31b20
c5a855f
77c587a
393941e
b6640bc
8861894
053da99
092f76d
1cb7060
ab1242a
20e3c79
1d2816d
c80ad03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| HELP.md | ||
| target/ | ||
| !.mvn/wrapper/maven-wrapper.jar | ||
| !**/src/main/**/target/ | ||
| !**/src/test/**/target/ | ||
|
|
||
| ### STS ### | ||
| .apt_generated | ||
| .classpath | ||
| .factorypath | ||
| .project | ||
| .settings | ||
| .springBeans | ||
| .sts4-cache | ||
|
|
||
| ### IntelliJ IDEA ### | ||
| .idea | ||
| *.iws | ||
| *.iml | ||
| *.ipr | ||
|
|
||
| ### NetBeans ### | ||
| /nbproject/private/ | ||
| /nbbuild/ | ||
| /dist/ | ||
| /nbdist/ | ||
| /.nb-gradle/ | ||
| build/ | ||
| !**/src/main/**/build/ | ||
| !**/src/test/**/build/ | ||
|
|
||
| ### VS Code ### | ||
| .vscode/ | ||
|
|
||
| ### maven ### | ||
| mvnw | ||
| mvnw.cmd | ||
| .mvn |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| normal,ea7bda51-f029-43f6-bcb2-d95afd77f23d,엄진환 | ||
| black,0042406f-f5ff-4b4a-b062-02fa6fc9eaf7,AAA | ||
| normal,b7603e9e-dc0d-4376-8e7a-7bd4586954a6,BBB | ||
| normal,ce26496d-c3eb-44ed-acbd-0e5a908b2b4a,CCC | ||
| black,0042406f-f5ff-4b4a-b062-01fa6fc9eaf7,DDD |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| 18:04:05.296 [main] INFO o.p.k.a.KdtSpringDemoApplication - Starting KdtSpringDemoApplication using Java 16.0.2 on DESKTOP-8KCHBP3 with PID 3312 (C:\Users\Qup\Documents\GitHub\w3-SpringBoot\target\classes started by Qup in C:\Users\Qup\Documents\GitHub\w3-SpringBoot) | ||
| 18:04:05.298 [main] DEBUG o.p.k.a.KdtSpringDemoApplication - Running with Spring Boot v2.5.4, Spring v5.3.9 | ||
| 18:04:05.298 [main] INFO o.p.k.a.KdtSpringDemoApplication - The following profiles are active: dev | ||
| 18:04:05.823 [main] DEBUG o.p.k.order.OrderProperties - version -> v1.0 | ||
| 18:04:05.823 [main] DEBUG o.p.k.order.OrderProperties - minimumOrderAmount -> 1 | ||
| 18:04:05.823 [main] DEBUG o.p.k.order.OrderProperties - supportVendors -> [a, b, c, d] | ||
| 18:04:05.823 [main] DEBUG o.p.k.order.OrderProperties - description -> line 1 hello world | ||
| line 2 xxxx | ||
| line 3 | ||
| 18:04:05.909 [main] INFO o.p.k.a.KdtSpringDemoApplication - Started KdtSpringDemoApplication in 1.124 seconds (JVM running for 4.021) | ||
| 18:11:16.195 [main] INFO o.p.k.a.KdtSpringDemoApplication - Starting KdtSpringDemoApplication using Java 16.0.2 on DESKTOP-8KCHBP3 with PID 22428 (C:\Users\Qup\Documents\GitHub\w3-SpringBoot\target\classes started by Qup in C:\Users\Qup\Documents\GitHub\w3-SpringBoot) | ||
| 18:11:16.198 [main] DEBUG o.p.k.a.KdtSpringDemoApplication - Running with Spring Boot v2.5.4, Spring v5.3.9 | ||
| 18:11:16.199 [main] INFO o.p.k.a.KdtSpringDemoApplication - The following profiles are active: dev | ||
| 18:11:16.768 [main] DEBUG o.p.k.order.OrderProperties - version -> v1.0 | ||
| 18:11:16.768 [main] DEBUG o.p.k.order.OrderProperties - minimumOrderAmount -> 1 | ||
| 18:11:16.769 [main] DEBUG o.p.k.order.OrderProperties - supportVendors -> [a, b, c, d] | ||
| 18:11:16.769 [main] DEBUG o.p.k.order.OrderProperties - description -> line 1 hello world | ||
| line 2 xxxx | ||
| line 3 | ||
| 18:11:16.856 [main] INFO o.p.k.a.KdtSpringDemoApplication - Started KdtSpringDemoApplication in 1.13 seconds (JVM running for 2.362) | ||
| 18:21:51.419 [main] INFO o.p.k.a.KdtSpringDemoApplication - Starting KdtSpringDemoApplication using Java 16.0.2 on DESKTOP-8KCHBP3 with PID 20888 (C:\Users\Qup\Documents\GitHub\w3-SpringBoot\target\classes started by Qup in C:\Users\Qup\Documents\GitHub\w3-SpringBoot) | ||
| 18:21:51.422 [main] DEBUG o.p.k.a.KdtSpringDemoApplication - Running with Spring Boot v2.5.4, Spring v5.3.9 | ||
| 18:21:51.422 [main] INFO o.p.k.a.KdtSpringDemoApplication - The following profiles are active: dev | ||
| 18:21:52.015 [main] DEBUG o.p.k.order.OrderProperties - version -> v1.0 | ||
| 18:21:52.015 [main] DEBUG o.p.k.order.OrderProperties - minimumOrderAmount -> 1 | ||
| 18:21:52.015 [main] DEBUG o.p.k.order.OrderProperties - supportVendors -> [a, b, c, d] | ||
| 18:21:52.016 [main] DEBUG o.p.k.order.OrderProperties - description -> line 1 hello world | ||
| line 2 xxxx | ||
| line 3 | ||
| 18:21:52.099 [main] INFO o.p.k.a.KdtSpringDemoApplication - Started KdtSpringDemoApplication in 1.147 seconds (JVM running for 2.116) | ||
| 18:21:52.102 [main] WARN o.p.k.a.KdtSpringDemoApplication - logger name -> org.prgrms.kdtspringdemo.KdtSpringDemoApplication | ||
| 18:21:52.103 [main] ERROR o.p.k.a.KdtSpringDemoApplication - version -> v1.0 | ||
| 18:21:52.104 [main] ERROR o.p.k.a.KdtSpringDemoApplication - minimumOrderAmount -> 1 | ||
| 18:21:52.104 [main] INFO o.p.k.a.KdtSpringDemoApplication - supportVendors -> [a, b, c, d] | ||
| 18:21:52.104 [main] DEBUG o.p.k.a.KdtSpringDemoApplication - description -> line 1 hello world | ||
| line 2 xxxx | ||
| line 3 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 01:00:29.550 [main] ERROR o.p.kdtspringdemo.console.Console - Invalid command type. Your input -> asdf |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-parent</artifactId> | ||
| <version>2.5.4</version> | ||
| <relativePath/> <!-- lookup parent from repository --> | ||
| </parent> | ||
| <groupId>org.prgrms.kdtspringdemo</groupId> | ||
| <artifactId>w3-SpringBoot</artifactId> | ||
| <version>0.0.1-SNAPSHOT</version> | ||
| <name>w3-SpringBoot</name> | ||
| <description>w3-SpringBoot</description> | ||
| <properties> | ||
| <java.version>16</java.version> | ||
| </properties> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter</artifactId> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-test</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-configuration-processor</artifactId> | ||
| <optional>true</optional> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-maven-plugin</artifactId> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| </project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package org.prgrms.kdtspringdemo; | ||
|
|
||
| public enum CommandType { | ||
| CREATE, LIST, CUSTOMERS, BLACKS, EXIT, ERROR | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package org.prgrms.kdtspringdemo; | ||
|
|
||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
|
||
| @SpringBootApplication | ||
| public class KdtSpringDemoApplication { | ||
| public static void main(String[] args) { | ||
| SpringApplication.run(KdtSpringDemoApplication.class, args); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package org.prgrms.kdtspringdemo; | ||
|
|
||
| import org.prgrms.kdtspringdemo.voucher.FixedAmountVoucher; | ||
| import org.prgrms.kdtspringdemo.voucher.PercentDiscountVoucher; | ||
| import org.prgrms.kdtspringdemo.voucher.Voucher; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| public enum VoucherType { | ||
| FIXED_AMOUNT("F"), PERCENT_DISCOUNT("P"); | ||
|
|
||
| private final String inputCommand; | ||
|
|
||
| VoucherType(String p) { | ||
| this.inputCommand = p; | ||
| } | ||
|
|
||
| public static boolean isInVoucherType(String command) { | ||
| long count = Arrays.stream(VoucherType.values()) | ||
| .filter(voucherType -> voucherType.inputCommand.equals(command)) | ||
| .count(); | ||
| return count != 0; | ||
| } | ||
|
|
||
| private static VoucherType findByCommand(String command) { | ||
| return Arrays.stream(VoucherType.values()) | ||
| .filter(voucherType -> voucherType.inputCommand.equals(command)) | ||
| .findAny() | ||
| .orElseThrow(IllegalArgumentException::new); | ||
| } | ||
|
|
||
| public static Optional<Voucher> getVoucher(String inputCommand, long value) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 음..비즈니스 로직상 getVoucher 라는 naming 보다는 createVoucher 가 나을 것 같고 |
||
| Optional<Voucher> voucher = Optional.empty(); | ||
|
|
||
| switch (findByCommand(inputCommand)) { | ||
| case FIXED_AMOUNT -> voucher = Optional.of(new FixedAmountVoucher(UUID.randomUUID(), value)); | ||
| case PERCENT_DISCOUNT -> voucher = Optional.of(new PercentDiscountVoucher(UUID.randomUUID(), value)); | ||
| } | ||
| return voucher; | ||
| } | ||
|
|
||
| ; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package org.prgrms.kdtspringdemo.application; | ||
|
|
||
| import org.prgrms.kdtspringdemo.console.Console; | ||
| import org.prgrms.kdtspringdemo.console.CustomerOperator; | ||
| import org.prgrms.kdtspringdemo.console.VoucherOperator; | ||
| import org.springframework.boot.ApplicationArguments; | ||
| import org.springframework.boot.ApplicationRunner; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| public class CommandLineApplication implements ApplicationRunner { | ||
| private final VoucherOperator voucherOperator; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기서 구현체로 타입을 선언하는 것이 아니라 다형성을 위해 인터페이스 타입으로 선언합니다. |
||
|
|
||
| public CommandLineApplication(VoucherOperator voucherOperator) { | ||
| this.voucherOperator = voucherOperator; | ||
| } | ||
|
|
||
| @Override | ||
| public void run(ApplicationArguments args) throws Exception { | ||
| new ConsoleApp(new Console(), voucherOperator, new CustomerOperator()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package org.prgrms.kdtspringdemo.application; | ||
|
|
||
| import org.prgrms.kdtspringdemo.CommandType; | ||
| import org.prgrms.kdtspringdemo.console.Console; | ||
| import org.prgrms.kdtspringdemo.console.CustomerOperator; | ||
| import org.prgrms.kdtspringdemo.console.VoucherOperator; | ||
|
|
||
| public class ConsoleApp { | ||
| public ConsoleApp(Console console, VoucherOperator voucherOperator, CustomerOperator customerOperator) { | ||
| console.printStartAppInfo(); | ||
|
|
||
| while (true) { | ||
| CommandType command = console.getInputCommand(); | ||
| switch (command) { | ||
| case CREATE -> { | ||
| console.printCreateTypes(); | ||
| String[] createCommand = console.getCreateLine().split(" "); | ||
| voucherOperator.create(createCommand); | ||
| } | ||
| case CUSTOMERS -> customerOperator.printAll(); | ||
| case BLACKS -> customerOperator.printBlacklist(); | ||
| case LIST -> voucherOperator.printAll(); | ||
| case EXIT -> System.exit(0); | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package org.prgrms.kdtspringdemo.configuration; | ||
|
|
||
| import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
| import org.springframework.context.annotation.ComponentScan; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.context.annotation.PropertySource; | ||
|
|
||
| @Configuration | ||
| @ComponentScan(basePackages = { | ||
| "org.prgrms.kdtspringdemo.order", | ||
| "org.prgrms.kdtspringdemo.voucher", | ||
| "org.prgrms.kdtspringdemo.customer", | ||
| "org.prgrms.kdtspringdemo.configuration" | ||
| }) | ||
| @PropertySource(value = "application.yaml", factory = YamlPropertiesFactory.class) | ||
| @EnableConfigurationProperties | ||
| public class AppConfiguration { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package org.prgrms.kdtspringdemo.configuration; | ||
|
|
||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| public class AwsConfiguration { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package org.prgrms.kdtspringdemo.configuration; | ||
|
|
||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| public class GcpConfiguration { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package org.prgrms.kdtspringdemo.configuration; | ||
|
|
||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| public class SecurityConfiguration { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package org.prgrms.kdtspringdemo.configuration; | ||
|
|
||
| import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; | ||
| import org.springframework.core.env.PropertiesPropertySource; | ||
| import org.springframework.core.env.PropertySource; | ||
| import org.springframework.core.io.support.EncodedResource; | ||
| import org.springframework.core.io.support.PropertySourceFactory; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class YamlPropertiesFactory implements PropertySourceFactory { | ||
| @Override | ||
| public PropertySource<?> createPropertySource(String s, EncodedResource encodedResource) throws IOException { | ||
| var yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean(); | ||
| yamlPropertiesFactoryBean.setResources(encodedResource.getResource()); | ||
|
|
||
| var properties = yamlPropertiesFactoryBean.getObject(); | ||
| return new PropertiesPropertySource(encodedResource.getResource().getFilename(), properties); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package org.prgrms.kdtspringdemo.console; | ||
|
|
||
| public interface CommandOperator { | ||
| public void create(String[] splitList); | ||
|
|
||
| public void printAll(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package org.prgrms.kdtspringdemo.console; | ||
|
|
||
| import org.prgrms.kdtspringdemo.CommandType; | ||
| import org.prgrms.kdtspringdemo.io.*; | ||
| import org.prgrms.kdtspringdemo.order.OrderProperties; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.text.MessageFormat; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Console implements Input, Output { | ||
| private final static Logger logger = LoggerFactory.getLogger(Console.class); | ||
| private Scanner input; | ||
|
|
||
| public Console() { | ||
| this.input = new Scanner(System.in); | ||
| } | ||
|
|
||
| @Override | ||
| public void printStartAppInfo() { | ||
| System.out.println("=== Voucher Program ==="); | ||
| printCommandList(); | ||
| } | ||
|
|
||
| @Override | ||
| public void printCommandList() { | ||
| System.out.println(""" | ||
| Type exit to exit the program. | ||
| Type create to create a new voucher. | ||
| Type list to list all vouchers. | ||
| Type customers to list all customers | ||
| Type blacks to list all blacklist customers"""); | ||
| } | ||
|
|
||
| @Override | ||
| public void printCommandError(String command) { | ||
| System.out.println(MessageFormat.format("[ERROR]Invalid command type \nYour input : {0}", command)); | ||
| printCommandList(); | ||
| System.out.println("================="); | ||
| } | ||
|
|
||
| @Override | ||
| public void printCreateTypes() { | ||
| System.out.println(""" | ||
| input voucher info | ||
| <vouhcerType> <data> | ||
| voucherType | ||
| \tP : FixedAmountVoucher | ||
| \tF : PercentDiscountVoucher | ||
| data is long value | ||
| ex) P 10"""); | ||
| } | ||
|
|
||
| @Override | ||
| public CommandType getInputCommand() { | ||
| ; | ||
| String command = input.nextLine(); | ||
| try { | ||
| return CommandType.valueOf(command.toUpperCase()); | ||
| } catch (IllegalArgumentException e) { | ||
| logger.error(MessageFormat.format("Invalid command type. Your input -> {0}", command)); | ||
| printCommandError(command); | ||
| return CommandType.ERROR; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String getCreateLine() { | ||
| return input.nextLine(); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
잘못 입력했을때 예외를 던지는 것 말고 친절하게 다시 command를 작성할 수 있도록 바꿔볼까요?