我希望能够给一个特定的值取决于一定的要求,如以下年龄打折:> 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