Skip to content

Instantly share code, notes, and snippets.

@luisjunco
Last active November 17, 2025 10:36
Show Gist options
  • Select an option

  • Save luisjunco/c7ea4d03c6db5c6923e5b767e6935ca1 to your computer and use it in GitHub Desktop.

Select an option

Save luisjunco/c7ea4d03c6db5c6923e5b767e6935ca1 to your computer and use it in GitHub Desktop.

JS OOP Bakery exercise

In this exercise, you'll be creating a Bakery class using JavaScript. This class will represent a bakery and have some properties and methods to track the bakery's location, the number of cakes in stock, and the ability to bake cakes.

image


Iteration 1.

Create a Bakery class with these properties and methods:

  • property location: this property represents the specific location of each bakery (each instance of the bakery class can have a different location).
  • property brandName: this property will represent the name of the brand. It will be the same for all bakeries.
  • method printLocation: this method should display the location of the bakery (when you call this method, it will show the location of the bakery).

Iteration 2:

We will add functionality to bake cakes & keep track of the number of cakes we have:

  • Add a property amountOfCakes: this property represents the number of cakes that we have in stock. The initial value should be zero (when a bakery is created, it starts with zero cakes).
  • Create a method printAmountOfCakes: this method should display the current number of cakes in stock.
  • Create a method makeCake: each time you call this method, it will increase the number of cakes in stock by 1. After baking a cake, the method should automatically display the updated number of cakes (reuse the function that you already have).

Iteration 3:

  • Add a property cash, were we will keep information about the money that we have (initial value: 0)
  • Add functionality to display the money we have
  • Add functionality to sell one cake (decrease stock by 1 & increases money by 3)

Iteration 4:

  • We shouldn't sell a cake if we don't have cakes, right? When a cake is sold, make sure there's actually stock ;)

Bonus:

Let's apply some real life economics...

  • Every time a new cake is created, it costs 1 (deducted from the cash available)
  • Every time a cake is sold, you collect 3
  • You can not make new cakes if there's no money
  • Initial cash: 2 (we need some initial investment, otherwise we can not start making cakes :p)


Solution: https://stackblitz.com/edit/js-kftsnf?file=index.js


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment