Skip to content
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

Step4 lotto manual #7

Open
wants to merge 44 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 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
32c7105
Wrap int money to Money Class
Woomin-Jeon Jun 24, 2020
0b10061
Wrap int bonusBall to LottoBall Class
Woomin-Jeon Jun 24, 2020
906e4ce
Modify int ball to LottoBall Class
Woomin-Jeon Jun 24, 2020
fca1d9f
Modify factory method name
Woomin-Jeon Jun 24, 2020
a8997b1
Add Exception handler by using try-catch
Woomin-Jeon Jun 24, 2020
bd32c1b
Add makeManualLotto() in LottoMachine Class
Woomin-Jeon Jun 24, 2020
b735f82
Fix makeTicketsWithMoney() to makeTickets()
Woomin-Jeon Jun 24, 2020
dc90ae6
Make ManualLottoTicketWriter class to controller
Woomin-Jeon Jun 24, 2020
b536e07
Remove Money Class getter
Woomin-Jeon Jun 24, 2020
2286fef
Remove LottoBall Class getter
Woomin-Jeon Jun 24, 2020
43a5c28
Remove LottoTicket Class getter
Woomin-Jeon Jun 24, 2020
f78e059
Override toString() to LottoBall and LottoTicket
Woomin-Jeon Jun 24, 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.
42 changes: 42 additions & 0 deletions src/main/java/lotto/controller/LottoApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package lotto.controller;

import lotto.domain.*;
import lotto.view.Input;
import lotto.view.Output;

import java.util.List;

public class LottoApplication {
public static void main(String[] args) {
Money money;
try {
money = Money.newMoney(Input.getUserInputMoney());
} catch (IllegalArgumentException e) {
Output.showErrorMessage(e);
money = Money.newMoney(1000);
}

ManualLottoTicketWriter manualWriter = ManualLottoTicketWriter.newWriter();
List<LottoTicket> manualTickets = manualWriter.write(Input.getUserInputManualCount());

LottoMachine lottoMachine = LottoMachine.newMachine();
lottoMachine.makeTickets(money, manualTickets);

Output.showLottoTickets(lottoMachine);

String winningNumbers = Input.getWinningNumbers();

LottoBall lottoBallNumber;
try {
lottoBallNumber = LottoBall.newBall(Input.getBonusBallNumber());
} catch (IllegalArgumentException e) {
Output.showErrorMessage(e);
lottoBallNumber = LottoBall.newBall(45);
}

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

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

import lotto.domain.LottoBall;
import lotto.domain.LottoTicket;
import lotto.view.Input;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ManualLottoTicketWriter {

public ManualLottoTicketWriter() {}

public static ManualLottoTicketWriter newWriter() {
return new ManualLottoTicketWriter();
}

public List<LottoTicket> write(int manualLottoCount) {
ArrayList<LottoTicket> manualTickets = new ArrayList<>();

for (int i = 0; i < manualLottoCount; i += 1) {
String numberString = Input.getManualLottoNumber();
LottoTicket ticket = convertToLottoTickets(numberString);

manualTickets.add(ticket);
}

return manualTickets;
}

private LottoTicket convertToLottoTickets(String numberString) {
int[] numbers = Arrays.stream(numberString.split(",")).mapToInt(Integer::parseInt).toArray();
List<LottoBall> lottoNumbers = Arrays.stream(numbers).mapToObj(LottoBall::newBall).collect(Collectors.toList());

return LottoTicket.newTicket(lottoNumbers);
}
}
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));
}
}
48 changes: 48 additions & 0 deletions src/main/java/lotto/domain/LottoBall.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package lotto.domain;

import java.util.List;

public class LottoBall implements Comparable{

private final int ballNumber;

public LottoBall(int number) {
this.validate(number);
this.ballNumber = number;
}

public static LottoBall newBall(int number) {
return new LottoBall(number);
}

private void validate(int number) {
if (number < 1 || number > 45) {
throw new IllegalArgumentException("보너스 볼로 가능한 숫자는 1 ~ 45 입니다.");
}
}

@Override
public int compareTo(Object obj) {
LottoBall next = (LottoBall) obj;
return this.ballNumber - next.ballNumber;
}

@Override
public boolean equals(Object obj) {
LottoBall next = (LottoBall) obj;
return this.ballNumber == next.ballNumber;
}

public boolean isElementOf(List<Integer> numbers) {
return numbers.contains(this.ballNumber);
}

public boolean isSameWith(int number) {
return number == this.ballNumber;
}

@Override
public String toString() {
return this.ballNumber + "";
}
}
64 changes: 64 additions & 0 deletions src/main/java/lotto/domain/LottoChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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 LottoBall bonusBall;

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

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

public CheckCounter checkAllTickets(LottoMachine lottoMachine) {
List<LottoTicket> tickets = lottoMachine.getTickets();
for (LottoTicket ticket : tickets) {
int matchCount = ticket.countMatchedNumber(this.winningNumbers);
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.isContaining(this.bonusBall);
}
}
43 changes: 43 additions & 0 deletions src/main/java/lotto/domain/LottoMachine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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<LottoBall> numbers) {
LottoTicket newTicket = LottoTicket.newTicket(numbers);
this.lottoTickets.add(newTicket);
}

public void makeTickets(Money money, List<LottoTicket> manualTickets) {
int manualLottoCount = manualTickets.size();
int autoLottoCount = money.getAutoLottoCount(manualLottoCount);

makeAutoLottos(autoLottoCount);
makeManualLottos(manualTickets);
}

private void makeAutoLottos(int autoLottoCount) {
for (int i = 0; i < autoLottoCount; i += 1) {
List<LottoBall> randomNumbers = RandomGenerator.newGenerator().getNumbers();
this.makeTicket(randomNumbers);
}
}

private void makeManualLottos(List<LottoTicket> manualTickets) {
this.lottoTickets.addAll(manualTickets);
}
}
Loading