2017-02-01 65 views
0

我正在做一個足球博彩的數據生成項目,它的工作原理。爲了避免使用開關情況(因爲生成了大量的燈具),我使用lambda表達式來簡化事情。不過,我對Java相當陌生並陷入困境。在java中返回一個字符串值到一個lambda參數

我想來比賽名稱對聯賽Fixtures ...

eg。利物浦 - 曼聯應該返回英超 而 巴薩 - 皇馬應該返回西甲

public static String english() {return "Premier League";} 
public static String spanish() {return "La Liga" ;} 
public static String french() {return "Ligue 1";} 
public static String german() {return "Bundesliga";} 
public static String italian() {return "Seria A";} 

用於lambda表達式的值是從這裏使用fluttercode產生燈具產生的。

String match = df.getItem(StaticTestData.match, 80, "Liverpool vs Manchester Utd") 

我用比較。當字符串的一部分()lambda表達式

DefineLeague.of(match) 
       .when("Arsenal",() -> english()).when("Bournemouth",() ->english()).when("Burnley",() -> english()) 
       .when("Chelsea",() -> english()).when("Crystal Palace",() -> english()) 
       .when("Everton",() -> english()).when("Hull City",() -> english()).when("Leicester City",() -> english()) 
       .when("Liverpool",() -> english()).when("Manchester City",() -> english()).when("Manchester Utd",() -> english()) 
       .when("Middlesborough",() -> english()).when("Southampton",() -> english()).when("Stoke City",() -> english()) 
       .when("Sunderland",() -> english()).when("Swansea",() -> english()).when("Tottenham",() -> english()) 
       .when("Watford",() -> english()).when("West Brom",() -> english()).when("West Ham Utd",() -> english()); 

,而應返回到

String leagueName = ; 

的DefineLeague類如下:

package com.sportsRecommendationsData.data.dataGeneration; 

public class DefineLeague { 

private static final DefineLeague DEFINED_LEAGUE = new DefineLeague(null) { 
    @Override 
    public DefineLeague when(String subString, Runnable r) { 
     return this; 
    } 

    @Override 
    public void orElse(Runnable r) { 

    } 
}; 

private final String str; 

private DefineLeague(String str) { 
    this.str = str; 
} 

public DefineLeague when(String subString, Runnable r) { 
    if(str.contains(subString)) { 
     r.run(); 
     return DEFINED_LEAGUE; 
    } 
    return this; 
} 

public void orElse(Runnable r) { 
    r.run(); 
} 

public static DefineLeague of(String str) { 
    return new DefineLeague(str); 
} 

} 

我會很高興感謝任何有關如何將靜態值傳遞給參數leagueName的指導,因爲我被卡住了,我不確定我是否做了適當的編碼。在此先感謝

+0

爲什麼不只是使用枚舉而不是lambda表達式? 'leagueName'可能是enum的一個字段,而ENGLISH,FRENCH等可能是enum的值。 –

+0

你想做什麼?我從來沒有見過這樣的代碼,我無法遵循它。我懷疑你需要使用國家的「地圖」到聯盟名稱,並給球隊一個國家屬性。然後你的代碼看起來像'String leagueName = map.get(team.getCountry());'。或者你可以走得更遠,並且有'Country'和'League'類,所以'String leagueName = team.getCountry()。getLeague()。getName();' – Bohemian

+0

順便說一下,這種類型的程序應該真的支持一個數據庫,而不是加載硬編碼的字符串。 –

回答

0

@Holger這就是我確實是和工作完美精細。我仍然是一名學生,一直在學習。正確的實現如下所示;

public static String generateLeague(String match) { 

    String club = null; 
    String league; 
    if (match.toLowerCase().contains(" ")) { 
    club = match.toLowerCase().substring(0, match.indexOf(" ")); 
    } else { 
    club = match; 
    } 
    switch (club) { 
    case "arsenal": 
    case "bournemouth": 
    case "burnley": 
    case "chelsea": 
    case "crystal": 
    case "everton": 
    case "hull": 
    case "leicester": 
    case "liverpool": 
    case "manchester": 
    case "middlesborough": 
    case "southampton": 
    case "stoke": 
    case "sunderland": 
    case "swansea": 
    case "tottenham": 
    case "watford": 
    case "west": 
     league = "Premier League"; 
     break; 
    case "angers": 
    case "bastia": 
    case "bordeaux": 
    case "caen": 
    case "dijon": 
    case "guingamp": 
    case "lille": 
    case "lorient": 
    case "lyon": 
    case "marseille": 
    case "metz": 
    case "monaco": 
    case "montpellier": 
    case "nancy": 
    case "nantes": 
    case "nice": 
    case "psg": 
    case "rennes": 
    case "saint-ettiene": 
    case "toulouse": 
     league = "Ligue 1"; 
     break; 
    case "alavés": 
    case "athletic": 
    case "atlético": 
    case "barcelona": 
    case "celta": 
    case "deportivo": 
    case "eibar": 
    case "espanyol": 
    case "granada": 
    case "las": 
    case "leganes": 
    case "malaga": 
    case "osasuna": 
    case "real": 
    case "sevilla": 
    case "sporting": 
    case "valencia": 
    case "villarreal": 
     league = "La Liga"; 
     break; 
    case "1.": 
    case "1899": 
    case "bayer": 
    case "bayern": 
    case "borussia": 
    case "darmstadt": 
    case "eintracht": 
    case "fc": 
    case "hamburger": 
    case "hertha": 
    case "mainz": 
    case "rb": 
    case "sc": 
    case "schalke": 
    case "vfl": 
    case "werder": 
     league = "Bundesliga"; 
     break; 
    case "ac": 
    case "as": 
    case "atalanta": 
    case "bologna": 
    case "cagliari": 
    case "chievo": 
    case "crotone": 
    case "empoli": 
    case "fiorentina": 
    case "genoa": 
    case "internazionale": 
    case "juventus": 
    case "lazio": 
    case "napoli": 
    case "palermo": 
    case "sampdoria": 
    case "sassuolo": 
    case "torino": 
    case "udinese": 
    case "us": 
     league = "Seria A"; 
     break; 
    default: league = null; 

    } 

    return league; 
} 

並且輸出傳遞給匹配變量;

String match = df.getItem(StaticTestData.match, 95, "Liverpool vs Manchester Utd"); 
0

爲什麼你在這裏使用runnable? Runnables用於多線程並且不返回值。一個更好的選擇將是一個Supplier這是一種功能性的界面,需要沒有輸入和返回結果

你可能想要寫when這樣

public DefineLeague when(String subString, Supplier<String> s) { 
    if(str.contains(subString)) { 
    return new(DefineLeague(s.get())); 
    } 
return this; 
}