Your program is to check to see if a file called readinglist
Your program is to check to see if a file called “reading_list.txt” already exists and if it does, read it into an array of structures of type book_list.
You will define a structure called book_list that will hold a number associated with each book (int), book title (string), author (string), a description of the book (string), a date when the book was read stored in 2 parts – month (int) and year (int) and a rating of the book (int). The number will be generated by the program as each book is entered. The rating should be limited to a number between 1 and 10. It will be a scale the user can enter what they thought of the book.
The program should have a menu that asks the user to 1) print books 2) enter a new book 3) modify a book 4) print how many books are in the list 5) print all of the books that have a certain rating 6) exit the program. If the user enters 1, all of the books will be printed to the screen including the book number. If the user enters 2, the program will ask the user for the title, author, description, date, and rating. If the user selects 3, the program should ask the user for the book number to be modified and all of the new information for that book. If the user selects 4, you are to print the total number of books in the database to the screen.
When the user enters 5, you are to ask for the rating and then print out only the books with that rating. When the user selects 6 to exit the program, you are to create a new file or overwrite the existing file and write each record to the text file “reading_list.txt”.
****Structure member definitions:
Book number – to be generated by the programmer for each new book that is entered
Book title – This should be a able to hold spaces
Author – should be able to hold spaces
Description – should be able to hold spaces
Date – should be a month stored as 1 – 12 and year stored with all four digits, i.e. 2017. It should be printed at XX/XXXX i.e 9/2016
Rating – should be a number between 1 and 10
*******Requirements:
Create an array of structures containing the structure members as defined above
The program needs to be able to handle data for up to 500 books.
Read the data from the file “reading_list.txt” and save each book record in a structure in your array when you start your program. (Only read once.)
Write to the file once – when the user selects exit.
The title, author, and description should be able to contain spaces.
Book numbers must start with 1 and be generated by the program
You need to keep track of how many books there are.
***************Sample output if 1 selected****************
1 Les Miserables Victor Hugo About a released prisoner and his changed life 9/2016 10
2 Of Men and Angels Bodie Thoene About the Irish discontent 7/2015 8
**********Sample input if 2 selected*************
Enter title: Raising Boys
Enter author: James Dobson
Enter description: A book on raising boys
Enter month: 01
Enter year: 2017
Enter rating: 9
**************Sample input if 3 selected****************
Enter part to modify: 1
1 Les Miserables Victor Hugo About a released prisoner and his changed life 9/2016 10
Enter title: Les Miserables
Enter author: Victor Hugo
Enter description: About a changed life
Enter month: 12
Enter year: 2016
Enter rating: 10
**************Sample output if 4 selected****************
Total number of books: 3
**************Sample output if 5 selected******************
Enter rating: 10
1 Les Miserables Victor Hugo About a released prisoner and his changed life 9/2016 10
Solution
Python 2.7 Code for given problem:
import os.path
import sys
record = []
if os.path.exists(\"reading_list.txt\"):
with open(\"reading_list.txt\") as f:
content = f.readlines()
if(len(content) > 0):
n_books = int(content[0])
field = 0
book = []
for i in range(1,len(content)):
if(field == 6):
field = 0
record.append(book)
book = []
field = field + 1
book.append(content[i].strip())
else:
field = field + 1
book.append(content[i].strip())
record.append(book)
while(True):
print \"1 To print books\"
print \"2 To enter a new book\"
print \"3 To modify a book\"
print \"4 To print how many books are in the list\"
print \"5 To print all of the books that have a certain rating\"
print \"6 To exit the program\"
action = int(raw_input().strip())
if(action == 1):
for i in range(0,len(record)):
for j in range(0,6):
print record[i][j],
print \"\ \"
if(action == 2):
book = []
book.append(len(record) + 1)
print \"Enter title:\"
book.append(raw_input().strip())
print \"Enter author:\"
book.append(raw_input().strip())
print \"Enter description:\"
book.append(raw_input().strip())
print \"Enter month:\"
month = raw_input().strip()
print \"Enter year:\"
year = raw_input().strip()
book.append(str(month) + \"/\" + str(year))
print \"Enter rating:\"
book.append(raw_input().strip())
record.append(book)
if(action == 3):
print \"Enter part to modify:\"
bookid = int(raw_input().strip())
for j in range(0,6):
print record[bookid-1][j],
print \"\ \"
book = []
book.append(bookid)
print \"Enter title:\"
book.append(raw_input().strip())
print \"Enter author:\"
book.append(raw_input().strip())
print \"Enter description:\"
book.append(raw_input().strip())
print \"Enter month:\"
month = raw_input().strip()
print \"Enter year:\"
year = raw_input().strip()
book.append(str(month) + \"/\" + str(year))
print \"Enter rating:\"
book.append(raw_input().strip())
record[bookid-1] = book
if(action ==4):
print \"Total number of books: \", len(record)
if(action ==5):
print \"Enter rating:\"
rating = int(raw_input().strip())
for i in range(0,len(record)):
if(int(record[i][5]) == rating ):
for j in range(0,6):
print record[i][j],
print \"\ \"
if(action == 6):
if os.path.exists(\"reading_list.txt\"):
os.remove(\"reading_list.txt\")
f= open(\"reading_list.txt\",\"w+\")
f.write(str(len(record)));
f.write(\"\ \");
for i in range(0,len(record)):
for j in range(0,6):
f.write(str(record[i][j]))
f.write(\"\ \")
f.close()
sys.exit()
Format of reading_list.txt:
First line contains the total number of books, then next 6 lines for a book\'s ID,Author name, book name, describtion, date, rating. And so on.
Sample reading_list.txt
2
1
Les Miserables
Victor Hugo
About a released prisoner and his changed life
9/2016
10
2
Of Men and Angels
Bodie Thoene
About the Irish discontent
7/2015
8



