Как я могу распечатать несколько классов в arraylist
I'm a first grader of a software engineering study and I have some trouble with a showcase I have to make. I give you a quick rundown of my project. I don't know if anyone is familiar with the game Clash Royal? Its a game based on getting higher in trophies and collecting cards. And each arena unlocks new cards. I want to make a deck builder. Where you can select 8 cards, and I calculate the average elixer cost. I want a function that can display all the cards available for your trophy range, and a function to see what cards you have unlocked in the arena you are at. With that out of the way, let me explain my problem: I made a Superclass: Card, and the classes Troop, Spell, DefBuilding, and PassiveBuilding extend on that superclass. After that, Then I made a Class: Arena, and in my main, I wanna declare al the arena's. The thing I am struggling with is the fact that I don't know how I can print all the cards in a vector of the matching Arena. In 1 arena you have multiple classes, for ex. you have spells and troops in the arraylist. So the actual question is as follow: How can i print out a list of all the cards that are in that arraylist, showing the name and elixer (only the attributes in the superclass Card).
MAIN: package logic; import data.Arena; import data.Card; import data.Spell; import data.Troop; import java.util.ArrayList; public class Main { public static void main(String[] args) { /* Create Arena's */ Arena trainingCamp = new Arena("Training Camp", 0, 0); /* Create Cards Arena 1 */ Card arrow = new Spell("Arrows", 3, 243, 86, 4); Card minions = new Troop("Minions", 3, 84, 84, 190, 2, 1, "Medium", 0, 3); /* Create ArrayList Arena 1 */ ArrayList<Card> trainingCampList = new ArrayList<>(); /*Put cards in the arraylist*/ trainingCamp.getTrainingCampList().add(arrow); trainingCamp.getTrainingCampList().add(minions); /*Print cards out on the screen*/ for (Card card : trainingCampList) { System.out.println(card.toString()); }
ARENA: package data; import java.util.ArrayList; public class Arena { private String name; private int minTrophies; private int maxTrophies; public Arena(String name, int minTrophies, int maxTrophies) { this.name = name; this.minTrophies = minTrophies; this.maxTrophies = maxTrophies; } ArrayList<Card> trainingCampList = new ArrayList<>(); public ArrayList<Card> getTrainingCampList() { return trainingCampList; } public void setTrainingCampList(ArrayList<Card> trainingCampList) { this.trainingCampList = trainingCampList; } }
CARD: package data; public class Card { private String name; private int elixer; public Card(String name, int elixer) { this.name = name; this.elixer = elixer; } @Override public String toString() { return "Card{" + "name='" + name + '\'' + ", elixer=" + elixer + '}'; } }
TROOP: package data; public class Troop extends Card { private int dammage; private int dammagePerSecond; private int hitpoints; private double range; private double hitSpeed; private String speed; private int deathDammage; private int count; public Troop(String name, int elixer, int dammage, int dammagePerSecond, int hitpoints, double range, double hitSpeed, String speed, int deathDammage, int count) { super(name, elixer); this.dammage = dammage; this.dammagePerSecond = dammagePerSecond; this.hitpoints = hitpoints; this.range = range; this.hitSpeed = hitSpeed; this.speed = speed; this.deathDammage = deathDammage; this.count = count; } }
SPELL: package data; public class Spell extends Card{ private int dammage; private int crowTowerDammage; private double radius; public Spell(String name, int elixer, int dammage, int crowTowerDammage, double radius) { super(name, elixer); this.dammage = dammage; this.crowTowerDammage = crowTowerDammage; this.radius = radius; } }
Что я уже пробовал:
The last ting I tried was to add a to a to string method in my class card that to print out the name and elixer. I only put 1 Troop card and 1 spell Card in the list to see if it should word. But if failed. I got no errors, so i dont know what i am doing wrong :( NOTE: I only put the classes of Spell and Troop in the question, i dont need the other classes. If i can make this work. I can do it for all the classes.