Instructions
Requirements and Specifications
Source Code
'''
This file contains the main function and is the driver for the program
'''
from book import *
from library import *
def main():
# First of all, create the library
library = Library()
# Now, read the data file
with open('library_data.txt', 'r') as file:
# Read lines
lines = file.readlines()
# Loop through lines
for line in lines:
# Split line
data = line.split(',')
title = data[0].strip()
author = data[1].strip()
# Add to the library
library.add_book(title, author)
file.close()
# At this point, all lines from data file have been parsed and all books
# have been added to the library, so now print the library
print("The current contents of the library are:")
print(library)
# Now, begin interacting with the user until s/he enters 'q'
print("Enter new books to add to the library.")
running = True
while running:
title = input("Title ('q' to quit): ")
if title.lower() == 'q': # user entered 'q', so quit
running = False
else:
# ask for author
author = input("Author: ")
# Add to library
library.add_book(title, author)
# When we reach this line, is because the while-loop was exited, so now we display the new content
# of the library
print("Now the contents of the library are:")
print(library)
# Now save data to file
with open('library_data.txt', 'w') as output_file:
for book in library.get_book_list():
output_file.write(book.title + ", " + book.author + "\n")
if __name__ == '__main__':
main()
BOOK