-
Notifications
You must be signed in to change notification settings - Fork 151
[4기 - 김재웅] 1~2주차 과제 : 계산기 구현 미션 제출합니다. #147
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
base: kju2405
Are you sure you want to change the base?
Changes from 16 commits
1c5c20b
31d64fe
d42a6d9
b36e927
c0c1a07
14789d6
ef5eeca
1bd816a
2b75683
8e7fc67
2324418
71917d0
15ffeda
50b581d
d31597e
057119f
8e49f93
1429b49
0f7a612
c22e1c6
4f44f1b
f8ef218
63e778c
8f97f36
eee0518
cb39427
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,27 @@ | ||
| package org.example; | ||
|
|
||
| import org.example.Calculate.*; | ||
| import org.example.Input.Input; | ||
| import org.example.Input.UserInput; | ||
| import org.example.Output.Show; | ||
| import org.example.Output.ShowText; | ||
| import org.example.Repository.ExpressionRepository; | ||
| import org.example.Repository.Repository; | ||
|
|
||
| public class CalConfig { | ||
| public Calculate calculate(){ | ||
| return new CalculateImpl(new CalOrderImpl()); | ||
| } | ||
| public Repository repository(){ | ||
| return new ExpressionRepository(); | ||
| } | ||
| public Input input(){ | ||
| return new UserInput(); | ||
| } | ||
| public Show show(){ | ||
| return new ShowText(); | ||
| } | ||
| public PreProcess preProcess() { | ||
| return new PreProcessImpl(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,9 @@ | ||||||||||
| package org.example.Calculate; | ||||||||||
|
|
||||||||||
| import java.util.Stack; | ||||||||||
|
|
||||||||||
| public interface CalOrder { | ||||||||||
| void calMultiplyDivide(); | ||||||||||
| int calPlusMinus(); | ||||||||||
|
||||||||||
| void calMultiplyDivide(); | |
| int calPlusMinus(); | |
| void calculateMultiplyDivide(); | |
| int calculatePlusMinus(); |
메소드 이름이 길어지더라도 명시적으로 작성해주세요!
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package org.example.Calculate; | ||
|
|
||
| import java.util.Stack; | ||
|
|
||
| public class CalOrderImpl implements CalOrder { | ||
| private int result = 0; | ||
| Stack<String> expressionStack = new Stack<>(); | ||
|
|
||
| @Override | ||
| public void calMultiplyDivide() { | ||
| for (int i = 1; i < expressionStack.size(); i += 2) { | ||
| if (expressionStack.get(i).equals("*")) { | ||
| multiply(i); | ||
| i -= 2; | ||
| } else if (expressionStack.get(i).equals("/")) { | ||
| divide(i); | ||
| i -= 2; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public int calPlusMinus() { | ||
|
||
| result = Integer.parseInt(expressionStack.get(0)); | ||
| for (int i = 1; i < expressionStack.size(); i += 2) { | ||
| if (expressionStack.get(i).equals("+")) { | ||
| result += Integer.parseInt(expressionStack.get(i + 1)); | ||
| } else { | ||
| result -= Integer.parseInt(expressionStack.get(i + 1)); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public void setStack(Stack<String> expressionStack) { | ||
| this.expressionStack = expressionStack; | ||
| } | ||
|
|
||
| public void multiply(int idx) { | ||
| result = Integer.parseInt(expressionStack.get(idx - 1)) * Integer.parseInt(expressionStack.get(idx + 1)); | ||
| expressionStack.add(idx - 1, String.valueOf(result)); | ||
| expressionStack.remove(idx); | ||
| expressionStack.remove(idx); | ||
| expressionStack.remove(idx); | ||
| } | ||
|
|
||
| public void divide(int idx) { | ||
| result = Integer.parseInt(expressionStack.get(idx - 1)) / Integer.parseInt(expressionStack.get(idx + 1)); | ||
| expressionStack.add(idx - 1, String.valueOf(result)); | ||
| expressionStack.remove(idx); | ||
| expressionStack.remove(idx); | ||
| expressionStack.remove(idx); | ||
| } | ||
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package org.example.Calculate; | ||
|
|
||
| import java.util.Stack; | ||
|
|
||
| public interface Calculate { | ||
| int calculate(Stack<String> expressionStack); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package org.example.Calculate; | ||
|
|
||
| import java.util.Stack; | ||
|
|
||
| public class CalculateImpl implements Calculate { | ||
| CalOrder calOrder; | ||
|
|
||
| public CalculateImpl(CalOrder calOrder) { | ||
| this.calOrder = calOrder; | ||
| } | ||
|
|
||
| public int calculate(Stack<String> expressionStack) { | ||
| calOrder.setStack(expressionStack); | ||
| calOrder.calMultiplyDivide(); | ||
| return calOrder.calPlusMinus(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package org.example.Calculate; | ||
|
|
||
| import java.util.Stack; | ||
|
|
||
| public interface PreProcess { | ||
| Stack<String> expressionToStack(String expression); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,18 @@ | ||||||||||||
| package org.example.Calculate; | ||||||||||||
|
|
||||||||||||
| import java.util.Stack; | ||||||||||||
|
|
||||||||||||
| public class PreProcessImpl implements PreProcess{ | ||||||||||||
| private String expression; | ||||||||||||
| Stack<String> expressionStack = new Stack<>(); | ||||||||||||
| @Override | ||||||||||||
|
Comment on lines
+7
to
+9
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.
Suggested change
|
||||||||||||
| public Stack<String> expressionToStack(String expression) { | ||||||||||||
| this.expression = expression; | ||||||||||||
| String[] expressionArr = expression.split(" "); | ||||||||||||
| expressionStack.clear(); | ||||||||||||
| for (String inputVal : expressionArr) { | ||||||||||||
| expressionStack.add(inputVal); | ||||||||||||
| } | ||||||||||||
| return expressionStack; | ||||||||||||
| } | ||||||||||||
|
Comment on lines
+9
to
+15
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. 너무 요구사항과 강하게 결합 되어 있는 것 같아서, 재사용성을 고려해보는 것도 좋을 거 같아요 :) |
||||||||||||
| } | ||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package org.example.Input; | ||
|
|
||
| public interface Input { | ||
| int inputChoice(); | ||
| String inputExpression(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package org.example.Input; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class UserInput implements Input { | ||
| Scanner scanner = new Scanner(System.in); | ||
| @Override | ||
| public int inputChoice() { | ||
| return scanner.nextInt(); | ||
|
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. inputChoice()라는 메서드명과 실제로 하고 있는 일이 조금 달라보여요. |
||
| } | ||
|
|
||
| @Override | ||
| public String inputExpression() { | ||
| scanner.nextLine(); | ||
| return scanner.nextLine(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package org.example; | ||
|
|
||
| import org.example.Calculate.Calculate; | ||
| import org.example.Calculate.PreProcess; | ||
| import org.example.Input.Input; | ||
| import org.example.Output.Show; | ||
| import org.example.Repository.Repository; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Stack; | ||
|
|
||
| public class Main { | ||
| private static int choice; | ||
| public static void main(String[] args) { | ||
| CalConfig calConfig = new CalConfig(); | ||
| Calculate calculator = calConfig.calculate(); | ||
| Repository repository = calConfig.repository(); | ||
| Input input = calConfig.input(); | ||
| Show show = calConfig.show(); | ||
| PreProcess preProcess = calConfig.preProcess(); | ||
|
|
||
| while (true) { | ||
| show.showMenu(); | ||
| choice = input.inputChoice(); | ||
| System.out.println(); | ||
|
||
| if (choice == 1) { | ||
|
||
| List<String> records = repository.getRecords(); | ||
| show.showRecords(records); | ||
| } else { | ||
| String expression = input.inputExpression(); | ||
| Stack<String> expressionStack = preProcess.expressionToStack(expression); | ||
| int result = calculator.calculate(expressionStack); | ||
| repository.save(expression,result); | ||
| show.showResult(result); | ||
| } | ||
| System.out.println(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package org.example.Output; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface Show { | ||
| void showMenu(); | ||
| void showRecords(List<String> records); | ||
| void showResult(int result); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package org.example.Output; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class ShowText implements Show{ | ||
|
||
| @Override | ||
| public void showMenu() { | ||
| System.out.println("1. 조회\n2. 계산\n"); | ||
| System.out.print("선택 : "); | ||
| } | ||
|
|
||
| @Override | ||
| public void showRecords(List<String> records) { | ||
| records.forEach(System.out::println); | ||
| } | ||
|
|
||
| @Override | ||
| public void showResult(int result) { | ||
| System.out.println(result); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package org.example.Repository; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class ExpressionRepository implements Repository { | ||
| private static List<String> store = new ArrayList<>(); | ||
|
|
||
| @Override | ||
| public void save(String expression, int result) { | ||
| store.add(expression + " = " + result); | ||
| } | ||
|
|
||
| @Override | ||
| public List<String> getRecords() { | ||
| return store; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package org.example.Repository; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface Repository { | ||
| void save(String expression, int result); | ||
| List<String> getRecords(); | ||
| } |
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.
Config를 통해 객체를 생성하면 어떤 장점이 있나요?