- #36
Nono713
Gold Member
MHB
- 618
- 4
JaAnTr said:Ok, I've got the first line giving me no errors but the second one still is:
Code:Customer customer = booking.getCustomer(); Discount discount = customer.getDiscount();
I can see on my UML diagram that the Discount class is connected to the Customer class by aggregation. So does this mean that the Discount class needs a Customer passed to it in it's constructor and then a getDiscount method or is it the other way around? Because the Discount class is just an interface that looks like this and I know interfaces can't have constructors so how do I connect it to the customer?
Code:import java.math.BigDecimal; import java.util.Calendar; import java.util.Date;public interface Discount { public BigDecimal getDiscount(Date date); }
You are right that the Discount is an interface. However, the classes derived from Discount (such as CorporateDiscount) are not, so these are the ones you attach to the Customer. If the customer has no discount, then it just stays null, otherwise [say, in the CorporateCustomer's constructor] you can create a CorporateDiscount and assign it to that customer's discount field (so that getDiscount() returns that).
(you can assign a CorporateDiscount to a Discount because a CorporateDiscount "is" a Discount, i.e. it inherits from it - this is what allows polymorphism)