2012-01-03 31 views
2

我希望能够给一个特定的值取决于一定的要求,如以下年龄打折:> 25和职业=老师/教授获得10%的折扣,<年龄25和gradepoint> 7得到25%的折扣我将如何使用if语句来更改属性的值?

这是到目前为止我的代码我使用双OO范式:

public class customer { 

    //attribute definitions 
    private String name; 
    private String address; 
    private String profession; 
    private Integer age; 
    private Integer gradepoint; 
    private double discount; 

    //constructor 
    public customer(String newName, String newAddress, String newProfession, Integer newAge, Integer newGradepoint, double newDiscount) 
    { 
     setName(newName); 
     setAddress(newAddress); 
     setProfession(newProfession); 
     setAge(newAge); 
     setGradepoint(newGradepoint); 
     setDiscount (newDiscount); 
    } 

    //getters 
    public String getName() 
    { return name;} 
    public String getAddress() 
    { return address;} 
    public String getProfession() 
    { return profession;} 
    public Integer getAge() 
    { return age;} 
    public Integer getGradepoint() 
    { return gradepoint;} 
    public double getDiscount() 
    { return discount;} 

    //setters 
    public void setName (String newName) 
    { name = newName;} 
    public void setAddress (String newAddress) 
    { address = newAddress;} 
    public void setProfession (String newProfession) 
    { profession = newProfession;} 
    public void setAge (Integer newAge) 
    { age = newAge;} 
    public void setGradepoint (Integer newGradepoint) 
    { gradepoint = newGradepoint;} 
    public void setDiscount (double newDiscount) 
    { discount = newDiscount;} 

    //methods 


} 

我需要创建一个子类称为折扣或每种类型的优惠吗?或者我可以直接写入这个客户类来控制折扣?

回答

3

直接写一个方法到这个客户类控制折扣?

这个。使其成为计算的字段。杀setDiscount功能,杀死discount变量,使getDiscount功能到是这样的:

public double getDiscount() { 
    if (...) return ...; 
    if (....) return ...; 
    ... 
} 

...除非你想有这样的作为默认折扣,仍然允许修改,在这种情况下保持discount作为属性,并将此整个逻辑移入构造函数中,并调用条件setDiscount()

+0

好吧有点混淆,所以你想我删除setDiscount setter并使用getDiscount()并使用多个if语句为它? @amadan – 2012-01-03 22:27:47

1

您的getDiscount函数将理想地执行计算并为当前对象返回适当的折扣。例如:

public double getDiscount() 
{ 
    if (getAge() < 25 && getGradepoint() > 7) 
    { 
     return .25; 
    } 
    else if // other logic... 
} 
+0

我该如何做getAge> 25和getProfession ='Teacher',因为它不适用'&&'操作数,因为我认为它是一个String值。任何想法? @jknupp – 2012-01-03 22:37:55

0

可能随着时间的推移,不同的规则会发展。在折扣发生的地点,在订单中,折扣和对所用规则的引用应该一起存储。 这种业务逻辑可以有它自己的类。通用的解决方案甚至可以将规则存储为脚本代码(BeanShell = Java或JavaScript)并使用Java的脚本API。因此,这种业务逻辑更多地与业务经理驻留在一起,并且可以呈现和编辑规则。

1

虽然不是最简单的解决方案,但我会将折扣计算抽象为单独的界面和类,并且在客户对象中具有覆盖折扣值。

E.g.

public interface DiscountManager<T> 
{ 
    public double getDiscount(T discountObject); 
} 

public abstract class AbstractCustomerDiscountManager extends DiscountManager<Customer> 
{ 
    public double getDiscount(Customer customer) 
    { 
    if (customer.hasCustomDiscount()) { return customer.getDiscount(); } 
    else { return calculateDiscount(customer); } 
    } 

    public abstract double calculateDiscount(Customer customer); 
} 

public class DefaultDiscountManager extends AbstractCustomerDiscountManager 
{ 
    public double calculateDiscount(Customer customer) 
    { 
    double discount = 0; 
    if ((customer.getAge() != null) && (customer.getAge() < 25)) { discount += 25; } 
    ... 
    return discount; 
    } 
}