2012-01-03 69 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; 
    } 
}