public static void commandLine(ArtistRepository repository) { Artist artist = null; while (true) { String[] command = getInput(); if (command.length > 0) { switch (command[0]) { case "generate": if (artist == null) { System.out.println("Search for an artist before generating text."); break; } System.out.println(artist.getMarkovModel().generate(Integer.parseInt(command[1]))); break; case "search": String searchQuery = ""; for (int i = 1; i < command.length; i++) { searchQuery += command[i] + " "; } artist = RapGenius.searchArtist(searchQuery.trim()); Artist databaseQuery = checkDatabaseForArtist(artist.getArtistName(), repository); if (databaseQuery != null) { System.out.println("Artist already stored in database."); artist = databaseQuery; } else { RapGenius.populateSongsFromArtistPage(artist); RapGenius.populateSongLyricsFromSongs(artist); repository.save(artist); } break; case "markov": artist.populateArtistMarkov(Integer.parseInt(command[1])); System.out.println( "Created Markov model with an order = " + Integer.parseInt(command[1])); break; default: System.out.println( "Welcome to Markov Chainz.\n\nCommands:\n" + "\n\tsearch <artist name>" + "\n\tgenerate <number of words>" + "\n\tmarkov <order>"); break; } } } }
public static Artist checkDatabaseForArtist(String artistName, ArtistRepository repository) { return repository.findByArtistName(artistName); }