Message in a Bottle #SongsInCode

Sting

This afternoon I did something extraordinarily productive and created an extended #SongsInCode rendition of Message in a Bottle that compiles. Now all somebody in this distant future needs to do is create a new class to fetch Sting's bottle and read his message!

Sting.java

public class Sting {
  public static void main(String[] args) {
    Bottle oceanBottle = new Bottle("SOS!");
  }
}

Bottle.java

public class Bottle {
  String message = "";
  boolean opened = false;

  public Bottle(String message) {
    this.message = message;
  }

  public void pullCork() {
    opened = true;
  }

  public String getMessage() throws NotOpenException {
    if (opened)
      return message;
    else
      throw new NotOpenException();
  }
}

NotOpenException.java

public class NotOpenException extends Throwable {
  public NotOpenException() {
    super("You haven't opened the bottle yet!");
  }
}

Imprint

This is one of about 5000 posts on Rubénerd. View the home page for the latest, or related posts also tagged with:

If you liked this post, feel free to buy me a coffee, leave me a comment on Twitter, or email me at weblog2017@rubenschade.com. Thanks :).