Skip to content

Instantly share code, notes, and snippets.

@soeunkk
Last active September 17, 2022 11:28
Show Gist options
  • Save soeunkk/be3c1701e02a8e5666802b9bae7d26b6 to your computer and use it in GitHub Desktop.
Save soeunkk/be3c1701e02a8e5666802b9bae7d26b6 to your computer and use it in GitHub Desktop.
/*
* 김소은
* 과제7. 로또 당첨 프로그램
*
* 6개의 로또 번호와 관련된 변수, 함수를 묶어 클래스를 만들었습니다.
* 입력값이 잘못됐으면 재입력할 수 있도록 구현하였습니다.
*/
import java.util.ArrayList;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.stream.Collectors;
class LotterySerial {
private final List<Integer> lotteryNumbers;
public LotterySerial() {
lotteryNumbers = createLotteryNumbers();
}
private List<Integer> createLotteryNumbers() {
Random random = new Random();
HashSet<Integer> lotteryNumberSet = new HashSet<>();
while (lotteryNumberSet.size() < 6) {
int lotteryNumber = random.nextInt(45) + 1;
lotteryNumberSet.add(lotteryNumber);
}
return lotteryNumberSet.stream().sorted().collect(Collectors.toList());
}
public int countMatchingNumber(LotterySerial winningLotterySerial) {
int count = 0;
for (int winningNumber : winningLotterySerial.lotteryNumbers) {
for (int myNumber : lotteryNumbers) {
if (winningNumber == myNumber) {
count++;
}
}
}
return count;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
lotteryNumbers
.forEach(lotteryNumber -> sb.append(String.format("%02d", lotteryNumber)).append(","));
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
}
public class Test7 {
private static final ArrayList<LotterySerial> myLotterySerials = new ArrayList<>();
private static LotterySerial winningLotterySerial;
private static Scanner scanner;
public static void main(String[] args) {
scanner = new Scanner(System.in);
System.out.println("[로또 당첨 프로그램]");
int num = inputLotteryNumber();
printMyLotterySerials(num);
System.out.println("[로또 발표]");
printWinningSerial();
System.out.println("[내 로또 결과]");
printMyLotterySerialsResult();
closeResource();
}
private static int inputLotteryNumber() {
int num = 0;
boolean isValid = false;
while (! isValid) {
System.out.print("로또 개수를 입력해 주세요.(숫자 1 ~ 10):");
try {
checkIntType();
num = scanner.nextInt();
checkValidLotteryNumber(num);
isValid = true;
} catch (InputMismatchException e) {
System.out.println(e.getMessage());
}
}
return num;
}
private static void checkIntType() {
if (! scanner.hasNextInt()) {
removeBuffer();
throw new InputMismatchException("숫자만 입력 가능합니다.");
}
}
private static void removeBuffer() { scanner.next(); }
private static void checkValidLotteryNumber(int lotteryNumber) {
if (lotteryNumber < 1 || lotteryNumber > 10) {
throw new InputMismatchException("로또 개수는 1 ~ 10 사이어야 합니다.");
}
}
private static void printMyLotterySerials(int num) {
StringBuilder sb = new StringBuilder();
for (int idx = 0; idx < num; idx++) {
LotterySerial lotterySerial = new LotterySerial();
myLotterySerials.add(lotterySerial);
sb.append(toStringIndexAndLotterySerial(idx, lotterySerial));
sb.append("\n");
}
System.out.println(sb);
}
private static void printWinningSerial() {
winningLotterySerial = new LotterySerial();
System.out.println("\t" + winningLotterySerial + "\n");
}
private static void printMyLotterySerialsResult() {
StringBuilder sb = new StringBuilder();
for (int idx = 0; idx < myLotterySerials.size(); idx++) {
LotterySerial lotterySerial = myLotterySerials.get(idx);
sb.append(toStringIndexAndLotterySerial(idx, lotterySerial));
int count = lotterySerial.countMatchingNumber(winningLotterySerial);
sb.append(toStringMatchingResultFormat(count));
sb.append("\n");
}
System.out.println(sb);
}
private static String toStringMatchingResultFormat(int count) {
return String.format(" => %d개 일치", count);
}
private static String toStringIndexAndLotterySerial(int idx, LotterySerial lotterySerial) {
return toStringAlphabetFormat(idx) + lotterySerial;
}
private static String toStringAlphabetFormat(int idx) {
return String.format("%c\t", (char) ('A' + idx));
}
private static void closeResource() { scanner.close(); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment