Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1efa085
Add Calculator and StringReader with TDD
Woomin-Jeon Jun 16, 2020
8bfa08f
Add view and make getUserInput()
Woomin-Jeon Jun 16, 2020
a28e919
Make StringCalculationApp.java
Woomin-Jeon Jun 16, 2020
0811c66
Seperate view logic from controller
Woomin-Jeon Jun 16, 2020
5d9321c
Add custom separator logic
Woomin-Jeon Jun 16, 2020
145a4c4
Do refactoring to StringReader
Woomin-Jeon Jun 16, 2020
e7b9cf7
Modify Constructor to FactoryMethod
Woomin-Jeon Jun 17, 2020
6f42552
Do refactoring to test code. given-when-then
Woomin-Jeon Jun 17, 2020
02db90c
Add getLottoNumbers() in LottoTicket by TDD
Woomin-Jeon Jun 19, 2020
0f4a39a
Add makeLottoTicket() in LottoMachine by TDD
Woomin-Jeon Jun 19, 2020
5394c14
Add test case to makeLottoTicket()
Woomin-Jeon Jun 19, 2020
ce252d0
Add makeTicketsWithMoney() in LottoMachin by TDD
Woomin-Jeon Jun 19, 2020
4351be9
Change the method name short
Woomin-Jeon Jun 19, 2020
ec3449f
Add getNumbers() in RandomGenerator by TDD
Woomin-Jeon Jun 19, 2020
8ac2e1e
Fix makeTicketsWithMoney() => inject randomNumbers
Woomin-Jeon Jun 19, 2020
a52d764
Add getUserInputMoney() in Input
Woomin-Jeon Jun 19, 2020
58d6ae1
Add showLottoTickets() in Output
Woomin-Jeon Jun 19, 2020
8e0bae1
Add getWinningNumbers() in Input
Woomin-Jeon Jun 19, 2020
d1a06e9
Add converToLottoNumber() in LottoChecker by TDD
Woomin-Jeon Jun 19, 2020
94c6b1b
Modify convertToLottoNumbers() to constructor
Woomin-Jeon Jun 19, 2020
6ad8d9b
Add checkTicket() in LottoChecker by TDD
Woomin-Jeon Jun 19, 2020
fc8691e
Add checkAllTickets() in LottoChecker by TDD
Woomin-Jeon Jun 19, 2020
bd9d719
Add result view
Woomin-Jeon Jun 19, 2020
5f818e2
Add getting totalWinning logic to checkAllTickets
Woomin-Jeon Jun 19, 2020
0d0cbf7
End
Woomin-Jeon Jun 19, 2020
a1f483f
remove unused package in LottoChecker Class
Woomin-Jeon Jun 22, 2020
50cc570
Extract Map<>checkCounter to FirstClassCollection
Woomin-Jeon Jun 22, 2020
4eed6d9
Fix lints
Woomin-Jeon Jun 22, 2020
5a6317c
Modify method name getCount() to plusCount()
Woomin-Jeon Jun 22, 2020
4d0d33d
Rename the methodes in LottoChecker class
Woomin-Jeon Jun 22, 2020
22c93f8
Add enum ShowWinner in Output class
Woomin-Jeon Jun 22, 2020
7c19908
Add bonusBall logic to LottoChecker class
Woomin-Jeon Jun 22, 2020
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
21 changes: 21 additions & 0 deletions src/main/java/calculator/controller/StringCalculatorApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package calculator.controller;

import calculator.model.Calculator;
import calculator.model.StringReader;
import calculator.view.Input;
import calculator.view.Result;

public class StringCalculatorApp {
public static void main(String[] args) {
Result.showStartMessage();
String userInput = Input.getUserInput();

System.out.println(userInput);

StringReader stringReader = new StringReader();
int[] arr = stringReader.read(userInput);

Calculator calculator = new Calculator(arr);
Result.showResult(calculator.executeAddition());
}
}
24 changes: 24 additions & 0 deletions src/main/java/calculator/model/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package calculator.model;

import java.util.Arrays;

