#DB File import sqlite3 import sys import os from contextlib import closing from Objects import Player, Lineup conn = None def connect(): global conn if not conn: if sys.platform =="win32": DB_FILE = "player_db.sqlite" else: HOME = os.environ["HOME"] DB_FILE = HOME + "player_db.sqlite" conn = sqlite3.connect(DB_FILE) conn.row_factory = sqlite3.Row def close(): if conn: conn.close() def get_players(): query = '''SELECT * FROM Player;''' with closing(conn.cursor()) as c: c.execute(query) results = c.fetchall() player = Player(playerID, batOrder, first, last, position, at_bats, hits) for row in results: for player in row: lineup = Lineup.add(Player(player)) return lineup # SQL statement to select all 7 fields for all players # Use a with statement to execute the query # Create a lineup object # use a loop to populate the lineup object with player objects # return the lineup object def get_player(playerID): query = '''SELECT * FROM Player WHERE playerID = ?;''' with closing(conn.cursor()) as c: c.execute(query, playerID) results = c.fetchall() for row in results: player = Player(playerID, batOrder, first, last, position, at_bats, hits) return player # SQL statement to select all 7 fields for a player # Use a with statement to execute the query & return a player object if the player exists def add_player(player): query = '''INSERT INTO Player VALUES (?, ?, ?, ?, ?, ?);''' with closing(conn.cursor()) as c: c.execute(query, batOrder, first, last, position, at_bats, hits) conn.commit() # SQL statement to insert 6 fields for a player added to the table # Use a with statement to execute the query def delete_player(player): query = '''DELETE FROM Player WHERE playerID = ?''' with closing(conn.cursor()) as c: c.execute(query, player) conn.commit() # SQL statement to delete a single player # Use a with statement to execute the query   def update_bat_order(lineup): query = '''SELECT * FROM Player UPDATE Player SET batOrder = playerID;''' with closing(conn.cursor()) as c: c.execute(query) conn.commit() # Use a loop to call a SQL statement that updates # the batOrder for each player based on their playerID # Use a with statement to execute the query def update_player(player): query = '''UPDATE Player SET firstName = first, lastName = last, position = position, at_bats = at_bats, hits = hits WHERE playerID = ?;''' with closing(conn.cursor()) as c: c.execute(query) conn.commit() # SQL statement to update 6 fields of a player based on the playerID # Use a with statement to execute the query def main(): # code to test the get_players function connect() players = get_players() if players != None: for player in players: print(player.batOrder, player.firstName, player.lastName, player.position, player.atBats, player.hits, player.getBattingAvg()) else: print("Code is needed for the get_players function.") if __name__ == "__main__": main() #Objects File #player class class Player: def __init__(self, playerID, batOrder, first, last, position, at_bats, hits): self.playerID = playerID self.batOrder = batOrder self.first = first self.last = last self.position = position self.at_bats = at_bats self.hits = hits self.avg = (self.hits / self.at_bats) def name(self): return self.first + " " + self.last def average(self): return self.avg def __str__(self): return (self.name()).ljust(25) + str(self.position).rjust(2) + " " \ + str(self.at_bats).rjust(3) + " " + str(self.hits).rjust(3) + " " \ + f"{self.average():1.3f}" class Lineup: def __init__(self): self.__data = [] # add method def add(self, player): if isinstance(player, Player): self.__data.append(player) else: print("Invalid object!") # remove method def remove(self, location): if location <= len(self.__data): del self.__data[location - 1] else: print("Invalid line up location") def get(self, location): if location <= len(self.__data) + 1: return self.__data[location - 1] else: print("Invalid line up location") def __iter__(self): self.index = 0 return self def __next__(self): if self.index == len(self.__data): raise StopIteration else: self.index += 1 return self.__data[self.index - 1] # help with finish please i need for tonight

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

#DB File


import sqlite3
import sys
import os
from contextlib import closing

