鱼C论坛

 找回密码
 立即注册
查看: 2525|回复: 9

[已解决]求助,编写这个系统,运行结果相同(急)!

[复制链接]
匿名鱼油
匿名鱼油  发表于 2022-12-19 19:36:04 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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.)
最佳答案
2022-12-19 20:21:39
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}')
回复

使用道具 举报

发表于 2022-12-19 19:52:31 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2022-12-19 20:00:45 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2022-12-19 20:03:16 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2022-12-19 20:21:39 | 显示全部楼层    本楼为最佳答案   
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2022-12-19 20:39:47 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2022-12-19 21:31:56 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2022-12-19 21:32:28 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2022-12-20 17:58:49 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2022-12-20 18:09:33 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-25 07:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表