Solution4: state programming