66 lines
2.0 KiB
PHP
66 lines
2.0 KiB
PHP
<?php
|
||
|
||
namespace Common;
|
||
|
||
require_once __DIR__ . '/PaymentInterface.php';
|
||
|
||
/**
|
||
* 注文処理を行うクラス
|
||
* 決済方法をDI(依存性注入)で受け取る
|
||
*/
|
||
class OrderProcessor
|
||
{
|
||
/** @var PaymentInterface 決済方法 */
|
||
private $paymentMethod;
|
||
|
||
/**
|
||
* コンストラクタ
|
||
*
|
||
* @param PaymentInterface $paymentMethod 決済方法(インタフェース型)
|
||
*/
|
||
public function __construct(PaymentInterface $paymentMethod)
|
||
{
|
||
// TODO: 決済方法をプロパティに設定してください
|
||
// ポイント: この引数の型は PaymentInterface なので、
|
||
// CreditCardPayment や BankTransferPayment など、
|
||
// PaymentInterface を実装したどのクラスのインスタンスでも受け取れます。
|
||
// これが「依存性注入(DI)」と「多態性」の組み合わせです。
|
||
}
|
||
|
||
/**
|
||
* 注文を処理する
|
||
*
|
||
* @param int $amount 注文金額(円)
|
||
* @return void
|
||
*/
|
||
public function processOrder($amount)
|
||
{
|
||
// TODO: 以下の処理を実装してください
|
||
// 1. "注文処理を開始します" を表示
|
||
// 2. "決済方法: " に続けて、$this->paymentMethod->getPaymentMethodName() の結果を表示
|
||
// 3. "---" を表示(区切り線)
|
||
// 4. $this->paymentMethod->pay($amount) を呼び出して、戻り値を $result に格納
|
||
// 5. $result が true の場合:
|
||
// - "---" を表示
|
||
// - "✅ 注文が完了しました!" を表示
|
||
// 6. $result が false の場合:
|
||
// - "---" を表示
|
||
// - "❌ 決済に失敗しました。" を表示
|
||
// ※各行の最後に <br> を付けること
|
||
}
|
||
|
||
/**
|
||
* 決済方法を変更する
|
||
*
|
||
* @param PaymentInterface $paymentMethod 新しい決済方法
|
||
* @return void
|
||
*/
|
||
public function changePaymentMethod(PaymentInterface $paymentMethod)
|
||
{
|
||
// TODO: 以下の処理を実装してください
|
||
// 1. $paymentMethod をプロパティに設定
|
||
// 2. "決済方法を「" + 決済方法名 + "」に変更しました" を表示
|
||
// ※最後に <br> を付けること
|
||
}
|
||
}
|