Da Sie immer fünf Kategorien haben, haben Sie hier zwei Möglichkeiten:
. Entweder Sie legen jedes von ihnen Attribute der Klasse hat
public class Card {
Category yearOfRelease;
Category ramSize;
Category cpuSpeed;
Category nbrOfGames;
Category geekFactor;
...
}
. Oder Sie könnten sie in einer Sammlung haben:
public class Card {
Collection<Category> categories;
...
}
Ich würde Ihnen raten, den zweiten Ansatz zu folgen.
Als eine mögliche Lösung, könnten Sie eine Map anstelle eines List verwenden, weil es Ihnen erlauben würde, später mit Ihrem bot, direkt jede Ihrer Kategorien zugreifen.
So konnten Sie durch die Schaffung einer Aufzählung beginnen, wo Sie Ihre Kategorietypen setzen würde:
public enum CategoryType {
YEAR_OF_RELEASE,
RAM_SIZE,
CPU_SPEED,
NUMBER_GAMES,
GEEK_FACTOR;
}
Die Definition Ihrer Category
Klasse wie folgt aussehen könnte:
public class Category {
// The category type
private CategoryType categoryType;
// The category value
private int value;
public Category(CategoryType categoryType, int value) {
this.categoryType = categoryType;
this.value = value;
}
// getters ands setters
}
Ihre Card
Klasse könnte auf folgende Weise implementiert werden:
public class Card {
// Defines the best category (for later use)
private CategoryType bestCategoryType;
// Holds all categories, easily accessible by type
private Map<CategoryType, Category> categories;
public Card() {
categories = new HashMap<>();
// by default, the year of release will be the best category
bestCategory = CategoryType.YEAR_OF_RELEASE;
}
public void createCategory(CategoryType categoryType, int value, boolean isBest) {
createCategory(categoryType, value);
if (isBest) {
bestCategoryType = categoryType;
}
}
public void createCategory(CategoryType categoryType, int value) {
Category category = new Category(categoryType, value);
categories.put(categoryType, category);
}
// Attains the best category
public CategoryType getBestCategoryType() {
return bestCategoryType;
}
// Attains the map of categories
public Map<CategoryType, Category> getCategories() {
return categories;
}
}
Schließlich zeigt die folgenden ein Beispiel, wie Sie es verwenden könnte:
public class TopTrumps {
public static void main(String[] args) {
// list of cards per user
List<Card> cards = new ArrayList<>();
Card card1 = new Card();
card1.createCategory(CategoryType.YEAR_OF_RELEASE, 1995);
card1.createCategory(CategoryType.RAM_SIZE, 128);
card1.createCategory(CategoryType.CPU_SPEED, 300);
card1.createCategory(CategoryType.NUMBER_GAMES, 1000);
// for card #1, the geek factor will be the best category
card1.createCategory(CategoryType.GEEK_FACTOR, 7, true);
// for card #2, since the best category, by default, is the year of
// release, we'll leave it as it is
Card card2 = new Card();
card2.createCategory(CategoryType.YEAR_OF_RELEASE, 2005);
card2.createCategory(CategoryType.RAM_SIZE, 256);
card2.createCategory(CategoryType.CPU_SPEED, 500);
card2.createCategory(CategoryType.NUMBER_GAMES, 750);
card2.createCategory(CategoryType.GEEK_FACTOR, 4);
// we'll add each card to our list
cards.add(card1);
cards.add(card2);
// then, in order to access each category, one could do the following
for (Card card : cards) {
// retrieve the best category
Category bestCategory = card.getCategories().get(card.getBestCategoryType());
System.out.println("Best category: " + bestCategory.getCategoryType() + "=" + bestCategory.getValue());
System.out.println("Card categories: ");
// retrieve each card's category value
for (Category category : card.getCategories().values()) {
System.out.println(category.getCategoryType() + "=" + category.getValue());
}
}
}
}
Sie sollten in unserem 'Card' Klasse eine Liste der Kategorien haben:' Liste categoryList'. –
aribeiro
Danke. Könntest du das weiter erläutern? Liste ist ein neues Konzept für mich. – Jason
Im Grunde sehen Sie eine Liste als Tasche, wo Sie Elemente eines Typs speichern können. Bevor Sie fortfahren, schlage ich vor, dass Sie sich die [Dokumentation] (https://docs.oracle.com/javase/7/docs/api/java/util/List.html) ansehen, um ihre Vorteile zu verstehen . Werfen Sie auch einen Blick auf [ArrayList] (https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html), das eine der Implementierungen von List ist und vielleicht am besten geeignet ist für dich. – aribeiro