3
\$\begingroup\$

Given the code below, is there a better way to essentially create different native objects without changing the native constructor or the nativeList.json file? The only option I can see is adding a String nativeType variable to the constructor and creating the native object based on that the type passed. So to create all the types of natives I would create an array and just loop through that, each time passing the same native name and a different type.

I would like to avoid this because I believe there are like 20 - 25 different types, so I was wondering if you had any suggestions.

Entity.java

package models;

public class Entity {

    private int xPos, yPos; // used to keep track of the entity's position on the board
    private String imgFilePath; // used to show image file for entity 
    private int entityNum; // used to uniquely identify a character
    private String entityName; // used for entity name

    /***** Getter Methods *****/
    // Get the entity's x position on the board
    public int getxPos() {
        return xPos;
    }

    // Get the entity's y position on the board
    public int getyPos() {
        return yPos;
    }

    // Get the entity's image
    public String getImgFilePath() {
        return imgFilePath;
    }

    // Get entity number
    public int getEntityNum() {
        return entityNum;
    }

    // Get the entity's name
    public String getEntityName() {
        return entityName;
    }

    /***** Setter Methods *****/
    // Set the entity's x position on the board
    public void setxPos(int xPos) {
        this.xPos = xPos;
    }

    // Set the entity's y position on the board
    public void setyPos(int yPos) {
        this.yPos = yPos;
    }

    // Set the entity's image
    public void setImgFilePath(String imgFilePath) {
        this.imgFilePath = imgFilePath;
    }

    // Set the entity's unique identifying number
    public void setEntityNum(int entityNum) {
        this.entityNum = entityNum;
    }

    // Set the entity's name
    public void setEntityName(String entityName) {
        this.entityName = entityName;
    }

}

Native.java

package models;

import java.io.FileReader;

import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;

public class Native extends Entity {

    private Weapon weapon;
    private long wage;
    private long[] bounty; // the first element of bounty is notoriety, second element is gold
    private String move[]; // move for native
    private String weight;
    private String[] vul; // vulnerability first element is vulnerability of native and second is armor of native
    private String[] readyStats; // First element of array letter, second is effort, third is fatigue, fourth is sharpness
    private String[] unReadyStats; // First element of array letter, second is effort, third is fatigue, fourth is sharpness
    private String group; // group that native is part of
    JSONParser parser;