public class Calculator {
private final int result;

public Calculator(int[] numbers) {
int minusCount = Arrays.stream(numbers).filter(v -> v < 0).toArray().length;
if (minusCount > 0) {
throw new RuntimeException();
}

this.result = Arrays.stream(numbers).reduce(Integer::sum).orElse(0);
}

public static Calculator newCalculator(int[] numbers) {
return new Calculator(numbers);
}

public int executeAddition() {
return this.result;
}
}
27 changes: 27 additions & 0 deletions src/main/java/calculator/model/StringReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package calculator.model;

import java.util.Arrays;

public class StringReader {
public int[] read(String str) {
String[] arr = str.split("n");

return arr.length == 1
? withNoCustomSeparator(arr[0])
: withCustomSeparator(arr[0], arr[1]);
}

private int[] withNoCustomSeparator(String str) {
String[] numbers = str.split("[:,]");

return Arrays.stream(numbers).mapToInt(Integer::parseInt).toArray();
}

private int[] withCustomSeparator(String separatorPart, String numberPart) {
String customSeparator = Character.toString(separatorPart.charAt(2));
String regex = "[:," + customSeparator + "]";
String[] numbers = numberPart.split(regex);

return Arrays.stream(numbers).mapToInt(Integer::parseInt).toArray();
}
}
11 changes: 11 additions & 0 deletions src/main/java/calculator/view/Input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package calculator.view;

import java.util.Scanner;

public class Input {

public static String getUserInput() {
Scanner scanner = new Scanner(System.in);
return scanner.nextLine();
}
}
12 changes: 12 additions & 0 deletions src/main/java/calculator/view/Result.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package calculator.view;

public class Result {

public static void showStartMessage() {
System.out.println("숫자들을 입력해주세요");
}

public static void showResult(int result) {
System.out.printf("합계: %d", result);
}
}
Empty file removed src/main/java/empty.txt
Empty file.
26 changes: 26 additions & 0 deletions src/main/java/lotto/controller/LottoApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package lotto.controller;

import lotto.domain.CheckCounter;
import lotto.domain.LottoChecker;
import lotto.domain.LottoMachine;
import lotto.view.Input;
import lotto.view.Output;

public class LottoApplication {
public static void main(String[] args) {
int money = Input.getUserInputMoney();

LottoMachine lottoMachine = LottoMachine.newMachine();
lottoMachine.makeTicketsWithMoney(money);

Output.showLottoTickets(lottoMachine);

String winningNumbers = Input.getWinningNumbers();
int bonusBallNumber = Input.getBonusBallNumber();

LottoChecker lottoChecker = LottoChecker.newChecker(winningNumbers, bonusBallNumber);
CheckCounter counter = lottoChecker.checkAllTickets(lottoMachine);

Output.showResult(counter, money);
}
}
36 changes: 36 additions & 0 deletions src/main/java/lotto/domain/CheckCounter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package lotto.domain;

import java.util.Map;
import java.util.TreeMap;

public class CheckCounter {
private final Map<Integer, Integer> checkCounter;

public CheckCounter() {
this.checkCounter = new TreeMap<>();
}

public void setInitial(int key) {
this.checkCounter.put(key, 1);
}

public int get(int key) {
return this.checkCounter.getOrDefault(key, 0);
}

public boolean has(int key) {
return this.checkCounter.containsKey(key);
}

public void countUp(int key) {
int previousValue = this.checkCounter.get(key);
this.checkCounter.put(key, previousValue + 1);
}

public int getTotalWinningMoney() {
return (5_000 * this.checkCounter.getOrDefault(3, 0))
+ (50_000 * this.checkCounter.getOrDefault(4, 0))
+ (1_500_000 * this.checkCounter.getOrDefault(5, 0))
+ (2_000_000_000 * this.checkCounter.getOrDefault(6, 0));
}
}
83 changes: 83 additions & 0 deletions src/main/java/lotto/domain/LottoChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package lotto.domain;

import java.util.Arrays;
import java.util.List;

