Modify the inventory program of Exercise 14.5. The modified program allows you to delete a record for a tool that you no longer have and allows you to update any information in the file.
What will be an ideal response?
```
# Hardware inventory.
import sys
import shelve
# prompt for an input menu choice
def enterChoice():
print "\nEnter your choice"
print "1 - add a new tool"
print "2 - list tools"
print "3 - update record"
print "4 - delete record"
print "5 - end program"
while 1:
menuChoice = int( raw_input( "? " ) )
if not 1 <= menuChoice <= 5:
print >> sys.stderr, "Incorrect choice"
else:
break
return menuChoice
# add item to shelve file
def addItem( inventory ):
toolID = int( raw_input( "Enter tool identification number: " ) )
if 0 < toolID <= 100:
userInput = raw_input( "Enter: tool quantity cost\n? " )
inventory[ str( toolID ) ] = userInput.split()
else:
print "Invalid record number."
# list tools
def listTools( inventory ):
print "ToolID".ljust( 10 ),
print "Tools".ljust( 10 ),
print "Quantity".ljust( 10 ),
print "Price".ljust( 10 )
for key in inventory.keys():
print key.ljust( 10 ),
print inventory[ key ][ 0 ].ljust( 10 ),
print inventory[ key ][ 1 ].ljust( 10 ),
print inventory[ key ][ 2 ].ljust( 10 )
# delete tool
def deleteTool( inventory ):
toolID = raw_input( "Enter toolID number: " )
if inventory.has_key( toolID ):
del inventory[ str( toolID ) ]
else:
print >> sys.stderr, "Tool ID", toolID, \
"does not exist."
# update item
def updateItem( inventory ):
selection = ""
toolID = raw_input( "Enter ID number of tool to be updated: " )
if inventory.has_key( toolID ):
tempRecord = inventory[ toolID ] # copy of record
print """
Select an item to update.
[0] Tool Name
[1] Quantity
[2] Price"""
selection = raw_input( "Enter a selection: " )
# change indicated item
if selection == "0":
tempRecord[ 0 ] = raw_input( "Enter new tool name: " )
if selection == "1":
tempRecord[ 1 ] = raw_input( "Enter new quantity: " )
if selection == "2":
tempRecord[ 2 ] = raw_input( "Enter new price: " )
# delete old record and add updated one
if 0 <= int( selection ) <= 2:
del inventory[ toolID ]
inventory[ toolID ] = tempRecord
else:
print >> sys.stderr, "Tool ID", toolID, \
"does not exist."
options = [ addItem, listTools, updateItem, deleteTool ]
# open shelve file
try:
inventory = shelve.open( "hardware.dat" )
except IOError:
print >> sys.stderr, "File could not be opened"
sys.exit( 1 )
# process user commands
while 1:
choice = enterChoice()
if choice == 5:
break
options[ choice - 1 ]( inventory )
inventory.close()
```
Enter your choice
1 - add a new tool
2 - list tools
3 - update record
4 - delete record
5 - end program
? 2
ToolID Tools Quantity Price
88 saw 34 18.50
21 wrench 45 8.00
Enter your choice
1 - add a new tool
2 - list tools
3 - update record
4 - delete record
5 - end program
? 3
Enter ID number of tool to be updated: 21
Select an item to update.
[0] Tool Name
[1] Quantity
[2] Price
Enter a selection: 2
Enter new price: 10.00
Enter your choice
1 - add a new tool
2 - list tools
3 - update record
4 - delete record
5 - end program
? 4
Enter toolID number: 88
Enter your choice
1 - add a new tool
2 - list tools
3 - update record
4 - delete record
5 - end program
? 2
ToolID Tools Quantity Price
21 wrench 45 10.00
Enter your choice
1 - add a new tool
2 - list tools
3 - update record
4 - delete record
5 - end program
? 5
You might also like to view...
If s is a structure variable and p, a pointer, is a member of the structure, the statement cout << *s.p; will
A) output the dereferenced value pointed to by p. B) result in a compiler error. C) output the address stored in p. D) output the value stored in s. E) None of the above
The command ________ executes a Java application.
a. run b. javac c. java d. None of the above
Critical Thinking QuestionsCase 2-2A new colleague of yours has been eyeing your computer skills with envy, as you seem to know all of the shortcuts. He wants to know what your "secret" is. Which of the following allows you to remove character formatting? a. Press and hold down the CTRL key and then click the paragraph.b. Move the mouse to the left of the first line until the mouse pointer changes to a right-pointing block arrow and then click.c. Move the mouse to the left of the text until the mouse pointer changes to a right-pointing block arrow and then triple-click.d. Press the CTRL+SPACEBAR keys.
What will be an ideal response?
Describe character devices and block devices, and explain the difference between them. Provide examples of each type of device.
What will be an ideal response?