Shreyas arya

Generation

code to explanationMon, 23 Jan 2023

public class Order { public enum State { PENDING, SUSPENDED, ACTIVE, CANCELLED, COMPLETED } Status state; public Order() { this.state = State.PENDING; } public void approve() { if (this.state == State.PENDING) { this.state = State.ACTIVE; } } public void suspend() { if (this.state == State.ACTIVE) { this.state = State.SUSPENDED; } } public void complete() { if (this.state == State.ACTIVE || this.state == State.SUSPENDED) { this.state = State.COMPLETED; } } public void cancel() { if (this.state == State.ACTIVE || this.state == State.PENDING) { this.state = State.CANCELLED; } }

The program shows how to write an enum State in Java and how to use the enum State to create a new object of type Order. The Order class's constructor sets the state to PENDING. The program also shows how to use enum State to write conditional statements like if () {} to control the state of Order.

Questions about programming?Chat with your personal AI assistant