# Enums

Enums (Enumerations)

#### What is an Enum?

An **enum** (short for *enumeration*) is a special Java type used to define a collection of named constants. Enums are used when you have a fixed set of related values that a variable can hold, making your code more readable and less error-prone.

***

#### Why Use Enums?

* **Readability**: Instead of using arbitrary numbers or strings to represent states or modes, enums give meaningful names.
* **Type Safety**: Enums restrict values to a predefined set, preventing invalid assignments.
* **Maintainability**: If you need to add or modify states, you update the enum in one place.
* **Better Code Structure**: Enums can have methods and fields, allowing richer behavior.

***

#### Simple Example

Suppose you have a shooter subsystem with different states:

```java
public enum ShooterState {
    IDLE,
    SPINNING_UP,
    SHOOTING,
    COOLING_DOWN
}
```

You can now use this enum to represent the current state:

```java
ShooterState currentState = ShooterState.IDLE;

if (currentState == ShooterState.SPINNING_UP) {
    // Run motors to spin up the shooter
}
```

This is much clearer than using integers like 0, 1, 2, or strings like `"idle"` which are prone to typos.

***

#### Enums with Data and Methods

Enums can also have variables, constructors, and methods. For example:

```java
public enum ShooterState {
    IDLE(0),
    SPINNING_UP(5000),  // RPM target
    SHOOTING(6000),
    COOLING_DOWN(0);

    private final int targetRPM;

    ShooterState(int rpm) {
        this.targetRPM = rpm;
    }

    public int getTargetRPM() {
        return targetRPM;
    }
}
```

You can then access the associated value:

```java
int rpm = ShooterState.SPINNING_UP.getTargetRPM();  // rpm = 5000
```

This allows your enum to not only represent a state but also store related parameters.

***

#### Enums in Subsystem State Machines

Enums are ideal to manage subsystem states clearly and reliably. For example, an intake roller subsystem might have:

```java
public enum IntakeRollerState {
    OFF,
    INTAKING,
    EXHAUSTING
}
```

Your code can then switch behavior based on the state, like:

```java
switch (currentState) {
    case OFF:
        stopMotors();
        break;
    case INTAKING:
        runIntakeMotors();
        break;
    case EXHAUSTING:
        reverseIntakeMotors();
        break;
}
```

***

#### Summary

* Enums represent a **fixed set of related constants**.
* They improve **code readability**, **safety**, and **maintainability**.
* Can store **associated data** and **methods**.
* Perfect for managing **states** and **modes** in robot code.
