(三)Bill

#include<iostream>
#include<vector>
#include<string>

using namespace std;

class Item
{
    private:
        string name;
        double price;
    public:
        Item(string name, double price)
        {
            this->name = name;
            this->price= price;
        }
};

class PaymentMethod
{
    public:
        virtual void pay(int cents) = 0;
};
/*Cash,DebitCard和Item实现略,Item中getPrice()获取当前对象的价格*/

class Card : public PaymentMethod
{
    private:
        string name, num;
    
    public:
        Card(string name, string num)
        {
            this->name = name;
            this->num = num;
        }

        string toString()
        {
            return this->getType()+" card[name = "+ name + " , num = "+num+"]";
        }

        void pay(int cents)
        {
            cout << "Payed" << endl;
            this->executeTransaction(cents);
        }
    
    protected:
        virtual string getType() = 0;
        virtual void executeTransaction(int cents)=0;  //(1)
};

class CreditCard : public Card //(2)
{
    public:
        CreditCard(string name,string num):Card(name,num) //(3)
        {

        }
    protected:
        string getType()
        {
            return " CREDIT ";
        }

        void executeTransaction(int cents)
        {
            cout <<"Pay:"<< cents <<" CreditCard." <<endl;
        }
};

/*包含所有购买商品的账单*/
class Bill
{
    private:
        vector<Item*> items;

    public:
        void add(Item* item)
        {
            items.push_back(item);
        }

        /*计算所有item的总价格,代码略*/
        int getTotalPrice()
        {
            return 0;
        }

        void pay(PaymentMethod* paymentMethod)
        {
            paymentMethod->pay(getTotalPrice());  //(4)
        }
};

class PaymentSystem
{
    public:
        void pay()
        {
            Bill *bill = new Bill();
            Item *item1 = new Item("1234",10);
            Item *item2 = new Item("5678",40);
            bill->add(item1);
            bill->add(item2);

            bill->pay(new CreditCard("LISI","98765432101")); //信用卡支付  //(5)
        }
};

int main()
{
    PaymentSystem* payment =new PaymentSystem();  //(6)
    payment->pay();
    return 0;
}

答案:

(1) executeTransaction(int cents)

(2) : public Card

(3) :Card(name,num)

(4) paymentMethod->pay

(5) bill->pay

(6) PaymentSystem* payment

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容