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

Lotto step4 #23

Open
wants to merge 5 commits into
base: pasudo123
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 7 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ dependencies {
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.4'

// https://mvnrepository.com/artifact/com.sparkjava/spark-core
compile group: 'com.sparkjava', name: 'spark-core', version: '2.8.0'

// https://mvnrepository.com/artifact/com.sparkjava/spark-template-handlebars
compile group: 'com.sparkjava', name: 'spark-template-handlebars', version: '2.7.1'

testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}

test {
useJUnitPlatform()
}
}
80 changes: 80 additions & 0 deletions src/main/java/launcher/web/WebMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package launcher.web;

import launcher.web.config.WebConfiguration;
import launcher.web.service.LottoMatchService;
import lotto.Money;
import lotto.Won;
import lotto.dto.LottoDto;
import lotto.model.BuyingInfo;
import lotto.model.BuyingPocket;
import lotto.model.Lottos;
import lotto.model.ManualLottoPapers;
import spark.ModelAndView;
import spark.Session;
import spark.template.handlebars.HandlebarsTemplateEngine;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static spark.Spark.get;
import static spark.Spark.post;


public class WebMain {

private static WebConfiguration webConfiguration;
private static LottoMatchService lottoMatchService;

private static final String MONEY = "money";
private static final String LOTTOS = "lottos";

static {
webConfiguration = new WebConfiguration();
lottoMatchService = new LottoMatchService();
}
Comment on lines +32 to +35
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

별건 아닌데 이부분 그냥 처음에 선언할 때

private static WebConfiguration webConfiguration = new WebConfiguration();
private static LottoMatchService lottoMatchService = new LottoMatchService();

이렇게 안하시고 나중에 따로 초기화 하신 이유가 있나요??


public static void main(String[]args){

get("/", (request, response) -> {
return render(new HashMap<>(), "index.html");
});


post("/buyLotto", (request, response) -> {

final Money money = Won.from(Integer.parseInt(request.queryParamOrDefault("inputMoney", "0")));
final String[] manualNumbers = request.queryParams("manualNumber").split("\r\n");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

매직 문자열 상수로 빼보아요! 저도 안빼긴 했던거 같기도 하네요...ㅎㅎ 😅

final ManualLottoPapers manualLottoPapers = ManualLottoPapers.from(Arrays.asList(manualNumbers));
final BuyingInfo buyingInfo = BuyingInfo.of(money, manualLottoPapers.getManualLottoPapers().size());
final BuyingPocket pocket = BuyingPocket.of(buyingInfo, manualLottoPapers);

final Lottos lottos = Lottos.create(pocket);
final LottoDto lottoDto = new LottoDto(lottos, pocket);

request.session().attribute(MONEY, money);
request.session().attribute(LOTTOS, lottos);

Map<String, Object> model = new HashMap<>();
model.put("lottos", lottoDto);
model.put("lottoList", lottoDto.getMyLottos());

return render(model, "show.html");
});

post("/matchLotto", (request, response) -> {

Session session = request.session(true);
final Money money = session.attribute(MONEY);
final Lottos lottos = session.attribute(LOTTOS);
final String winningNumber = request.queryParams("winningNumber");
final Integer bonusNumber = Integer.parseInt(request.queryParams("bonusNumber"));

return render(lottoMatchService.getMyWinningLottos(money, lottos, winningNumber, bonusNumber), "result.html");
});
}

static String render(Map<String, Object> model, String templatePath){
return new HandlebarsTemplateEngine().render(new ModelAndView(model, templatePath));
}
}
10 changes: 10 additions & 0 deletions src/main/java/launcher/web/config/WebConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package launcher.web.config;

import static spark.Spark.port;

public class WebConfiguration {

static {
port(8080);
}
}
Comment on lines +5 to +10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

웹 관련 설정 파일을 빼니까 한곳에서 관리할 수 있어서 더 좋겠네요 👍

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋은 방법이네요 배워갑니다!@

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 이렇게도 깔끔하네요!

34 changes: 34 additions & 0 deletions src/main/java/launcher/web/dto/WebWinningLottoDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package launcher.web.dto;

import lotto.type.Rank;

public class WebWinningLottoDto {

private boolean bonus;
private int matchCount;
private int money;
private long count;

public WebWinningLottoDto(Rank rank, long count){
this.bonus = (rank.name().equalsIgnoreCase("SECOND"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요기는, rank.matchType.isMatchBous를 가져오면 좀더 유연한 대응이 될 수 있을것 같아요!

this.matchCount = rank.countOfMatch();
this.money = rank.winningMoney();
this.count = count;
}

public boolean getBonus(){
return this.bonus;
}

public int getMatchCount(){
return this.matchCount;
}

public int getMoney(){
return money;
}

public long getCount(){
return count;
}
}
41 changes: 41 additions & 0 deletions src/main/java/launcher/web/service/LottoMatchService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package launcher.web.service;

import launcher.web.dto.WebWinningLottoDto;
import lotto.Money;
import lotto.dto.WinningLottoDto;
import lotto.model.Lottos;
import lotto.model.RankResults;
import lotto.model.WinningLotto;
import lotto.service.LottoResultService;
import lotto.type.Rank;

import java.util.*;

public class LottoMatchService {

private final LottoResultService lottoResultService;

public LottoMatchService(){
lottoResultService = new LottoResultService();
}

public Map<String, Object> getMyWinningLottos(Money money, Lottos lottos, String winningLottos, Integer bonusNumber){
final WinningLotto winningLotto = new WinningLotto(winningLottos, bonusNumber);
final RankResults rankResults = winningLotto.getRankResults(lottos);

Map<String, Object> model = new HashMap<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

model을 사용하는위치(하단)에서 선언해도 좋을것 같아요!

WinningLottoDto winningLottoDto = lottoResultService.getWinningLottoByResults(money, rankResults);

List<WebWinningLottoDto> webWinningLottoDtos = new ArrayList<>();
Comment on lines +26 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final 을 여기서도 사용해도 될 것 같은데 제외하신 이유가 있나요? 👀


final Map<Rank, Long> ranks = winningLottoDto.getRanks();
for(Rank rank : ranks.keySet()){
webWinningLottoDtos.add(new WebWinningLottoDto(rank, ranks.get(rank)));
}

model.put("ranks", webWinningLottoDtos);
model.put("revenue", winningLottoDto.getRevenue());

return model;
}
}
6 changes: 3 additions & 3 deletions src/main/java/lotto/dto/WinningLottoDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

public class WinningLottoDto {

private double revenue;
private String revenue;
private Map<Rank, Long> ranks;

public WinningLottoDto(final double revenue, final Map<Rank, Long> ranks){
public WinningLottoDto(final String revenue, final Map<Rank, Long> ranks){
this.revenue = revenue;
this.ranks = ranks;
}

public double getRevenue(){
public String getRevenue(){
return revenue;
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/lotto/model/RankResults.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lotto.Money;
import lotto.type.Rank;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -21,8 +22,9 @@ private List<RankResult> getWinningResults(){
.filter(RankResult::isPrize)
.collect(Collectors.toList());
}
public double getRevenue(final Money money){
return ((double)totalMoneyBuyLottos(money) / (double)winningMoney()) * PERCENTS_OF_100;
public String getRevenue(final Money money){
double value = ((double)totalMoneyBuyLottos(money) / (double)winningMoney()) * PERCENTS_OF_100;
return BigDecimal.valueOf(value).toPlainString();
}

public long countOfRank(final Rank rank){
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/lotto/service/LottoResultService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class LottoResultService {

public WinningLottoDto getWinningLottoByResults(final Money money, final RankResults rankResults){
final double revenue = rankResults.getRevenue(money);
final String revenue = rankResults.getRevenue(money);
return new WinningLottoDto(revenue, Rank.toCountOfRank(rankResults));
}
}
8 changes: 4 additions & 4 deletions src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,19 @@ <h2 class="text-center">로또 게임</h2>
<div class="form-show-div" class="form-group">
<h2>수동 구매 번호</h2>
예) 1,2,3,4,5,6(한 라인에 6개를 입력해주세요.)
<textarea class="form-control" rows="3" name="manualNumber"
<textarea class="form-control" rows="3" name="manualNumber"
placeholder="1,2,3,4,5,6
1,2,3,4,5,6"></textarea>
</div>
</div>
<div class="submit-button">
<button class="btn btn-lg btn-primary btn-block" type="submit">로또
구매</button>
</div>
</form>
</form>
</div>
<div class="col-md-4"></div>
</div>
</div>

</body>
</html>
</html>
23 changes: 7 additions & 16 deletions src/main/resources/templates/result.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
font-size: 16px;
}
</style>

</head>
<body>

Expand All @@ -72,26 +71,18 @@
<th><h3 class="text-center">당첨 통계</h3></th>
</thead>
<tbody>
{{#each ranks}}
<tr>
<th>3개 일치 (5000원)- 3개</th>
</tr>
<tr>
<th>4개 일치 (50000원)- 1개</th>
</tr>
<tr>
<th>5개 일치 (1500000원)- 0개</th>
</tr>
<tr>
<th>5개 일치, 보너스 볼 일치(30000000원)- 0개</th>
</tr>
<tr>
<th>6개 일치 (2000000000원)- 0개</th>
<th>
{{matchCount}} 개 일치{{#if bonus}},보너스 볼 일치{{/if}} ({{money}}원) - {{count}}개
</th>
Comment on lines +76 to +78
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if 를 사용하는 법이 있었네요 ㅎㅎ 👍

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아.. 전 보너스볼일치 텍스트넣는걸 깜빡했네요 저도 if 사용해서 변경해봐야겠네요

</tr>
{{/each}}
</tbody>
<tfoot>
<th><h4 class="text-center">총 수익률은 20%입니다.</h4></th>
<th><h4 class="text-center">총 수익률은 {{revenue}}%입니다.</h4></th>
</tfoot>
</table>
</pre>
</body>
</html>
</html>
32 changes: 9 additions & 23 deletions src/main/resources/templates/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,47 +66,33 @@
<div class="form-show-div form-group">
<label for="winningNumber">지난 주 당첨 번호</label>
<input type="text" id="inputMoney" class="form-control"
name="winningNumber" placeholder="1,2,3,4,5,6" required autofocus>
name="winningNumber" placeholder="1,2,3,4,5,6" required autofocus>
</div>
<div class="form-show-div form-group">
<label for="bonusNumber">2등 보너스 볼</label>
<input type="text" class="form-control" id="bonusNumber"
name="bonusNumber" required autofocus>
<input type="text" class="form-control" id="bonusNumber"
name="bonusNumber" required autofocus>
</div>
<div class="submit-button">
<button class="btn btn-lg btn-primary btn-block" type="submit">당첨 번호</button>
</div>
</form>
<pre class="pre-scrollable">
<h4 class="text-center"><5개를 구매 하셨습니다></h4>
<h4 class="text-center">수동으로 {{lottos.manualCount}}장, 자동으로 {{lottos.randomCount}}장 구매</h4>
<table class="table">
<thead>
<th></th>
<th class="text-center">로또 번호</th>
</thead>
<tbody>
{{#each lottoList}}
<tr>
<th>1</th>
<td>[8, 21, 23, 41, 42, 43]</td>
</tr>
<tr>
<th>2</th>
<td>[3, 5, 11, 16, 32, 38]</td>
</tr>
<tr>
<th>3</th>
<td>[7, 11, 16, 35, 36, 44]</td>
</tr>
<tr>
<th>4</th>
<td>[1, 8, 11, 31, 41, 42]</td>
</tr>
<tr>
<th>5</th>
<td>[13, 14, 16, 38, 42, 45]</td>
<th>{{@index_1}}</th>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{{@index}}는 알겠는데, {{@index_1}} 은 어떻게 출력되는 건가요??

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nokchax
http://mozmonkey.com/2014/03/ember-getting-the-index-in-each-loops/

handlebars에서 기본으로 지원하는 기능인듯합니다! 저도 처음 보네요... 헬퍼 안만들어도 됐을 걸 그랬네요...ㅠㅠ

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 .... 이런방법이

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 이런 방법도 있군요!

<td>{{this}}</td>
</tr>
{{/each}}
</tbody>
</table>
</pre>
</body>
</html>
</html>