美文网首页
(三)Bill

(三)Bill

作者: GoodTekken | 来源:发表于2023-03-24 08:47 被阅读0次
#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

相关文章

  • Day77 提单

    一.三种提单 提单三种形式:OCEAN BILL OF LADING 正本提单,OCEAN BILL OF LAD...

  • 【百天聆听】第16天 原典英语训练教材

    初级故事 Pecos Bill Part 1: Bill and the Coyotes Pecos Bill w...

  • To:Bill

    我从你的小镇经过 在烟雨蒙蒙的三月里 你家门前的那条小溪 静静流淌的溪水 似乎还在沉睡 但我 闻到了河岸上 第一朵...

  • Bill

    1.支付宝账单 月份 -----金额---月度金额 支付宝结算: 31551 2.微信账单 月份-----金额-...

  • Beating Nature at Its Own Game 青

    Bill Gates,Co-chair, Bill & Melinda Gates Foundation 比尔·盖...

  • 巴菲特和比尔盖茨都推崇的成功特质,只有2个字

    Shortly after I met Bill Gates, Bill's dad asked each of ...

  • Pig-Bill

    I t's a little funny Bull. Bill is a big pig. Bill sits ...

  • 首富的书单

    Netflix制作的《解码比尔·盖茨|Inside Bill's Brain: Decoding Bill Gat...

  • Bill·Manager

    是否在寻找轻松上手,最清晰的记账软件? 记账不需要复杂的操作。 帮你跟踪日常的金钱流向,随时查看历史交易。 产品功...

  • Bill List

    Bill List- the fastest way to record the daily cost. Thro...

网友评论

      本文标题:(三)Bill

      本文链接:https://www.haomeiwen.com/subject/qspwrdtx.html