from Objects import Player, Lineup

conn = None

def connect():
global conn
if not conn:
if sys.platform =="win32":
DB_FILE = "player_db.sqlite"
else:
HOME = os.environ["HOME"]
DB_FILE = HOME + "player_db.sqlite"
conn = sqlite3.connect(DB_FILE)
conn.row_factory = sqlite3.Row


def close():
if conn:
conn.close()


def get_players():
query = '''SELECT * FROM Player;'''
with closing(conn.cursor()) as c:
c.execute(query)
results = c.fetchall()
player = Player(playerID, batOrder, first, last, position, at_bats, hits)
for row in results:
for player in row:
lineup = Lineup.add(Player(player))

return lineup

# SQL statement to select all 7 fields for all players

# Use a with statement to execute the query

# Create a lineup object
# use a loop to populate the lineup object with player objects
# return the lineup object

def get_player(playerID):
query = '''SELECT * FROM Player
WHERE playerID = ?;'''
with closing(conn.cursor()) as c:
c.execute(query, playerID)
results = c.fetchall()
for row in results:
player = Player(playerID, batOrder, first, last, position, at_bats, hits)
return player
# SQL statement to select all 7 fields for a player

# Use a with statement to execute the query & return a player object if the player exists


def add_player(player):
query = '''INSERT INTO Player
VALUES (?, ?, ?, ?, ?, ?);'''
with closing(conn.cursor()) as c:
c.execute(query, batOrder, first, last, position, at_bats, hits)
conn.commit()
# SQL statement to insert 6 fields for a player added to the table

# Use a with statement to execute the query


def delete_player(player):
query = '''DELETE FROM Player
WHERE playerID = ?'''
with closing(conn.cursor()) as c:
c.execute(query, player)
conn.commit()
# SQL statement to delete a single player


# Use a with statement to execute the query

 

def update_bat_order(lineup):
query = '''SELECT * FROM Player
UPDATE Player
SET batOrder = playerID;'''
with closing(conn.cursor()) as c:
c.execute(query)
conn.commit()
# Use a loop to call a SQL statement that updates
# the batOrder for each player based on their playerID

# Use a with statement to execute the query


def update_player(player):
query = '''UPDATE Player
SET firstName = first,
lastName = last,
position = position,
at_bats = at_bats,
hits = hits
WHERE playerID = ?;'''
with closing(conn.cursor()) as c:
c.execute(query)
conn.commit()
# SQL statement to update 6 fields of a player based on the playerID

# Use a with statement to execute the query


def main():
# code to test the get_players function
connect()
players = get_players()
if players != None:
for player in players:
print(player.batOrder, player.firstName, player.lastName,
player.position, player.atBats, player.hits, player.getBattingAvg())
else:
print("Code is needed for the get_players function.")

if __name__ == "__main__":
main()

#Objects File


#player class
class Player:
def __init__(self, playerID, batOrder, first, last, position, at_bats, hits):
self.playerID = playerID
self.batOrder = batOrder
self.first = first
self.last = last
self.position = position
self.at_bats = at_bats
self.hits = hits
self.avg = (self.hits / self.at_bats)

def name(self):
return self.first + " " + self.last

def average(self):
return self.avg

def __str__(self):
return (self.name()).ljust(25) + str(self.position).rjust(2) + " " \
+ str(self.at_bats).rjust(3) + " " + str(self.hits).rjust(3) + " " \
+ f"{self.average():1.3f}"


class Lineup:
def __init__(self):
self.__data = []

# add method
def add(self, player):
if isinstance(player, Player):
self.__data.append(player)
else:
print("Invalid object!")

# remove method
def remove(self, location):
if location <= len(self.__data):
del self.__data[location - 1]
else:
print("Invalid line up location")

def get(self, location):
if location <= len(self.__data) + 1:
return self.__data[location - 1]
else:
print("Invalid line up location")

def __iter__(self):
self.index = 0
return self

def __next__(self):
if self.index == len(self.__data):
raise StopIteration
else:
self.index += 1
return self.__data[self.index - 1]

# help with finish please i need for tonight

Sample Console Output
Baseball Team Manager
CURRENT DATE:
2020-07-12
GAME DATE:
2020-07-19
DAYS UNTIL GAME: 7
MENU OPTIONS
1 - Display lineup
2 - Add player
3 - Remove player
4 - Move player
5 - Edit player position
6 - Edit player stats
Exit program
7
POSITIONS
с, 1в, 2в, зв, ss, LF, СЕ, RF, P
Menu option:
Player
POS
AB
H
AVG
Dominick Gilbert
1B
545
170
0.317
2 Craig Mitchell
Jack Quinn
Simon Harris
CF
396
125
0.316
0.287
3
RF
345
99
4
450
135
0.300
5 Darryl Moss
6 Grady Guzman
7 Wallace Cruz
Cedric Cooper
ЗВ
501
120
0.240
463
114
0.246
443
131
0.296
8
2B
165
54
0.327
Alberto Gomez
72
19
0.264
Menu option:
General Specifications
Use a Player class that provides attributes that store the playerID, batOrder,
first name, last name, position, at bats, and hits for a player. The class
constructor should use these seven attributes as parameters. This class should
also provide a method that returns the full name of a player and a method that
returns the batting average for a player.
The playerlD is not used when adding a player to the lineup. A PlayerlD, the
primary key for the Player table, is automatically created by the database.
• Use a Lineup class to store the lineup for the team as a list of player objects. This
class should include methods with appropriate parameters that allow you to add,
remove, move, and edit a player. In addition, it should include an iterator so you
can easily loop through each player in the lineup. A Lineup object replaces the list
that holds all players.
Use the same console Input/output system that was used in project 3. The file for
this system will be named UI.
• Use a file named Objects to store the code for the Player and Lineup classes.
• Use a file named DB to store the functions that work with the file that stores the data. A
db_comments.py file is included as a template for the DB file that must be turned in.
Transcribed Image Text:Sample Console Output Baseball Team Manager CURRENT DATE: 2020-07-12 GAME DATE: 2020-07-19 DAYS UNTIL GAME: 7 MENU OPTIONS 1 - Display lineup 2 - Add player 3 - Remove player 4 - Move player 5 - Edit player position 6 - Edit player stats Exit program 7 POSITIONS с, 1в, 2в, зв, ss, LF, СЕ, RF, P Menu option: Player POS AB H AVG Dominick Gilbert 1B 545 170 0.317 2 Craig Mitchell Jack Quinn Simon Harris CF 396 125 0.316 0.287 3 RF 345 99 4 450 135 0.300 5 Darryl Moss 6 Grady Guzman 7 Wallace Cruz Cedric Cooper ЗВ 501 120 0.240 463 114 0.246 443 131 0.296 8 2B 165 54 0.327 Alberto Gomez 72 19 0.264 Menu option: General Specifications Use a Player class that provides attributes that store the playerID, batOrder, first name, last name, position, at bats, and hits for a player. The class constructor should use these seven attributes as parameters. This class should also provide a method that returns the full name of a player and a method that returns the batting average for a player. The playerlD is not used when adding a player to the lineup. A PlayerlD, the primary key for the Player table, is automatically created by the database. • Use a Lineup class to store the lineup for the team as a list of player objects. This class should include methods with appropriate parameters that allow you to add, remove, move, and edit a player. In addition, it should include an iterator so you can easily loop through each player in the lineup. A Lineup object replaces the list that holds all players. Use the same console Input/output system that was used in project 3. The file for this system will be named UI. • Use a file named Objects to store the code for the Player and Lineup classes. • Use a file named DB to store the functions that work with the file that stores the data. A db_comments.py file is included as a template for the DB file that must be turned in.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY