Reputation: 111
I have parent class CompositeDiscount. It has a linkedlist to store some dicount. It has two child class BestForVersityStrategy and BestForStudentStrategy. I populate parent linked list in another class Registration. When I try to iterate LL from UI it gives the discount list but when i call it from child (i.e BestForVersity) it shows the list is empty.
package registrationui;
import java.util.LinkedList;
public class CompositeDiscount implements IDiscountStrategy{
LinkedList<IDiscountStrategy> disList;
public CompositeDiscount(){
disList= new LinkedList<>();
}
@Override
public int getDiscount(Registration reg) {
return 0;
}
public void addDiscount(IDiscountStrategy ids){
disList.add(ids);
}
}
this is parent class
package registrationui;
public class BestForStudentStrategy extends CompositeDiscount{
public int getDisocunt(Registration reg){
int amount=0;
for(IDiscountStrategy ids:disList){
if(ids.getDiscount(reg)>amount){
amount=ids.getDiscount(reg);
}
}
return amount;
}
}
this is child class
private void academicActionPerformed(java.awt.event.ActionEvent evt) {
if(academic.isSelected())
rcc.addCompositeDiscount(rcc.getDiscountPloicy("registrationui.AcademicExcellenceDiscount"));
else{
for(IDiscountStrategy ids:rcc.getCompositeDisocunt().disList)
{
if(ids.getClass().toString().equals("class registrationui.AcademicExcellenceDiscount"))
rcc.getCompositeDisocunt().disList.remove(ids);
}
}
}
UI from where it id populated via controller that call Resitration class
public void addCompositeDiscount(IDiscountStrategy ids){
cds.addDiscount(ids);
bfn=new BestForNsuStrategy();
bfs=new BestForStudentStrategy();
}
public IDiscountStrategy getDiscountPolicy(String policy){
try {
try {
ids=(IDiscountStrategy)Class.forName(policy).newInstance();
} catch (InstantiationException ex) {
Logger.getLogger(Registration.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(Registration.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(Registration.class.getName()).log(Level.SEVERE, null, ex);
}
return ids;
}
Upvotes: 0
Views: 138
Reputation:
public static LinkedList<IDiscountStrategy> disList;
you must user public modifier for static
Upvotes: 0
Reputation: 201
Your linked list in CompositeDiscount is static. Static variables in Java are not inherited, they exist only in the class which declares them
Upvotes: 1