Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

L

ab -
Report 04
NAME:
Zain Ul Abideen
SUBJECT:
DATA STRUCTURE AND ALGORITHMS
REG NO:
FA21-BCE-009
CLASS:
BCE-3A
Date:
14/10/2022
Question: Write python program to accomplish these tasks:
1)Write a class called Product. The class should have fields called name, amount,
and price, holding the product's name, the number of items of that product in
stock, and the regular price of the product. There should be a method get price
that receives the number of items to be bought and returns the cost of buying
that many items, where the regular price is charged for orders of less than 10
items, a 10% discount is applied for orders of between 10 and 99 items, and a
20% discount is applied for orders of 100 or more items. There should also be a
method called makes purchase that receives the number of items to be bought
and decreases amount by that much. The regular prices of these products to be
used are: shirt: 500, wristwatch:1200, textbook: 800, history book: 500,
memorycard:1400

Code:
class Product:
def __init__(self, name, amount, price):
self.name = name
self.amount = amount
self.price = price
def get_price(self, number_to_be_bought):
discount = 0
if number_to_be_bought < 10:
pass
elif 10 <= number_to_be_bought < 99:
discount = 10
else:
discount = 20
price = (100 - discount) / 100 * self.price
return price * number_to_be_bought
def make_purchase(self, quantity):
self.amount -= quantity
name = input('name: ')
amount = int(input('Amount of items: '))
price = int(input('Price of items: '))
print("=================================\n")
name = Product(name, amount, price)
quantity = int(input('Amount of items to buy: \n'))
print('========================================')
q1 = 9
print(f'cost for {q1} {name.name} = {name.get_price(q1)}')
name.make_purchase(q1)
print(f'remaining stock: {name.amount}\n')
q2 = 50
print(f'cost for {q2} {name.name} = {name.get_price(q2)}')
name.make_purchase(q2)
print(f'remaining stock: {name.amount}\n')
q3 = 120
print(f'cost for {q3} {name.name} = {name.get_price(q3)}')
name.make_purchase(q3)
print(remaining stock: {name.amount}\n')
2) Write a class called Converter. The user will pass a length and a unit when
declaring an object from the class—for example, c = Converter (9, 'inches'). The
possible units are inches, feet, yards, miles, kilometers, meters, centimeters, and
millimeters. For each of these units there should be a method that returns the length
converted into those units. For example, using the Converter object created above,
the user could call c.feet() and should get 0.75 as the result.

You might also like