Pythonオブジェクト指向演習:ECプラットフォーム設計と注文管理システム

演習1:ECプラットフォームの商品クラス設計

ECプラットフォームを開発していると仮定し、商品情報を表す親クラス(Product)と、それぞれ書籍と衣類商品を表す2つの子クラス(Book、Clothing)を設計する必要があります。オブジェクト指向の思想とポリモーフィズムを使用して、これら3つのクラスを設計および実装してください。

要件:

  1. 親クラス(Product)は以下の属性とメソッドを持つ:
    • 属性:商品名(item_name)、価格(item_price)
    • メソッド:商品名の取得(fetch_name())、価格の取得(fetch_price())
  2. 子クラス(Book、Clothing)は親クラスを継承し、それぞれ固有の属性とメソッドを持つ:
    • Bookクラス固有の属性:著者(writer)、出版社(publishing_company)
    • Clothingクラス固有の属性:サイズ(garment_size)、色(shade)
  3. ECプラットフォームに『Python入門ガイド』(item_name = "Python入門ガイド")、価格50元、著者"田中"、出版社"B出版"という書籍と、「スタンダードTシャツ」(item_name = "スタンダードTシャツ")、価格100元、サイズ"L"、色"青"という衣類があると仮定します。上記のクラスを設計・実装し、以下の情報を出力するメインプログラムを作成してください:
    • 『Python入門ガイド』の名前、価格、著者、出版社
    • 「スタンダードTシャツ」の名前、価格、サイズ、色

参考として、以下の方法でインスタンスを作成します:

python_guide = Book("Python入門ガイド", 50, "田中", "B出版")
standard_tshirt = Clothing("スタンダードTシャツ", 100, "L", "青")
class Product:
    def __init__(self, item_name, item_price):
        self.item_name = item_name
        self.item_price = item_price
    
    def fetch_name(self):
        return self.item_name
    
    def fetch_price(self):
        return self.item_price

class Book(Product):
    def __init__(self, item_name, item_price, writer, publishing_company):
        super().__init__(item_name, item_price)
        self.writer = writer
        self.publishing_company = publishing_company
    
    def fetch_author(self):
        return self.writer
    
    def fetch_publisher(self):
        return self.publishing_company

class Clothing(Product):
    def __init__(self, item_name, item_price, garment_size, shade):
        super().__init__(item_name, item_price)
        self.garment_size = garment_size
        self.shade = shade
    
    def fetch_size(self):
        return self.garment_size
    
    def fetch_color(self):
        return self.shade

python_guide = Book("Python入門ガイド", 50, "田中", "B出版")
standard_tshirt = Clothing("スタンダードTシャツ", 100, "L", "青")
print(python_guide.fetch_name())
print(python_guide.fetch_price())
print(python_guide.fetch_author())
print(python_guide.fetch_publisher())
print('<------------------------------------------>')
print(standard_tshirt.fetch_name())
print(standard_tshirt.fetch_price())
print(standard_tshirt.fetch_size())
print(standard_tshirt.fetch_color())

演習2:注文決済システムと在庫管理システムの設計

ECプラットフォームの注文決済システムと在庫管理システムを設計していると仮定します。ユーザーが注文する際に支払い方法を選択する必要があり、対応する商品の在庫数量がそれに応じて減少します。これらのシステムのクラスと関連操作を設計・実装し、関数の実装(# TODOと記述されている部分)と不足しているパラメータを補完してください。

要件:

  1. 決済クラス(Payment)は以下の属性とメソッドを持つ:
    • メソッド:支払い(process_payment(amount))
    • メソッド:返金(process_refund(transaction_id))
  2. 在庫管理クラス(StockManagement)は以下の属性とメソッドを持つ:
    • 属性:在庫数量(available_stock)
    • メソッド:在庫更新(modify_stock(stock_change))
  3. 注文クラス(PurchaseOrder)は以下の属性とメソッドを持つ:
    • 属性:注文ID(transaction_id)と商品数量(item_quantity)
    • メソッド:注文作成(create_order(payment_method, stock_system))、各商品の単価は100元と仮定
  4. 決済オブジェクト、在庫管理オブジェクト、注文オブジェクトを作成し、以下のシナリオをシミュレートするメインプログラムを作成してください:
    • ユーザーがクレジットカードで200元を支払う
    • ユーザーが電子マネーで100元を支払う
    • それぞれの支払結果と在庫管理オブジェクトの最新在庫数量を出力する

ユーザーがクレジットカードで200元を支払い、2個の商品を購入した場合

支払結果: クレジットカードで200元の支払いが成功しました!

現在の在庫数量: 8

ユーザーが電子マネーで100元を支払い、1個の商品を購入した場合

支払結果: 電子マネーで100元の支払いが成功しました!

現在の在庫数量: 7

class Payment:
    def process_payment(self, amount):
        pass
    
    def process_refund(self, transaction_id):
        pass

class CreditCard(Payment):
    def process_payment(self, amount):
        return f"クレジットカードで{amount}元の支払いが成功しました!"
    
    def process_refund(self, transaction_id):
        return f"クレジットカードで注文{transaction_id}の返金が成功しました!"

class DigitalWallet(Payment):
    def process_payment(self, amount):
        return f"電子マネーで{amount}元の支払いが成功しました!"
    
    def process_refund(self, transaction_id):
        return f"電子マネーで注文{transaction_id}の返金が成功しました!"

class StockManagement:
    def __init__(self, available_stock):
        self.available_stock = available_stock
    
    def modify_stock(self, stock_change):
        self.available_stock -= stock_change

class PurchaseOrder:
    def __init__(self, transaction_id, item_quantity):
        self.transaction_id = transaction_id
        self.item_quantity = item_quantity
    
    def create_order(self, payment_method, stock_system):
        total_amount = self.item_quantity * 100  # 各商品の単価は100元と仮定
        payment_result = payment_method.process_payment(total_amount)
        stock_system.modify_stock(self.item_quantity)
        return payment_result

タグ: Python オブジェクト指向 クラス設計 ポリモーフィズム ECプラットフォーム

7月23日 17:37 投稿