-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.py
167 lines (145 loc) · 5.11 KB
/
ui.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
############################################
# Class: CPTR 226 - Computer Science I
# Assignment: Case Study ch11
# Author(s): Lee
# Date: 11/10/19
############################################
#imports
from db import *
from objects import Player,Lineup
import csv
from datetime import datetime, timedelta, date
#tuple
TeamPositionList = ("C", "1B", "2B", "3B", "SS", "LF", "CF", "RF", "P")
#intro
def menu():
print("=" * 64)
print(" Baseball Team Manager ")
print()
print("CURRENT DATE (YYYY-MM-DD): ", date.today())
GameDate = str(input("GAME DATE (YYYY-MM-DD): "))
if GameDate == "":
menuoptions()
else:
GameDate = datetime.strptime(GameDate, "%Y-%m-%d")
DifferenceOfDays = GameDate-datetime.now()
if DifferenceOfDays <= timedelta(days = 0):
menuoptions()
else:
print("DAYS UNTIL GAME:", DifferenceOfDays.days)
menuoptions()
#menu function
def menuoptions():
print()
print("MENU OPTIONS")
print("1 - Display lineup")
print("2 - Add player")
print("3 - Remove player")
print("4 - Move player")
print("5 - Edit player position")
print("6 - Edit player stats")
print("7 - Exit program")
print()
print("POSITIONS")
print(TeamPositionList[0] + " , " + TeamPositionList[1]+ " , " +TeamPositionList[2]+ " , " +TeamPositionList[3]+ " , " +TeamPositionList[4]+ " , " +TeamPositionList[5]+ " , " +TeamPositionList[6]+ " , " +TeamPositionList[7]+ " , " +TeamPositionList[8])
print()
try:
with open("player_list.csv") as file:
print("File found")
except FileNotFoundError:
print("Team data file could not be found")
print("You can create a new one if you want")
print("=" * 64)
#batting average function
def battingaverage(x,y):
if y > 0:
BattingAverage = x / y
BattingAverage = round(BattingAverage, 2)
BattingAverage = str(BattingAverage)
return BattingAverage
else:
BattingAverage = 0
return BattingAverage
def ErrorCheckerAtBats(x):
while True:
if x < 0:
print("Error, official times at bat can't be less than 0, please try again.")
print()
x = int(input("Official number of at bats: "))
else:
break
def ErrorCheckerHits(x,y):
while True:
if y > x:
print("Error, official at bats can't be greater than number of hits please try again.")
print()
y = int(input("Number of hits: "))
x = int(input("Official number of at bats: "))
elif y < 0:
print("Error, Can't have negative number of hits, please try again.")
print()
y = int(input("Number of hits: "))
else:
break
#main function
def main():
menu()
playerlist = read()
players = Lineup(playerlist)
while True:
try:
UserVariable = int(input("Menu option: "))
except Exception as e:
print("Not a valid option. Please try again.")
continue
if UserVariable == 1:#note couldn't add into Lineup becuase I couldn't get it to iterate
print("{:>9} {:>30} {:>7} {:>7} {:>7}".format("Player", "POS", "AB", "H", "AVG"))
print("-" * 64)
PlayerFormat = "{:<2} {:<30} {:>6} {:>7} {:>7} {:>7}"
counter = 1
for player in playerlist:
Name = str(player.FirstName) + " " + str(player.LastName)
Position = player.Position
AB = player.AtBats
Hits = player.Hits
Average = battingaverage(int(Hits), int(AB))
print(PlayerFormat.format(counter,Name,Position,AB,Hits,Average))
counter += 1
print()
elif UserVariable == 2:
FirstName = str(input("First Name: "))
LastName = str(input("Last Name: "))
Position = (str(input("Position: ")))
AtBats = int(input("At bats: "))
ErrorCheckerAtBats(AtBats)
Hits = int(input("Hits: "))
ErrorCheckerHits(AtBats,Hits)
players.AddPlayer(FirstName,LastName,Position,AtBats,Hits)
continue
elif UserVariable == 3:
number = int(input("Number: "))
number -= 1
players.DeletePlayer(number)
print()
continue
if UserVariable == 4:
players.MovePlayer()
print()
continue
elif UserVariable == 5:
players.EditPlayerPosition()
print()
continue
elif UserVariable == 6:
players.EditPlayer()
print()
continue
elif UserVariable == 7:
print("Bye!")
#write(players) currently can't update file because Lineup isn't iterable yet
break
elif UserVariable < 1 or UserVariable > 7:
print("Not a valid option. Please try again.")
continue
if __name__ == "__main__":
main()