    public Native(String nativeName){
        System.out.println(nativeName);
        // Take native name and parse native JSON. Used returned values to fill in details for native
        parser = new JSONParser();

        try {
            JSONArray array = (JSONArray) parser.parse(new FileReader("res/nativeList.json"));

            for(Object o : array) {
                JSONObject currNative = (JSONObject) o;

                String n = (String) currNative.get("Name");

                if(n.equals(nativeName)) {
                    setImgFilePath((String) currNative.get("File Path")); // Set the image file for weapon
                    setEntityName((String) currNative.get("Name")); // Set the name for the entity
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***** Getter Methods *****/
    // Return weapon native has
    public Weapon getWeapon() {
        return weapon;
    }

    // Return wage to hire native
    public long getWage() {
        return wage;
    }

    // Return bounty for native
    public long[] getBounty() {
        return bounty;
    }

    // Return move of native
    public String[] getMove() {
        return move;
    }

    // Return weight of native
    public String getWeight() {
        return weight;
    }

    // Return vulnerability and armor of native
    public String[] getVul() {
        return vul;
    }

    // Return the ready statistics of native
    public String[] getReadyStats() {
        return readyStats;
    }

    // Return the un-ready statistics of native
    public String[] getUnreadyStats() {
        return unReadyStats;
    }

    // Return what group native is part of
    public String getGroup() {
        return group;
    }

    /***** Setter Methods *****/
    // Set the current native's inventory
    public void setWeapon(Weapon weapon) {
        this.weapon = weapon;
    }

    // Set the wage to hire native
    public void setWage(long wage) {
        this.wage = wage;
    }

    // Set bounty for native
    public void setBounty(long[] bounty) {
        this.bounty = bounty;
    }

    // Set move strength for native
    public void setMove(String[] move) {
        this.move = move;
    }

    // Set the weight for the native
    public void setWeight(String weight) {
        this.weight = weight;
    }

    // Set the vulnerability of the native
    public void setVul(String[] vul) {
        this.vul = vul;
    }

    // Set the ready statistics for the native
    public void setReadyStats(String[] readyStats){
        this.readyStats = readyStats;
    }

    // Set the un-ready statistics for the native
    public void setUnreadyStats(String[] unReadyStats) {
        this.unReadyStats = unReadyStats;
    }

    // Set the group native is in
    public void setGroup(String group) {
        this.group = group;
    }

    // Used for testing purposes
    public String toString() {
        String strToReturn = "Native Details\n";
        return strToReturn;
    }

    public static void main(String[] args){
        Native n = new Native("Knight");
    }
}

nativeList.json

[
    {
        "Name": "Knight",
        "Weapon": {
            "Length": 7,
            "Type": "Broadsword"
        },
        "VUL": {
            "Vulnerability": "Tremendous",
            "Armor": "Armored" 
        },
        "Wage": 8,
        "Bounty": {
            "Notoriety": 12,
            "Gold": 8
        },
        "Move": "Tremendous",
        "Weight": "Heavy",
        "Type": {
            "OHQ": {
                "Attack": {
                    "Unalerted": {
                        "letter": "H",
                        "effort": "6",
                        "fatigue": "1"
                    },
                    "Alerted": {
                        "letter": "T",
                        "effort": "4",
                        "fatigue": "1"
                    }
                },
                "Move": {
                    "Unalerted": 4,
                    "Alerted": 6
                }
            },
            "O1": {
                "Attack": {
                    "Unalerted": {
                        "letter": "H",
                        "effort": "4",
                        "fatigue": "1"
                    },
                    "Alerted": {
                        "letter": "T",
                        "effort": "5",
                        "fatigue": "1"
                    }
                },
                "Move": {
                    "Unalerted": 6,
                    "Alerted": 5
                }
            },
            "O2": {
                "Attack": {
                    "Unalerted": {
                        "letter": "H",
                        "effort": "5",
                        "fatigue": "1"
                    },
                    "Alerted": {
                        "letter": "T",
                        "effort": "5",
                        "fatigue": "1"
                    }
                },
                "Move": {
                    "Unalerted": 5,
                    "Alerted": 6
                }
            },
            "O3": {
                "Attack": {
                    "Unalerted": {
                        "letter": "H",
                        "effort": "4",
                        "fatigue": "1"
                    },
                    "Alerted": {
                        "letter": "T",
                        "effort": "5",
                        "fatigue": "1"
                    }
                },
                "Move": {
                    "Unalerted": 6,
                    "Alerted": 6
                }
            }
        },
        "Hiring Costs": {
            "Order": "Individual"
        },
        "File Path": "natives/knight.gif"
    }
]
\$\endgroup\$
  • \$\begingroup\$ I'm not entirely sure what you are trying to achieve... Currently there's only a "Knight" in your nativeList.json and you parse that into a Native object. Are you asking how to parse the Knight into a Knight extends Native object and then perhaps another native named "Healer" into a Healer extends Native object? Or are you just troubled by the amount of parsing you would need to do with your current way? \$\endgroup\$ – ZeroOne Jun 11 '15 at 13:48
3
\$\begingroup\$

In general, parsing JSON files manually as JSONObject classes etc. is not very nice or productive. You need to use some object mapper. I can recommend Jackson for you, it's quite nice. See, for example, this example: http://www.journaldev.com/2324/jackson-json-processing-api-in-java-example-tutorial

Basically your parsing code would then boil down to this part of the tutorial:

//read json file data to String
byte[] jsonData = Files.readAllBytes(Paths.get("employee.txt"));

//create ObjectMapper instance
ObjectMapper objectMapper = new ObjectMapper();

//convert json string to object
Employee emp = objectMapper.readValue(jsonData, Employee.class);

System.out.println("Employee Object\n"+emp);

Just replace "Employee" with "Native" etc...

\$\endgroup\$
  • \$\begingroup\$ Yes a object mapper is really the way to go. I'd recommend you to take a look at Gson (github.com/google/gson) as well. \$\endgroup\$ – RobAu Jun 11 '15 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.