-2

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

tgrim90
  • 339
  • 1
  • 2
  • 13
  • 1
    Please do you a favour and download the latest eclipse version (Eclipse Photon). Eclipse Juno was released in 2012 and didn't include support for the Java 8 features like lambdas and method references. – Thomas Kläger Sep 17 '18 at 21:11
  • does photon allow for Maven/Android dev as well? – tgrim90 Sep 17 '18 at 21:13
  • 1
    Maven: yes, Android: most probably not. Google switched the android development toolchain from Eclipse to AndroidStudio in 2015 and no longer supports the eclipse based toolchain. – Thomas Kläger Sep 17 '18 at 21:20
  • @ThomasKläger should I install for Java Developers or Java EE Developers – tgrim90 Sep 17 '18 at 21:20
  • 1
    If you are just beginning to learning Java the Java Developers edition is sufficient (and a smaller download). If you want to learn about web development with Java (web services, enterprise architecture etc) you can download the Java EE Developers edition (this contains the Java Developers edition and many more plugins) – Thomas Kläger Sep 17 '18 at 21:23
  • @ThomasKläger thanks, that fixed it – tgrim90 Sep 17 '18 at 21:44

1 Answers1

0

The issue was the ECLIPSE IDE I was using. I've updated to Photon and everything is working well.

tgrim90
  • 339
  • 1
  • 2
  • 13