Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.58 KB | None | 0 0
  1. // Importer ArrayList
  2. import java.util.ArrayList;
  3. /**
  4.  * Klassen representerer en Player i et RPG.
  5.  *
  6.  * @author Terje Nesthus (tne037)
  7.  * @version 1.0
  8.  */
  9. public class Player {
  10.  
  11.     // Feltene til klassen Player
  12.     private String name;
  13.     private String type;
  14.     private int gold;  
  15.     private int health;
  16.     private ArrayList<Item> items;
  17.    
  18.     /**
  19.      * Konstruktor
  20.      * @param newName navnet på Player
  21.      * @param newType typen til Player
  22.      */
  23.     public Player(String newName, String newType) { //2 parameter av typen String
  24.         name = newName;
  25.         type = newType;
  26.         health = 100;
  27.         this.items = new ArrayList<Item>();        
  28.     }
  29.    
  30.     /** Endrer helsa til Player
  31.      * @param value
  32.      */
  33.     public void changeHealth(int value) {
  34.         if((health += value) < 0) {
  35.             health = 0;
  36.         }
  37.     }
  38.    
  39.     /** Sjekker om Player er i live.
  40.      * @return true hvis health er større enn 0
  41.      */
  42.     public boolean isAlive() {
  43.         if(health > 0) {
  44.             return true;
  45.         } else {
  46.             return false;
  47.         }
  48.     }
  49.    
  50.     /**
  51.      * Skriver ut info om Player
  52.      */
  53.     public void easyprint() {
  54.         System.out.println("Name: " + name);
  55.         System.out.println("Type: " + type);
  56.         System.out.println("Health: " + health);
  57.     }
  58.  
  59.     /** Henter navnet til Player
  60.      * @return navnet til Player
  61.      */
  62.     public String getName() { //aksessormetode
  63.         return name;
  64.     }
  65.  
  66.     /** Setter navnet til Player
  67.      * @param name nytt navn
  68.      */
  69.     public void setName(String name) { //mutatormetode
  70.         this.name = name;
  71.     }
  72.  
  73.     /** Henter typen til Player
  74.      * @return  typen til Player
  75.      */
  76.     public String getType() {
  77.         return type;
  78.     }
  79.  
  80.     /** Setter navnet til Player
  81.      * @param type ny type
  82.      */
  83.     public void setType(String type) {
  84.         checkString(type);
  85.         switch(type) {
  86.             case type.equals("Warrior"): this.type = "Warrior";
  87.         }
  88.     }
  89.  
  90.     /** Henter health til Player
  91.      * @return Players health
  92.      */
  93.     public int getHealth() {
  94.         return health;
  95.     }
  96.    
  97.     /**
  98.      * Legger til et item til player
  99.      *
  100.      * @param Itemobject
  101.      */
  102.     public void addItem(String name, String description, String action, int value)
  103.     {
  104.         Item item = new Item(name, description, action, value);
  105.         this.items.add(item);
  106.     }
  107.    
  108.     /**
  109.      * Finn et player-item fra name
  110.      *
  111.      * @param Itemnavn
  112.      * @return Itemobject
  113.      */
  114.     public Item findItem(String name)
  115.     {
  116.         for (int i = 0; i < this.items.size(); i++)
  117.         {
  118.             Item item = this.items.get(i);
  119.             if (item.getName() == name)
  120.             {
  121.                 return item;
  122.             }
  123.             continue;
  124.         }
  125.         return null;
  126.     }
  127.    
  128.     /**
  129.      * Selg ett player item
  130.      *
  131.      * @param Itemnavn
  132.      */
  133.     public void sellItem(String name)
  134.     {
  135.         Item item = this.findItem(name);
  136.         if (item != null)
  137.         {
  138.             this.setGold(this.getGold() + item.getValue());
  139.             this.items.remove(item);
  140.         }
  141.     }
  142.    
  143.     /**
  144.      * Bruk et item
  145.      *
  146.      * @param Item navn man bruker
  147.      */
  148.     public void useItem(String name)
  149.     {
  150.         Item item = this.findItem(name);
  151.         if (item != null)
  152.         {
  153.             System.out.println(this.getName() + " " + item.getAction() + " a " + item.getName().toLowerCase());
  154.         }
  155.     }
  156.    
  157.     /**
  158.      * Setter gull-verdien til en player
  159.      *
  160.      * @param Antall gullpenger
  161.      */
  162.     public void setGold(int gold) // mutator method
  163.     {
  164.         this.gold = gold;
  165.     }
  166.    
  167.     public int getGold() // accessor method
  168.     {
  169.         return this.gold;
  170.     }
  171.    
  172.     /**
  173.      * Printer ut info om Playeren-objektet og dens itemer
  174.      */
  175.     public void print() {
  176.         System.out.print("\f");
  177.         System.out.println("Name: " + this.name);
  178.         System.out.println("Type: " + this.type);
  179.         System.out.println("Health: " + this.health);
  180.  
  181.         // Only print item info if any items exist
  182.         if (this.items.size() > 0)
  183.         {
  184.             System.out.println();
  185.             System.out.println("Player items (" + this.items.size() + "):");
  186.            
  187.             for (int i = 0; i < this.items.size(); i++)
  188.             {
  189.                 Item item = this.items.get(i);
  190.                 item.print();
  191.             }
  192.         }
  193.     }
  194.  
  195.     /**
  196.      * Metode for å sjekke om en streng er tom, hvis den er det setter den valuen til "Unspecified"
  197.      *
  198.      * @param Input string to check if empty
  199.      * @return Non-empty input string or default value "Unspecified", ellers returnerer den inputen
  200.      */
  201.     public static String checkString(String string)
  202.     {
  203.         if (string.isEmpty())
  204.         {
  205.             return "Unspecified";
  206.         }
  207.         else
  208.         {
  209.             return string;
  210.         }
  211.     }
  212.  
  213.     /**
  214.      * Den statiske main-metoden
  215.      *
  216.      * @param Args
  217.      */
  218.     public static void main(String[] args) {
  219.         // Create instance of player class
  220.         Player aPlayer = new Player("Terje", "Warrior");
  221.         aPlayer.addItem("Sword", "Magic sword used for battle", "started using his", 25);
  222.         aPlayer.addItem("Shield", "Super shield for super protection", "started using his", 100);
  223.         aPlayer.addItem("Potion", "Potion to increase your health", "drinks his", 10);
  224.  
  225.         //Print player info
  226.         aPlayer.print();
  227.     }  
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement