Skip to content

Instantly share code, notes, and snippets.

@luisjunco
Last active April 15, 2024 10:03
Show Gist options
  • Save luisjunco/c7ea4d03c6db5c6923e5b767e6935ca1 to your computer and use it in GitHub Desktop.
Save luisjunco/c7ea4d03c6db5c6923e5b767e6935ca1 to your computer and use it in GitHub Desktop.

JS OOP Bakery exercise

Iteration 1. Create a Bakery class with these properties and methods...

  • property "brandName", which needs to be the same one for all our bakeries (ie. for all instances of the Bakery class)
  • property "location" (specific for each bakery we build)
  • method "printLocation" (displays 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", were we will keep information about the amount of cakes in stock (initial value: 0)
  • Create a method "printAmountOfCakes (displays the amount of cakes in stock)
  • Create a method "makeCake" (when this method is invoked, we need to increase the amount of cakes by 1). After you bake a cake, you can display the number of cakes in stock (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...

  • Everytime a new cake is created, it costs 1 (deducted from the cash available)
  • Everytime 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