user4313659 user4313659 - 3 months ago 8
Java Question

Trying to condense code that references a spreadsheet

I'm writing a bot for a Discord server that allows people to look up information from a spreadsheet about a video game, including skills that characters may have. Characters are sorted by base attack power, abbreviated as BP.

I allow filtering by character class, and parentheses are put around characters who don't start with that skill but may obtain it via an item (this is why at some points it looks for something equaling

(skill + "*")
. My code is as follows:

private String getSkillUsers(String skill, String class) {

String bp90 = "";
String bp80 = "";
String bp70 = "";
String bp60 = "";
String bp50 = "";
String bp40 = "";
String bp30 = "";

//lfChars is a ListFeed from a Google Spreadsheet containing the chart I am referencing
for (ListEntry le : Main.lfChars.getEntries())
{
CustomElementCollection cec = le.getCustomElements();

if (type.equals(""))
{
if (cec.getValue("skill").equalsIgnoreCase(skill))
{
switch (Integer.valueOf(cec.getValue("bp")))
{
case 30: bp30 += cec.getValue("name") + ", "; break;
case 40: bp40 += cec.getValue("name") + ", "; break;
case 50: bp50 += cec.getValue("name") + ", "; break;
case 60: bp60 += cec.getValue("name") + ", "; break;
case 70: bp70 += cec.getValue("name") + ", "; break;
case 80: bp80 += cec.getValue("name") + ", "; break;
case 90: bp90 += cec.getValue("name") + ", "; break;
}
}
else if (cec.getValue("skill").equalsIgnoreCase(skill + "*"))
{
switch (Integer.valueOf(cec.getValue("bp")))
{
case 30: bp30 += "(" + cec.getValue("name") + "), "; break;
case 40: bp40 += "(" + cec.getValue("name") + "), "; break;
case 50: bp50 += "(" + cec.getValue("name") + "), "; break;
case 60: bp60 += "(" + cec.getValue("name") + "), "; break;
case 70: bp70 += "(" + cec.getValue("name") + "), "; break;
case 80: bp80 += "(" + cec.getValue("name") + "), "; break;
case 90: bp90 += "(" + cec.getValue("name") + "), "; break;
}
}
}
else
{
if (cec.getValue("skill").equalsIgnoreCase(skill) && cec.getValue("type").equalsIgnoreCase(type))
{
switch (Integer.valueOf(cec.getValue("bp")))
{
case 30: bp30 += cec.getValue("name") + ", "; break;
case 40: bp40 += cec.getValue("name") + ", "; break;
case 50: bp50 += cec.getValue("name") + ", "; break;
case 60: bp60 += cec.getValue("name") + ", "; break;
case 70: bp70 += cec.getValue("name") + ", "; break;
case 80: bp80 += cec.getValue("name") + ", "; break;
case 90: bp90 += cec.getValue("name") + ", "; break;
}
}
else if (cec.getValue("skill").equalsIgnoreCase(skill + "*") && cec.getValue("type").equalsIgnoreCase(type))
{
switch (Integer.valueOf(cec.getValue("bp")))
{
case 30: bp30 += "(" + cec.getValue("name") + "), "; break;
case 40: bp40 += "(" + cec.getValue("name") + "), "; break;
case 50: bp50 += "(" + cec.getValue("name") + "), "; break;
case 60: bp60 += "(" + cec.getValue("name") + "), "; break;
case 70: bp70 += "(" + cec.getValue("name") + "), "; break;
case 80: bp80 += "(" + cec.getValue("name") + "), "; break;
case 90: bp90 += "(" + cec.getValue("name") + "), "; break;
}
}
}
}

String skillUsers = "";

if (!bp90.equals(""))
{
skillUsers += "90: " + bp90.substring(0, bp90.length() - 2) + Main.LS;
}
if (!bp80.equals(""))
{
skillUsers += "80: " + bp80.substring(0, bp80.length() - 2) + Main.LS;
}
if (!bp70.equals(""))
{
skillUsers += "70: " + bp70.substring(0, bp70.length() - 2) + Main.LS;
}
if (!bp60.equals(""))
{
skillUsers += "60: " + bp60.substring(0, bp60.length() - 2) + Main.LS;
}
if (!bp50.equals(""))
{
skillUsers += "50: " + bp50.substring(0, bp50.length() - 2) + Main.LS;
}
if (!bp40.equals(""))
{
skillUsers += "40: " + bp40.substring(0, bp40.length() - 2) + Main.LS;
}
if (!bp30.equals(""))
{
skillUsers += "30: " + bp30.substring(0, bp30.length() - 2) + Main.LS;
}

return skillUsers;


This is very difficult to edit whenever I want to change something. Is there any way I can make this shorter? I can add extra methods if necessary.

Answer

You can create a Map called baseAttackPower instead of having bp30-bp90.

Map<String, String> baseAttackPowers = new HashMap<>();

Then you can get and put entries into it like so:

baseAttackPowers.put("123", "456");

Instead of the initial switch, you can do something like this:

String value = cec.getValue("bp");
String bp = baseAttackPowers.get(value);
baseAttackPowers.put(value, bp + cec.getValue("name") + ", ");

You can condense the chain of if statements in a similar way.

Comments