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

题目大意:

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

2019年上半年程序员考试第六题目,解析_第1张图片

解析代码:

#include
#include
#include
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 "<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< 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.

 

你可能感兴趣的:(2019年上半年程序员考试第六题目,解析)