Created
May 2, 2014 11:28
-
-
Save l-ray/11472754 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.lray.intercom.highscore; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
/** | |
* Runner class for command line calls. | |
* | |
* Extracts the given parameter, transforms them to the sub components and shows the result. | |
*/ | |
public class TopNRunner { | |
private static final String RESULT_MESSAGE = "\n\nThe following numbers are the top positions:\n"; | |
private static final String FILE_NOT_FOUND_MESSAGE = "The file could not be found: "; | |
private static final String POSITION_IS_NAN_MESSAGE = "Position parameter is not a number."; | |
private static final String PARAMETER_MISSING = "Please use the following parameter: <filename> [<numberOfRanks>]"; | |
private static final String IO_EXCEPTION_MESSAGE = "IOException occurred:"; | |
public static void main(String[] args) { | |
String fileName = ""; | |
int numRankings = TopNStrategy.DEFAULT_HIGHSCORE_LENGTH; | |
try { | |
fileName = args[0]; | |
if (args.length > 1) { | |
numRankings = Integer.valueOf(args[1]); | |
} | |
FileTraverser importer = FileTraverser.createInstance( | |
fileName, | |
TopNStrategy.createInstance(numRankings), | |
System.out | |
); | |
Integer[] highScore = importer.traverseWithStrategy(); | |
System.out.println(RESULT_MESSAGE); | |
for (int i:highScore) { | |
System.out.println("->"+ i); | |
} | |
} catch (FileNotFoundException fnfe) { | |
System.out.println(FILE_NOT_FOUND_MESSAGE + fileName); | |
} catch (IOException ioe) { | |
System.out.println(IO_EXCEPTION_MESSAGE + ioe.getMessage() ); | |
} catch (NumberFormatException nfe) { | |
System.out.println(POSITION_IS_NAN_MESSAGE); | |
} catch (IndexOutOfBoundsException iobe) { | |
System.out.println(PARAMETER_MISSING); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment