0

I'm doing a project using semantic technologies to recommend clothing based on weather. I'm retrieving clothing items through a clothing ontology, and have gathered weather data and made an RDF model from it, using the Apache Jena API.

Everything is working OK, but i have ended up with multiple methods that contain huge if/ if-else statements that can be hard to read, and not at all optimal.

I have queries for both Weather and Clothing with SPARQL. Resultsets from each weather query have been made into Weather-objects, and then added them to lists based on weather dates. Each clothing item is just a Clothing-object.

Here is some of the code: Here, i'm setting weather-icons based locales.

private void setImageIcon(List<Weather> list, int index) {

    Image image = null;

    if (list.get(index).getWeatherType().equals("Skyet")) {
        image = setImage("GUI/Icons/dist/png/04.png");
    } else if (list.get(index).getWeatherType().equals("Lettskyet")) {
        image = setImage("GUI/Icons/dist/png/02d.png");
    } else if (list.get(index).getWeatherType().equals("Klarvær") || list.get(index).getWeatherType().equals("Sol")) {
        image = setImage("GUI/Icons/dist/png/01d.png");
    } else if (list.get(index).getWeatherType().equals("Delvis skyet")) {
        image = setImage("GUI/Icons/dist/png/03d.png");
    } else if (list.get(index).getWeatherType().equals("Regn")
            || list.get(index).getWeatherType().equals("Kraftig regn") || list.get(index).getWeatherType().equals("Lett regn")){
        image = setImage("GUI/Icons/dist/png/09.png");
    }else if (list.get(index).getWeatherType().equals("Regnbyger og torden") || list.get(index).getWeatherType().equals("Lette regnbyger og torden") || list.get(index).getWeatherType().equals("Kraftige regnbyger og torden")){
        image = setImage(("GUI/Icons/dist/png/06d.png"));
    }else if(list.get(index).getWeatherType().equals("Lette regnbyger") || list.get(index).getWeatherType().equals("Regnbyger") || list.get(index).getWeatherType().equals("Kraftige regnbyger")){
        image = setImage("GUI/Icons/dist/png/05d.png");
    }else if(list.get(index).getWeatherType().equals("Snøbyger") || list.get(index).getWeatherType().equals("Lette snøbyger") || list.get(index).getWeatherType().equals("Kraftige snøbyger")){
        image = setImage("GUI/Icons/dist/png/08d.png");
    }else if(list.get(index).getWeatherType().equals("Sluddbyger") || list.get(index).getWeatherType().equals("Lette sluddbyger") || list.get(index).getWeatherType().equals("Kraftige sluddbyger")){
        image = setImage("GUI/Icons/dist/png/07d.png");
    }else if (list.get(index).getWeatherType().equals("Regn og torden") || list.get(index).getWeatherType().equals("Lett regn og torden") || list.get(index).getWeatherType().equals("Kraftig regn og torden")){
        image = setImage("GUI/Icons/dist/png/22.png");
    }else if (list.get(index).getWeatherType().equals("Snø") || list.get(index).getWeatherType().equals("Lett snø") || list.get(index).getWeatherType().equals("Kraftig snø")){
        image = setImage("GUI/Icons/dist/png/13.png");
    }

Here is a locale of types of weather, where the key is an ID associated with a weather-icon, but i'm not sure if i should use a ResourceBundle or something else. "Klarvær" and "Skyet" means "clear" and "cloudy" in Norwegian.

{
"s01": "klarvær",
"s02": "lettskyet",
"s03": "delvis skyet",
"s04": "skyet",
"s40": "lette regnbyger",
"s05": "regnbyger",
"s41": "kraftige regnbyger",
"s24": "lette regnbyger og torden",
"s06": "regnbyger og torden";
}

Here, i'm querying clothing based on different weather-phenomenon.

@SuppressWarnings("Duplicates")
public MensClothing setMensClothingRecommendation(List<Weather> wList, int index) {
    MensClothing mensClothing = null;

    if (wList.get(index).getWeatherType().equals("Skyet")) {
        mensClothing = clothingQueries.mensToObject("Cloudy", returnWeatherString(wList, index), getSeasons(wList, index));
    }else if (wList.get(index).getWeatherType().equals("Delvis skyet")) {
        mensClothing = clothingQueries.mensToObject("Partly Cloudy", returnWeatherString(wList, index), getSeasons(wList, index));
    }else if (wList.get(index).getWeatherType().equals("Klarvær") || wList.get(index).getWeatherType().equals("Lettskyet")) {
        mensClothing = clothingQueries.mensToObject("Clear", returnWeatherString(wList, index), getSeasons(wList, index));
    } else if (wList.get(index).getWeatherType().equals("Regn") || wList.get(index).getWeatherType().equals("Regnbyger") || wList.get(index).getWeatherType().equals("Kraftig regn") || wList.get(index).getWeatherType().equals("Lett regn")) {
        mensClothing = clothingQueries.mensToObject("Wet", returnWeatherString(wList, index), getSeasons(wList, index));
    }else if (wList.get(index).getWeatherType().equals("Lette regnbyger og torden") || wList.get(index).getWeatherType().equals("Regnbyger og torden") || wList.get(index).getWeatherType().equals("Kraftige regnbyger og torden")){
        mensClothing = clothingQueries.mensToObject("Wet", returnWeatherString(wList, index), getSeasons(wList, index));
    }else if (wList.get(index).getWeatherType().equals("Lette sluddbyger") || wList.get(index).getWeatherType().equals("Sluddbyger") || wList.get(index).getWeatherType().equals("Kraftige sluddbyger")){
        mensClothing = clothingQueries.mensToObject("Wet", returnWeatherString(wList, index), getSeasons(wList, index));
    }else if (wList.get(index).getWeatherType().equals("Lette snøbyger") || wList.get(index).getWeatherType().equals("Snøbyger") || wList.get(index).getWeatherType().equals("Kraftige snøbyger")){
        mensClothing = clothingQueries.mensToObject("Wet", returnWeatherString(wList, index), getSeasons(wList, index));
    }else if (wList.get(index).getWeatherType().equals("Lette regnbyger") || wList.get(index).getWeatherType().equals("Kraftige regnbyger")){
        mensClothing = clothingQueries.mensToObject("Wet", returnWeatherString(wList, index), getSeasons(wList, index));
    }else if (wList.get(index).getWeatherType().equals("Lett regn og torden") || wList.get(index).getWeatherType().equals("Regn og torden") || wList.get(index).getWeatherType().equals("Kraftig regn og torden")){
        mensClothing = clothingQueries.mensToObject("Wet", returnWeatherString(wList, index), getSeasons(wList, index));
    }else if (wList.get(index).getWeatherType().equals("Sludd") || wList.get(index).getWeatherType().equals("Lett sludd") || wList.get(index).getWeatherType().equals("Kraftig sludd")){
        mensClothing = clothingQueries.mensToObject("Wet", returnWeatherString(wList, index), getSeasons(wList, index));
    }else if (wList.get(index).getWeatherType().equals("Lett snø") || wList.get(index).getWeatherType().equals("Snø") || wList.get(index).getWeatherType().equals("Kraftig snø")){
        mensClothing = clothingQueries.mensToObject("Wet", returnWeatherString(wList, index), getSeasons(wList, index));
    }else if (wList.get(index).getWeatherType().equals("Tåke")){
        mensClothing = clothingQueries.mensToObject("Cloudy", returnWeatherString(wList, index), getSeasons(wList, index));
    }
    return mensClothing;
}

The point of these last statements, is to check the incoming list for weather-types. If its equal to a certain string in the list, for example "Skyet", then it will query with "Cloudy" as its parameter, including the season and temperature, and return a MensClothing-object which will be displayed in the GUI.

I have looked into using case-statements, but i'm not sure if that would give me any less code as a result. I've search Stackoverflow but i cant seem to find a solution

  • For the first I'd consider using a Map, the second a switch – baao May 08 '18 at 17:09
  • 1
    1. Perhaps you shouldn't hardcode this logic in Java. 2. Do something with all these `wList.get(index).getWeatherType().equals`. 3. Translate everything into English. – Stanislav Kralin May 08 '18 at 17:22

1 Answers1

0

You can just use a switch, which is more readable.

   switch (list.get(index).getWeatherType()){
       case "Skyet": 
           image = setImage("GUI/Icons/dist/png/04.png");
           break;
       case "Regn":
       case "Kraftig regn": 
       case "Lett regn": 
           image = setImage("GUI/Icons/dist/png/09.png");
           break;
       // (...)
   }
Wish Master
  • 71
  • 1
  • 1
  • 7