-
Notifications
You must be signed in to change notification settings - Fork 5
3주차 미션 구현(김종하) #1
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
whdgk0602
wants to merge
5
commits into
allrounder-backend:main
Choose a base branch
from
whdgk0602:whdgk0602
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,28 @@ | ||
| # All-rounder Backend Study | ||
| ** 기능 별 구현 완료 ** | ||
|
|
||
| Program Start | ||
|
|
||
| 프린터 실행 | ||
|
|
||
| 사용 가능 기능 표시 | ||
|
|
||
| User Input | ||
|
|
||
| 사용자가 기능 선택 (1~4) | ||
|
|
||
| Feature Execution | ||
|
|
||
| (1) 숫자 출력 | ||
| 출력할 숫자 입력 | ||
| 용지 크기 입력 | ||
| ASCII 변환 | ||
| 용지 크기 맞춰 출력 | ||
| 잉크 차감 | ||
| (2) 잉크 잔량 확인 | ||
| 현재 잉크 잔량 표시 | ||
| (3) 잉크 교체 | ||
| 잉크 1000으로 초기화 | ||
| (4) 프로그램 종료 | ||
|
|
||
| 기능별 MVC 패턴 적용 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,17 @@ | ||
| package mission; | ||
|
|
||
| import mission.model.Printer; | ||
| import mission.control.PrinterController; | ||
| import mission.utils.AsciiGenerator; | ||
| import mission.view.PrinterView; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| //Todo: 프로그램 구현 | ||
| AsciiGenerator asciiGenerator = new AsciiGenerator(); | ||
| Printer printer = new Printer(asciiGenerator); | ||
| PrinterView view = new PrinterView(); | ||
| PrinterController controller = new PrinterController(printer, view); | ||
|
|
||
| controller.start(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package mission.control; | ||
|
|
||
| import mission.model.Printer; | ||
| import mission.view.PrinterView; | ||
|
|
||
| public class PrinterController { | ||
| private final Printer printer; | ||
| private final PrinterView view; | ||
|
|
||
| public static final String PRINT_OPTION = "1"; | ||
| public static final String CHECK_INK_OPTION = "2"; | ||
| public static final String REPLACE_INK_OPTION = "3"; | ||
| public static final String EXIT_OPTION = "4"; | ||
|
|
||
| public PrinterController(Printer printer, PrinterView view) { | ||
| this.printer = printer; | ||
| this.view = view; | ||
| } | ||
|
|
||
| public void start() { | ||
| view.displayMessage("프린터를 실행합니다."); | ||
| while (true) { | ||
| String input = view.getUserInput("사용할 기능을 입력해주세요. 1) 출력, 2) 잉크 잔량 확인, 3) 잉크 교체, 4) 프로그램 종료"); | ||
|
|
||
| switch (input) { | ||
| case PRINT_OPTION -> handlePrint(); | ||
| case CHECK_INK_OPTION -> printer.checkInk(); | ||
| case REPLACE_INK_OPTION -> printer.replaceInk(); | ||
| case EXIT_OPTION -> { | ||
| view.displayMessage("프로그램을 종료합니다."); | ||
| return; | ||
| } | ||
| default -> view.displayMessage("올바른 번호를 입력해주세요."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void handlePrint() { | ||
| String input = view.getUserInput("출력할 문자를 입력해주세요."); | ||
| int paperSize = getPaperSize(); | ||
|
|
||
| try { | ||
| printer.print(input, paperSize); | ||
| } catch (IllegalStateException e) { | ||
| view.displayError(e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private int getPaperSize() { | ||
| while (true) { | ||
| String paperSizeInput = view.getUserInput("용지 크기를 입력해주세요."); | ||
| try { | ||
| return Integer.parseInt(paperSizeInput); | ||
| } catch (NumberFormatException e) { | ||
| view.displayMessage("숫자만 입력 가능합니다."); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package mission.model; | ||
|
|
||
| import mission.utils.AsciiGenerator; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| public class Printer { | ||
| private static final int MAX_INK = 1000; | ||
| private int ink = MAX_INK; | ||
| private final AsciiGenerator asciiGenerator; | ||
|
|
||
| public Printer(AsciiGenerator asciiGenerator) { | ||
| this.asciiGenerator = asciiGenerator; | ||
| } | ||
|
|
||
| public void print(String input, int paperSize) { | ||
| consumeInk(input.length(), paperSize); | ||
| String ascii = generateAscii(input); | ||
| System.out.println(ascii); | ||
| System.out.println("잉크 잔량: " + ink + "/" + MAX_INK); | ||
| } | ||
|
|
||
| public void checkInk() { | ||
| System.out.println("잉크 잔량: " + ink + "/" + MAX_INK); | ||
| } | ||
|
|
||
| public void replaceInk() { | ||
| ink = MAX_INK; | ||
| System.out.println("잉크를 교체하였습니다."); | ||
| } | ||
|
|
||
| private void consumeInk(int inputLength, int paperSize) { | ||
| int requiredInk = inputLength * paperSize * 5; | ||
| if (ink < requiredInk) { | ||
| throw new IllegalStateException("잉크가 부족하여 인쇄할 수 없습니다."); | ||
| } | ||
| ink -= requiredInk; | ||
| } | ||
|
|
||
| private String generateAscii(String input) { | ||
| int numRows = asciiGenerator.getNumberAsciiDesign(0).size(); | ||
|
|
||
| return IntStream.range(0, numRows) | ||
| .mapToObj(row -> input.chars() | ||
| .mapToObj(c -> { | ||
| int digit = Character.getNumericValue(c); | ||
| List<String> design = asciiGenerator.getNumberAsciiDesign(digit); | ||
| return design.get(row); | ||
| }) | ||
| .collect(Collectors.joining(" "))) | ||
| .collect(Collectors.joining(System.lineSeparator())); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package mission.view; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class PrinterView { | ||
| private final Scanner scanner = new Scanner(System.in); | ||
|
|
||
| public String getUserInput(String message) { | ||
| System.out.println(message); | ||
| return scanner.nextLine().trim(); | ||
| } | ||
|
|
||
| public void displayMessage(String message) { | ||
| System.out.println(message); | ||
| } | ||
|
|
||
| public void displayError(String message) { | ||
| System.out.println("[ERROR] " + message); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
예외처리도 고려하시면 좋을 것 같아요