Skip to content

Instantly share code, notes, and snippets.

@JeffML
Last active March 18, 2017 20:33
Show Gist options
  • Save JeffML/692baf9612f24d96490af536afb12602 to your computer and use it in GitHub Desktop.
Save JeffML/692baf9612f24d96490af536afb12602 to your computer and use it in GitHub Desktop.
BowlingBall, inner class
interface IBowlingBall {
String getPetName();
}
class Bowler {
static private class BowlingBall implements IBowlingBall{
private String petName;
BowlingBall() {
}
private void setPetName(String petName) {
this.petName = petName;
}
String getPetName() {
return this.petName;
}
}
private BowlingBall bowlingBall;
Bowler() {
this.bowlingBall = new BowlingBall();
}
void setPetName(String petName) {
if (petName ==~ /[A-Z][a-z\s]*/) {
bowlingBall.setPetName(petName);
} else {
throw Exception("only letters, please")
}
}
String getPetName() {
return bowlingBall.getPetName();
}
}
// test
Bowler bowler = new Bowler();
bowler.setPetName("Gutter Duster");
print bowler.getPetName()
bowler.setPetName("Pentium 386"); // exception thrown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment