0

In the program I am making, I am trying to get a formatted season name for a given season(formatted so it . I keep the formatted names in an interface, since if I were to use a map, it would be unnecessarily regenerated, since I don't make an instance of TeamBuilder

The Seasons interface:

public interface Seasons {
    /*
     * Contains a formatted list of seasons.
     * 
     * An interface is being used as an alternative to using a Map in the 
     * TeamBuilder class,  since calling parseTeam would have to build 
     * mappings for the seasons each time it
     * was called. This way, the formatted name can simply be grabbed
     */
    final String Skyrise = "Skyrise";
    final String Toss_Up = "Toss%20Up";
    final String Sack_Attack = "Sack%20Attack";
    final String GateWay = "Gateway";
    final String Round_Up = "Round%20Up";
    final String Clean_Sweep = "Clean%20Sweep";
    final String Elevation = "Elevation";
    final String Bridge_Battle = "Bridge%20Battle";
    final String Nothing_But_Net = "Nothing%20But%20Net";
    final String Starstruck = "Starstruck";
    final String In_The_Zone = "In%20The%20Zone";
    final String Turning_Point = "Turning%20Point";
}

The problem comes when I try to grab these seasons. My TeamBuilder class takes in an argument(String season), which is unformatted. My question is, is there any way that I can use a String argument for a method to get a specific item from an interface? This is the most preferable to using a HashMap, which would needlessly regenerate the same information

All these classes can be found on the Github page for this project.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Robert Engle
  • 45
  • 1
  • 7

2 Answers2

3

If you want to do it in a typed way, you can use Enum for this:

enum Season{
    Skyrise,Toss_Up, Sack_Attack;

    @Override
    public String toString() {
        switch(this){
            case Skyrise: return "Skyrise";
            case Toss_Up: return "Toss%20Up";
            case Sack_Attack: return "Sack_Attack";
            default: return "";
        }
    }
}
public class main{
    public static void printSeason(Seasons seasons){
        System.out.println(seasons);
    }
    public static void main(String[] args) {
        Seasons e = Seasons.Skyrise;
        printSeason(e);
        System.out.println(e);
    }
}

Since the compiler internally invokes the toString(), you can pass the argument as a Seasons or a String like my example.


And if you still want to use a map without "unnecessarily regenerated" you can use a static field with static initializer like this:

class Seasons {
    private static Map<String,String> map = new HashMap<>();
    static {
        map.put("Skyrise", "Skyrise");
        map.put("Toss_Up", "Toss%20Up");
    }
    public static String getFormatted(String key){
        return map.getOrDefault(key,"");
    }
}
class main{
    public static void main(String[] args) {
        System.out.println(Seasons.getFormatted("Skyrise"));
    }
}
cdxf
  • 5,501
  • 11
  • 50
  • 65
1

Just to integrate on Snoob answer you can have enum with fields, so:

enum Season
{
   Skyrise("Skyrise"),
   Toss_Up("Toss%20Up"), 
   Sack_Attack("Sack%20Attack")

   ;

   public final String fancyName;

   private Season(String fancyName)
   {
     this.fancyName = fancyName;
   }
}

You really have all the benefits without any drawback.

Jack
  • 131,802
  • 30
  • 241
  • 343