This is what I have so far:
I need help with the addorReplace method, which adds entry alphabetically, and updates the number if the node exists. Please help! I can't get it add alphabetically!
public class OABlist{
private static class ABLNode{
public String name;
public int number;
public ABLNode (String in_name, int in_number){
name = in_name;
number = in_number;
}
}
private ABLNode[] data; //phoneentry
private int size;
private int capacity;
private static final int initial_cap = 5;
public OABlist(){
size =0;
capacity = initial_cap;
data = new ABLNode[capacity];
}
public int size() {
return size;
}
private int getName(String name) {
for (int i = 0; i < size; i++) {
if (data[i].name.equals(name)){
System.out.println(data[i].name);
return i;// The name found at index i
}
}
return -1; // The name DNE in list.
}
public int getNumber(String name) {
int j = getName(name);
if (j == -1)
return -1; // name not found
else
return data[j].number;
}
public boolean addorReplace(String name, int number){
if (name == null || number == -1)
throw new IllegalArgumentException("Name/Number are null.");
int i = getName(name);
if (i >= 0) {
data[i].number = number;// Name exists in position i, replace number.
}
else { //add new name/number to list. if list = full, create larger array
if(size == capacity){
ABLNode[] newPair = new ABLNode[2 * size];
System.arraycopy(newPair,0,data,0,capacity);
data = newPair;
}
data[size] = new ABLNode(name, number);
size++;
}
return true;
}
public void showList(){
int i;
for(i = 0; i >= 0 || i < size; i++){
System.out.println("List: " + data[i].name + " " + data[i].number );
}
}
public static void main(String[] args){
OABlist myList = new OABlist();
myList.addorReplace("Joe", 123);
myList.addorReplace("Jim", 123);
myList.addorReplace("Al", 123);
myList.addorReplace("Bill", 123);
myList.addorReplace("Carl", 123);
myList.showList();
}
}