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.
homework/OrderValidator.java

62 lines
1.8 KiB

This file contains ambiguous Unicode characters!

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.

public class OrderValidator {
private static final double MIN_ORDER_AMOUNT = 0.01;
private static final double MAX_ORDER_AMOUNT = 1000000.0;
/**
* 验证订单是否有效
* @param order 要验证的订单
* @return 如果订单有效则返回true否则返回false
*/
public boolean validateOrder(Order order) {
if (order == null) {
return false;
}
// 验证订单ID
if (order.getOrderId() == null || order.getOrderId().trim().isEmpty()) {
return false;
}
// 验证订单金额
double amount = order.getAmount();
if (amount < MIN_ORDER_AMOUNT || amount > MAX_ORDER_AMOUNT) {
return false;
}
// 验证客户名称
if (order.getCustomerName() == null || order.getCustomerName().trim().isEmpty()) {
return false;
}
return true;
}
/**
* 获取订单验证失败的具体原因
* @param order 要验证的订单
* @return 验证失败的原因如果订单有效则返回null
*/
public String getValidationError(Order order) {
if (order == null) {
return "订单对象为空";
}
if (order.getOrderId() == null || order.getOrderId().trim().isEmpty()) {
return "订单ID不能为空";
}
double amount = order.getAmount();
if (amount < MIN_ORDER_AMOUNT) {
return "订单金额不能小于最小金额";
}
if (amount > MAX_ORDER_AMOUNT) {
return "订单金额不能超过最大金额";
}
if (order.getCustomerName() == null || order.getCustomerName().trim().isEmpty()) {
return "客户名称不能为空";
}
return null; // 订单有效
}
}