Our users do not want to have to re-enter the price levels each time the app starts. Therefor, we need to save the price levels to a file on disk - called levelsFile - and populate the levelsList with the file's items when the app starts. Also, every time the user manipulates the values in levelsList, levelsFile needs to be updated accordingly. The easiest way is not to update the file, but to simply override it (create a new file) with the new values in levelsList (each time the user manipulates items in the list). Two new methods need to be created in the PriceChecker class. They are readLevelsFromFile() and writelevelsToFile(). And then the Main Code Section must also be updated to use these methods in the obiect.
Please fill in the ellipsis(...) of how to read and write the levels(values) from a file for a price_checker:
#Method : Load levelsList using the data in levelsFile
def readLevelsFromFile(self):
try:
# Set levelsList to an empty list
self.levelsList = []
# Open the file
...
# Use a loop to read through the file line by line
...
# If the last two characters in the line is "\n", remove them
...
...
# Append the line to levelsList
...
# Close the file
...
except:
return
#Method: Write levelsList to levelsFile (override the existing file)
def writeLevelsToFile(self):
# Open the file in a way that will override the existing file (if it already exists)
...
# Use a loop to iterate over levelsList item by item
...
# Convert everything in the item to and then add \n to it - before writing to the file
...
# Close the file
...
# *************************************************************************************************
# Main Code Section
# *************************************************************************************************
# Create an object based on the PriceChecker class
checkerObj = PriceChecker()
#Load levelsList from the records in levelsFile
checkerObj.readLevelsFromFile()
# Display the levelsList and Menu; and then get user input for what actions to take
userInput = 99
while userInput != 0:
checkerObj.displayList()
userInput = checkerObj.displayMenu()
if (userInput == 1):
checkerObj.addLevel()
checkerObj,writeLevelsToFile() #Write levelsList to levelsFile
elif (userInput == 2):
checkerObj.removeLevel()
checkerObj.writeLevelsToFile() #Write levelsList to levelsFile
elif (userInput == 3):
checkerObj.removeAllLevels()
checkerObj.writeLevelsToFile() #Write levelsList to levelsFile
Step by step
Solved in 3 steps