马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 匿名 于 2022-12-20 18:40 编辑
Simulate one invoice system
1.Implement a class called Item with the following specification:
An attribute/field called name to store the name of the item
An attribute/field called price that stores the price in pounds
An attribute called origin that stores the place of the item’s origin
A constructor with two parameters (the name of the item and the price) that initialises origin to null
Getter methods (getName, getPrice, getOrigin) and setter methods (setName, setPrice, setOrigin) for the three attributes above
A method to display the name, price and origin of the item.
2.Create in the main program a list called bill that stores items
3.Write code that asks the user to input the name, price and origin of item then create instance of the class Item and add them to list bill until user input other characters (not “YES”). When the user enters "YES", the program can continue adding item.
4.Print an invoice by displaying the items bought with their prices and the total payment.
For example:
RESTART:F:\teach\2022Problem Solving and Programming\asse
Add item to invoice("YES" to continue, others to stop)? YES
Input name:a
Input price:1
Input origin:aa
Add item to invoice("YES"to continue, others to stop)? YES
Input name:b
Input price:2
Input origin:bb
Add item to invoice("YES" to continue, others to stop)?YES
Input name:c
Input price:3
Input origin:cc
Add item to invoice("YES" to continue, others to stop)? stop
name: a price: 1.0 origin: aa
name: b price: 2.0 origin: bb
name: c price: 3.0 origin: cc Total price: 6.0
>>>
(Hint: Do not access the attributes of the class directly, please use getter methods.)
class Item:
def __init__(self, name, price):
self.name = name
self.price = price
self.origin = None
def getter(self, getName = False, getPrice = False, getOrigin = False):
if getName:
return self.name
if getPrice:
return self.price
if getOrigin:
return self.origin
def setter(self, setName = None, setPrice = None, setOrigin = None):
if setName is not None:
self.name = setName
if setPrice is not None:
self.price = setPrice
if setOrigin is not None:
self.origin = setOrigin
def print_out(self):
print(f'name: {self.getter(getName = True)} price: {self.getter(getPrice = True)} origin: {self.getter(getOrigin = True)}', end = ' ')
bill = []
while input('Add item to invoice("YES" to continue, others to stop)?') == 'YES':
name = input('Input name:')
price = input('Input price:')
origin = input('Input origin:')
item = Item(name, price)
item.setter(setOrigin = origin)
bill.append(item)
for i in bill:
i.print_out()
print()
x = sum([int(i.price) for i in bill])
print(f'Total price: {x}')
|