2

创建可与 RetailItem 类一起使用的 CashRegister 类。CashRegister 类应该能够在内部保留一个 RetailItem 对象列表。该类应具有以下方法:

• 一个名为 purchase_item 的方法,它接受一个 RetailItem 对象作为参数。每次调用 purchase_item 方法时,应将作为参数传递的 RetailItem 对象添加到列表中。• 方法名称get_total,它返回存储在CashRegister 对象的内部列表中的所有RetailItem 对象的总价。• 方法名称show_items,它显示有关存储在CashRegister 对象的内部列表中的RetailItem 对象的数据。• 一个名为 clear 的方法,它应该清除 CashRegister 对象的内部列表。

在一个程序中演示 CashRegister 类,该类允许用户选择多个要购买的项目。当用户准备结账时,程序应该显示他或她选择购买的所有物品的清单,以及总价。

class RetailItem:

    # The __init__ method initializes the attributes.
    def __init__(self, item, units, price):
        self.__item = item
        self.__units = units
        self.__price = price

    # The following methods are mutators for the class.
    def set_item(self, item):
        self.__item = item
    def set_units(self, units):
        self.__units = units
    def set_price(self, price):
        self.__price = price

    # The following methods are accessors for the class.
    def get_item(self):
        return self.__item
    def get_units(self):
        return self.__units
    def get_price(self):
        return self.__price

    # The __str__ method returns the object's state as a
    #   string.
    def __str__(self):
        return 'Item Description:' + self.__item, \
              '\tNumber of Units:' + self.__units, \
              '\tPrice: $' + self.__price

    # The decrementInventory function decreases the number
    #   units each time called.
    def decrementInventory(units):
        if units > 0:
            units -= 1
        else:
            units = 0
# EOF

我相信这个 RetailItem 类是正确的,没有必要

class CashRegister:

    # The __init__ method initializes the attributes.
    def __init__(self, purchase_item):
        self.__purchase_item = []

    def purchase_item(item, units):
        purchase_item.append(item)
        decrementInventory(units)

    def get_total(price):
        total += price
        return total

    def show_items(item_list):
        for item in item_list:
            print(item.get_item())
            print(item.get_units())
            print(item.get_price())
            print()

    def clear():
        purchase_list [:] = []
# EOF

我不确定我是否根据要求正确编码。
请求。CashRegister 对象有一个属性,该属性初始化为一个空列表,并编写前面提到的其他四个方法。对于程序:main方法,创建3个零售对象,然后为用户初始化一个CashRegister对象。编写一个方法来显示创建的零售商品的信息(调用str方法)。

下面的代码是我开始使用的,我在使用 CashRegister 类的四个方法时遇到了问题。

import retail_item
import cash_register

SHOW = 1
PURCHASE = 2
CART = 3
TOTAL = 4
EMPTY = 5
QUIT = 6

def main():
    mylist = make_list()
    mycashregister = cash_register.CashRegister(mylist)

    choice = 0

    # Process menu selections until user quits program.
    while choice != QUIT:
        # Get the user's menu choice.
        choice = get_menu_choice()
        # Proces the choice.
        if choice == SHOW:
            __str__(item)
        elif choice == PURCHASE:
            purchase()
        elif choice == CART:
            cart(mycashregister)
        elif choice == TOTAL:
            total(mycashregister)
        elif choice == EMPTY:
            empty(mycashregister)

def make_list():
    item_list = []

    item = 'Jacket'
    units = 12
    price = 59.95
    entry = retail_item.RetailItem(item, units, price)
    item_list.append(entry)

    item = 'Jeans'
    units = 40
    price = 34.95
    entry = retail_item.RetailItem(item, units, price)
    item_list.append(entry)

    item = 'Shirt'
    units = 20
    price = 24.95
    entry = retail_item.RetailItem(item, units, price)
    item_list.append(entry)

    return item_list

# The get_menu_choice function displays the menu and gets
#   a validated choice from the user.
def get_menu_choice():
    print()
    print('CASH REGISTER MENU')
    print('-------------------------')
    print('1. Show Retial Items')
    print('2. Purchase an Item(s)')
    print('3. Show Current Shopping Cart')
    print('4. Show Total of Items Purchased')
    print('5. Empty Your Shopping Cart')
    print('6. Quit the program')
    print()

    # Get the user's choice.
    choice = int(input('Enter your choice: '))

    # Validate the choice.
    while choice < SHOW or choice > QUIT:
        choice = int(input('Enter a valid choice: '))

    # Return the user's choice.
    return choice

def display_list(item_list):
    for item in item_list:
        print('Item Description:', item.get_item(), \
              '\tNumber of Units:', item.get_units(), \
              '\tPrice:', item.get_price())
    print()

def purchase():
    item_purchase = input('Enter the item you wish to purchase: ')

main()
#not EOF more to come if I can figure out the first parts.

任何帮助,将不胜感激。

4

0 回答 0