Jatinder Singh
Jatinder Singh

Reputation: 317

how would I use an if statment to change the value of an attribute?

I want to be able to give a specific value to discount depending on certain requirements like the following age: > 25 and profession = teacher / professor get 10% discount, age < 25 and gradepoint > 7 get 25% discount

this is my code so far I am using double OO paradigm:

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


}

Would I need to create a sub class called discount or each type of discount? or I can write a method directly into this customer class to control the discount?

Upvotes: 2

Views: 109

Answers (4)

Trevor Freeman
Trevor Freeman

Reputation: 7232

Although not the simplest solution, I would abstract the discount calculation to a separate interface and class as well as having an override discount value in the customer object.

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;
  }
}

Upvotes: 1

Joop Eggen
Joop Eggen

Reputation: 109567

Probably over time different rules evolve. At the spot where the discounting takes place, in the order, the discount and and a reference to the rule applied should be stored together. This kind of business logic could have its own class. A generic solution would even be to store the rule as scriptable code (BeanShell = Java, or JavaScript) and use java's scripting API. So that this kind of business logic resides more with the business managers, and the rules can be presented and edited.

Upvotes: 0

Amadan
Amadan

Reputation: 198334

write a method directly into this customer class to control the discount?

This. Make it a calculated field. Kill setDiscount function, kill discount variable, and make the getDiscount function into something like:

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

...unless you want to have this as the default discount, and still allow modification, in which case keep discount as a property, and move this whole logic into the constructor, having conditional setDiscount() calls.

Upvotes: 3

jeffknupp
jeffknupp

Reputation: 6274

Your getDiscount function would ideally do the calculation and return the appropriate discount for the current object. For example:

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

Upvotes: 1

Related Questions