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();
	}
}

Recommended Answers

All 3 Replies

What is the purpose of the addAttribute() method?
What are 'CP's?

Sounds like you are implementing a text-based game...

Anyway, a suggestion is that you should use HashMap instead of 2 arrays. The reason is that HashMap will map your attribute name with a value (in this case is an integer). Using 2 arrays could create a mess later on because you need to keep checking in both arrays regardless you are updating them or not. You can read HashMap on java 6 API doc. If you do not know how to use it, please post again. Good luck on your game.

NormR1: Its purpose is to check to see if there are enough CP (character points, the equivalent of experience points, but something you can spend to raise an attribute, skills and abilities).

Taywin: As your name suggests you are. I am pretty sure that is exactly what I am looking for and 5 chapters in front of me in the book I am reading. I can read the api docs, not well, but between those and the internet I am having fun.

Thank you for the super fast replies!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.