I am a newb, do not go to college and am learning Java on my own time. This little project is a learning exercise so I ask that any help just points me toward a right direction. It is a character generator for a table top game, that may or may not ever be used.
What I want to have is a method "addAttribute(String attName)" that searches for that attribute name in an array (String attributeName[]) finds its index, finds the corresponding index in my int attribute[], performs a check to see if there are enough CPs available and if so increments the correct int attribute up, removing CP from its available pool.
My question is; should I be learning/using another tool (probably much later in the book I am following) to get this kind of method to work?
Linked list or another interface?
Some code (messy, it compiles but does not work quite right):
class Attributes {
public String cName = null;
public int cpSpent = 0;
public int cpAvailable = 0;
//various getters/setters for these
public int[] attributes = {0,0,0,0,0,0};
public String[] attributeNames = {"vigor", "agility", "cunning",
"presence", "reason", "knowledge"};
//where the issue lies
public void addAttribute(String att){
for(int i = 0; i < attributeNames.length; i++){
int index = attributeNames[i].indexOf(att);
if(att == attributeNames[i]){
int cost = (attributes[index] * 2) + 1;
while(cost >= cpAvailable){
System.out.println("You do not have enough CP available.");
return;
}
this.cpAvailable = cpAvailable - cost;
this.cpSpent = cpSpent + cost;
this.attributes[index] = attributes[index] + 1;
}
public int getVigor() {
return attributes[0];
}
public void setVigor(int vigor) {
this.attributes[0] = attributes[0];
}
public int getAgility() {
return attributes[1];
}
public void setAgility(int agility) {
this.attributes[1] = attributes[1];
}
public int getCunning() {
return attributes[2];
}
public void setCunning(int cunning) {
this.attributes[2] = attributes[2];
}
public int getPresence() {
return attributes[3];
}
public void setPresence(int presence) {
this.attributes[3] = attributes[3];
}
public int getReason() {
return attributes[4];
}
public void setReason(int reason) {
this.attributes[4] = attributes[4];
}
public int getKnowledge() {
return attributes[5];
}
public void setKnowledge(int knowledge) {
this.attributes[5] = attributes[5];
}
void print(){
System.out.println(
"Char Name: \t" + getcName() + "\n" +
"CP(total): \t" + getCpTotal() + "\n" +
"Available CP: \t" + getCpAvailable() + "\n" +
"Attribute\tValue\n" +
"Vigor: \t\t" + getVigor() + "\n" +
"Agility: \t" + getAgility() + "\n" +
"Cunning: \t" + getCunning() + "\n" +
"Presence: \t" + getPresence() + "\n" +
"Reason: \t" + getReason() + "\n" +
"Knowledge: \t" + getKnowledge() + "\n");
}
public class Stats {
public static void main(String[] args){
Attributes a = new Attributes();
a.print();
//a.addCp(500);
a.addCp(200);
a.setcName("Alourn Malice");
a.addAttribute("vigor");
a.addAttribute("vigor");
a.addAttribute("vigor");
a.addAttribute("vigor");
a.addAttribute("agility");
a.addAttribute("agility");
a.addAttribute("agility");
a.addAttribute("vigor");
a.addAttribute("vigor");
a.addAttribute("vigor");
a.print();
a.addAttribute("vigor");
a.print();
}
}