Previous Index Next

Creating List of objects

The following Book class demonstrates use of setters and getters methods. It is a common practice to make all of data attributes private and to provide public methods for accessing them.

The following program implements CRUD operations using list of objects having attributes.

# Define Book class

class Book:
    def __init__(self, isbn, name, price):
        self.__isbn = isbn
        self.__name = name
        self.__price = price

    def set_isbn(self, isbn):
        self.__isbn = isbn

    def set_name(self, name):
        self.__name = name

    def set_price(self, price):
        self.__price = price
    
    def get_isbn(self):
        return self.__isbn

    def get_name(self):
        return self.__name

    def get_price(self):
        return self.__price

    def __str__(self):
        return "ISBN:" + str(self.__isbn) + \
               ", Name:" + self.__name + \
               ", Price:" + str(self.__price)
    

# Creating and storing objects in list
books =[]
n = int(input('How many records you want to enter? '))
for i in range(n):
    isbn = int(input('Enter ISBN number: '))
    name = input('Enter book name: ')
    price = int(input('Enter book price: '))
    print() # print blank line
    book = Book(isbn,name,price)
    books.append(book)    

# Display books details
print('Books detail:\n')
for book in books:
    print('ISBN: ', book.get_isbn())
    print('Name: ', book.get_name())
    print('Price: ', book.get_price())
    print()

# Search the book
isbn = int(input('Enter ISBN to search: '))
for book in books:
    if book.get_isbn() == isbn:
        print(book)
        break
else:
    print('Record not found')

# Delete the book
isbn = int(input('\nEnter ISBN to delete book: '))
for book in books:
    if book.get_isbn() == isbn:
        books.remove(book)
        print('Record deleted')
        break
else:
    print('Record not found')

# Updating price of book
isbn = int(input('\nEnter ISBN to update price: '))
for book in books:
    if book.get_isbn() == isbn:
        new_price = int(input('Enter new price of book:'))
        book.set_price(new_price)
        print('Record updated')
        break
else:
    print('Record not found')  
             

Output

How many records you want to enter? 3
Enter ISBN number: 8175255766
Enter book name: PyForSchool Computer Science
Enter book price: 49

Enter ISBN number: 8123456790
Enter book name: PyForSchool Python
Enter book price: 129

Enter ISBN number: 9876543210
Enter book name: Learn Python
Enter book price: 350

Books detail:

ISBN:  8175255766
Name:  PyForSchool Computer Science
Price:  49

ISBN:  8123456790
Name:  PyForSchool Python
Price:  129

ISBN:  9876543210
Name:  Learn Python
Price:  350

Enter ISBN to search: 8123456790
ISBN:8123456790, Name:PyForSchool Python, Price:129

Enter ISBN to delete book: 8123456790
Record deleted

Enter ISBN to update price: 9876543210
Enter new price of book:200
Record updated

Previous Index Next