Coming from Java, you may be tempted to overload your enum with functionality:
// Java code
enum PizzaOrderStatus {
ORDER_RECEIVED,
PIZZA_BEING_MADE,
OUT_FOR_DELIVERY,
COMPLETED;
public PizzaOrderStatus nextStatus() {
switch (this) {
case ORDER_RECEIVED: return PIZZA_BEING_MADE;
case PIZZA_BEING_MADE: return OUT_FOR_DELIVERY;
case OUT_FOR_DELIVERY: return COMPLETED;
case COMPLETED:return COMPLETED;
}
}
}
Instead, you can use the sealed class:
sealed class PizzaOrderStatus(protected val orderId: Int) {
abstract fun nextStatus() : PizzaOrderStatus
class OrderReceived(orderId: Int) : PizzaOrderStatus(orderId) {
override fun nextStatus(): PizzaOrderStatus {
return PizzaBeingMade(orderId)
}
}
class PizzaBeingMade(orderId: Int) : PizzaOrderStatus(orderId...