Skip to content

Instantly share code, notes, and snippets.

@soeunkk
Last active September 17, 2022 11:18
Show Gist options
  • Save soeunkk/6b8707e3ea9f09dc10ebf8278969b273 to your computer and use it in GitHub Desktop.
Save soeunkk/6b8707e3ea9f09dc10ebf8278969b273 to your computer and use it in GitHub Desktop.
/*
* 김소은
* 과제6. 가상 대선 당선 시뮬레이션 프로그램
*
* 대선과 관련된 상수, 변수, 함수를 묶어 PresidentialElection 클래스를 만들었습니다.
* PresidentialElection 클래스의 내부 클래스로 후보자와 관련된 변수, 함수를 담는 Candidate 클래스를 만들었습니다.
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
class PresidentialElection {
private static final int TOTAL_VOTES = 10000;
private final List<Candidate> candidates;
private int currentVotes;
PresidentialElection() {
candidates = new ArrayList<>();
candidates.add(new Candidate("이재명", 1));
candidates.add(new Candidate("윤석열", 2));
candidates.add(new Candidate("심상정", 3));
candidates.add(new Candidate("안철수", 4));
}와
public void elect() {
for (currentVotes = 1; currentVotes <= TOTAL_VOTES; currentVotes++) {
int votedIdx = voteRandom();
Candidate votedCandidate = candidates.get(votedIdx);
votedCandidate.vote();
printVotingProgress(votedCandidate);
printVotingProgressByCandidate();
}
printElectionResult();
}
private int voteRandom() {
return new Random().nextInt(candidates.size());
}
private void printVotingProgress(Candidate votedCandidate) {
float votingPercentage = (float) currentVotes / TOTAL_VOTES * 100;
System.out.printf("[투표진행율]: %.2f%%, %d명 투표 => %s\n", votingPercentage, currentVotes, votedCandidate.getName());
}
private void printVotingProgressByCandidate() {
for (int i = 0; i < candidates.size(); i++) {
Candidate candidate = candidates.get(i);
float votingPercentageByCandidate = (float) candidate.getVotes() / currentVotes * 100;
System.out.printf("[기호:%d] %s: %05.2f%%, (투표수: %d)\n", candidate.getNumber(), candidate.getName(), votingPercentageByCandidate, candidate.getVotes());
}
}
private void printElectionResult() {
Collections.sort(candidates);
Candidate first = candidates.get(0);
Candidate second = candidates.get(1);
if (first.getVotes() != second.getVotes()) {
System.out.printf("[투표결과] 당선인: %s\n", first.getName());
} else {
System.out.println("동률이므로 당선자가 없습니다.");
}
}
class Candidate implements Comparable<Candidate> {
private final String name;
private final int number;
private int votes;
Candidate(String name, int number) {
this.name = name;
this.number = number;
this.votes = 0;
}
public String getName() {
return name;
}
public int getNumber() {
return number;
}
public int getVotes() {
return votes;
}
public void vote() {
votes++;
}
@Override
public int compareTo(Candidate o) {
return Integer.compare(o.votes, this.votes); // 투표 수 내림차순
}
}
}
public class Test6 {
public static void main(String[] args) {
PresidentialElection presidentialElection = new PresidentialElection();
presidentialElection.elect();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment