mercredi 17 janvier 2018

New instance in switch

I'm currently learning java and oop. I'm actually meeting a problem which I have to instantiate a new object depending a condition. Each object has the same type. Depending of the condition, I would like to pass my object playerAnimal and animalSauvage in order to let them fight. (it's kind of a player meeting a savage pokemon).

   switch (animalChosen) {
      case "Chien":
          Chien playerChien = new Chien(animalName);
          playerChien.displayStat();
          break;
      case "Chat":
          Chat playerChat = new Chat(animalName);
          playerChat.displayStat();
          break;
      case "Furet":
          Furet playerFuret = new Furet(animalName);
          playerFuret.displayStat();
          break;
        default
          break;
        }

    switch (randAnimalSauvage()) {
        case "Chien":
            Animaux chienSauvage = new Chien();
            chienSauvage.displayStat();
            break;
        case "Chat":
            Animaux chatSauvage = new Chat();
            chatSauvage.displayStat();
            break;
        case "Furet":
            Animaux furetSauvage = new Furet();
            furetSauvage.displayStat();
            break;
        default:
            break;
    }
    Fight letsFight = new Fight(Animaux playerAnimal, Animaux animalSauvage);
    letsFight.battle(Animaux playerAnimal, Animaux animalSauvage);

PS: I'm french , don't mind the meaning of the variable name ;)

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire