0

I have a data generation project and I need to match the data generated to another variable.

eg. Man utd - liverpool returns Premier League barca - real madrid returns La Liga

I need to use a switch case method generateLeague which should act on String match variable from class MatchEvent. The coding I have done does not seem to catch either the MatchEvent match value generated and does not return a string as String leagueName = generateLeague does not receive a String. It says that generateLeague (String) in BetEvent cannot be applied.

This is the MatchEvent class with data generation (This does work).

    /**
 * Mock a transaction for testing purposes
 */
public MatchEvent generateEvent() {
    try {
        DataFactory df = new DataFactory();

        // BetEvent
        int betID = df.getNumberBetween(220000000, Integer.MAX_VALUE);
        float odds = df.getNumberBetween((int) 1.05, 30);
        // Selections
        String selectionID = UUID.randomUUID().toString();
        // Market
        // Event
        String eventID = UUID.randomUUID().toString();
        String match = df.getItem(StaticTestData.match, 80, "Liverpool vs Manchester Utd"); // should match league and categoryID
        //SubCategory
        // category
        final int categoryID = 1;       // LeagueID by number eg. 1 is for LaLiga, should match MatchLeague and Match
        final String categoryName = "Football";
        // Subcategory
        String subCategoryID = UUID.randomUUID().toString();
        String leagueName = generateLeague();      // should match Match and category ID
        final String parentSubCategory = null;
        // market
        final String name = null;
        float matchOdds = df.getNumberBetween((int) 1.05, 30);  // since focusing on prematch should keep maxNo 20?
        //betEvent
        float stake = df.getNumberBetween(0, 1200);
        boolean mobile = Math.random() < 0.5;
        final String providerName = "EPBetsSportsbook";
        float totalOdds = matchOdds;
        float totalStake = stake;
        String nation = df.getItem(StaticTestData.countryCodes);     // should make it as realistic as possible
        final String currency = "EUR";


        Date date = new Date();
        java.text.DateFormat formatter = new java.text.SimpleDateFormat("MM-dd-yyyy");
        String calendarDate = formatter.format(date);

        return new MatchEvent(betID, odds, selectionID, eventID, match, categoryID, categoryName, subCategoryID,
                leagueName, parentSubCategory, name, matchOdds, stake, mobile, providerName,
                totalOdds, totalStake, nation, date, currency, calendarDate);


    } catch (Exception e) {

        // For demo purposes, we are not going to log errors/send to a kafka stream

        throw e;
    }
}

And this is the generateLeague method using Switch case statements.

public static String generateLeague(String match) {

    String club = null;
    String league;
    if (match.toLowerCase().contains(" ")) {
        club = match.substring(0, match.indexOf(" "));
    }
        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 "atletico":
            case "barcelona":
            case "real":
                league = "La Liga";
                break;
                default: league = "";

        }

    return league;
}

I would appreciate and thank you in advance for any guidance.

5
  • 1
    which version of java are you using? Commented Feb 4, 2017 at 10:39
  • I am using Java 8 Commented Feb 4, 2017 at 10:41
  • 1
    Please remove everything that is unrelated to the question (I hardly think that you need the whole generateEvent method to reproduce your issue) and make sure that club really is one of these cases (use a debugger). You may also want to read: minimal reproducible example. Commented Feb 4, 2017 at 10:43
  • why you are not passing any String argument in generateLeague(String match) Commented Feb 4, 2017 at 10:43
  • You should learn how to use a debugger. Step through your code, line by line, until you find the problem. Commented Feb 4, 2017 at 10:44

1 Answer 1

1

If there are no spaces in String match, then club will be null. Then of course it won't switch properly. you need to add an else statement to your if statement. You also might want to make sure that club is lowercase. Like this:

if (match.toLowerCase().contains(" ")) {
    club = match.toLowerCase().substring(0, match.indexOf(" "));
} else {
    club = match.toLowerCase();
}

Another thing: in generateEvent: you have generateLeague(). This won't compile. Do generateLeague(match);

Sign up to request clarification or add additional context in comments.

3 Comments

But then wouldn't generateLeague() still return a null string in this case?
No it won't. If none of the things in the switch get run, the default will get run, and league would be the empty string. So it is always not-null.
Thanks for your help, it now does work and the problem is solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.