Created
September 22, 2019 08:45
-
-
Save ShiftedClock/e1b676dfd5eaa9f478ccc3f3f11b127c to your computer and use it in GitHub Desktop.
Integer that can only be incremented and decremented, and loops around
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
| class LoopyIndex { | |
| private int basement; | |
| private int ceiling; | |
| private int value; | |
| private int initialValue; | |
| public LoopyIndex (int basement, int ceiling, int initialValue) { | |
| this.basement = basement; | |
| this.ceiling = ceiling; | |
| //TODO: Error checking to make sure initialValue is between basement and ceiling. | |
| this.value = initialValue; | |
| } | |
| public void Increment () { | |
| if (this.value == this.ceiling) { | |
| this.value = this.basement; | |
| } else { | |
| this.value++; | |
| } | |
| } | |
| public void Decrement () { | |
| if (this.value == this.basement) { | |
| this.value = this.ceiling; | |
| } else { | |
| this.value--; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment