Marian Pazioch
Marian Pazioch

Reputation: 83

Casting string as a integer member, possible?

Ok my problem isnt really a serious one, im just trying to find a clever way of access/modification of class member variables. Here is the code:

public class Storage{
 private int cookies= 0; 
 private int rolls= 0; 
 private int candies= 0; 
 private int lolipops= 0; 
 private int iceCreams= 0; 

 public void addCookies(int howMuch){  //this is the dirty way of creating method for
    this.cookies = cookies+howMuch;     //every member variable    
 }

 public void addValue(String stat, int howMuch){ //i would like to do it only
                                       //by passing the name
                                      //of variable and then cast it as integer
                                      //so that it would relate to my class members

    int value = this.(Integer.parseInt(stat));   //  <- YES i know its ridiculous
                                      //im just trying to explain what is my aim       
    value = value + howMuch;

    this.(Integer.parseInt(stat)) = value;
 }
}

Generally i would like to access a field by passing its name to a method, read value of that member, add to it some value, and then store it. Yes i know that it easily can be done with separate methods, or even with one by using some arraylist and comparisons of member names with parameter passed to method. But i would like to do it "fast" without redundant code writing.

Now i have like 5 members, but what about 15000? My aim is to simplify the whole processing and code writing. So generally is it possible to do such redundant code writing bypass? Since i know that i will always pass appropriate name to method... Unless the rule of thumb is to create method for each variable?

Upvotes: 1

Views: 126

Answers (3)

Mesop
Mesop

Reputation: 5263

I guess that by using reflection you can iterate through the fields/methods of your object and do your computation.

For one specific field:

    Field member = myObject.getClass().getField(fieldName);
    // If you know the class: Field member = MyClass.class.getField(fieldName);
    System.out.println(member.getInt(myObject)); // Get the value
            member.setInt(myObject, 4); // Set the value

If you want to something for all the public members:

    for(Field member: myObject.getClass().getFields())
        // Or you can do: for(Field member: myClass.class.getFields())
    {
        member.getInt(myObject)); // Get the value
        member.setInt(myObject, 4); // Set the value
    }

Basically, what you do is that you find the Field object that represents the members of you object, then you can manipulate it.

Upvotes: 2

Peter Lawrey
Peter Lawrey

Reputation: 533680

Normally you would use a collection like a Map.

public class Storage{
    private final Map<String, Integer> inventory = ...

    public void addCount(String key, int count) {
        Integer i = inventory.get(key);
        if (i == null) i = 0;
        inventory.put(key, i + count);
    }

Upvotes: 3

Tony Ennis
Tony Ennis

Reputation: 12299

Most IDEs will generate setters and getters for you. This will do what you want with no bother or effort. If this is insufficient, write a method which uses reflection to set the values.

If you have a class with 15000 members, and by this I assume you mean variables private to a class, then you have other issues to resolve.

Upvotes: 1

Related Questions