Getter and Setter

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },


 get appetizers() {
    this._courses.appetizers
  },
  get mains() {
    this._courses.mains
  },
  get desserts() {
    this._courses.desserts
  },
  set appetizers(appetizers) {
    this._courses.appetizers = appetizers
  },
  set mains(mains) {
    this._courses.mains = mains
  },
  set desserts(desserts) {
    this._courses.desserts = desserts
  },

 get courses() {
    return {
      appetizers: this.appetizers,
      mains: this.mains,
      desserts: this.desserts
    }
  },

  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice,
    };
    return this._courses[courseName].push(dish); 
  },

  getRandomDishFromCourse(courseName) {
    const dishes = this._courses[courseName];
    const randomIndex = Math.floor(Math.random() * dishes.length);
    return dishes[randomIndex];
  },

  generateRandomMeal() {
    const appetizer = this.getRandomDishFromCourse('appetizers');
    const main = this.getRandomDishFromCourse('mains');
    const dessert = this.getRandomDishFromCourse('desserts');
    let totalPrice = appetizer.price + main.price + dessert.price.toFixed(0);

    return `Sua carne é ${appetizer.name}, ${main.name}, ${dessert.name} e o preço total é de R$${totalPrice}.`

  }

};


menu.addDishToCourse('appetizers', 'Caesar Salad', 5.00)
menu.addDishToCourse('appetizers', 'Cenoura', 10.00)
menu.addDishToCourse('appetizers', 'batata', 15.00)

menu.addDishToCourse('mains', 'Picanha', 55.00)
menu.addDishToCourse('mains', 'File Mignon', 15.55)
menu.addDishToCourse('mains', 'Contra filé', 80.60)

menu.addDishToCourse('desserts', 'Brigadeiro', 50.55)
menu.addDishToCourse('desserts', 'Pudim', 15.00)
menu.addDishToCourse('desserts', 'Canjica', 25.00)


let meal = menu.generateRandomMeal();

console.log(meal)
  

I’m trying to understand when we use the ‘getter’ and ‘setter’, but I can’t connect the codes.

1 Like

The setters and getters never get implemented save for maybe, get courses(). They are not even needed, but could be implemented with a little extra effort once you pass the exercise.

I am doing an exercise on the setter method! And I am wondering If the “return” statement in the code I wrote is enough to make the exercise wrong ?

Because it’s the only difference between what I wrote and the exercise solution. And if so , why ?

1 Like

A setter has direct access to the property so a return is not needed. Also be sure that you are setting the backing variable.

 this._... = ...
1 Like

Yeah , that’s correct sir !

1 Like

Sir , I’ve read in this forum that you dont use the get or set method that often whilst coding.

Is that true ?

Can neither confirm nor refute the veracity of that statement. What seems the case now that JS has classes is that getters and setters support the object oriented programming model by using methods to get and set properties. I would think that it is common as dirt in production code with collaborative writers.

It is just a model, but one that all authors can understand if they see it employed and will know to adhere to it when updating the code.

2 Likes

Now , still inside the getter function which I am working on , I am wondering why is it that when i call it , it only “gets” 1/3 of the properties/players in my object called _players ?

Anyone can help ? EDIT : I FOUND OUT :smiley: , that’s because I REPEAT the key value (firstName , lastName and age) 3 times for each players. I changed the key values name to firstName1, lastName1 and firstName2 , LastName2 and it logged all the players names and age