题目



试题六

阅读以下说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。

【说明】

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

image.png

【Java代码】 

Import java.util. ArrayList; 

import java.util.List; 

interface PaymentMethod {

public ( ) ;

}

// Cash、DebitCard和Item实现略,Item中getPrice( )获取当前物品对象的价格

abstract class Card ( ) {

private final String name, num;

public Card(String name, String num) {this.name = name; this.num = num; } 

@Oveiride

public String toString ( ) {

return String.format("%s card[name = %s, num = %s]",this.getType ( ), name, num);

}

@Override

public void pay(int cents) {

 

System.out.println("Payed " + cents + " cents using " + toString( )); 

this.executeTransaction(cents);

}

protected abstract String getType( );

protected abstract void executeTransaction(int cents);

}

class CreditCard ( ) {

public CreditCard(String name, String num) { ( ) ; }

@Override

protected String getType( ) { return "CREDIT"; }

@Override

protected void executeTransaction(int cents) {

System.out.println(cents + " paid using Credit Card. "');

}

 

class Bill {//包含所有购买商品的账单

private List<Item> items = new ArrayList<>( );

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

public intgetTotalPrice( ){/*计算所有 item 的总价格,代码略*/ }

public void pay(PaymentMethod paymentMethod){//用指定的支付方式完成支付

( ) (getTotalPrice( ));

}

}

public 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", "98765432101")); //信用卡支付

}

 

public static void main(String[ ] args) {

( ) = new PaymentSystem( ); 

payment.pay( );

}

}

作答
本题暂不支持做答,请点击“解析“以对比解题思路
答案/解析
查看试卷及答案