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