Advertisement
Guest User

Untitled

a guest
May 26th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.15 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. import java.util.Collection;
  4.  
  5. import java.util.HashMap;
  6.  
  7.  
  8.  
  9.  
  10.  
  11. public abstract class Lifeform {
  12.  
  13.  
  14.  
  15.     /**
  16.  
  17.      * Creates a new Lifeform
  18.  
  19.      *
  20.  
  21.      * @param name
  22.  
  23.      * @effect  getName == name
  24.  
  25.      * @effect  getMode == mode.NORMAL
  26.  
  27.      * @effect  setMaxHitpoints(101)
  28.  
  29.      * @effect  setHitpoints(101)
  30.  
  31.      * @post    getStrength() == 10.00
  32.  
  33.  
  34.  
  35.      *
  36.  
  37.      * @throws IllegalArgumentException
  38.  
  39.      */
  40.  
  41.     public Lifeform(String name) throws IllegalArgumentException {
  42.  
  43.         if (isValidName(name)) this.name = name; // throws possible an IllegalArgumentException
  44.  
  45.         else throw new IllegalArgumentException();
  46.  
  47.         mode = Mode.NORMAL;
  48.  
  49.         setMaxHitpoints(101);
  50.  
  51.         setHitpoints(101);
  52.  
  53.         this.strength = 10.00D; // default value of a hero
  54.  
  55.     }
  56.  
  57.     private String name;
  58.  
  59.     private int hitpoints;
  60.  
  61.     private int maxHitpoints;
  62.  
  63.     private double strength;
  64.  
  65.     private static final int strengthPrecision = 2;
  66.  
  67.     private Mode mode;
  68.  
  69.     private HashMap<String,Entity> anchor;
  70.  
  71.     /**
  72.  
  73.      * Sets the hero's hitpoints.
  74.  
  75.      *
  76.  
  77.      * @pre ...
  78.  
  79.      *      | isValidHitpoints(hitpoints, getMaxHitpoints(), getMode())
  80.  
  81.      * @post    ...
  82.  
  83.      *          | new.getHitpoints()==hitpoints
  84.  
  85.      * @param hitpoints
  86.  
  87.      */
  88.  
  89.     public void setHitpoints(int value) throws AssertionError
  90.  
  91.     {
  92.  
  93.     assert isValidHitpoints(hitpoints, getMaxHitpoints(), getMode()): "Wrong value for hitpoints!";
  94.  
  95.     this.hitpoints = hitpoints;
  96.  
  97.     }
  98.  
  99.     /**
  100.  
  101.      * Checks whether a given amount of hitpoints is valid.
  102.  
  103.      * @param   hitpoints
  104.  
  105.      * @param   mode
  106.  
  107.      * @return  ...
  108.  
  109.      *          | hitpoints>0 && (mode==NORMAL)?isPrime(hitpoints):true
  110.  
  111.      */
  112.  
  113.     private static boolean isValidHitpoints(int hitpoints, int maxHitpoints, Mode mode) {
  114.  
  115.         return ((hitpoints<=maxHitpoints) && (hitpoints>0) && ((mode==Mode.NORMAL)?ExtraMath.isPrime(hitpoints):true));
  116.  
  117.     }
  118.  
  119.     /**
  120.  
  121.      * Returns the hero's hitpoints.
  122.  
  123.      * @return hitpoints
  124.  
  125.      */
  126.  
  127.     @Basic
  128.  
  129.     public int getHitpoints() {
  130.  
  131.         return hitpoints;
  132.  
  133.     }
  134.  
  135.     /**
  136.  
  137.      * Sets the hero's maximum hitpoints.
  138.  
  139.      *
  140.  
  141.      * @pre     the hitpoints must be valid (see isValidHitpoints)
  142.  
  143.      *          | isValidHitpoints(getmaxHitpoints(), Integer.MAX_VALUE, Mode.COMBAT)
  144.  
  145.      * @post    the maximum of the hitpoints will be changed if there was no assert thrown.
  146.  
  147.      *          | new.getMaxHitpoints()==maxHitpoints
  148.  
  149.      * @param maxHitpoints
  150.  
  151.      */
  152.  
  153.     public void setMaxHitpoints(int maxHitpoints) {
  154.  
  155.         assert isValidHitpoints(maxHitpoints, Integer.MAX_VALUE, Mode.COMBAT): "Wrong value for maxHitpoints!";
  156.  
  157.         this.maxHitpoints = maxHitpoints;
  158.  
  159.     }
  160.  
  161.    
  162.  
  163.     /**
  164.  
  165.      * Returns the hero's maximum hitpoints.
  166.  
  167.      * @return maxHitpoints
  168.  
  169.      */
  170.  
  171.     @Basic
  172.  
  173.     public int getMaxHitpoints() {
  174.  
  175.         return maxHitpoints;
  176.  
  177.     }
  178.  
  179.    
  180.  
  181.     /**
  182.  
  183.      * @return  returns the Lifeforms strength
  184.  
  185.      *          | return strength
  186.  
  187.      */
  188.  
  189.     public double getStrength() {return strength;}
  190.  
  191.    
  192.  
  193.     /**
  194.  
  195.      *
  196.  
  197.      * @post    Multiplies the hero's strength by factor and ensures the result is positive.
  198.  
  199.      *          | new.getStrength() == ExtraMath.round(strength*(double)Math.abs(factor), strengthPrecision)
  200.  
  201.      * @param value
  202.  
  203.      */
  204.  
  205.     public void multiplyStrength(int value) {
  206.  
  207.         this.strength = ExtraMath.round(strength*(double)Math.abs(value), strengthPrecision);
  208.  
  209.     }
  210.  
  211.    
  212.  
  213.     /**
  214.  
  215.      *
  216.  
  217.      * @post    Divides the hero's strength by factor and ensures the result is positive..
  218.  
  219.      *          | if( factor!=0 ) then
  220.  
  221.      *          |   new.getStrength() == ExtraMath.round(strength/(double)Math.abs(factor), strengthPrecision)
  222.  
  223.      *          | else
  224.  
  225.      *          |   new.getStrength() == getStrength()
  226.  
  227.      * @param factor
  228.  
  229.      */
  230.  
  231.     public void divideStrength(int value) {
  232.  
  233.         if(value!=0) {
  234.  
  235.             this.strength = ExtraMath.round(strength/(double)Math.abs(value), strengthPrecision);
  236.  
  237.         }
  238.  
  239.     }
  240.  
  241.    
  242.  
  243.     /**
  244.  
  245.      * function which create a key into the hashmap with a given key as name,
  246.  
  247.      * but only if the key didn't exist before..
  248.  
  249.      * @param   key
  250.  
  251.      * @throws  IllegalArgumentException
  252.  
  253.      * @post    the anchormap will have a key with the name of the parrameter.
  254.  
  255.      *          | getItem(key) == null
  256.  
  257.      */
  258.  
  259.     public void addAnchor(String key) throws IllegalArgumentException
  260.  
  261.     {
  262.  
  263.         if(key.equals("") || key == null) throw new IllegalArgumentException(); // keys die niet mogen bestaan !
  264.  
  265.         if(anchor.containsKey(key)) throw new IllegalArgumentException(); // bestaat al een key met die naam !!!
  266.  
  267.         anchor.put(key, null);
  268.  
  269.     }
  270.  
  271.    
  272.  
  273.     /**
  274.  
  275.      * funchtion which delete the given key and with this also the reference to the content.
  276.  
  277.      * @param key
  278.  
  279.      */
  280.  
  281.     public void deleteAnchor(String key) {
  282.  
  283.         anchor.remove(key);
  284.  
  285.     }
  286.  
  287.    
  288.  
  289.     /**
  290.  
  291.      * function which change the content of the given key,
  292.  
  293.      * or makes a key with the given content
  294.  
  295.      * @param key
  296.  
  297.      * @param item
  298.  
  299.      * @throws  IllegalArgumentException
  300.  
  301.      *          | (key.equals("") || key == null)
  302.  
  303.      * @post    the anchormap will contain the entry with the given key and item
  304.  
  305.      *          | new.getItem(key) == item
  306.  
  307.      */
  308.  
  309.     public void joinItem(String key,Entity item) {
  310.  
  311.         if(key.equals("")) throw new IllegalArgumentException(); // keys die niet mogen bestaan !
  312.  
  313.         if(item != null && (getCapacity()-getLoad()-item.getWeight()<0))
  314.  
  315.                 return;
  316.  
  317.         anchor.put(key, item);
  318.  
  319.     }
  320.  
  321.    
  322.  
  323.     /**
  324.  
  325.      *
  326.  
  327.      * @param   key (the name of the anchor)
  328.  
  329.      * @return  returns the item that is into the anchor with the given key
  330.  
  331.      *          | returns Entity;
  332.  
  333.      * @throws  IllegalArgumentException
  334.  
  335.      *          throws an illegal argument exception if you give a empty string or a null refference
  336.  
  337.      *          | (key == null || key.equals(""))
  338.  
  339.      */
  340.  
  341.     public Entity getItem(String key) throws IllegalArgumentException {
  342.  
  343.         if(key.equals("") || key == null) throw new IllegalArgumentException();
  344.  
  345.         return anchor.get(key);
  346.  
  347.     }
  348.  
  349.    
  350.  
  351.     /**
  352.  
  353.      * function for getting the hole hashmap as an array of content
  354.  
  355.      * @return Entity[] = {foreach(getItem())}
  356.  
  357.      */
  358.  
  359.     public Entity[] getList() {
  360.  
  361.        
  362.  
  363.         return (Entity[]) anchor.values().toArray();
  364.  
  365.     }
  366.  
  367.    
  368.  
  369.     /**
  370.  
  371.      * getter for the name
  372.  
  373.      * @return  name
  374.  
  375.      */
  376.  
  377.     public String getName() {
  378.  
  379.         return name;
  380.  
  381.     }
  382.  
  383.    
  384.  
  385.     /**
  386.  
  387.      * gives the current load
  388.  
  389.      * @return  return the weight of all the items the hero carry
  390.  
  391.      */
  392.  
  393.     public double getLoad() {
  394.  
  395.         Entity[] list = getList();
  396.  
  397.         double load = 0;
  398.  
  399.         for(Entity item:list)
  400.  
  401.             load+=item.getWeight();
  402.  
  403.         return load;
  404.  
  405.     }
  406.  
  407.    
  408.  
  409.     public abstract boolean isValidName(String name);
  410.  
  411.     public abstract int getProtection();
  412.  
  413.     public abstract double getCapacity();
  414.  
  415.     /**
  416.  
  417.      * private enumeration for use of the Lifeforms hitpoint system
  418.  
  419.      * @author Joris De Rijck
  420.  
  421.      */
  422.  
  423.     private enum Mode {
  424.  
  425.         COMBAT,
  426.  
  427.         NORMAL
  428.  
  429.     }
  430.  
  431.     protected void setBattle() {mode = Mode.COMBAT;}
  432.  
  433.     protected void setPeace() {mode = Mode.NORMAL;}
  434.  
  435.    
  436.  
  437.     /**
  438.  
  439.      * Sets the hero's current mode.
  440.  
  441.      * @pre ...
  442.  
  443.      *      | mode != null
  444.  
  445.      * @post    ...
  446.  
  447.      *          | new.getMode()==mode
  448.  
  449.      * @param mode
  450.  
  451.      */
  452.  
  453.     public void setMode(Mode mode) {
  454.  
  455.         assert mode != null: "Null mode given!";
  456.  
  457.         this.mode = mode;
  458.  
  459.     }
  460.  
  461.     /**
  462.  
  463.      *
  464.  
  465.      * @return  returns the Lifeforms mode (can either be combat or normal mode)
  466.  
  467.      *          | return mode
  468.  
  469.      */
  470.  
  471.     public Mode getMode() { return mode;}
  472.  
  473. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement