We extend the result of our earlier study [Angoshtari, Bayraktar, and Young; Optimal consumption under a habit-formation constraint, available at: arXiv:2012.02277, (2020)] to a market setup that includes a risky asset whose price process is a geometric Brownian motion. We formulate an infinite-horizon optimal investment and consumption problem, in which an individual forms a habit based on the exponentially weighted average of her past consumption rate, and in which she invests in a Black-Scholes market. The novelty of our model is in specifying habit formation through a constraint rather than the common approach via the objective function. Specifically, the individual is constrained to consume at a rate higher than a certain proportion $alpha$ of her consumption habit. Our habit-formation model allows for both addictive ($alpha=1$) and nonaddictive ($0<alpha<1$) habits. The optimal investment and consumption policies are derived explicitly in terms of the solution of a system of differential equations with free boundaries, which is analyzed in detail. If the wealth-to-habit ratio is below (resp. above) a critical level $x^*$, the individual consumes at (resp. above) the minimum rate and invests more (resp. less) aggressively in the risky asset. Numerical results show that the addictive habit formation requires significantly more wealth to support the same consumption rate compared to a moderately nonaddictive habit. Furthermore, an individual with a more addictive habit invests less in the risky asset compared to an individual with a less addictive habit but with the same wealth-to-habit ratio and risk aversion, which provides an explanation for the equity-premium puzzle.