You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
/**
* 百分比折扣策略,按照订单金额的百分比计算折扣
*/
package com.orderprocessing ;
public class PercentageDiscountStrategy implements DiscountStrategy {
private double percentage ;
/**
* 构造方法
* @param percentage 折扣百分比( 如0.1表示10%折扣)
*/
public PercentageDiscountStrategy ( double percentage ) {
if ( percentage < 0 | | percentage > 1 ) {
throw new IllegalArgumentException ( "折扣百分比必须在0到1之间" ) ;
}
this . percentage = percentage ;
}
@Override
public double calculateDiscount ( Order order ) {
if ( order = = null ) {
return 0 ;
}
return order . getTotalAmount ( ) * percentage ;
}
public double getPercentage ( ) {
return percentage ;
}
public void setPercentage ( double percentage ) {
if ( percentage < 0 | | percentage > 1 ) {
throw new IllegalArgumentException ( "折扣百分比必须在0到1之间" ) ;
}
this . percentage = percentage ;
}
}