Last active
March 18, 2017 20:33
-
-
Save JeffML/692baf9612f24d96490af536afb12602 to your computer and use it in GitHub Desktop.
BowlingBall, inner class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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