我希望能夠給一個特定的值取決於一定的要求,如以下年齡打折:> 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
}
我需要創建一個子類稱爲折扣或每種類型的優惠嗎?或者我可以直接寫入這個客戶類來控制折扣?
好吧有點混淆,所以你想我刪除setDiscount setter並使用getDiscount()並使用多個if語句爲它? @amadan – 2012-01-03 22:27:47