Want more features on Pastebin? Sign Up, it's FREE!
Guest

Inventory class

By: a guest on Apr 19th, 2013  |  syntax: Java  |  size: 2.52 KB  |  views: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package game.items;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. /**
  7.  * User: geekocioso
  8.  * Date: 4/17/13
  9.  * Time: 5:46 AM
  10.  */
  11. public class Inventory {
  12.  
  13.     private int size;
  14.     /**
  15.      * We'll use this data structure to store the items and how many of them we have
  16.      */
  17.     private List<String> contents = new ArrayList<String>();
  18.  
  19.     /**
  20.      * Constructor. Will need to specify the size of inventory to create it.
  21.      * @param size Slots in the inventory
  22.      */
  23.     public Inventory(int size) {
  24.         this.size = size;
  25.     }
  26.  
  27.     /**
  28.      * Checks how many of an item we have
  29.      * @param name Item name
  30.      * @return true if in inventory, false if not
  31.      */
  32.     public Boolean checkForItem(String name) {
  33.         return contents.contains(name);
  34.     }
  35.  
  36.     /**
  37.      * Adds an item to the inventory
  38.      * @param name Item to add
  39.      * @return true if added successfully, false if not (cannot add when inventor is full)
  40.      */
  41.     public Boolean addItem(String name) {
  42.         // do not add if inventory is null
  43.         if (isFull()) return false;
  44.         try {
  45.             contents.add(name);
  46.             System.out.println("Item " + name + " added to your inventory!");
  47.         } catch (Exception e) {
  48.             return false;
  49.         }
  50.         return true;
  51.     }
  52.  
  53.     /**
  54.      * Removes an item from inventory
  55.      * @param name Item to remove
  56.      * @return true if removed, false if not
  57.      */
  58.     public Boolean removeItem(String name) {
  59.         if (contents.contains(name)) {
  60.             try {
  61.                 contents.remove(name);
  62.             } catch (Exception e) {
  63.                 return false;
  64.             }
  65.         } else {
  66.             return false;
  67.         }
  68.         return true;
  69.     }
  70.  
  71.  
  72.     /**
  73.      * Checks if inventory is full
  74.      * @return true if full, false if not full
  75.      */
  76.     public Boolean isFull() {
  77.         return contents.size() >= size;
  78.     }
  79.  
  80.     /**
  81.      * Queries for the number of empty slots left
  82.      * @return number of empty slots in inventory
  83.      */
  84.     public Integer numberOfFreeSlots() {
  85.         return size - contents.size();
  86.     }
  87.  
  88.     /**
  89.      * Writes a list of your inventory to the console
  90.      */
  91.     public void listContents() {
  92.         if (contents.size() == 0) {
  93.             System.out.println("Empty.");
  94.         } else {
  95.             System.out.println("Here's what you have:");
  96.             for (String s: contents) {
  97.                 System.out.println("* " + s);
  98.             }
  99.         }
  100.     }
  101.  
  102. }
clone this paste RAW Paste Data