Package javaEffect.planet

Source Code of javaEffect.planet.Planet

package javaEffect.planet;

import java.util.ArrayList;
import java.util.zip.DataFormatException;

import javaEffect.ErrEmptyString;
import javaEffect.ErrNegativeNumber;
import javaEffect.ErrNotFountOrMistake;
import javaEffect.Size;
import javaEffect.characters.Character;

/**
* This is the planet class
*
* @author Thibaut LOCQUET & Jack OUTRAN
* @version 0.2
*
*/
public class Planet {

  /**
   * This ArrayList contain the all references to the planets
   */
  public static ArrayList<Planet> planetList;

  static {
    planetList = new ArrayList<Planet>();
  }

  private String name;
  private Integer inhabitants;

  private ArrayList<Object> character;
  private ArrayList<Object> spacecraft;

  /**
   *
   * @param name
   *            The name of the planet
   * @param size
   *            The size of the planet
   * @throws Exception
   */
  public Planet(String name, int size) throws ErrEmptyString,
      ErrNegativeNumber {

    planetList.add(this);

    if (name.isEmpty()) {
      throw new ErrEmptyString(
          "You didn't enter a name for a planet in the configuration file.");
    }
    this.name = name;

    if (size <= 0) {
      throw new ErrNegativeNumber(
          "You enter a negative number of inhabitants in the configuration file.");
    }
    this.inhabitants = size;

    this.character = new ArrayList<Object>();
    this.spacecraft = new ArrayList<Object>();
  }

  /**
   *
   * @param name
   *            The name of the planet
   * @param size
   *            The size of the planet
   * @throws ErrEmptyString
   */
  public Planet(String name, Size size) throws ErrEmptyString {

    planetList.add(this);

    if (name.isEmpty()) {
      throw new ErrEmptyString(
          "You didn't enter a name for a planet in the configuration file.");
    }
    this.name = name;

    this.character = new ArrayList<Object>();
    this.spacecraft = new ArrayList<Object>();

    if (size.compareTo(Size.BIG) == 0)
      this.inhabitants = (int) (Math.random() * 1000000000);
    else if (size.compareTo(Size.MEDIUM) == 0)
      this.inhabitants = (int) (Math.random() * 10000000);
    else
      this.inhabitants = (int) (Math.random() * 200000);
  }

  /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    Planet other = (Planet) obj;
    if (name == null) {
      if (other.name != null)
        return false;
    } else if (!name.equals(other.name))
      return false;
    return true;
  }

  /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString() {
    String inhabitants = this.inhabitants.toString();
    String size;

    if (this.inhabitants > 100000000) {
      size = "big";
    } else if (this.inhabitants > 1000000) {
      size = "medium";
    } else {
      size = "small";
    }

    return "Planet \"" + this.name + "\", size: " + size + ", inhabitants="
        + inhabitants;
  }

  /**
   *
   * @return The name of the character
   */
  public String getName() {
    return this.name;
  }

  /**
   * @return The number of inhabitants
   */
  public int getInhabitants() {
    return inhabitants;
  }

  /**
   * @param inhabitants
   *            the number of inhabitants
   * @throws ErrNegativeNumber
   */
  public void setInhabitants(int inhabitants) throws ErrNegativeNumber {

    if (inhabitants >= 0)
      this.inhabitants = inhabitants;
    else {
      throw new ErrNegativeNumber("The number of inhabitants is negative");
    }
  }

  /**
   * Display all the characters on the planet.
   */
  public static void printAllPlanets() {
    for (Planet p : planetList) {
      System.out.println(p.toString());
    }
  }

  /**
   *
   * @return The number of character on the planet
   */
  public static int printAllPlanetsAndNumbers() {
    int number = 0;
    for (Planet p : planetList) {
      System.out.println("\t" + number + "-> " + p.toString());
      number++;
    }

    return number;

  }

  /**
   * @param index
   *            index corresponding of the planet get the planet corresponding
   *            of the index (of all the planets)
   * @return the selected planet
   */
  public static Planet getPlanet(int index) {

    if (index >= 0 && index < planetList.size())
      return planetList.get(index);

    return null;
  }

  /**
   *
   * @param name
   * @return The planet with the name name.
   * @throws ErrNotFountOrMistake
   */
  public static Planet getPlanet(String name) throws ErrNotFountOrMistake {
    for (Planet p : planetList) {
      if (p.getName().equalsIgnoreCase(name)) {
        return p;
      }
    }
    throw new ErrNotFountOrMistake(name + " not found");
  }

  /**
   * get number of characters on this planet
   *
   * @return the crew
   */
  public int getCharacterNumber() {

    return this.character.size();
  }

  /**
   * get the character corresponding of the index who is on the planet
   *
   * @return the crew
   */
  public Object getCharacter(int i) {

    return this.character.get(i);
  }

  /**
   * get the crew names who are on the planet
   *
   * @return the crew
   */
  public String getCharacterNames() {
    String characterNames = "Characters in the planet \"" + this.name
        + "\": \n";

    if (this.character.size() == 0)
      return this.name + " has no character";

    for (int i = 0; i < this.character.size(); i++) {
      characterNames += "\t -> " + this.character.get(i).toString()
          + "\n";
    }

    return characterNames;
  }

  /**
   * get the crew name corresponding of the index who is on the planet
   *
   * @return the crew
   */
  public String getCharacterName(int i) {
    String characterName = "";

    if (i >= 0 && i < this.character.size())
      characterName += this.character.get(i).toString();

    return characterName;
  }

  /**
   * Add new character on the planet
   *
   * @param character
   *            The character to add
   * @throws DataFormatException
   */
  public void addCharacter(Object character) throws DataFormatException {
    if (character instanceof javaEffect.characters.Character) {
      this.character.add(character);
    } else {
      throw new DataFormatException("This is not a character");
    }
  }

  /**
   * Remove character of the planet
   *
   * @param character
   *            The character to remove
   */
  public void removeCharacter(Object character) {
    this.character.remove(character);
  }

  /**
   * get number of spacecrafts on this planet
   *
   * @return the crew
   */
  public int getSpacecraftNumber() {

    return this.spacecraft.size();
  }

  /**
   * @return spacecraft's name which are on the planet
   */
  public String getSpacecraftName(int i) {
    String spacecraftNames = "";

    if (i >= 0 && i < this.spacecraft.size()) {
      spacecraftNames = this.spacecraft.get(i).toString();
    }

    return spacecraftNames;
  }

  /**
   * @return spacecraft's names which are on the planet
   */
  public String getSpacecraft() {
    String spacecraftNames = "Spacecraft in the planet " + this.name
        + ": \n ";

    if (this.spacecraft.size() == 0)
      return this.name + " has no spacecraft\n";

    for (int i = 0; i < this.spacecraft.size(); i++) {
      spacecraftNames += "\t -> " + this.spacecraft.get(i) + " \n ";
    }

    return spacecraftNames;
  }

  /**
   * Add new spacecraft on the planet
   *
   * @param spacecraft
   *            The spacecraft to add
   * @throws DataFormatException
   */
  public void addSpacecraft(Object spacecraft) throws DataFormatException {

    if (spacecraft instanceof javaEffect.spacecraft.Spacecraft) {
      this.spacecraft.add(spacecraft);
    } else {
      throw new DataFormatException("This is not a Spacecraft");
    }
  }

  /**
   * get the spaceship corresponding of the index who is on the planet
   *
   * @return the crew
   */
  public Object getSpaceship(int i) {

    return this.spacecraft.get(i);
  }

  /**
   * Remove spacecraft of the planet
   *
   * @param spacecraft
   *            The spacecraft to remove
   */
  public void removeSpacecraft(Object spacecraft) {
    this.spacecraft.remove(spacecraft);
  }

  /**
   *
   * @param charactersToRemove
   *            A list of character to remove from the planet
   */
  public void removeInList(ArrayList<Character> charactersToRemove) {
    character.removeAll(charactersToRemove);
  }
}
TOP

Related Classes of javaEffect.planet.Planet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.