-
Notifications
You must be signed in to change notification settings - Fork 21
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
[2주차]객체지향 코드 연습(enohs) #30
base: main
Are you sure you want to change the base?
Changes from 6 commits
398d8d6
5a61b85
c9c60eb
8589be8
2fe9f9b
8391724
223e3c7
e7e931a
174503c
b8ffdae
e7bb528
3c62a49
2b1aefc
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,15 @@ | ||
## 구현 기능 목록 ## | ||
|
||
# View | ||
- 로또 가격 입력 | ||
- 로또 개수 출력 | ||
- 로또 개수만큼 리스트 출력 | ||
- 당첨 번호 입력 | ||
- 보너스 번호 입력 | ||
- 당첨 통계 출력 | ||
|
||
# Controller | ||
- | ||
|
||
# Model | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package lotto.InOutputSystem; | ||
|
||
import java.util.Scanner; | ||
import lotto.manageError.CheckError; | ||
|
||
public class InputNums { | ||
static Scanner scanner = new Scanner(System.in); | ||
|
||
public static int price(){ | ||
System.out.println("구입금액을 입력해 주세요."); | ||
String lottoPrice = scanner.nextLine(); | ||
int intPrice = CheckError.isNum(lottoPrice); | ||
return intPrice; | ||
} | ||
|
||
public static String winningNums() { | ||
System.out.println("\n당첨 번호를 입력해 주세요."); | ||
String winningNums = scanner.nextLine(); | ||
return winningNums; | ||
} | ||
|
||
public static int bonusNum(){ | ||
System.out.println("\n보너스 번호를 입력해 주세요."); | ||
int bonusNum = scanner.nextInt(); | ||
CheckError.numRange(bonusNum); | ||
return bonusNum; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package lotto.InOutputSystem; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class PrintStrings { | ||
|
||
public static void printLottos (List<List<Integer>> saveLottos, int count){ | ||
System.out.println("\n" + count + "개를 입력했습니다."); | ||
for (int i =0; i < saveLottos.size(); i++){ | ||
System.out.println(saveLottos.get(i)); | ||
} | ||
} | ||
|
||
public static void lottoResult(Map<String, Integer> prizeMap, double profit){ | ||
System.out.println("\n당첨 통계"); | ||
System.out.println("---------"); | ||
|
||
System.out.println("3개 일치 (5,000원)- " + prizeMap.get("5등") + "개"); | ||
System.out.println("4개 일치 (50,000원)- " + prizeMap.get("4등") + "개"); | ||
System.out.println("5개 일치 (1,500,000원)- "+ prizeMap.get("3등") + "개"); | ||
System.out.println("5개 일치, 보너스 볼 일치 (30,000,000원)-"+ prizeMap.get("2등") + "개"); | ||
System.out.println("6개 일치 (2,000,000,000원)- "+ prizeMap.get("1등") + "개"); | ||
|
||
System.out.println("총 수익률은 " + String.format("%.1f", profit) + "%입니다."); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package lotto.mainSystem; | ||
|
||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
StartLotto startLotto = new StartLotto(); | ||
startLotto.run(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package lotto.mainSystem; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import lotto.InOutputSystem.InputNums; | ||
import lotto.InOutputSystem.PrintStrings; | ||
import lotto.manageLotto.CalcProfit; | ||
import lotto.manageLotto.CompareLottos; | ||
import lotto.manageLotto.MakeLottoLists; | ||
import lotto.manageLotto.MakeWinLotto; | ||
|
||
public class StartLotto { | ||
enohs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public void run(){ | ||
int inputPrice = InputNums.price(); | ||
|
||
MakeLottoLists lottoLists = new MakeLottoLists(); | ||
enohs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
List<List<Integer>> myLottos = lottoLists.makeInputLottoList(inputPrice); | ||
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. Lotto 객체를 잘 활용하지 못하고 계신 것 같습니다. Lotto 객체를 활용해주세요 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. 맞습니다. 제가 직접 만들어서 사용해보려고 하다가 활용을 못했습니다 ㅎㅎ 사용하도록 노력해보겠습니다!
enohs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
String winningNums = InputNums.winningNums(); | ||
enohs marked this conversation as resolved.
Show resolved
Hide resolved
enohs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
List<Integer> winLotto = MakeWinLotto.makeWinLotto(winningNums); | ||
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. WinLotto라는 객체를 하나 생성하는 것은 어떤 가요? 필드로 Lotto와 보너스 번호, 두개를 둬서 관리하는 게 좋아보입니다. 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. 객체는 상태와 행위로 이루어져있는데, 상태 = 필드, 행위 = 메소드 입니다.
위와같이 선언해서 사용하라는 말이었습니다! 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. 아~~~ 그렇군요! 참고해보겠습니다 ㅎㅎ 감사합니다! |
||
|
||
int bonusNum = InputNums.bonusNum(); | ||
|
||
Map prize = CompareLottos.compareNums(myLottos, winLotto, bonusNum); | ||
|
||
double profit = CalcProfit.calcProfit(prize, inputPrice); | ||
|
||
PrintStrings.lottoResult(prize, profit); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package lotto.manageError; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class CheckError { | ||
enohs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public static void dividedThousand(int price){ | ||
enohs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (price % 1000 != 0){ | ||
enohs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
System.out.println("[Error] 남은 동전이 없어 잔돈을 드리지 못 합니다. 다시 입력해 주세요."); | ||
throw new IllegalArgumentException(); | ||
enohs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}} | ||
|
||
public static int isNum(String num){ | ||
try { | ||
int intNum = Integer.parseInt(num); | ||
return intNum; | ||
}catch (IllegalArgumentException e){ | ||
System.out.println("숫자를 입력해 주세요."); | ||
throw new IllegalArgumentException(); | ||
} | ||
} | ||
|
||
public static void numRange(int num){ | ||
if (num < 1 || num > 45){ | ||
System.out.println("[Error] 1부터 45까지의 숫자만 입력해 주세요."); | ||
throw new IllegalArgumentException(); | ||
} | ||
} | ||
|
||
public static void onlySixNums(List<Integer> nums){ | ||
if (nums.size() != 6){ | ||
System.out.println("[Error] 숫자 6개를 입력해 주세요."); | ||
throw new IllegalArgumentException(); | ||
} | ||
} | ||
|
||
public static List<Integer> inputOnlyNums(String[] winningNumsArr){ | ||
try { | ||
List<Integer> integerList = Arrays.stream(winningNumsArr) | ||
enohs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.map(Integer::parseInt) | ||
.collect(Collectors.toList()); | ||
return integerList; | ||
} catch (IllegalArgumentException e) { | ||
System.out.println("[Error] 숫자를 입력해 주세요."); | ||
throw new IllegalArgumentException(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package lotto.manageLotto; | ||
|
||
import java.util.Map; | ||
|
||
public class CalcProfit { | ||
public static double calcProfit(Map prize, int inputPrice) { | ||
enohs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int totalPrize = (int) prize.get("1등") * 2000000000 + (int) prize.get("2등") * 30000000 + (int) prize.get("3등") * 1500000 + (int) prize.get("4등") * 50000 + (int) prize.get("5등") * 5000; | ||
double profitRate = (double) totalPrize/ inputPrice * 100; | ||
|
||
return profitRate; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,54 @@ | ||||||
package lotto.manageLotto; | ||||||
|
||||||
import java.util.HashMap; | ||||||
import java.util.List; | ||||||
import java.util.Map; | ||||||
|
||||||
public class CompareLottos { | ||||||
|
||||||
public static Map<String, Integer> compareNums(List<List<Integer>> myLottos, List<Integer> winLotto, int bonusNum) { | ||||||
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. 이부분도 static을 사용한 이유가 있으실까요?
enohs marked this conversation as resolved.
Show resolved
Hide resolved
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.
Suggested change
Lotto객체를 활용하지 못하는 것 같습니다.. Lotto 객체를 활용해주세요 |
||||||
int sixMatch = 0; | ||||||
int fiveAndBonusMatch = 0; | ||||||
int fiveMatch = 0; | ||||||
int fourMatch = 0; | ||||||
int threeMatch = 0; | ||||||
enohs marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
Map<String, Integer> prizeMap = new HashMap<>(); | ||||||
|
||||||
for (List<Integer> myLotto : myLottos) { | ||||||
int matchCount = 0; | ||||||
boolean bonusMatch = false; | ||||||
|
||||||
for (int num : myLotto) { | ||||||
if (winLotto.contains(num)) { | ||||||
matchCount++; | ||||||
} | ||||||
} | ||||||
|
||||||
if (myLotto.contains(bonusNum)) { | ||||||
bonusMatch = true; | ||||||
} | ||||||
|
||||||
|
||||||
if (matchCount == 6) { | ||||||
sixMatch++; | ||||||
} else if (matchCount == 5 && bonusMatch) { | ||||||
fiveAndBonusMatch++; | ||||||
} else if (matchCount == 5) { | ||||||
fiveMatch++; | ||||||
} else if (matchCount == 4) { | ||||||
fourMatch++; | ||||||
} else if (matchCount == 3) { | ||||||
threeMatch++; | ||||||
} | ||||||
} | ||||||
enohs marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
prizeMap.put("1등", sixMatch); | ||||||
prizeMap.put("2등", fiveAndBonusMatch); | ||||||
prizeMap.put("3등", fiveMatch); | ||||||
prizeMap.put("4등", fourMatch); | ||||||
prizeMap.put("5등", threeMatch); | ||||||
enohs marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
return prizeMap; | ||||||
} | ||||||
} | ||||||
enohs marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package lotto.manageLotto; | ||
|
||
import camp.nextstep.edu.missionutils.Randoms; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lotto.manageError.CheckError; | ||
import lotto.InOutputSystem.PrintStrings; | ||
|
||
public class MakeLottoLists { | ||
private List<List<Integer>> saveLottos; | ||
enohs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public MakeLottoLists() { | ||
this.saveLottos = new ArrayList<>(); | ||
enohs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public List<List<Integer>> makeInputLottoList(int price) { | ||
int LOTTO_COUNT = price / 1000; | ||
|
||
CheckError.dividedThousand(price); | ||
|
||
for (int i = 0; i < LOTTO_COUNT; i++) { | ||
List<Integer> numbers = Randoms.pickUniqueNumbersInRange(1, 45, 6); | ||
saveLottos.add(numbers); | ||
} | ||
|
||
PrintStrings.printLottos(saveLottos, LOTTO_COUNT); | ||
return saveLottos; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package lotto.manageLotto; | ||
|
||
import java.util.List; | ||
import lotto.manageError.CheckError; | ||
|
||
public class MakeWinLotto { | ||
|
||
public static List<Integer> makeWinLotto(String winningNums) { | ||
String[] winningNumsArr = winningNums.split(","); | ||
List<Integer> integerList = CheckError.inputOnlyNums(winningNumsArr); | ||
|
||
CheckError.onlySixNums(integerList); | ||
|
||
for (int i = 0; i < integerList.size(); i++) { | ||
CheckError.numRange(integerList.get(i)); | ||
} | ||
|
||
return integerList; | ||
} | ||
} |
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.
input 메소드를 static으로 구현하신 이유가 있으실까요??
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을 사용한 것은 객체 생성 없이 프로그램을 진행시키기 위함이었습니다. 그런데 static에 대해 알아보니 메모리에서 지워지지 않는 특징이 있더군요!
그래서 남용하면 성능에 영향을 준다는 사실을 알게 되었습니다.
아무래도 static을 어떻게 쓰는 것이 메모리 사용에 있어 효율적일지 생각해봐야 할 것 같습니다