I need help on this assignment In the Files folder in a folder named Assignment4
ID: 3920297 • Letter: I
Question
I need help on this assignment
In the Files folder in a folder named Assignment4, I will provide . A Python program, named travelSystem.py 2. The Python application program interface (API) in travelClassAPI.py defining a class travelltem. (You will complete this file as travelClass.py.) . The Python API in travelToolboxAPI.py defining four functions. The functions readltems0 and printltems0 are complete, but the files readTransactions0 and printTransactions0 are only declarations of the interfaces. (You will complete this file as travelToolbox.py.) 4. Two text files that provide one test example, items4.c sv and transactions4.csv. (You will want to create similar test files.) a The items file is a sequence of lines, each of which has three comma-separated values: i. A unique tour ID (a string) . The name of that tour (a string). iThe starting availability of seats for the tour (an integer) For example: 12345,Italy,100 If a string value in a CSV file includes a comma, it is enclosed b. The transactions file is a sequence of lines, each of i. A tour ID (a string), for which there is a in quotation marks. which has two commaseparated values: corresponding item in the items4.csv file i. The number of seats for the transaction with that item (an integer). For example: 12345,10 A positive integer indicates a reservation and a negative integer indicates a cancellation. You are to complete the class travelltem in travelClass.py (from travelClassAPI.py) and the functions in travelToolbox.py (from travelToolboxAPI.py). This will give you the experience of working on an existing code base, something that is common in software development. You will need to read and understand the existing code, whic h is a necessary and valuable skill. I suggest that you write and test your work in the followingExplanation / Answer
travelSystem6.py
from travelToolbox import readItems, readTransactions
import tkinter as tk
from tkinter import Text, END
class myApp(tk.Tk):
def __init__(self, itemRecords) :
tk.Tk.__init__(self)
self.itemRecords = itemRecords
self.grid()
self.entry = tk.Entry(self)
self.entry.grid(column=1,row=0)
button1 = tk.Button(self,text="Show",
command=self.showStockItem)
button1.grid(column=2,row=0)
button2 = tk.Button(self,text="Quit",
command=self.quitit)
button2.grid(column=1,row=1)
label = tk.Label(self, anchor="w", text='Item ID')
label.grid(column=0,row=0)
def showStockItem(self):
self.clear()
content = self.entry.get()
print(content)
IDLabel = tk.Label(self, anchor="w", text='ID', font = "Calibri 14 bold")
IDLabel.grid(column=0,row=0)
NameLabel = tk.Label(self, anchor="w", text='Name', font = "Calibri 14 bold")
NameLabel.grid(column=0,row=1)
StartLabel = tk.Label(self, anchor="w", text='Start', font = "Calibri 14 bold")
StartLabel.grid(column=0,row=2)
EndLabel = tk.Label(self, anchor="w", text='End', font = "Calibri 14 bold")
EndLabel.grid(column=0,row=3)
# self.T = Text(self, height=4, width=50)
# self.T.grid(column=0,row=2)
# self.T.insert(END, "hello")
return
def clear(self):
for l in self.grid_slaves():
l.destroy()
def quitit(self):
self.destroy()
return
itemsFileName = "items4.csv"
transactionsFileName = "transactions4.csv"
# itemRecords is a dictionary of stockItem records indexed by item ID
itemRecords = {}
# Read the items from itemsFileName into itemRecords
readItems(itemsFileName, itemRecords)
# Read the transactions from transactionsFileName into itemRecords
readTransactions(transactionsFileName, itemRecords)
app = myApp(itemRecords)
app.title('Travel')
app.mainloop()
travelToolbox.py
import csv
import sys
from travelClass import travelItem
def readItems(itemsFileName, itemRecords) :
# readItems reads items from itemsFileName into the itemRecords dictionary
# Open itemsFileName and create a CSV file reader
itemsFile = open(itemsFileName, 'r')
itemsReader = csv.reader(itemsFile)
# Process each row of the items file
for row in itemsReader :
# Get the values for the record
(iid, iname, icount) = row
iid = str(iid)
iname = str(iname)
icount = int(icount)
if icount < 0:
print("""Found a travel item with an available start value less than 0.
It will be skipped, but the program will continue.""")
else:
# Check if this ID is not yet in itemRecords
if (not(iid in itemRecords)) :
# Create a travelItem object and add it to itemRecords
itemRecord = travelItem(iid, iname, icount)
itemRecords[iid] = itemRecord
def printItems(itemRecords) :
# printItems prints the list of items from the itemRecords dictionary
# Print the header
print("ID NAME")
print("----- ------------")
# For each item record in itemRecords
for rec in itemRecords.values() :
# Print the item ID and name
print("{0:5s} {1:12s}".format(rec.getID(), rec.getName()))
# Print the footer
print("")
def readTransactions(transactionsFileName, itemRecords) :
transactionsFile = open(transactionsFileName, 'r')
transactionsReader = csv.reader(transactionsFile)
for row in transactionsReader :
(iid, transaction) = row
iid = str(iid)
transaction = int(transaction)
if (not(iid in itemRecords)) :
print("""Found an unknown iid when reading transactions. It was skipped,
but the program will continue.""")
else:
itemRecord = itemRecords[iid]
itemRecord.appendTransaction(transaction)
def printSummary(itemRecords) :
print("Tours")
print("=====")
print("ID NAME Start Resv. Cancl. End ")
print("----- ------------ ------ ------ ------ ------")
for rec in itemRecords.values() :
reservations = sum(rec.getReservations())
if rec.getAvailableEnd() < 0:
print("Error: {} had an available end less than 0. Exiting now.".format(rec.getName()))
sys.exit()
if rec.getAvailableEnd() > rec.getAvailableStart():
print("Error: {} had an available end less than 0. Exiting now.".format(rec.getName()))
sys.exit()
cancellations = sum(rec.getCancellations())
formatted_str = "{0:5s} {1:12s} {2:5} {3:5} {4:5} {5:5}"
print(formatted_str.format(rec.getID(), rec.getName(),
rec.getAvailableStart(), reservations, cancellations,
rec.getAvailableEnd()))
print("")
travelSystem.py
from travelToolbox import readItems, printItems, readTransactions, printSummary
from travelPlot import plotTravel
itemsFileName = "items4.csv"
transactionsFileName = "transactions4.csv"
# itemRecords is a dictionary of travelItem records indexed by item ID
itemRecords = {}
# Read the items from itemsFileName into itemRecords
readItems(itemsFileName, itemRecords)
# Print out the list of items from itemRecords
printItems(itemRecords)
# Read the transactions from transactionsFileName into itemRecords
readTransactions(transactionsFileName, itemRecords)
# Print the summary for each item from itemRecords
printSummary(itemRecords)
# Plot the travel for each item from itemRecords plotTravel(itemRecords)
plotTravel(itemRecords)
travelClass.py
class travelItem :
def __init__(self, itemID, itemName, itemCount):
self.itemID = itemID
self.itemName = itemName
self.itemCount = itemCount
self.transactions = list()
def getID(self) :
return self.itemID
def getName(self) :
return self.itemName
def setName(self, newName) :
self.itemName = newName
def getAvailableStart(self) :
# todo: double check this
return self.itemCount
def appendTransaction(self, num) :
self.transactions.append(num)
def getTransactions(self) :
return self.transactions
def getReservations(self) :
reservations = list()
for num in self.transactions:
if num >= 0:
reservations.append(num)
return reservations
def getCancellations(self) :
cancellations = list()
for num in self.transactions:
if num < 0:
cancellations.append(num)
return cancellations
def getAvailableEnd(self) :
return self.itemCount - sum(self.transactions)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.