I was given the below scenario to write a java program by a Software company. I did my best and was well tested as well. They came back rejecting my application without giving any review comments. I would like to know what I did wrong so that I can improve on what is missing.
The test was easy and the code is in the github link. Can someone review my code and point out my mistakes? Proper use of object oriented design to ensure extensibility. As an example, how much would the design have to change if 10 new patterns were to be added?
Evaluation criteria:
Loose coupling between input source, core logic, and output source.
Unit tests that test behavior instead of just methods.
Program should be packaged such that no setup is required to make it run.
Write a program (source and unit tests) "PatternCounter" that reads an input document from a file (path provided as first command line argument) and calculates the number of occurrences of one of the 3 patterns based on the second command line argument:
1: Counts occurrences of each unique word in the document
2: Counts occurrences of each unique number in the document
3: Counts occurrences of each unique phrase of three consecutive words in the document
It then prints each word, number or phrase and its count on standard output separated by a comma in a new line. For the purpose of this exercise, use space character as the de-limiter for words. String matching should be case sensitive. Here are some examples of output with a sample input file using different arguments:
Contents of Input document (Input.txt): "1000 a big surprise 2000 hello is a big surprise 1000"
PatternCounter Input.txt 1 a, 2 big, 2 surprise, 2 hello, 1 is, 1
PatternCounter Input.txt 2 1000, 2 2000, 1
PatternCounter Input.txt 3 1000 a big, 1 a big surprise, 2 big surprise 2000, 1 surprise 2000 hello, 1 2000 hello is, 1 hello is a, 1 is a big, 1 big surprise 1000, 1
The output could be empty if there are less than 3 words in the document.
As there are a lot of classes, please check this link for full code.
Code:
Main Class:
public class PatternCounterRunner {
private static PatternCounterService patternCounterService;
public static void main(String[] args) {
/*inject Keyboard and console to IO runner*/
IORunner io = new IORunner(new KeyBoardInput(), new ConsoleOutput());
String inputs[] = io.getUserInputs();
patternCounterService = new PatternCounterServiceImpl();
Map<String, Integer> result = null;
/*Service call to find and return the distinct pattern*/
try {
result = patternCounterService.findPatternFromFile(inputs[0], inputs[1]);
} catch (Exception e) {
System.err.println("Run time exception occured :" + e);
}
io.printResult(result);
}
}
service class:
public class PatternCounterServiceImpl implements PatternCounterService {
PatternFactory patternFactory = new PatternFactory();
IPatternCounter patternCounter;
@Override
public Map<String, Integer> findPatternFromFile(String file, String patternType) throws Exception {
Map<String, Integer> output = null;
if (file == null || patternType == null) {
return output;
} else {
patternCounter = patternFactory.getPatternCounter(patternType);
}
String[] values = PatternCounterUtils.getContentsFromFile(file);
if (patternCounter != null) {
output = patternCounter.getCount(values[0], values[1]);
} else {
throw new Exception("Pattern type entered is null or not a number");
}
return output;
}
}
this is my factory method to get appropriate counter:
public class PatternFactory {
/**
* Return an instance of {@linkplain IPatternCounter}
* @param type - type of pattern counter
* @return {@linkplain IPatternCounter} instance
* <br>
* Presently supported pattern counters with type are:
* <br>
* type = 1 => {@link WordCounter}<br>
* type = 2 => {@link NumberCounter}<br>
* type = 3 => {@link PhraseCounter}<br>
*
*/
public IPatternCounter getPatternCounter(String type) {
IPatternCounter patternCounter = null;
if ((type == null) && PatternCounterUtils.isNumeric(type)) {
return patternCounter;
} else {
type = type.trim();
if (type.equals("1")) {
patternCounter = new WordCounter();
} else if (type.equals("2")) {
patternCounter = new NumberCounter();
} else if (type.equals("3")) {
patternCounter = new PhraseCounter();
}
}
return patternCounter;
}
}