What is the correct way to do this?

Travis Black

I know this must be a fundamental design problem because I clearly can't do this. I want to call the ownGrokk, ownTyce, etc methods from another class depending on the value of the integer assigned to OwnedSpirits(int). This in turn fills arrays.

The problem is, I do this multiple times, and doing it from another class it seems like I have to make a new object every time to pass the new int argument, and doing so resets the value of spiritInstance. And, since that resets to zero, the arrays don't fill properly. I try to print out my array values later and I get an "ArrayIndexOutOfBoundsException".

public class OwnedSpirits {       
    private int spiritTypeInt = 0;

    public static int spiritInstance=0;                                                  
    public static int[] spiritarray = new int[9];
    public static String[] spiritName = new String[9];
    public static int[] party = new int[3];

    public OwnedSpirits(int spiritcall){
        if(spiritcall == 1){
            ownGrokk();
        }     
        if(spiritcall == 2){
            ownRisp();
        }
        if(spiritcall == 3){
            ownTyce();
        }
        if(spiritcall == 4){
            ownDaem();
        }
        if(spiritcall == 5){
            ownCeleste();
        }
    }

    private void ownGrokk(){ 
        spiritName[spiritInstance] = "Grokk";
        spiritInstance++;                
    }

    private void ownRisp(){     
        spiritName[spiritInstance] = "Risp";
        spiritInstance++;              
    }

    private void ownDaem(){ 
        spiritName[spiritInstance] = "Daem";
        spiritInstance++;      
    }

    private void ownCeleste(){ 
        spiritName[spiritInstance] = "Celeste";
        spiritInstance++;          
    }

    private void ownTyce(){ 
        spiritName[spiritInstance] = "Tyce";
        spiritInstance++; 
    }

and this code is in another class, where it attempts to call the methods to fill the array

buttonConfirm.addListener(new ClickListener(){
    @Override
    public void clicked(InputEvent event, float x, float y) {                                
        if(xcounter==3){
            for(x=0; x<3; x++){
                if(setdaemtrue == true){
                    new OwnedSpirits(4);
                }
                if(setrisptrue == true){
                    new OwnedSpirits(2);
                }
                if(setcelestetrue == true){
                    new OwnedSpirits(5);
                }
                if(settycetrue == true){
                    new OwnedSpirits(3);
                }
                if(setgrokktrue == true){
                    new OwnedSpirits(1);
                }    
            }
        }
    }
});   

and finally in yet another class:

System.arraycopy(OwnedSpirits.spiritName, 0, partylist, 0, 3);

@Override
public void show() {
    System.out.println(partylist[0]);
    System.out.println(partylist[1]);
    System.out.println(partylist[2]);

    spiritlist.setItems(partylist);
    table.add(spiritlist);
    table.setFillParent(true);
    stage.addActor(table);      
}

If the last part is confusing, it's because I am using libgdx. the print statements are there just to try to figure out why my list was having an error

someoneigna

I can show you what I would do to handle Spirits, and Parties. The Spirit class, contains name and current party its assigned to:

package com.stackoverflow.spirit;

public class Spirit {
    private String name;
    private Party party;
    private SpiritType type;
    private static int id = 0;

    public static enum SpiritType {
        Grokk, Risp, Tyce, Daem, Celeste
    };

    public Spirit(String name, SpiritType type) {
        create(name, type);
    }

    public Spirit(SpiritType type) {
        create(null, type);
    }

