aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2014-04-06 14:34:59 +0200
committerPacien TRAN-GIRARD2014-04-06 14:34:59 +0200
commitdb2c22c9b4b71a2f1b4221aaf2db5934def41ce3 (patch)
treeb0ff1697a02680017579b6c5e4b8dd3fa00a7fc8
parent484c4c1e06c060f0185126ddd00d3a905dd2f05f (diff)
downloadesieequest-db2c22c9b4b71a2f1b4221aaf2db5934def41ce3.tar.gz
Implements "ItemList" in Inventory, that is now a class
-rw-r--r--src/esieequest/controller/Performer.java4
-rw-r--r--src/esieequest/model/Game.java4
-rw-r--r--src/esieequest/model/entities/Player.java43
-rw-r--r--src/esieequest/model/items/Inventory.java36
-rw-r--r--src/esieequest/model/map/Room.java42
5 files changed, 50 insertions, 79 deletions
diff --git a/src/esieequest/controller/Performer.java b/src/esieequest/controller/Performer.java
index 1abc887..d1f1bb6 100644
--- a/src/esieequest/controller/Performer.java
+++ b/src/esieequest/controller/Performer.java
@@ -138,7 +138,7 @@ class Performer {
138 * the item's name 138 * the item's name
139 */ 139 */
140 public void take(final String itemName) { 140 public void take(final String itemName) {
141 this.moveItem(this.game.getPlayer().getCurrentRoom(), this.game.getPlayer(), itemName); 141 this.moveItem(this.game.getPlayer().getCurrentRoom().getItems(), this.game.getPlayer().getInventory(), itemName);
142 } 142 }
143 143
144 /** 144 /**
@@ -148,7 +148,7 @@ class Performer {
148 * the item's name 148 * the item's name
149 */ 149 */
150 public void drop(final String itemName) { 150 public void drop(final String itemName) {
151 this.moveItem(this.game.getPlayer(), this.game.getPlayer().getCurrentRoom(), itemName); 151 this.moveItem(this.game.getPlayer().getInventory(), this.game.getPlayer().getCurrentRoom().getItems(), itemName);
152 } 152 }
153 153
154 /** 154 /**
diff --git a/src/esieequest/model/Game.java b/src/esieequest/model/Game.java
index 5ef4c22..63c1eee 100644
--- a/src/esieequest/model/Game.java
+++ b/src/esieequest/model/Game.java
@@ -191,8 +191,8 @@ public class Game {
191 * Creates and adds items into rooms. 191 * Creates and adds items into rooms.
192 */ 192 */
193 private void createItems() { 193 private void createItems() {
194 this.rooms.get("Cafeteria").putItem("banana", new Item("A yellow banana", 12)); 194 this.rooms.get("Cafeteria").getItems().putItem("banana", new Item("A yellow banana", 12));
195 this.rooms.get("Cafeteria").putItem("orange", new Item("An orange orange", 15)); 195 this.rooms.get("Cafeteria").getItems().putItem("orange", new Item("An orange orange", 15));
196 } 196 }
197 197
198 /** 198 /**
diff --git a/src/esieequest/model/entities/Player.java b/src/esieequest/model/entities/Player.java
index ef47d3f..5a97726 100644
--- a/src/esieequest/model/entities/Player.java
+++ b/src/esieequest/model/entities/Player.java
@@ -1,25 +1,22 @@
1package esieequest.model.entities; 1package esieequest.model.entities;
2 2
3import java.util.HashMap;
4import java.util.Set;
5import java.util.Stack; 3import java.util.Stack;
6 4
7import esieequest.model.items.Inventory; 5import esieequest.model.items.Inventory;
8import esieequest.model.items.Item;
9import esieequest.model.map.Room; 6import esieequest.model.map.Room;
10 7
11public class Player implements Inventory { 8public class Player {
12 9
13 private Room currentRoom; 10 private Room currentRoom;
14 private final Stack<Room> previousRooms; 11 private final Stack<Room> previousRooms;
15 12
16 private final HashMap<String, Item> inventory; 13 private final Inventory inventory;
17 14
18 public Player() { 15 public Player() {
19 this.currentRoom = null; 16 this.currentRoom = null;
20 this.previousRooms = new Stack<Room>(); 17 this.previousRooms = new Stack<Room>();
21 18
22 this.inventory = new HashMap<String, Item>(); 19 this.inventory = new Inventory();
23 } 20 }
24 21
25 /** 22 /**
@@ -51,33 +48,13 @@ public class Player implements Inventory {
51 } 48 }
52 } 49 }
53 50
54 @Override 51 /**
55 public Item takeItem(final String itemName) { 52 * Gets the player's inventory.
56 final Item item = this.inventory.get(itemName); 53 *
57 this.inventory.remove(itemName); 54 * @return the player's inventory
58 return item; 55 */
59 } 56 public Inventory getInventory() {
60 57 return this.inventory;
61 @Override
62 public boolean hasItem(final String itemName) {
63 return this.inventory.get(itemName) != null;
64 }
65
66 @Override
67 public void putItem(final String itemName, final Item item) {
68 this.inventory.put(itemName, item);
69 }
70
71 @Override
72 public Set<String> getItemList() {
73 // TODO Auto-generated method stub
74 return null;
75 }
76
77 @Override
78 public String listItems() {
79 // TODO Auto-generated method stub
80 return null;
81 } 58 }
82 59
83} 60}
diff --git a/src/esieequest/model/items/Inventory.java b/src/esieequest/model/items/Inventory.java
index d78b682..99b48a9 100644
--- a/src/esieequest/model/items/Inventory.java
+++ b/src/esieequest/model/items/Inventory.java
@@ -1,14 +1,22 @@
1package esieequest.model.items; 1package esieequest.model.items;
2 2
3import java.util.HashMap;
3import java.util.Set; 4import java.util.Set;
4 5
6import esieequest.controller.Utils;
7
5/** 8/**
6 * The inventory interface that describes an inventory containing Items refered 9 * An inventory containing Items referred by their names.
7 * by their names.
8 * 10 *
9 * @author Pacien TRAN-GIRARD 11 * @author Pacien TRAN-GIRARD
10 */ 12 */
11public interface Inventory { 13public class Inventory {
14
15 private final HashMap<String, Item> items;
16
17 public Inventory() {
18 this.items = new HashMap<String, Item>();
19 }
12 20
13 /** 21 /**
14 * Takes (returns and removes) an item from the inventory. 22 * Takes (returns and removes) an item from the inventory.
@@ -18,7 +26,11 @@ public interface Inventory {
18 * 26 *
19 * @return the item 27 * @return the item
20 */ 28 */
21 public Item takeItem(String itemName); 29 public Item takeItem(final String itemName) {
30 final Item item = this.items.get(itemName);
31 this.items.remove(itemName);
32 return item;
33 }
22 34
23 /** 35 /**
24 * Tells is an inventory contains a given item by its name. 36 * Tells is an inventory contains a given item by its name.
@@ -28,7 +40,9 @@ public interface Inventory {
28 * 40 *
29 * @return the existence of the item 41 * @return the existence of the item
30 */ 42 */
31 public boolean hasItem(String itemName); 43 public boolean hasItem(final String itemName) {
44 return this.items.get(itemName) != null;
45 }
32 46
33 /** 47 /**
34 * Puts an item in the inventory. 48 * Puts an item in the inventory.
@@ -38,20 +52,26 @@ public interface Inventory {
38 * @param item 52 * @param item
39 * the item 53 * the item
40 */ 54 */
41 public void putItem(String itemName, Item item); 55 public void putItem(final String itemName, final Item item) {
56 this.items.put(itemName, item);
57 }
42 58
43 /** 59 /**
44 * Returns a set of item names contained in the inventory. 60 * Returns a set of item names contained in the inventory.
45 * 61 *
46 * @return a set of item names 62 * @return a set of item names
47 */ 63 */
48 public Set<String> getItemList(); 64 public Set<String> getItemList() {
65 return this.items.keySet();
66 }
49 67
50 /** 68 /**
51 * Lists all items names contained in the inventory in a String. 69 * Lists all items names contained in the inventory in a String.
52 * 70 *
53 * @return the list of items 71 * @return the list of items
54 */ 72 */
55 public String listItems(); 73 public String listItems() {
74 return Utils.list(this.items.keySet(), "Items:", "No items.");
75 }
56 76
57} 77}
diff --git a/src/esieequest/model/map/Room.java b/src/esieequest/model/map/Room.java
index a5aa6d9..fc20cc5 100644
--- a/src/esieequest/model/map/Room.java
+++ b/src/esieequest/model/map/Room.java
@@ -1,11 +1,9 @@
1package esieequest.model.map; 1package esieequest.model.map;
2 2