-
Notifications
You must be signed in to change notification settings - Fork 5
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
base: pasudo123
Are you sure you want to change the base?
Lotto step4 #23
Changes from all commits
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,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(); | ||
} | ||
|
||
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"); | ||
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. 매직 문자열 상수로 빼보아요! 저도 안빼긴 했던거 같기도 하네요...ㅎㅎ 😅 |
||
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)); | ||
} | ||
} |
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
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. 웹 관련 설정 파일을 빼니까 한곳에서 관리할 수 있어서 더 좋겠네요 👍 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. 좋은 방법이네요 배워갑니다!@ 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,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")); | ||
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. 요기는, |
||
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; | ||
} | ||
} |
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<>(); | ||
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.
|
||
WinningLottoDto winningLottoDto = lottoResultService.getWinningLottoByResults(money, rankResults); | ||
|
||
List<WebWinningLottoDto> webWinningLottoDtos = new ArrayList<>(); | ||
Comment on lines
+26
to
+29
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. 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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,7 +56,6 @@ | |
font-size: 16px; | ||
} | ||
</style> | ||
|
||
</head> | ||
<body> | ||
|
||
|
@@ -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
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. if 를 사용하는 법이 있었네요 ㅎㅎ 👍 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. 아.. 전 보너스볼일치 텍스트넣는걸 깜빡했네요 저도 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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
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. {{@index}}는 알겠는데, {{@index_1}} 은 어떻게 출력되는 건가요?? 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. @nokchax handlebars에서 기본으로 지원하는 기능인듯합니다! 저도 처음 보네요... 헬퍼 안만들어도 됐을 걸 그랬네요...ㅠㅠ 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. 오 .... 이런방법이 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. 오 이런 방법도 있군요! |
||
<td>{{this}}</td> | ||
</tr> | ||
{{/each}} | ||
</tbody> | ||
</table> | ||
</pre> | ||
</body> | ||
</html> | ||
</html> |
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.
별건 아닌데 이부분 그냥 처음에 선언할 때
이렇게 안하시고 나중에 따로 초기화 하신 이유가 있나요??