    // This is to handle Java inexistance of default parameter values.
    private void create(String name, SpiritType type)
    {
        Spirit.id++;

        this.name = (name == null) ? (type.name() + " " + id) : name;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public Party getParty() {
        return party;
    }

    public SpiritType getType() {
        return type;
    }

    /**
     * Used internally by @see Party
     * @param party the party this Spirit belongs
     */
    public void setParty(Party party) {
        this.party = party;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString()
    {
        return this.name;
    }
}

Finally the Party class, contains a set of Spirits, you can add and remove Spirits from the party.

package com.stackoverflow.spirit;

import java.util.HashSet;

public class Party {
    private HashSet<Spirit> spirits = new HashSet<Spirit>();
    private static int id = 0;
    private String name = "Party " + Party.id++;;

    public Party() {
    }

    public Party(String name) {
        this.name = name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void add(Spirit spirit) {
        if (!spirits.contains(spirit)) {
            spirits.add(spirit);

            if (spirit.getParty() != null) {
                //Remove from previous party to update the other party set
                spirit.getParty().remove(spirit);
            }
            spirit.setParty(this);
        } else {
            // throw new SpiritAlreadyOnParty();
        }
    }


    public void remove(Spirit spirit)
    {
        if (spirits.contains(spirit))
        {
            spirit.setParty(null); // You could create a default empty party for "Nature/Neutral" Spirits perhaps :)
            spirits.remove(spirit);
        }
        else {
            //throw new SpiritNotInParty();
        }
    }

    public boolean isOnParty(Spirit spirit) {
        return spirits.contains(spirit);
    }

    public ArrayList<Spirit> getSpirits()
    {
        return new ArrayList<Spirit>(spirits);
    }

    public int getPartySize() {
        return spirits.size();
    }

    public String getPartyInfo()
    {
        StringBuilder builder = new StringBuilder();

        builder.append("Party:" + this.name + " Size:" + this.spirits.size() + "\n");
        for (Spirit s : spirits)
        {
            builder.append(s.getName() + "\n");
        }
        return builder.toString();
    }


    @Override
    public String toString()
    {
        return this.name;
    }
}

Here I use the Spirit and Party classes, you could add more functionality, like properties for party strength, magic buffs on the party, etc:

package com.stackoverflow.spirit;

import com.stackoverflow.spirit.Spirit.SpiritType;

public class Main {
    public static void main(String[] args) throws java.lang.Exception {
        Party griffindor = new Party("Griffindor"), slytherin = new Party(
                "Slytherin");

        // You can also do for (SpiritType type : SpiritType.values() then
        // type.ordinal()
        for (int i = 0; i < SpiritType.values().length; i++) {
            griffindor.add(new Spirit(SpiritType.values()[i]));
            slytherin.add(new Spirit(SpiritType.values()[i]));
        }

        Spirit mySpirit = new Spirit("NotAHPFan", SpiritType.Celeste);

        slytherin.add(mySpirit);

        System.out.println("Name of party:" + mySpirit.getParty().getName());
        System.out.println("Is on griffindor?:"
                + griffindor.isOnParty(mySpirit));

        // What now?
        griffindor.add(mySpirit);
        System.out.println("Is " + mySpirit.getName() + " on "
                + slytherin.getName() + "?:" + slytherin.isOnParty(mySpirit));

        System.out.println(mySpirit.getName() + " is now on "
                + mySpirit.getParty() + "\n");


        System.out.println(griffindor.getPartyInfo());
        System.out.println(slytherin.getPartyInfo());
    }
}

P.D: I'm not a HP fan.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the correct way to code in Go?

What is correct way to use goroutine?

What is the correct way to use prepareForReuse?

NSDecimalNumber to double. What is the correct way to do this in Swift

What is the correct way to do Port Forwarding using VMWare

What is the correct way to do a get for multiple resources (batch get) with REST?

What is the correct way to test this Task?

What's the correct way to do inheritance in TypeScript for React components?

clean architecture - what is the correct way to do data model mapping?

What Is the Correct Way To Use AbstractReactiveWebInitializer

What is the correct way to "do nothing" in a blocking way?

What is the correct way to cancel a RegNotifyChangeKeyValue?

SYNTAX: what is the correct way to do a forward declaration for a c function?

What is correct way to do message formatting and validation?

What is the correct way to do calculations based on state properties in Vuex?

What is correct way to implement A* algorithm? Do we update nodes in closedSet or not?

What is the correct way to do many to many entity relation insert?

What is the correct way to restore a fragment?

What is the correct way to run this class?

What is the: Correct way for subtraction in Fortran

what is correct way to do this php code

What's the correct way to do IN (date-range) in Postgres?

What is the correct way to use grids?

What's the correct way to do a rotating log with python twisted?

Elements of ArrayList in Enum field Null. What is the correct way to do this?

What is the correct way to define a BaseActivity

What's the Correct Way to Do a Read with While Loop

What is the correct way to filter a &[&str]?

What's the correct way to do a health check on a Twilio connection in TypeScript?