lundi 31 mai 2021

how to write nested if statement in more readable way [closed]

problem domain: I am creating an animal shelter for animals using queues, I have the reusable class Queue that I have built. in the dequeue method, I should return null if the pref is not cat either dog. and I don't want to tell the user that if the shelter (i.e cats) is empty so I have no cats in my shelter.

this is my animal class:

class AnimalShelter {
  constructor() {
    this.cats = new Queue();
    this.dogs = new Queue();
  }
  enqueue(animal) {
    if (animal.type === "cat") {
      this.cats.enqueue(animal);
    }

    if (animal.type === "dog") {
      this.dogs.enqueue(animal);
    } else {
      console.log("animal type should be either dog or cat");
      return "animal type should be either dog or cat";
    }
  }
  dequeue(pref) {
    if (pref === "cat") {
      if (!this.cats.isEmpty()) {
        return this.cats.dequeue(pref);
      } else {
        return "sorry the cats shelter is empty";
      }
    }
    if (pref === "dog") {
      if (!this.dogs.isEmpty()) {
        return this.dogs.dequeue(pref);
      } else {
        return "sorry the dogs shelter is empty";
      }
    } else {
      return null;
    }
  }
}

and this is the Queue class :

const Node = require("./Node");

/**
 * creates a queue with a storage and pointers to the elements in the storage
 */
class Queue {
  constructor() {
    this.top = 0;
    this.bottom = 0;
    this.storage = {};
  }
  /**
   *
   * @param {any} value method for pushing elements to the queue.
   */
  enqueue(value) {
    let node = new Node(value);
    this.storage[this.top] = node;
    this.top++;
  }
  /**
   * method for removing the first element entered the queue.
   * @returns {Object} removedElement: the removed element
   */
  dequeue() {
    try {
      this.emptyException();
      let removedElement = this.storage[this.bottom];
      delete this.storage[this.bottom];
      this.bottom++;
      return removedElement;
    } catch (error) {
      console.log(error);
    }
  }
  /**
   * method returning the first element enqueued without removing it.
   * @returns the first element entered the queue.
   */
  peek() {
    try {
      this.emptyException();
      return this.storage[this.bottom];
    } catch (error) {
      console.log(error);
    }
  }
  /**
   * method for calculating the size the queue.
   * @returns {Number} returns the number of elements in the queue.
   */
  size() {
    return this.top - this.bottom;
  }
  /**
   * method for checking if the queue is empty or not.
   * @returns {boolean}
   */
  isEmpty() {
    return this.size() === 0;
  }
  /**
   * method throwing an exception if the peek() or dequeue called on an empty queue
   */
  emptyException() {
    if (this.isEmpty()) {
      throw new Error("queue is empty !!");
    }
  }
}

and this is my Node class:

/**
 * creates a node object with a value and a empty pointer to the next node.
 */
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

Aucun commentaire:

Enregistrer un commentaire