-
Notifications
You must be signed in to change notification settings - Fork 34
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
[Lee, Nathan] 로또 2단계 - 보너스 번호 추가 #41
base: nathan29849
Are you sure you want to change the base?
Changes from all commits
97da692
edf45b6
c59c7d5
f438af0
cedce92
0a0f20d
2be4b31
00a3220
7162394
ff671c5
2778b9a
a89c370
6cc42b8
8cc12ad
68655b3
c988010
643fd1f
01591d8
a2bed80
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 |
---|---|---|
@@ -1,12 +1,16 @@ | ||
Main.java | ||
Main | ||
domain/Rank.java | ||
domain.Rank | ||
view/OutputView.java | ||
view.OutputView | ||
domain/LottoTicket.java | ||
domain.LottoTicket | ||
view/InputView.java | ||
view.InputView | ||
domain/ProfitAmount.java | ||
domain.ProfitAmount | ||
domain/User.java | ||
domain.User | ||
domain/TicketOffice.java | ||
domain.TicketOffice | ||
domain/LottoCompany.java | ||
domain.LottoCompany |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
import domain.LottoTicket; | ||
import domain.TicketOffice; | ||
import domain.*; | ||
|
||
import java.util.List; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
TicketOffice to = new TicketOffice(); | ||
List<LottoTicket> tickets = to.issueTickets(); | ||
to.setWinningNumber(); | ||
to.getStatistic(tickets); | ||
LottoCompany lottoCompany = new LottoCompany(); | ||
User user = new User(); | ||
user.goTicketOffice(); | ||
user.checkMyTicketsFrom(lottoCompany); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package domain; | ||
|
||
import java.util.*; | ||
|
||
public class AutoTicketOffice extends TicketOffice { | ||
private final List<Integer> lottoNumber = new ArrayList<>(); | ||
private final int SELECTABLE_NUMBER = 45; | ||
private final int SELECTED_NUMBER = 6; | ||
|
||
public AutoTicketOffice() { | ||
setLottoNumber(); | ||
} | ||
|
||
@Override | ||
public List<LottoTicket> issueTickets(int numberOfTickets) { | ||
List<LottoTicket> tickets = new ArrayList<>(); | ||
for (int j = 0; j < numberOfTickets; j++){ | ||
tickets.add(makeAutoTicket()); | ||
} | ||
return tickets; | ||
} | ||
|
||
private LottoTicket makeAutoTicket() { | ||
List<Integer> ticketNumber = new ArrayList<>(); | ||
Collections.shuffle(lottoNumber); | ||
for (int i = 0; i < SELECTED_NUMBER; i++) { | ||
ticketNumber.add(lottoNumber.get(i)); | ||
} | ||
Collections.sort(ticketNumber); | ||
return new LottoTicket(ticketNumber); | ||
} | ||
|
||
private void setLottoNumber() { | ||
for (int i = 1; i <= SELECTABLE_NUMBER; i++) { | ||
this.lottoNumber.add(i); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package domain; | ||
|
||
import view.InputView; | ||
import view.OutputView; | ||
|
||
import java.util.*; | ||
|
||
public class LottoCompany { | ||
private LottoTicket winningTicket; | ||
private int bonusNumber; | ||
private int totalPrice; | ||
private final int PRICE = 1000; | ||
private final Map<Rank, Integer> statistics = 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.
|
||
|
||
|
||
public void setInitialStatistic() { | ||
for (Rank rank : Rank.values()) { | ||
statistics.put(rank, 0); | ||
} | ||
} | ||
|
||
public void check(List<LottoTicket> tickets){ | ||
setWinningTicket(); | ||
setBonusNumber(); | ||
this.totalPrice = tickets.size() * PRICE; | ||
getStatistic(tickets); | ||
} | ||
|
||
public void setWinningTicket() { | ||
this.winningTicket = new LottoTicket(InputView.getWinningNumber()); | ||
} | ||
|
||
public void setBonusNumber() { // 추후 당첨 번호에 없는 번호만 받도록 예외처리 필요 | ||
this.bonusNumber = InputView.getBonusNumber(); | ||
} | ||
|
||
public void getStatistic(List<LottoTicket> tickets) { | ||
setInitialStatistic(); | ||
int matchedNumber; | ||
boolean isBonus; | ||
Rank currentRank; | ||
Comment on lines
+39
to
+41
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. 이 세가지 정보를 |
||
for (LottoTicket ticket : tickets) { | ||
matchedNumber = ticket.comparisonWinningTicket(this.winningTicket); | ||
isBonus = ticket.checkBonusNumber(this.bonusNumber); | ||
currentRank = Rank.designateRank(matchedNumber, isBonus); | ||
statistics.computeIfPresent(currentRank, (k, v) -> v + 1); | ||
} | ||
OutputView.showWinningResult(this.statistics, calculateProfit()); | ||
} | ||
|
||
public double calculateProfit() { | ||
int totalPrize = 0; | ||
for (Rank rank : this.statistics.keySet()) { | ||
totalPrize += rank.getPrize() * statistics.get(rank); | ||
} | ||
return ((double) (totalPrize - totalPrice) / totalPrice) * 100; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package domain; | ||
|
||
import view.InputView; | ||
|
||
import java.util.*; | ||
|
||
public class ManualTicketOffice extends TicketOffice { | ||
|
||
@Override | ||
public List<LottoTicket> issueTickets(int numberOfManualTicket) { | ||
List<LottoTicket> tickets = new ArrayList<>(); | ||
if (numberOfManualTicket > 0) { | ||
tickets.addAll(makeManualTickets(numberOfManualTicket)); | ||
} | ||
return tickets; | ||
} | ||
|
||
private List<LottoTicket> makeManualTickets(int numberOfManualTicket) { | ||
List<LottoTicket> manualLottoTickets = new ArrayList<>(); | ||
System.out.println("수동으로 구매할 번호를 입력해 주세요. (응모할 번호 6자리)"); | ||
for (int i = 0; i < numberOfManualTicket; i++) { | ||
manualLottoTickets.add(new LottoTicket(InputView.getManualNumber())); | ||
} | ||
return manualLottoTickets; | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package domain; | ||
|
||
public enum Rank { | ||
FAIL(0, 0), | ||
FIFTH(3, 5_000), | ||
FORTH(4, 50_000), | ||
THIRD(5, 1_500_000), | ||
SECOND(5, 30_000_000), | ||
FIRST(6, 2_000_000_000); | ||
|
||
private final int matchedNumber; | ||
private final int prize; | ||
|
||
private Rank(int matchedNumber, int prize) { | ||
this.matchedNumber = matchedNumber; | ||
this.prize = prize; | ||
} | ||
|
||
public int getMatchedNumber() { | ||
return matchedNumber; | ||
} | ||
|
||
public int getPrize() { | ||
return prize; | ||
} | ||
|
||
public static Rank designateRank(int matchedNumber, boolean isBonus) { | ||
switch (matchedNumber) { | ||
case 3: | ||
return FIFTH; | ||
case 4: | ||
return FORTH; | ||
case 5: | ||
return checkBonus(isBonus); | ||
case 6: | ||
return FIRST; | ||
default: | ||
return FAIL; | ||
} | ||
} | ||
Comment on lines
+28
to
+40
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. 스위치문 없앨 수 있습니다. 없애주세요. 힌트: |
||
|
||
private static Rank checkBonus(boolean isBonus){ | ||
if(isBonus) | ||
return SECOND; | ||
return THIRD; | ||
} | ||
} |
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.
인스턴스 변수인데 대문자로 선언되어 있고 값도 부여되어 있군요.
제 생각에 이 변수는 인스턴스 변수로 관리할 것이 아니라
static
키워드를 추가로 부여해서 런타임 상수로 보존하는 게 좋을 것 같군요.