-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosa12_player.py
206 lines (179 loc) · 8.31 KB
/
osa12_player.py
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#osa12-15
"""
Read player infomation from json file, implement the following requirements
- When user inputs 0, stop
- When user inputs 1, Output player info through player_name, format: player_name team_name goals + assists = ?
- When user inputs 2, Output all the team names
- When user inputs 3, Output all the nationalities
- When user inputs 4, Output player info through team_name, format: player_name team_name goals + assists = ?
- When user inputs 5, Output player info through nationality_name, format: player_name team_name goals + assists = ?
- When user inputs 6, Output the required number of players based on the ranking of goals + assists
- When user inputs 7, Output the required number of players based on the ranking of goals + assists
Take the osa.json file as an example:
yanjing@yanjingdeMacBook-Pro src % cd /Users/yanjing/Downloads/osa12/*/src;python3 *.py
tiedosto: osa.json
luettiin 14 pelaajan tiedot
komennot:
0 lopeta
1 hae pelaaja
2 joukkueet
3 maat
4 joukkueen pelaajat
5 maan pelaajat
6 eniten pisteitä
7 eniten maaleja
komento: 1
nimi: Andy Greene
Andy Greene NYI 2 + 12 = 14
komento: 2
BUF
CGY
DAL
NJD
NYI
OTT
PIT
WPG
WSH
komento: 3
CAN
CHE
CZE
SWE
USA
komento: 4
joukkue: OTT
Drake Batherson OTT 3 + 7 = 10
Jonathan Davidsson OTT 0 + 1 = 1
komento: 5
maa: CAN
Jared McCann PIT 14 + 21 = 35
Travis Zajac NJD 9 + 16 = 25
Taylor Fedun DAL 2 + 7 = 9
Mark Jankowski CGY 5 + 2 = 7
Logan Shaw WPG 3 + 2 = 5
komento: 6
kuinka monta: 3
Jakub Vrana WSH 25 + 27 = 52
Jared McCann PIT 14 + 21 = 35
John Klingberg DAL 6 + 26 = 32
komento: 7
kuinka monta: 5
Jakub Vrana WSH 25 + 27 = 52
Jared McCann PIT 14 + 21 = 35
Conor Sheary BUF 10 + 13 = 23
Travis Zajac NJD 9 + 16 = 25
John Klingberg DAL 6 + 26 = 32
komento: 0
"""
import json
class player:
def __init__(self, name: str, nationality: str, assists: int, goals: int, penalties: int, team: str, games: int):
self.name = name
self.nationality = nationality
self.assists = assists
self.goals = goals
self.penalties = penalties
self.team = team
self.games = games
def __str__(self):
return "{} {} {} {} {} {} {}".format(self.name, self.nationality, self.assists, self.goals, self.penalties, self.team, self.games)
class player_set:
def __init__(self):
self.player_set = []
def add_play(self, name: str, nationality: str, assists: int, goals: int, penalties: int, team: str, games: int):
self.player_set.append(player(name, nationality, assists, goals, penalties, team, games))
def playerset_list(self):
return self.player_set
class player_ui:
def __init__(self, content: list):
self.content = content
self.playerdb = player_set()
for element in self.content:
self.playerdb.add_play(element["name"], element["nationality"], element["assists"], element["goals"], element["penalties"], element["team"], element["games"])
def get_player_set_from_file(self):
return self.playerdb
def ui_display(self):
print("komennot:")
print("0 lopeta") # stop
print("1 hae pelaaja") # Output player info through player_name, format: player_name team_name goals + assists = ?
print("2 joukkueet") # Output all the team names
print("3 maat") # Output all the nationalities
print("4 joukkueen pelaajat") # Output player info through team_name, format: player_name team_name goals + assists = ?
print("5 maan pelaajat") # Output player info through nationality_name, format: player_name team_name goals + assists = ?
print("6 eniten pisteitä") # Output the required number of players based on the ranking of goals + assists
print("7 eniten maaleja") # Output the required number of players based on the ranking of goals + assists
def output_cmd_1(self, player_name: str):
result = filter(lambda player: player.name == player_name, self.get_player_set_from_file().playerset_list())
return list(map(lambda player: (player.name, player.team, player.goals, player.assists), result))
def output_cmd_2(self):
return sorted(set(list(map(lambda player: player.team, self.get_player_set_from_file().playerset_list()))))
def output_cmd_3(self):
return sorted(set(list(map(lambda player: player.nationality, self.get_player_set_from_file().playerset_list()))))
def output_cmd_4(self, team_name: str):
result1 = filter(lambda player: player.team == team_name, self.get_player_set_from_file().playerset_list())
result2 = list(map(lambda player: (player.name, player.team, player.goals, player.assists), result1))
return sorted(result2, key = lambda tuple_item: tuple_item[2] + tuple_item[3], reverse = True)
def output_cmd_5(self, nationality_name: str):
result1 = filter(lambda player: player.nationality == nationality_name, self.get_player_set_from_file().playerset_list())
result2 = list(map(lambda player: (player.name, player.team, player.goals, player.assists), result1))
return sorted(result2, key = lambda tuple_item: tuple_item[2] + tuple_item[3], reverse = True)
def output_cmd_6(self):
result1 = list(map(lambda player: (player.name, player.team, player.goals, player.assists), self.get_player_set_from_file().playerset_list()))
result2 = sorted(result1, key = lambda tuple_item: tuple_item[2] + tuple_item[3], reverse = True)
return result2
def output_cmd_7(self):
result1 = list(map(lambda player: (player.name, player.team, player.goals, player.assists, player.games), self.get_player_set_from_file().playerset_list()))
result2 = sorted(result1, key = lambda tuple_item: (tuple_item[2], tuple_item[4]), reverse = True)
return result2
def user_interaction(self):
self.ui_display()
while True:
print()
cmd = int(input("komento: "))
if cmd == 0:
break
elif cmd == 1:
player_name = input("nimi: ")
if self.output_cmd_1(player_name) != []:
player = self.output_cmd_1(player_name)[0]
print(f"{player[0]:21}{player[1]}{player[2]:>4}{'+':>2}{player[3]:>3}{'=':>2}{player[2]+player[3]:>4}")
elif cmd == 2:
for item in self.output_cmd_2():
print(item)
elif cmd == 3:
for item in self.output_cmd_3():
print(item)
elif cmd == 4:
team_name = input("joukkue: ")
for player in self.output_cmd_4(team_name):
print(f"{player[0]:21}{player[1]}{player[2]:>4}{'+':>2}{player[3]:>3}{'=':>2}{player[2]+player[3]:>4}")
elif cmd == 5:
nationality_name = input("maa: ")
for player in self.output_cmd_5(nationality_name):
print(f"{player[0]:21}{player[1]}{player[2]:>4}{'+':>2}{player[3]:>3}{'=':>2}{player[2]+player[3]:>4}")
elif cmd == 6:
num = int(input("kuinka monta: "))
for player in self.output_cmd_6()[0:num]:
print(f"{player[0]:21}{player[1]}{player[2]:>4}{'+':>2}{player[3]:>3}{'=':>2}{player[2]+player[3]:>4}")
elif cmd == 7:
num = int(input("kuinka monta: "))
play_list = self.output_cmd_7()[0:num]
for i in range(0, len(play_list)-1):
if play_list[i][2] == play_list[i+1][2]:
if play_list[i][4] > play_list[i+1][4]:
tmp = play_list[i]
play_list[i] = play_list[i+1]
play_list[i+1] = tmp
for player in play_list:
print(f"{player[0]:21}{player[1]}{player[2]:>4}{'+':>2}{player[3]:>3}{'=':>2}{player[2]+player[3]:>4}")
else:
self.ui_display()
filename = input("tiedosto: ")
with open(filename) as filename:
data = filename.read()
content = json.loads(data)
print("luettiin {} pelaajan tiedot".format(len(content)))
print()
instance = player_ui(content)
instance.user_interaction()