Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

The above article we have a full understanding of the use of enumeration class, this article we will start from the actual requirements, simple implementation of a small case of order state conversion restrictions, for your reference!

demand

Order is an indispensable part of e-commerce projects, and the transformation of order status is also a problem we often discuss. We all know that the order status conversion is logical and can not be arbitrarily converted.

Example: If you want to buy something and you just add it to your cart, it should be unpaid. If a request is made to convert it to refund status, the system should throw a message “status conversion failed, please complete purchase first!”

Next we use enumerations to fulfill the order state transition constraints.

implementation

Enumeration class definition:

public enum OrderStatus{
    NO_PAY("Unpaid".0) {@Override
        public Boolean canChange(OrderStatus orderStatus) {
            switch (orderStatus){
                case PAY:
                    return true;
                default:
                    return false;
            }
        }
    },
    PAY("Paid".1) {@Override
        public Boolean canChange(OrderStatus orderStatus) {
            // Since the refund interface usually has latency, it will first change to the "refund in" state
            switch (orderStatus){
                case REFUNDING:
                    return true;
                default:
                    return false;
            }
        }
    },
    REFUNDING("Refund in progress".2) {@Override
        public Boolean canChange(OrderStatus orderStatus) {
            switch (orderStatus){
                case REFUNDED:
                case FAIL_REFUNDED:
                    return true;
                default:
                    return false;
            }
        }
    },
    REFUNDED("Refund successful".3),
    FAIL_REFUNDED("Refund failed".4),;private final String name;
    private final int status;

    private OrderStatus(String name,int status){
        this.name = name;
        this.status = status;
    }

    // Customize the conversion method
    public Boolean canChange(OrderStatus orderStatus){
        return false; }}Copy the code

Call method:

public class EnumTest {

    public static void main(String[] args) {
        Boolean aBoolean = OrderStatus.NO_PAY.canChange(OrderStatus.PAY);
        String statusStr = aBoolean?"Can":"不可以";
        System.out.println("Can state transition be completed:"+ statusStr);

        Boolean flag = OrderStatus.REFUNDED.canChange(OrderStatus.FAIL_REFUNDED);
        String flagStr = flag?"Can":"不可以";
        System.out.println("Can state transition be completed:"+ flagStr); }}Copy the code

Return result:

Thus we implement the restriction of order state transitions with enumeration classes. This example only provides an idea for state transition, and the specific process needs to be handled according to the business in your system.

If you have different opinions or better idea, welcome to contact AH Q, add AH Q can join the technical exchange group to participate in the discussion!