0

//This is my first post ever so, please tell me if I did anything wrong. :D

I recently started "learning" Java and I just started coding a little csgo skin manager thingy. I wanted to add an object for every skin implemented in the game, so I started with an object array. Problem is there are ~608 skins in the game and I don't want to type if(i+1 == [ID]){weapons = new Skins("weaponName", "skinName", "randomQuality", "garbageCollection", i+1)} 600+ times, is there any faster way?

Edit: I got all the information in a .ods file, so the "problem" is actually about the "code structure" and not the initialization itself

//Here are the two classes if they are relevant to you:

    package cs.skins;
public class Main{  
    private static final int NUMBER_OF_SKINS = 608;

    private Skins[] weapons;

    public Main(){
        weapons = new Skins[NUMBER_OF_SKINS];
        initSkins();
    }

    private void initSkins(){
        for(int i = 0; i < weapons.length; i++){

                  if(i+1 == 1){
            weapons[i] = new Skins();
            }else if(i+1 == 2){
                weapons[i] = new Skins();
            }
        }
    }
}

and:

    package cs.skins;

public class Skins {

    private String weapon;
    private String skin;
    private String quality;
    private String collection;
    private int    id;
    private int    numberOwned;


    public Skins(String weapon, String skin, String quality, String collection, int id){
        this.weapon      = weapon;
        this.skin        = skin;
        this.quality     = quality;
        this.collection  = collection;
        this.id          = id;
        this.numberOwned = 0;
    }

    public String getWeapon() {
        return weapon;
    }
    public void setWeapon(String weapon) {
        this.weapon = weapon;
    }
    public String getSkin() {
        return skin;
    }
    public void setSkin(String skin) {
        this.skin = skin;
    }
    public String getQuality() {
        return quality;
    }
    public void setQuality(String quality) {
        this.quality = quality;
    }
    public String getCollection() {
        return collection;
    }
    public void setCollection(String collection) {
        this.collection = collection;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getNumberOwned() {
        return numberOwned;
    }
    public void setNumberOwned(int numberOwned) {
        this.numberOwned = numberOwned;
    }
}
Tom_There
  • 1
  • 1
  • I didn't get the question...If you want a code review then post in [SE Code Review](http://codereview.stackexchange.com/) – boxed__l Jan 24 '17 at 20:50
  • @Abhijith I was asking for exactly what deW1 responded, sorry if you didn't get... Could you tell me what I could have stated more specifically(if that is a word)? – Tom_There Jan 24 '17 at 21:05
  • You mentioned `..."code structure" and not the initialization itself...` in the body of the question, if you needed a review of you working code then CodeReview is the place. Good to know you got what you wanted :) – boxed__l Jan 24 '17 at 21:22
  • @Abhijith thank you, as I said, I am new to this platform so thanks for helping me :D – Tom_There Jan 24 '17 at 22:31

2 Answers2

2

But before you will need to figure out how to parse the ods. (I would suggest converting to other easier formats)

private void initSkins(){
            for(int i = 0; i < weapons.length; i++){
                weapons[i] = new Skins("weapon name", "skin name", "quality", "collection", 3);
            }
        }
Andy
  • 61
  • 6
0

Your best option imho is to put all skins into a JSON file. Loop through the file and create the objects out of the JSON.

If you google there are plenty of libraries doing exactly that, including documentation on how to serialize / deserialize objects.

There seem to also have been some attempts / working databases where you can retrieve skins in JSON format.

Should you have the Skin information in a CSV (Excel sheet) file instead, you can also use that and read a line from the file and put that information in the constructor of your Skins class after reading each line.

deW1
  • 5,562
  • 10
  • 38
  • 54
  • CSV file would be enough. – Grzegorz Górkiewicz Jan 24 '17 at 20:49
  • @GrzegorzGórkiewicz true, but knowing that you'll get CS:GO skins mostly in JSON format from APIs and Websites it'll make it easier for him :) – deW1 Jan 24 '17 at 20:50
  • lots of thanks for the fast response, I can just download the lib. and import it into my Eclipse project right? I guess you mean something like [this](http://stackoverflow.com/questions/1395551/convert-a-json-string-to-object-in-java-me) thread right? – Tom_There Jan 24 '17 at 20:52
  • @GustavoFring yes ofc. Then add the imports – deW1 Jan 24 '17 at 20:53