Advertisements
Advertisements
प्रश्न
Define a class ITEMINFO in Python with the following description:
ICode (Item Code)
Item (Item Name)
Price (Price of each item)
Qty (quantity in stock)
Discount (Discount percentage on the item)
Netprice (Final Price)
Methods
→ A member function FindDisc( ) to calculate discount as per the following rule:
If Qty<=10 Discount is 0
If Qty (11 to 20) Discount is 15
If Qty>=20 Discount is 20
→ A constructor( __init__ method) to assign the value with 0 for ICode, Price, Qty, Netprice and Discount and null for Item respectively
→ A function Buy( ) to allow user to enter values for ICode, Item, Price, Qty and call function FindDisc( ) to calculate the discount and Netprice(Price*Qty-Discount).
→ A Function ShowAll( ) to allow user to view the content of all the data members.
Advertisements
उत्तर
Logic: find_disc() determines the discount percentage from quantity. buy() accepts item details, calls find_disc(), computes the discount amount and then calculates netprice.
Note: At quantity 20, the stated conditions overlap. The program correctly treats quantity 20 and above as eligible for 20% discount.
class ItemInfo:
def __init__(self):
# Initialise numeric members with 0 and Item with an empty string
self.icode = 0
self.item = ""
self.price = 0.0
self.qty = 0
self.discount = 0
self.netprice = 0.0
def find_disc(self):
# Determine discount percentage from quantity
if self.qty <= 10:
self.discount = 0
elif self.qty < 20:
self.discount = 15
else:
self.discount = 20
def buy(self):
# Accept item details
self.icode = int(input("Enter item code: "))
self.item = input("Enter item name: ")
self.price = float(input("Enter price per item: "))
self.qty = int(input("Enter quantity: "))
self.find_disc()
# Calculate net price after discount
total_price = self.price * self.qty
discount_amount = total_price * self.discount / 100
self.netprice = total_price - discount_amount
def show_all(self):
# Display all data members
print("Item Code:", self.icode)
print("Item Name:", self.item)
print("Price per Item:", self.price)
print("Quantity:", self.qty)
print("Discount (%):", self.discount)
print("Net Price:", self.netprice)
item1 = ItemInfo()
item1.buy()
item1.show_all()
For example, if price is 100, quantity is 20, the discount is 20% and net price is 1600.0.
