class Library: def __init__(self): studentList = [] bookList = [] def addStudent(self, stuName, stuID): newStu = Student(stuName, stuID) studentList.append(newStu) newStu.write() def addBook(self, title, author, isbn, publisher): newBook = Book(title, author, isbn, publisher) bookList.append(newBook): newBook.write() class Student: def __init__(self, name, id, hasBook = false ): self.name = name self.id = id self.hasBook = hasbook #adds this student to the Student File.txt file. def write(self): sfile = open('Student File.txt', 'a') sfile.write('Name: ' + str(self.name) + '\n') sfile.write('ID: ' + str(self.id) + '\n') sfile.close() class Book: def __init__(self, title, author, isbn, publisher, lent = false ): self.title = title self.author = author self.isbn = isbn self.publisher = publisher self.lent = lent #adds this student to the Book File.txt file. def write(self): bfile = open('Book File.txt', 'a') bfile.write('Title: ' + str(self.title) + '\n') bfile.write('Author: ' + str(self.author) + '\n') bfile.write('ISBN: ' + str(self.isbn) + '\n') bfile.write('Publisher: ' + str(self.publisher) + '\n') bfile.close() def main(): #Creating the Menu print '**************************************************************************' print print ' 1. List of students Press 1' print ' 2. List of books Press 2' print ' 3. List of books that are available Press 3' print ' 4. List of books given out Press 4' print " 5. List of books lent and lender's name Press 5" print ' 6. List of students that borrowed books Press 6' print ' 7. To add a student Press 7' print ' 8. To add a book Press 8' print ' 9. To lend out a book to a friend Press 9' print ' 10. To return a book Press 10' print ' 11. To exit Press e' print print '**************************************************************************' menu_loop() def menu_loop(): sentinel = 'r' while sentinel != 'e': sentinel = raw_input('Type a menu option: ') #menu option 1 #produces a list of students if sentinel == '1': infile = open('Student File.txt', 'r') file_contents = infile.read() infile.close() print file_contents #menu option 2 #produces a list of books if sentinel == '2': infile = open('Book File.txt', 'r') file_contents = infile.read() infile.close() print file_contents #menu option 7 #adds a student to Student File.txt if sentinel == '7': name = raw_input("What is the student's name?: ") id = raw_input("What is the student's id?: ") library.addStudent(name, id) #menu option 8 #adds a book to Book File.txt if sentinel == '8': title = raw_input("What is the book's title?: ") author = raw_input("What is the book's author?: ") isbn = raw_input("What is the book's isbn?: ") publisher = raw_input("What is the book's publisher?: ") library.addBook(title, author, isbn, publisher) #menu option 11 #quits the menu if sentinel == 'e': pass #Initialization def __init__(self, company, owner, address): self.company = company self.owner = owner self.address = address #Mutators def writeC(self, company): self.company = company def writeO(self, owner): self.owner = owner def writeA(self, address): self.address = address #Accessors def accessC(self): print self.company def accessO(self): print self.owner def accessA(self): print self.address #toString method def __str__(self): print self.company print self.owner print self.address file_contents = infile.read() infile.close() print file_contents main()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question