Skip to content

Instantly share code, notes, and snippets.

@kyungjaecheong
Created October 7, 2024 03:15
Show Gist options
  • Save kyungjaecheong/f8bf8192ba0a0e8dfb240ec46c6ff50c to your computer and use it in GitHub Desktop.
Save kyungjaecheong/f8bf8192ba0a0e8dfb240ec46c6ff50c to your computer and use it in GitHub Desktop.
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
/**
* ZeroBase BackEnd School<br>
* Java 미니과제 6번 - 가상 선거 및 당선 시뮬레이션 프로그램
* @author 정경재 (30기)
*/
public class M1W2Ex06 {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
System.out.println("[가상 선거 시뮬레이션 프로그램]");
int population = getPopulation(sc);
Map<Integer, String> candidateMap = getCandidateMap(sc);
showVoting(population, candidateMap);
} catch (Exception e) {
System.out.println("[Exception 발생] : " + e);
} finally {
System.out.println("\n프로그램을 종료합니다.");
}
}
private static int getPopulation(Scanner sc) {
while (true) {
System.out.print("총 진행할 투표수를 입력해 주세요. : ");
String input = sc.nextLine();
if (input.matches("^[0-9]{1,5}$")) {
int value = Integer.parseInt(input);
if (value >= 1 && value <= 10000) {
return value;
}
}
System.out.println("[입력 오류] 총 투표수는 1 ~ 10000 사이로 입력해 주세요.");
}
}
private static Map<Integer, String> getCandidateMap(Scanner sc) {
Map<Integer, String> candidateMap = new LinkedHashMap<>();
while (true) {
System.out.print("가상 선거를 진행할 후보자 인원을 입력해 주세요. : ");
String candiInput = sc.nextLine();
if (candiInput.matches("^[0-9]{1,2}$")) {
int candidates = Integer.parseInt(candiInput);
if (candidates >= 2 && candidates <= 10) {
addCandidates(sc, candidateMap, candidates);
break;
} else {
System.out.println("[입력 오류] 후보자 인원은 2 ~ 10명 사이로 입력해 주세요.");
}
} else {
System.out.println("[입력 오류] 숫자를 입력해 주세요. (2 ~ 10)");
}
}
return candidateMap;
}
private static void addCandidates(
Scanner sc,
Map<Integer, String> candidateMap,
int candidates) {
for (int i = 1; i <= candidates; i++) {
while (true) {
System.out.printf("%d번째 후보자 이름을 입력해 주세요. : ", i);
String name = sc.nextLine();
if (name.matches("^[가-힣]{1,9}$")) {
candidateMap.put(i, name);
break;
} else {
System.out.println("[입력 오류] 후보자의 이름은 10자 미만의 한글로 입력해 주세요.");
}
}
}
}
private static void showVoting(
int population,
Map<Integer, String> candidateMap
) {
Random random = new Random();
Map<Integer, Integer> votesMap = new LinkedHashMap<>();
int candidates = candidateMap.size();
for (int i = 1; i <= candidates; i++) {
votesMap.put(i, 0);
}
for (int p = 1; p <= population; p++) {
int vote = random.nextInt(candidates) + 1;
votesMap.put(vote, votesMap.get(vote) + 1);
double totalRatio = (double) p / population * 100;
System.out.printf("\n[투표진행률]: %.2f%%, %d명투표 => %s\n",
totalRatio, p, candidateMap.get(vote));
for (int i = 1; i <= candidates; i++) {
String name = candidateMap.get(i);
int candiVotes = votesMap.get(i);
double candiRatio = (double) candiVotes / population * 100;
System.out.printf("[기호:%d] %s:\t%3.2f%%\t(투표수: %d)\n",
i, name, candiRatio, candiVotes);
}
}
int resultIdx = 0;
int resultVotes = 0;
for (Map.Entry<Integer, Integer> entry : votesMap.entrySet()) {
Integer key = entry.getKey();
Integer votes = entry.getValue();
if (votes > resultVotes) {
resultIdx = key;
resultVotes = votes;
}
}
System.out.println("\n[투표결과] 당선인 : " + candidateMap.get(resultIdx));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment