鱼C论坛

 找回密码
 立即注册
查看: 2936|回复: 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
  1. class Item:
  2.     def __init__(self, name, price):
  3.         self.name = name
  4.         self.price = price
  5.         self.origin = None
  6.     def getter(self, getName = False, getPrice = False, getOrigin = False):
  7.         if getName:
  8.             return self.name
  9.         if getPrice:
  10.             return self.price
  11.         if getOrigin:
  12.             return self.origin
  13.     def setter(self, setName = None, setPrice = None, setOrigin = None):
  14.         if setName is not None:
  15.             self.name = setName
  16.         if setPrice is not None:
  17.             self.price = setPrice
  18.         if setOrigin is not None:
  19.             self.origin = setOrigin
  20.     def print_out(self):
  21.         print(f'name: {self.getter(getName = True)}  price: {self.getter(getPrice = True)}  origin: {self.getter(getOrigin = True)}', end = ' ')

  22. bill = []
  23. while input('Add item to invoice("YES" to continue, others to stop)?') == 'YES':
  24.     name = input('Input name:')
  25.     price = input('Input price:')
  26.     origin = input('Input origin:')
  27.     item = Item(name, price)
  28.     item.setter(setOrigin = origin)
  29.     bill.append(item)
  30. for i in bill:
  31.     i.print_out()
  32.     print()
  33. x = sum([int(i.price) for i in bill])
  34. print(f'Total price: {x}')
复制代码
回复

使用道具 举报

发表于 2022-12-19 19:52:31 | 显示全部楼层
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

发表于 2022-12-19 20:00:45 | 显示全部楼层
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

发表于 2022-12-19 20:03:16 | 显示全部楼层
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

发表于 2022-12-19 20:21:39 | 显示全部楼层    本楼为最佳答案   
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

发表于 2022-12-19 20:39:47 | 显示全部楼层
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

发表于 2022-12-19 21:31:56 | 显示全部楼层
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

发表于 2022-12-19 21:32:28 | 显示全部楼层
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

发表于 2022-12-20 17:58:49 | 显示全部楼层
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

发表于 2022-12-20 18:09:33 | 显示全部楼层
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 13:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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