Valhalla Legends Forums Archive | Java Programming | My PropertyBag Class

AuthorMessageTime
JoeTheOdd
Anyone want to critique this? I thought it'd be interesting to write one of these, so eh. Possible uses, holding information on a user in a channel (variables Ping, Flags, Username, ChannelListIndex, Product, Statstring, etc), holding bot profile information, etc.

[tt]/*
* Class:  PropertyBag
* Package: Util
* Author:  Joe[e2]
* Purpose: This houses our variables, and their values.
*/

package util;

public class PropertyBag {

String names[]  = new String[255]; // The names of our variables
String values[] = new String[255]; // The values of our variables
int position = 0; // The position that we're inserting new variables in

/**
* @author Joe[e2]
* @param name: Variable name to return value for.
* @return: Value if variable exists, otherwise null.
*/
public String getValue(String name) {
for(int i = 0; i < 255; i++) {
if(names[i] == name) {
return values[i];
}
}
return null;
}

public void setValue(String name, String value) {
for(int i = 0; i < 255; i++) {
if(names[i] == name) {
values[i] = value;
return;
}
}
names[position]  = name;
values[position] = value;
position++;
return;
}

}[/tt]

Demonstration/test class:
[tt]/*
* Class:  Main
* Author:  Joe[e2]
* Purpose: Create + test our PropertyBag object.
*/

import util.PropertyBag;

public class Main {

public static void main(String args[]) {
PropertyBag pbag = new PropertyBag();
pbag.setValue("Name1", "Value1");
pbag.setValue("Name2", "Value2");
pbag.setValue("Name3", "Value3");
System.out.println(pbag.getValue("Name1"));
System.out.println(pbag.getValue("Name2"));
System.out.println(pbag.getValue("Name3"));
}

}[/tt]
December 4, 2005, 6:33 PM
K
Critique:
HashMap TreeMap

December 4, 2005, 6:50 PM
Kp
Why're you preallocating a fixed maximum length?  Let it grow dynamically.  That'll also avoid the problem that it can silently fail to store a property.
December 4, 2005, 6:51 PM

Search