Solutions - 2

1.

class BankAccount:
    def __init__(self, name, balance=0):
        self.name = name
        self.balance = balance

    def display(self):
         print(self.name, self.balance)

    def withdraw(self, amount):
        self.balance -= amount

    def deposit(self, amount):
        self.balance += amount
    
a1 = BankAccount('Mike', 200)
a2 = BankAccount('Tom')

a1.display()
a2.display()

a1.withdraw(100)
a2.deposit(500)

a1.display()
a2.display()


2.

class Book:
    
    def __init__(self,isbn, title,author,publisher,pages,price,copies):
        self.isbn = isbn
        self.title = title
        self.author = author
        self.publisher = publisher
        self.pages = pages
        self.price = price
        self.copies = copies
   
    def display(self):
        print(self.title)
        print(f'ISBN : {self.isbn}')
        print(f'Price : {self.price}')
        print(f'Number of copies : {self.copies}')
        print('.' * 50)

book1 = Book('957-4-36-547417-1', 'Learn Physics','Stephen', 'CBC', 350, 200,10)
book2 = Book('652-6-86-748413-3', 'Learn Chemistry','Jack', 'CBC', 400, 220,20)
book3 = Book('957-7-39-347216-2', 'Learn Maths','John', 'XYZ', 500, 300,5)
book4 = Book('957-7-39-347216-2', 'Learn Biology','Jack', 'XYZ', 400, 200,6)

book1.display()
book2.display()
book3.display()
book4.display()

3.

def in_stock(self):
        return True if self.copies>0 else False

def sell(self):
        if self.in_stock():
            self.copies -= 1
        else:
            print('The book is out of stock')


4.

books = [book1, book2, book3, book4]

for book in books:
    book.display()

jack_books = [book.title for book in books if book.author == 'Jack']

print(jack_books)


5.

@property
 def price(self):
       return self._price

@price.setter
 def price(self, new_price):
      if 50 <= new_price <= 1000:
          self._price = new_price
      else:
           raise ValueError ('Price cannot be less than 10 or more than 500')


6.

class Fraction:
    def __init__(self,nr,dr=1):
        self.nr = nr
        self.dr = dr
        if self.dr < 0:    
            self.nr *= -1
            self.dr *= -1

    def show(self):
        print(f'{self.nr}/{self.dr}')
       
f1 = Fraction(2,3)
f1.show()
f2 = Fraction(2,-3)
f2.show()
f3 = Fraction(-5,-6)
f3.show()


7.

def multiply(self,other):
        if isinstance(other,int):
            other = Fraction(other)
        return Fraction(self.nr * other.nr , self.dr * other.dr)

def add(self,other):
        if isinstance(other,int):
            other = Fraction(other)
        return Fraction(self.nr * other.dr + other.nr * self.dr, self.dr * other.dr)


8.

class Product():
    def __init__(self, id, marked_price, discount):
        self.id = id
        self.marked_price = marked_price
        self.discount = discount
        
    @property
    def selling_price(self):
        return self.marked_price - 0.01 * self.discount * self.marked_price
    
    def display(self):
        print(self.id,  self.marked_price,  self.discount)
    
p1 = Product('A234', 100, 5)
p2 = Product('X879', 400, 6)
p3 = Product('B987', 990, 4)
p4 = Product('H456', 800, 6)

print(p1.id, p1.selling_price)
print(p2.id, p2.selling_price)
print(p3.id, p3.selling_price)
print(p4.id, p4.selling_price)

9.

class Product():
    def __init__(self, id, marked_price, discount):
        self.id = id
        self.marked_price = marked_price
        self._discount = discount
        
    @property
    def selling_price(self):
        return self.marked_price - 0.01 * self.discount * self.marked_price

    @property
    def discount(self):
        return self._discount+2 if self.marked_price > 500 else self._discount

    @discount.setter
    def discount(self, new_discount):
        self._discount = new_discount
        
    def display(self):
        print(self.id, self.marked_price, self.discount)

p1 = Product('A234', 100, 5)
p2 = Product('X879', 400, 6)
p3 = Product('B987', 990, 4)
p4 = Product('H456', 800, 6)

print(p1.id, p1.selling_price)
print(p2.id, p2.selling_price)
print(p3.id, p3.selling_price)
print(p4.id, p4.selling_price)

p4.discount = 10
print(p4.id, p4.selling_price)        


10.

   class Circle:
    def __init__(self, radius):
        self.radius = radius

    @property
    def radius(self):
        return self._radius

    @radius.setter
    def radius(self, new_radius):
        if new_radius > 0:
            self._radius = new_radius
        else:
            raise ValueError('Radius should be positive')

    @property
    def diameter(self):
        return self._radius * 2

    @property
    def circumference(self):
        return 2 * 3.14 * self._radius

    def area(self):
        return 3.14 * self._radius * self._radius

c1 = Circle(7)
print( c1.radius, c1.diameter, c1.circumference, c1.area() )


Complete and Continue