Adds languages to monster cards.

This commit is contained in:
2020-09-01 23:13:17 -07:00
committed by Tom Hicks
parent 706b58fd2c
commit 8ff1cb8779
5 changed files with 138 additions and 65 deletions

View File

@@ -1,74 +1,37 @@
package com.majinnaibu.monstercards.models;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.Comparator;
import java.util.Objects;
public class Language implements Comparator<Language>, Comparable<Language> {
private String mName;
private boolean mSpeaks;
public Language(String name, boolean speaks) {
mName = name;
mSpeaks = speaks;
}
private String mName;
public String getName() {
return mName;
}
public void setName(String value) {
mName = value;
}
private boolean mSpeaks;
public boolean getSpeaks() {
return mSpeaks;
}
public void setSpeaks(boolean value) {
mSpeaks = value;
}
@Override
public int compareTo(Language o) {
if (this.mSpeaks && !o.mSpeaks) {
return -1;
}
if (!this.mSpeaks && o.mSpeaks) {
return 1;
}
return this.mName.compareToIgnoreCase(o.mName);
return this.getName().compareToIgnoreCase(o.getName());
}
@Override
public int compare(@NonNull Language o1, Language o2) {
if (o1.mSpeaks && !o2.mSpeaks) {
return -1;
}
if (!o1.mSpeaks && o2.mSpeaks) {
return 1;
}
return o1.mName.compareToIgnoreCase(o2.mName);
}
@Override
public boolean equals(@Nullable Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof Language)) {
return false;
}
Language otherLanguage = (Language) obj;
if (!Objects.equals(this.mName, otherLanguage.mName)) {
return false;
}
if (this.mSpeaks != otherLanguage.mSpeaks) {
return false;
}
return true;
public int compare(Language o1, Language o2) {
return o1.getName().compareToIgnoreCase(o2.getName());
}
}

View File

@@ -17,6 +17,7 @@ public class Monster {
mSkills = new HashSet<>();
mDamageTypes = new HashSet<>();
mConditionImmunities = new HashSet<>();
mLanguages = new HashSet<>();
}
private String mName;
@@ -730,22 +731,6 @@ public class Monster {
mTruesight = value;
}
private int mTelepathy;
public int getTelepathy() {
return mTelepathy;
}
public void setTelepathy(int value) {
mTelepathy = value;
}
private String mUnderstandsBut;
public String getUnderstandsBut() {
return mUnderstandsBut;
}
public void setUnderstandsBut(String value) {
mUnderstandsBut = value;
}
public String getSensesDescription() {
ArrayList<String> parts = new ArrayList<>();
@@ -770,4 +755,89 @@ public class Monster {
return StringHelper.join(", ", parts);
}
private HashSet<Language> mLanguages;
public Set<Language> getLanguages() {
return mLanguages;
}
public void addLanguage(Language value) {
mLanguages.add(value);
}
public void removeLanguage(Language value) {
mLanguages.remove(value);
}
public void clearLanguages() {
mLanguages.clear();
}
private int mTelepathy;
public int getTelepathy() {
return mTelepathy;
}
public void setTelepathy(int value) {
mTelepathy = value;
}
private String mUnderstandsBut;
public String getUnderstandsBut() {
return mUnderstandsBut;
}
public void setUnderstandsBut(String value) {
mUnderstandsBut = value;
}
public String getLanguagesDescription() {
ArrayList<String> spokenLanguages = new ArrayList<>();
ArrayList<String> understoodLanguages = new ArrayList<>();
for (Language language : mLanguages) {
if (language != null) {
if (language.getSpeaks()) {
spokenLanguages.add(language.getName());
} else {
understoodLanguages.add(language.getName());
}
}
}
Collections.sort(spokenLanguages);
Collections.sort(understoodLanguages);
String spokenLanguagesString = StringHelper.oxfordJoin(", ", ", and ", " and ", spokenLanguages);
String understoodLanguagesString = StringHelper.oxfordJoin(", ", ", and ", " and ", understoodLanguages);
String understandsBut = getUnderstandsBut();
boolean hasUnderstandsBut = understandsBut.length() > 0;
int telepathy = getTelepathy();
boolean hasTelepathy = telepathy > 0;
String telepathyString = String.format(Locale.US, ", telepathy %d ft.", telepathy);
if (spokenLanguages.size() > 0) {
if (understoodLanguages.size() > 0) {
return String.format(
"%s, understands %s%s%s",
spokenLanguagesString,
understoodLanguagesString,
hasUnderstandsBut ? " but " + understandsBut : "",
hasTelepathy ? telepathyString : "");
} else {
return String.format(
"%s%s%s",
spokenLanguagesString,
hasUnderstandsBut ? " but " + understandsBut : "",
hasTelepathy ? telepathyString : "");
}
} else {
if (understoodLanguages.size() > 0) {
return String.format(
"understands %s%s%s",
understoodLanguagesString,
hasUnderstandsBut ? " but " + understandsBut : "",
hasTelepathy ? telepathyString : "");
} else {
return String.format(
"%S%s",
hasUnderstandsBut ? "none but " + understandsBut : "",
hasTelepathy ? telepathyString : "");
}
}
}
}