将枚举值保存在类的哈希映射中

时间:2018-02-15 22:01:35

标签: java enums hashmap

我需要扩展课程会议室,以便通过在会议室课程中添加字段和方法,将房间添加到创建的会议室中,可以包含零个或多个字符。我已经创建了一个名为 Character 的枚举类,如下所示,但我不知道如何在我的Room类中使用它来为 Room 对象添加字符

我已经创建了一个HashMap字段,用于存储名为 charactersroom 的字符。我的代码中添加字符的方法称为 addCharacter

房间类

public class Room 
{
    private String description;
    private HashMap<Direction, Room> exits;        // stores exits of this room.
    private HashMap<Character, Room> charactersroom; 
    /**
     * Create a room described "description". Initially, it has
     * no exits. "description" is something like "a kitchen" or
     * "an open court yard".
     * @param description The room's description.
     * Pre-condition: description is not null.
     */
    public Room(String description) 
    {
        assert description != null : "Room.Room has null description";
        this.description = description;
        exits = new HashMap<Direction, Room>();
        charactersroom = new HashMap<Character, Room>();
        sane();
    }

    public void addCharacter(Character character)
    {

    }

    /**
     * Class invariant: getShortDescription() and getLongDescription() don't return null.
     */
    public void sane()
    {
        assert getShortDescription() != null : "Room has no short description" ;
        assert getLongDescription() != null : "Room has no long description" ;
    }

    /**
     * Define an exit from this room.
     * @param direction The direction of the exit.
     * @param neighbor  The room to which the exit leads.
     * Pre-condition: neither direction nor neighbor are null; 
     * there is no room in given direction yet.
     */
    public void setExit(Direction direction, Room neighbor) 
    {
        assert direction != null : "Room.setExit gets null direction";
        assert neighbor != null : "Room.setExit gets null neighbor";
        assert getExit(direction) == null : "Room.setExit set for direction that has neighbor";
        sane();
        exits.put(direction, neighbor);
        sane();
        assert getExit(direction) == neighbor : "Room.setExit has wrong neighbor";
    }

    /**
     * @return The short description of the room
     * (the one that was defined in the constructor).
     */
    public String getShortDescription()
    {
        return description;
    }

    /**
     * Return a description of the room in the form:
     *     You are in the kitchen.
     *     Items: map
     *     Exits: north west
     * @return A long description of this room
     */
    public String getLongDescription()
    {
        return "You are " + description + ".\n" + getExitString();
    }

    /**
     * Return a string describing the room's exits, for example
     * "Exits: north west".
     * @return Details of the room's exits.
     */
    private String getExitString()
    {
        String returnString = "Exits:";
        Set<Direction> keys = exits.keySet();
        for(Direction exit : keys) {
            returnString += " " + exit;
        }
        return returnString;
    }

    /**
     * Return the room that is reached if we go from this room in direction
     * "direction". If there is no room in that direction, return null.
     * @param direction The exit's direction.
     * @return The room in the given direction.
     * Pre-condition: direction is not null
     */
    public Room getExit(Direction direction) 
    {
        assert direction != null : "Room.getExit has null direction";
        sane();
        return exits.get(direction);
    }
}

字符枚举类

public enum Character
{
    LAURA("Laura",Item.SANDWICH),SALLY("Sally", Item.CRISPS),ANDY("Andy", Item.DRINK),ALEX("Alex", null);


private String charDescription;
private Item item;

private Character(String Chardescription, Item item) {
    this.charDescription = charDescription;
    this.item = item;
}

public String toString()
{
    return charDescription+item;
}

/**
 * Takes the indicated item from the character
 * @return true if successful
 * 
 */
public boolean take(Item item)
{   
  if (item == this.item) {
        this.item = null;
        return true;
    }
    return false;
}


}

编辑:此方法在我的超类游戏中用于创建房间及其出口:

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room trafalgarSquare, chinatown, oxfordStreet, soho, coventGarden, 
        britishMuseum, stPancras, kingsCross, britishLibrary, leicesterSquare;

        // create the rooms
        trafalgarSquare = new Room("on Trafalgar Square");
        chinatown = new Room("in Chinatown");
        oxfordStreet = new Room("on Oxford Street");
        soho = new Room("in Soho");
        coventGarden = new Room("in Covent Garden");
        britishMuseum = new Room("in the British Museum");
        stPancras = new Room("in St Pancras");
        kingsCross = new Room("in Kings Cross");
        britishLibrary = new Room("in the British Library");
        leicesterSquare = new Room("on Leicester Square");

        // initialise room exits

        kingsCross.setExit(Direction.WEST, stPancras);
        stPancras.setExit(Direction.EAST, kingsCross);
        stPancras.setExit(Direction.WEST, britishLibrary);
        britishLibrary.setExit(Direction.EAST, stPancras);
        britishLibrary.setExit(Direction.SOUTH, britishMuseum);
        britishMuseum.setExit(Direction.NORTH, britishLibrary);
        britishMuseum.setExit(Direction.WEST, oxfordStreet);
        oxfordStreet.setExit(Direction.EAST, britishMuseum);
        britishMuseum.setExit(Direction.SOUTH, coventGarden);
        coventGarden.setExit(Direction.NORTH, britishMuseum);
        oxfordStreet.setExit(Direction.SOUTH, soho);
        soho.setExit(Direction.NORTH, oxfordStreet);
        soho.setExit(Direction.SOUTH, chinatown);
        chinatown.setExit(Direction.NORTH, soho);
        chinatown.setExit(Direction.SOUTH, leicesterSquare);
        leicesterSquare.setExit(Direction.NORTH, chinatown);
        leicesterSquare.setExit(Direction.EAST, coventGarden);
        coventGarden.setExit(Direction.WEST, leicesterSquare);
        leicesterSquare.setExit(Direction.SOUTH, trafalgarSquare);
        trafalgarSquare.setExit(Direction.NORTH, leicesterSquare);

        currentRoom = stPancras;  // start game at St Pancras
    }

0 个答案:

没有答案