-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
92 lines (72 loc) ยท 2.42 KB
/
main.py
File metadata and controls
92 lines (72 loc) ยท 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import json
import rsa
from VotingModel.VotingSystem import VotingSystem
public_key, private_key = rsa.newkeys(1024)
voting_system = VotingSystem()
def init() -> dict:
with open('./voters.json', 'r') as file:
voters = json.load(file)
return voters
def get_pw(voter) -> str:
print(f"\nํ์ฌ ํฌํ์๋ {voter['name']}์
๋๋ค.")
pw = input("ํฌํ์์ ๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์ : ")
return pw
def check_pw(voter, pw_input):
if pw_input != voter["password"]:
print("๋น๋ฐ๋ฒํธ๊ฐ ์ผ์นํ์ง ์์ต๋๋ค. ๋ค์ ์
๋ ฅํ์ธ์.")
pw = get_pw(voter)
check_pw(voter, pw)
else:
act(voter)
def act(voter):
print("\n1. ํฌํํ๊ธฐ | 2. ์ฒด์ธ ํ์ธ | 3. ์ค๊ฐ ๊ฒฐ๊ณผ ๊ณ์ฐ | 4. ์ข
๋ฃ")
result = int(input("๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์ : "))
if result == 1:
voting(voter['id'])
act(voter)
elif result == 2:
get_chain()
act(voter)
elif result == 3:
print(get_results())
act(voter)
elif result == 4:
if voting_system.has_voted(voter['id']):
pass
else:
print("")
def voting(voter_id):
print("\nํฌํ๋ฅผ ์งํํฉ๋๋ค.")
print('-' * 50)
print(voting_system.get_candidates())
print('-' * 50)
selected_candidate = input("ํฌํ๋ฅผ ์ํ์๋ ํ๋ณด๋ฅผ ์
๋ ฅํ์ธ์ : ")
voting_system.cast_vote(voter_id=str(voter_id), candidate=selected_candidate, private_key=private_key,
public_key=public_key)
def get_chain():
print("\n์ฒด์ธ์ ํ์ธํฉ๋๋ค.")
chains = voting_system.get_chain()
for chain in chains:
print(chain.__dict__)
def get_results():
print("\nํฌํ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํฉ๋๋ค.")
return voting_system.calculate_results()
try:
voters = init()
for voter in voters:
pw = get_pw(voter)
check_pw(voter, pw)
winner_candidate = []
max_vote_num = 0
for candidate, vote_num in get_results().items():
if vote_num > max_vote_num:
max_vote_num = vote_num
winner_candidate = [candidate]
elif vote_num == max_vote_num:
winner_candidate.append(candidate)
if len(winner_candidate) == 1:
print(f"{winner_candidate[0]}์ด(๊ฐ) ๋น์ ๋์์ต๋๋ค.")
else:
print("๋์ ํ๊ฐ ๋์์ต๋๋ค. ํฌํ๋ฅผ ๋ค์ ์งํํด ์ฃผ์ธ์.")
except KeyboardInterrupt:
print("์ข
๋ฃ")