· 4 years ago · Aug 09, 2021, 04:36 AM
1# ------------------------------------------#
2# Title: CDInventory.py
3# Desc: Starter Script for Assignment 05
4# Change Log: (Who, When, What)
5# Charles Hodges(hodges11@uw.edu), 2021-Aug-08, Created File
6# ------------------------------------------#
7
8
9# Variables
10dctRow = {} # Dictionary data row.
11lstTbl = [] # List of dictionaries to hold data.
12objFile = None # file object.
13strChoice = '' # User input.
14
15# Strings
16keyArtist = 'Artist'
17keyId = 'ID'
18keyTitle = 'Title'
19strAdd = 'a'
20strAddSuccessMsg = 'Added a CD to the inventory.'
21strArtistName = 'Enter the Artist\'s Name: '
22strCdTitle = 'Enter the CD\'s Title: '
23strCdWasDeletedMsg = "CD with ID {} was deleted."
24strDelete = 'd'
25strDeleteById = "Which CD would you like to remove? Indicate using 'ID': "
26strDisplay = 'i'
27strEnterId = 'Enter an ID: '
28strErrorMsg = 'Please choose either l, a, i, d, s or x!'
29strExit = 'x'
30strFileName = 'CDInventory.txt'
31strInputHdr = '\nThe Magic CD Inventory'
32strLoad = 'l'
33strLoadSuccessMsg = 'Loading complete.'
34strMenu1 = """
35[l] Load Inventory from file(*This will overwrite any unsaved data*)
36[a] Add CD
37[i] Display Current Inventory
38[d] Delete CD from Inventory
39[s] Save Inventory to file
40[x] exit
41"""
42strMenuOptions = 'l, a, i, d, s, or x: '
43strRead = 'r'
44strSave = 's'
45strSaveSuccessMsg = 'Saved to text file.'
46strWrite = 'w'
47
48
49# Get user Input
50print(strInputHdr)
51while True:
52
53 # Display menu allowing the user to choose usage intent.
54 print(strMenu1)
55
56 # convert choice to lower case at time of input.
57 strChoice = input(strMenuOptions).lower()
58 print() # Empty line for readability.
59
60 # Exit the program.
61 if strChoice == strExit:
62 break
63
64 # Load existing data.
65 if strChoice == strLoad:
66 # If the file does not yet exist, create it, to avoid
67 # a FileNotFoundError. This will NOT overwrite
68 # the current data, if it exists already.
69 text_file = open(strFileName, strAdd)
70 text_file.close()
71
72 # Reset the Table first
73 lstTbl = []
74
75 # Then load data from text file.
76 with open(strFileName, strRead) as objFile:
77 for row in objFile:
78 lstRow = row.strip().split(',')
79 dicRow = {
80 'ID': int(lstRow[0]),
81 'Title': lstRow[1],
82 'Artist': lstRow[2]
83 }
84 lstTbl.append(dicRow)
85 print(strLoadSuccessMsg)
86 print() # Empty line for readability
87
88 # Add data to the table.
89 elif strChoice == strAdd:
90 intId = int(input(strEnterId))
91 strTitle = input(strCdTitle)
92 strArtist = input(strArtistName)
93 dctRow = {keyId: intId, keyTitle: strTitle, keyArtist: strArtist}
94 lstTbl.append(dctRow)
95 print() # Empty line for readability
96 print(strAddSuccessMsg)
97 print() # Empty line for readability
98
99 # Display the current data to the user.
100 elif strChoice == strDisplay:
101 print("{: <5} {: <20} {: <20}".format("ID", "| Title", "| Artist"))
102 print("{: <5} {: <20} {: <20}".format("--", "| -----", "| ------"))
103 counter = 0
104 for row in lstTbl:
105 dctRowToLst = list(lstTbl[counter].values())
106 lstToStr = ','.join([str(elem) for elem in dctRowToLst])
107 split_lines = lstToStr.split(",")
108 id_num, title, artist = split_lines
109 print("{: <5} {: <20} {: <20}".format
110 (
111 id_num, "| " + title, "| " + artist)
112 )
113 counter += 1
114 print() # Empty line for readability.
115
116 # Delete an entry
117 elif strChoice == strDelete:
118 deleteId = int(input(strDeleteById))
119 for items in range(len(lstTbl)):
120 if lstTbl[items]['ID'] == deleteId:
121 del lstTbl[items]
122 print(strCdWasDeletedMsg.format(deleteId))
123 break
124 print() # Empty line for readability.
125
126 # Save the data to a text file.
127 elif strChoice == strSave:
128 with open(strFileName, strWrite) as objFile:
129 counter = 0
130 for row in lstTbl:
131 idNum, title, artist = lstTbl[counter].values()
132 objFile.write(str(idNum) + ',' + title + "," + artist + '\n')
133 counter += 1
134 print(strSaveSuccessMsg)
135 print() # Empty line for readability.
136
137 else:
138 print(strErrorMsg)