From db2c22c9b4b71a2f1b4221aaf2db5934def41ce3 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 6 Apr 2014 14:34:59 +0200 Subject: Implements "ItemList" in Inventory, that is now a class --- src/esieequest/controller/Performer.java | 4 +-- src/esieequest/model/Game.java | 4 +-- src/esieequest/model/entities/Player.java | 43 +++++++------------------------ src/esieequest/model/items/Inventory.java | 36 ++++++++++++++++++++------ src/esieequest/model/map/Room.java | 42 ++++++------------------------ 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 { * the item's name */ public void take(final String itemName) { - this.moveItem(this.game.getPlayer().getCurrentRoom(), this.game.getPlayer(), itemName); + this.moveItem(this.game.getPlayer().getCurrentRoom().getItems(), this.game.getPlayer().getInventory(), itemName); } /** @@ -148,7 +148,7 @@ class Performer { * the item's name */ public void drop(final String itemName) { - this.moveItem(this.game.getPlayer(), this.game.getPlayer().getCurrentRoom(), itemName); + this.moveItem(this.game.getPlayer().getInventory(), this.game.getPlayer().getCurrentRoom().getItems(), itemName); } /** 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 { * Creates and adds items into rooms. */ private void createItems() { - this.rooms.get("Cafeteria").putItem("banana", new Item("A yellow banana", 12)); - this.rooms.get("Cafeteria").putItem("orange", new Item("An orange orange", 15)); + this.rooms.get("Cafeteria").getItems().putItem("banana", new Item("A yellow banana", 12)); + this.rooms.get("Cafeteria").getItems().putItem("orange", new Item("An orange orange", 15)); } /** 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 @@ package esieequest.model.entities; -import java.util.HashMap; -import java.util.Set; import java.util.Stack; import esieequest.model.items.Inventory; -import esieequest.model.items.Item; import esieequest.model.map.Room; -public class Player implements Inventory { +public class Player { private Room currentRoom; private final Stack previousRooms; - private final HashMap inventory; + private final Inventory inventory; public Player() { this.currentRoom = null; this.previousRooms = new Stack(); - this.inventory = new HashMap(); + this.inventory = new Inventory(); } /** @@ -51,33 +48,13 @@ public class Player implements Inventory { } } - @Override - public Item takeItem(final String itemName) { - final Item item = this.inventory.get(itemName); - this.inventory.remove(itemName); - return item; - } - - @Override - public boolean hasItem(final String itemName) { - return this.inventory.get(itemName) != null; - } - - @Override - public void putItem(final String itemName, final Item item) { - this.inventory.put(itemName, item); - } - - @Override - public Set getItemList() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String listItems() { - // TODO Auto-generated method stub - return null; + /** + * Gets the player's inventory. + * + * @return the player's inventory + */ + public Inventory getInventory() { + return this.inventory; } } 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 @@ package esieequest.model.items; +import java.util.HashMap; import java.util.Set; +import esieequest.controller.Utils; + /** - * The inventory interface that describes an inventory containing Items refered - * by their names. + * An inventory containing Items referred by their names. * * @author Pacien TRAN-GIRARD */ -public interface Inventory { +public class Inventory { + + private final HashMap items; + + public Inventory() { + this.items = new HashMap(); + } /** * Takes (returns and removes) an item from the inventory. @@ -18,7 +26,11 @@ public interface Inventory { * * @return the item */ - public Item takeItem(String itemName); + public Item takeItem(final String itemName) { + final Item item = this.items.get(itemName); + this.items.remove(itemName); + return item; + } /** * Tells is an inventory contains a given item by its name. @@ -28,7 +40,9 @@ public interface Inventory { * * @return the existence of the item */ - public boolean hasItem(String itemName); + public boolean hasItem(final String itemName) { + return this.items.get(itemName) != null; + } /** * Puts an item in the inventory. @@ -38,20 +52,26 @@ public interface Inventory { * @param item * the item */ - public void putItem(String itemName, Item item); + public void putItem(final String itemName, final Item item) { + this.items.put(itemName, item); + } /** * Returns a set of item names contained in the inventory. * * @return a set of item names */ - public Set getItemList(); + public Set getItemList() { + return this.items.keySet(); + } /** * Lists all items names contained in the inventory in a String. * * @return the list of items */ - public String listItems(); + public String listItems() { + return Utils.list(this.items.keySet(), "Items:", "No items."); + } } 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 @@ package esieequest.model.map; import java.util.HashMap; -import java.util.Set; import esieequest.controller.Utils; import esieequest.model.items.Inventory; -import esieequest.model.items.Item; /** * Represents one location in the scenery of the game, connected to others via @@ -16,12 +14,12 @@ import esieequest.model.items.Item; * @author Pacien TRAN-GIRARD * @author BenoƮt LUBRANO DI SBARAGLIONE */ -public class Room implements Inventory { +public class Room { private final String description; private final String imageName; private final HashMap exits; - private final HashMap items; + private final Inventory items; /** * Creates a room with a description and an associated image. @@ -35,7 +33,7 @@ public class Room implements Inventory { this.description = description; this.imageName = imageName; this.exits = new HashMap(); - this.items = new HashMap(); + this.items = new Inventory(); } public Room(final String description) { @@ -58,7 +56,7 @@ public class Room implements Inventory { * @return the informations about the room, including exits and items */ public String getInformations() { - return "You are " + this.description + ".\n" + this.listExits() + "\n" + this.listItems(); + return "You are " + this.description + ".\n" + this.listExits() + "\n" + this.items.listItems(); } /** @@ -101,37 +99,13 @@ public class Room implements Inventory { this.exits.put(direction, neighbor); } - @Override - public Item takeItem(final String itemName) { - final Item item = this.items.get(itemName); - this.items.remove(itemName); - return item; - } - - @Override - public boolean hasItem(final String itemName) { - return this.items.get(itemName) != null; - } - - @Override - public void putItem(final String itemName, final Item item) { - this.items.put(itemName, item); - } - - @Override - public Set getItemList() { - // TODO Auto-generated method stub - return null; - } - /** - * Lists the items contained in the room. + * Gets the room's items. * - * @return the list of the items + * @return the room's inventory */ - @Override - public String listItems() { - return Utils.list(this.items.keySet(), "This room contains:", "This room does not contain any item."); + public Inventory getItems() { + return this.items; } } -- cgit v1.2.3