Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Use JSON for storage
- import json
- bookStore = {}
- file = "store.json"
- def readFromStore():
- with open( file, "r" ) as f:
- global bookStore
- bookStore = json.loads(f.read())
- print( bookStore )
- def saveToStore():
- with open( file, "w" ) as f:
- global bookStore
- f.write(json.dumps(bookStore))
- def updateStore( name, data ):
- global bookStore
- bookStore[ name ] = data
- # Save the data
- saveToStore();
- # Let's read from store before starting
- readFromStore()
- while (1):
- print("\n")
- print("\tPadmalaya Book Store")
- print("\tA reliable one")
- print("1. A for append")
- print("2. D for delete")
- print("3. U for update")
- print("4. R for read")
- x = input("Your choice: ")
- name = input("Book name: ")
- if x == "A":
- # Input everything else here
- updateStore( name, { "cost": c, "edition": e, "price": p, "publisher": pub } )
- elif x == "R":
- if name in bookStore:
- print("Book name: ", name, "\n Book Details: ", bookStore[ name ] )
- else:
- print("The given book doesn't exist")
- else:
- print("Goodbye!")
- break
- # Example of store.json
- """
- {
- "H. C. Verma": {
- "cost": 390,
- "edition": 1,
- "publisher": "Balaji"
- },
- "Book 2": {
- "cost": 390,
- "edition": 1,
- "publisher": "Balaji"
- }
- }
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement