working my way through an online java video tutorial and running into some trouble. tutorial deals with the composite design pattern to which i have little experience. first two tutorials went well and the third is giving me issues. i've asked instructor/creator of tutorial for help but haven't heard back. coding example has to do with "unwrapping boxes" here is my code for each class/interface:
Gift:
package composite;
public class Gift implements SurpriseBox {
private String giftName;
public Gift(String name) {
this.giftName = name;
}
public void unWrap() {
System.out.println("Unwrapping gift: " + this.giftName);
}
}
SurpriseBox:
package composite;
public interface SurpriseBox {
void unWrap();
}
GiftSurpriseBox
package composite;
import java.util.ArrayList;
public class GiftSurpriseBox implements SurpriseBox {
private ArrayList<SurpriseBox> surpriseBoxes;
public GiftSurpriseBox() {
this.surpriseBoxes = new ArrayList<>();
}
public void add(SurpriseBox box){
this.surpriseBoxes.add(box);
}
public void remove(SurpriseBox box) {
this.surpriseBoxes.remove(box);
}
public void unWrap() {
surpriseBoxes.forEach(SurpriseBox::unWrap);
}
}
The SurpriseBox class particularly the last line of code is where I am running into issues
Client:
package composite;
public class Client {
public static void main(String[] args) {
GiftSurpriseBox giftBox = new GiftSurpriseBox();
GiftSurpriseBox subGiftBox = new GiftSurpriseBox();
Gift gift = new Gift("Toy Car");
Gift gift2 = new Gift("Toy Plane");
giftBox.add(gift);
subGiftBox.add(gift2);
giftBox.add(subGiftBox);
giftBox.unWrap();
}
}
I believe, with little understanding of the subject that the idea here is you are creating gifts and placing them inside boxes, the SurpriseBox has children boxes and the composite pattern allows when you call unwrap for each package inside the parents to be unwrapped.
Using ECLIPSE JUNO and getting the following error messages:
Syntax error on token(s), misplaced construct(s) Syntax error on token ":", EnhancedForStatementHeaderInit expected after this token
NOT SURE IF THIS HAS TO DO WITH MY JRE BECAUSE THE CREATOR OF THE TUTORIAL HAS GIVEN NO INFO ON HIS JRE/COMPILER