Skip to content

Commit e8596fc

Browse files
authored
[4기 - 황창현] 1~2주차 과제 : 계산기 구현 미션 제출합니다.
2 parents d46ad09 + 9dec394 commit e8596fc

23 files changed

+937
-0
lines changed

.gitignore

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Created by https://www.toptal.com/developers/gitignore/api/java,intellij,gradle
2+
# Edit at https://www.toptal.com/developers/gitignore?templates=java,intellij,gradle
3+
4+
### Intellij ###
5+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
6+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
7+
8+
# User-specific stuff
9+
.idea/**/workspace.xml
10+
.idea/**/tasks.xml
11+
.idea/**/usage.statistics.xml
12+
.idea/**/dictionaries
13+
.idea/**/shelf
14+
15+
# AWS User-specific
16+
.idea/**/aws.xml
17+
18+
# Generated files
19+
.idea/**/contentModel.xml
20+
21+
# Sensitive or high-churn files
22+
.idea/**/dataSources/
23+
.idea/**/dataSources.ids
24+
.idea/**/dataSources.local.xml
25+
.idea/**/sqlDataSources.xml
26+
.idea/**/dynamic.xml
27+
.idea/**/uiDesigner.xml
28+
.idea/**/dbnavigator.xml
29+
30+
# Gradle
31+
.idea/**/gradle.xml
32+
.idea/**/libraries
33+
34+
# Gradle and Maven with auto-import
35+
# When using Gradle or Maven with auto-import, you should exclude module files,
36+
# since they will be recreated, and may cause churn. Uncomment if using
37+
# auto-import.
38+
# .idea/artifacts
39+
# .idea/compiler.xml
40+
# .idea/jarRepositories.xml
41+
# .idea/modules.xml
42+
# .idea/*.iml
43+
# .idea/modules
44+
# *.iml
45+
# *.ipr
46+
47+
.idea
48+
gradle
49+
gradlew
50+
gradlew.bat
51+
52+
# CMake
53+
cmake-build-*/
54+
55+
# Mongo Explorer plugin
56+
.idea/**/mongoSettings.xml
57+
58+
# File-based project format
59+
*.iws
60+
61+
# IntelliJ
62+
out/
63+
64+
# mpeltonen/sbt-idea plugin
65+
.idea_modules/
66+
67+
# JIRA plugin
68+
atlassian-ide-plugin.xml
69+
70+
# Cursive Clojure plugin
71+
.idea/replstate.xml
72+
73+
# SonarLint plugin
74+
.idea/sonarlint/
75+
76+
# Crashlytics plugin (for Android Studio and IntelliJ)
77+
com_crashlytics_export_strings.xml
78+
crashlytics.properties
79+
crashlytics-build.properties
80+
fabric.properties
81+
82+
# Editor-based Rest Client
83+
.idea/httpRequests
84+
85+
# Android studio 3.1+ serialized cache file
86+
.idea/caches/build_file_checksums.ser
87+
88+
### Intellij Patch ###
89+
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
90+
91+
# *.iml
92+
# modules.xml
93+
# .idea/misc.xml
94+
# *.ipr
95+
96+
# Sonarlint plugin
97+
# https://plugins.jetbrains.com/plugin/7973-sonarlint
98+
.idea/**/sonarlint/
99+
100+
# SonarQube Plugin
101+
# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
102+
.idea/**/sonarIssues.xml
103+
104+
# Markdown Navigator plugin
105+
# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
106+
.idea/**/markdown-navigator.xml
107+
.idea/**/markdown-navigator-enh.xml
108+
.idea/**/markdown-navigator/
109+
110+
# Cache file creation bug
111+
# See https://youtrack.jetbrains.com/issue/JBR-2257
112+
.idea/$CACHE_FILE$
113+
114+
# CodeStream plugin
115+
# https://plugins.jetbrains.com/plugin/12206-codestream
116+
.idea/codestream.xml
117+
118+
# Azure Toolkit for IntelliJ plugin
119+
# https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij
120+
.idea/**/azureSettings.xml
121+
122+
### Java ###
123+
# Compiled class file
124+
*.class
125+
126+
# Log file
127+
*.log
128+
129+
# BlueJ files
130+
*.ctxt
131+
132+
# Mobile Tools for Java (J2ME)
133+
.mtj.tmp/
134+
135+
# Package Files #
136+
*.jar
137+
*.war
138+
*.nar
139+
*.ear
140+
*.zip
141+
*.tar.gz
142+
*.rar
143+
144+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
145+
hs_err_pid*
146+
replay_pid*
147+
148+
### Gradle ###
149+
.gradle
150+
**/build/
151+
!src/**/build/
152+
153+
# Ignore Gradle GUI config
154+
gradle-app.setting
155+
156+
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
157+
!gradle-wrapper.jar
158+
159+
# Avoid ignore Gradle wrappper properties
160+
!gradle-wrapper.properties
161+
162+
# Cache of project
163+
.gradletasknamecache
164+
165+
# Eclipse Gradle plugin generated files
166+
# Eclipse Core
167+
.project
168+
# JDT-specific (Eclipse Java Development Tools)
169+
.classpath
170+
171+
### Gradle Patch ###
172+
# Java heap dump
173+
*.hprof
174+
175+
# End of https://www.toptal.com/developers/gitignore/api/java,intellij,gradle

build.gradle

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
group = 'org.programmers.java'
6+
version = '1.0-SNAPSHOT'
7+
8+
repositories {
9+
mavenCentral()
10+
}
11+
12+
dependencies {
13+
testImplementation platform('org.junit:junit-bom:5.9.1')
14+
testImplementation 'org.junit.jupiter:junit-jupiter'
15+
}
16+
17+
test {
18+
useJUnitPlatform()
19+
}

settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rootProject.name = 'JavaCalculator'
2+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.programmers.java;
2+
3+
import org.programmers.java.calculation.Calculation;
4+
import org.programmers.java.repository.FormulaRepository;
5+
import org.programmers.java.repository.FormulaMemoryRepository;
6+
7+
public class App {
8+
public static void main(String[] args){
9+
Calculation calculation = new Calculation();
10+
FormulaRepository formulaRepository = new FormulaMemoryRepository();
11+
Calculator calculator = new Calculator(calculation, formulaRepository);
12+
calculator.run();
13+
}
14+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.programmers.java;
2+
3+
import org.programmers.java.calculation.Calculation;
4+
import org.programmers.java.console.Console;
5+
import org.programmers.java.console.Input;
6+
import org.programmers.java.console.Output;
7+
import org.programmers.java.message.Error;
8+
import org.programmers.java.message.FunctionSelect;
9+
import org.programmers.java.repository.FormulaRepository;
10+
import org.programmers.java.validator.Validator;
11+
12+
public class Calculator {
13+
private boolean programStatus = true;
14+
private final Input input;
15+
private final Output output;
16+
private final Calculation calculation;
17+
private final FormulaRepository formulaRepository;
18+
19+
public Calculator(Calculation calculation, FormulaRepository formulaRepository){
20+
this.input = Console.getInstance();
21+
this.output = Console.getInstance();
22+
this.calculation = calculation;
23+
this.formulaRepository = formulaRepository;
24+
}
25+
26+
void run() {
27+
while (programStatus) {
28+
output.menuMsg();
29+
String selectNum = input.selectNumInput();
30+
output.selectMsg(selectNum);
31+
FunctionSelect selectMenu = getFunctionSelectNumber(selectNum);
32+
switch (selectMenu) {
33+
case CHECK:
34+
output.getCalculationValues(formulaRepository.getFormulaList());
35+
break;
36+
case CALCULATION:
37+
formulaCalculate();
38+
break;
39+
case EXIT:
40+
output.exitMsg();
41+
programStatus = false;
42+
break;
43+
case WRONGINPUT:
44+
output.errorMsg(Error.SELECT_VALIDATION.getMsg());
45+
break;
46+
}
47+
}
48+
}
49+
50+
private FunctionSelect getFunctionSelectNumber(String selectNum) {
51+
return FunctionSelect.findSelect(selectNum).orElse(FunctionSelect.WRONGINPUT);
52+
}
53+
54+
private void formulaCalculate() {
55+
String inputFormula = input.formulaInput();
56+
Validator.formulaValidate(inputFormula);
57+
58+
String result = calculation.requestCalculate(inputFormula);
59+
output.calculationValue(result);
60+
61+
String formulaAndResult = formattingFormula(inputFormula, result);
62+
formulaRepository.save(formulaAndResult);
63+
}
64+
65+
private String formattingFormula(String inputFormula, String result){
66+
StringBuilder stringBuilder = new StringBuilder();
67+
return stringBuilder
68+
.append(inputFormula)
69+
.append("=")
70+
.append(result)
71+
.toString();
72+
}
73+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.programmers.java.calculation;
2+
3+
import java.util.*;
4+
5+
public class Calculation {
6+
private final InfixToPostfixConverter infixToPostfixConverter;
7+
private final PostfixCalculation postfixCalculation;
8+
9+
public Calculation() {
10+
this.infixToPostfixConverter = new InfixToPostfixConverter();
11+
this.postfixCalculation = new PostfixCalculation();
12+
}
13+
14+
public String requestCalculate(String inputFormula){
15+
String[] formulaList = inputFormula.split(" ");
16+
List<String> postfixOperator = infixToPostfixConverter.convert(formulaList);
17+
String result = postfixCalculation.calculate(postfixOperator);
18+
return result;
19+
}
20+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.programmers.java.calculation;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.ArrayList;
5+
import java.util.Deque;
6+
import java.util.List;
7+
8+
public class InfixToPostfixConverter {
9+
List<String> postfixOperator = new ArrayList<>();
10+
Deque<String> operatorDeque = new ArrayDeque<>();
11+
12+
public List<String> convert(String[] formulaList){
13+
for(String operatorOrOperand : formulaList){
14+
operandAddInPostfix(operatorOrOperand);
15+
checkOperatorToAddPostfix(operatorOrOperand);
16+
}
17+
18+
while(operatorDeque.size() > 0){
19+
postfixOperator.add(operatorDeque.removeLast());
20+
}
21+
22+
return postfixOperator;
23+
}
24+
25+
private void operandAddInPostfix(String operatorOrOperand){
26+
if(Operator.isNumber(operatorOrOperand)){
27+
postfixOperator.add(operatorOrOperand);
28+
}
29+
}
30+
31+
private void checkOperatorToAddPostfix(String operatorOrOperand) {
32+
if (Operator.findSymbol(operatorOrOperand).isPresent()) {
33+
checkOperatorStackAndCompare(operatorOrOperand);
34+
}
35+
}
36+
37+
private void checkOperatorStackAndCompare(String operatorOrOperand){
38+
while (!operatorDeque.isEmpty() && comparePriorities(operatorOrOperand)) {
39+
postfixOperator.add(operatorDeque.removeLast());
40+
}
41+
42+
operatorDeque.add(operatorOrOperand);
43+
}
44+
45+
private boolean comparePriorities(String operatorOrOperand){
46+
return Operator.comparePriorities(operatorDeque.peekLast(), operatorOrOperand);
47+
}
48+
}

0 commit comments

Comments
 (0)