public class LottoChecker {

private final CheckCounter checkCounter = new CheckCounter();
private final List<Integer> winningNumbers;
private final int bonusBall;

public LottoChecker(String winnings, int bonusBall) {
this.winningNumbers = convertToList(winnings);
this.bonusBall = bonusBall;
}

public static LottoChecker newChecker(String winnings, int bonusBall) {
return new LottoChecker(winnings, bonusBall);
}

public int countMatchedNumber(LottoTicket ticket) {
List<Integer> ticketNumbers = ticket.getLottoNumbers();

int count = 0;
for (Integer number : this.winningNumbers) {
count = plusCount(ticketNumbers, count, number);
}

return count;
}

private int plusCount(List<Integer> ticketNumbers, int count, Integer number) {
if (ticketNumbers.contains(number)) {
count += 1;
}

return count;
}

public CheckCounter checkAllTickets(LottoMachine lottoMachine) {
List<LottoTicket> tickets = lottoMachine.getTickets();
for (LottoTicket ticket : tickets) {
int matchCount = this.countMatchedNumber(ticket);
boolean bonusBallMatch = isContainingBonusBall(ticket);

executeCheckCounter(matchCount, bonusBallMatch);
}

return this.checkCounter;
}

private void executeCheckCounter(int matchCount, boolean bonusBallMatch) {
if (matchCount == 5 && bonusBallMatch) {
int secondWinner = 7;
addToCheckCounter(secondWinner);
return;
}

addToCheckCounter(matchCount);
}

private void addToCheckCounter(int matchCount) {
if (this.checkCounter.has(matchCount)) {
this.checkCounter.countUp(matchCount);
return;
}

this.checkCounter.setInitial(matchCount);
}

private List<Integer> convertToList(String string) {
return Arrays.asList(
Arrays.stream(string.split(","))
.mapToInt(Integer::parseInt)
.boxed()
.toArray(Integer[]::new)
);
}

private boolean isContainingBonusBall(LottoTicket ticket) {
return ticket.getLottoNumbers().contains(this.bonusBall);
}
}
31 changes: 31 additions & 0 deletions src/main/java/lotto/domain/LottoMachine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package lotto.domain;

import java.util.ArrayList;
import java.util.List;

public class LottoMachine {

private final List<LottoTicket> lottoTickets = new ArrayList<>();

public LottoMachine() {}

public static LottoMachine newMachine() {
return new LottoMachine();
}

public List<LottoTicket> getTickets() {
return this.lottoTickets;
}

public void makeTicket(List<Integer> numbers) {
LottoTicket newTicket = LottoTicket.newLotto(numbers);
this.lottoTickets.add(newTicket);
}

public void makeTicketsWithMoney(int money) {
for (int i = 0; i < money / 1000; i += 1) {
List<Integer> randomNumbers = RandomGenerator.newGenerator().getNumbers();
this.makeTicket(randomNumbers);
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/lotto/domain/LottoTicket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package lotto.domain;

import java.util.List;

public class LottoTicket {

private final List<Integer> lottoNumbers;

public LottoTicket(List<Integer> numbers) {
this.lottoNumbers = numbers;
}

public static LottoTicket newLotto(List<Integer> numbers) {
return new LottoTicket(numbers);
}

public List<Integer> getLottoNumbers() {
return this.lottoNumbers;
}
}
29 changes: 29 additions & 0 deletions src/main/java/lotto/domain/RandomGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package lotto.domain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RandomGenerator {

private final List<Integer> numbers;

public RandomGenerator() {
this.numbers = new ArrayList<>();
for (int i = 1; i <= 45; i += 1) {
numbers.add(i);
}
}

public static RandomGenerator newGenerator() {
return new RandomGenerator();
}

public List<Integer> getNumbers() {
Collections.shuffle(numbers);
List<Integer> sevenNumbers = numbers.subList(0, 7);
Collections.sort(sevenNumbers);

return sevenNumbers;
}
}
29 changes: 29 additions & 0 deletions src/main/java/lotto/view/Input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package lotto.view;

import java.util.Scanner;

public class Input {
public static int getUserInputMoney() {
System.out.println("로또 구입 금액을 입력해주세요.");
Scanner scanner = new Scanner(System.in);

int money = scanner.nextInt();
System.out.printf("%d개를 구매하셨습니다.\n", money / 1000);

return money;
}

public static String getWinningNumbers() {
System.out.println("당첨 번호를 입력해주세요.");
Scanner scanner = new Scanner(System.in);

return scanner.nextLine();
}

public static int getBonusBallNumber() {
System.out.println("보너스 볼을 입력해주세요.");
Scanner scanner = new Scanner(System.in);

return scanner.nextInt();
}
}
Loading