Skip to content

Instantly share code, notes, and snippets.

@ShiftedClock
Created September 22, 2019 08:45
Show Gist options
  • Select an option

  • Save ShiftedClock/e1b676dfd5eaa9f478ccc3f3f11b127c to your computer and use it in GitHub Desktop.

Select an option

Save ShiftedClock/e1b676dfd5eaa9f478ccc3f3f11b127c to your computer and use it in GitHub Desktop.
Integer that can only be incremented and decremented, and loops around
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