2019年上半年程序员考试第六题目,解析

题目大意:

    现如今线下支付可以用现金(Cash)、移动支付、银行卡(Card)(信用卡(Creditcard)和储蓄卡(DebitCard))等多种支付方式(PaymentMethod)对物品(Item)账单(Bill)进行支付。 下图是某系统的列类图

解析代码:

#include<iostream>
#include<vector>
#include<string>
using namespace std;
class PaymentMethod {
public:
	virtual void pay(int cents)=0;
};


class Card: public PaymentMethod{
private: 
	string name;
		string 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 "<<cents<< "cents using "<<toString()<<endl;
		this->executeTransaction(cents);
	}
protected:
	virtual string getType()=0;
	virtual void executeTransaction(int cents)=0;//-------------------1[executeTransaction(int cents)]
};

class CreditCard : public Card{//-----------------------2[ : public Card]
public:
	CreditCard(string name, string num):Card(name, num){//-------------------3[:Card(name, num)]
	}
protected:
	string getType(){return "CREDIT";}
	void executeTransaction(int cents){
		cout<<cents<<"paid using "<< getType()<<"Card."<<endl;
	}
};

class Item{
public:
	Item(string name, int num){};//实现
};

class Bill{
private:
	vector <Item*> items;
public:
	void add(Item* item){items.push_back(item);}
	int getTotalPrice(){ return 8; /*计算所有item总价格*/}
	void pay(PaymentMethod* paymentMethod){
		paymentMethod->pay(getTotalPrice());//------------------4[paymentMethod->pay]
	}
};

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("LI SI","234566788856"));//---------------------------------------5[bill->pay]
	}
};

int main()
{
	PaymentSystem* payment = new PaymentSystem();//--------------------------6[PaymentSystem* payment = ]
	payment->pay();
	for(;;){};
	return 0;
}
运行结果:

Payed 8cents using CREDIT card[name=LI SI, num= 234566788856]
8paid using CREDITCard.

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值