I started reading a Clean Code book and some other articles on the Internet about OOD and design patterns and I'm a little confused. I've started to code a little string to integer parser which currently parses a first integer in string larger than 0 or returns -1.
Could you look and give me some idea if this is written correctly with general rules of good programming? I'm not sure about usage of Index
instances in the IntegerSubstringer
class.
main
method
public static void main (String Args[])
{
IntegerParser parser = new IntegerParser();
System.out.println( parser.getFirstInt("Blaola") );
}
IntegerParser.java
package stringParser.integerParser;
public class IntegerParser
{
IntegerSubstringer substringer;
public IntegerParser()
{
substringer = new IntegerSubstringer();
}
public int getFirstPositiveInt(String textToParse)
{
String numberSubstring = substringer.cutFirstNumber(textToParse);
if(!numberSubstring.equals(""))
return parse(numberSubstring);
return -1;
}
private int parse(String substringContainingNumbersOnly)
{
return Integer.parseInt(substringContainingNumbersOnly);
}
}
IntegerSubstringer.java
package stringParser.integerParser;
public class IntegerSubstringer
{
IntegerFinder finder;
String text;
Index startIndex;
Index endIndex;
public IntegerSubstringer()
{
finder = new IntegerFinder();
}
public String cutFirstNumber( String text )
{
this.text = text;
setStartIndexAtTheFirstNumber();
iterateToTheEndOfTheNumberSeries();
if (startIndex.isInvalid()) return new String();
return makeSubstringAccordToIndexes();
}
private void iterateToTheEndOfTheNumberSeries()
{
setEndIndexAfterStartIndex();
while( endOfTextNotReached() && finder.letterIsANumber(text, endIndex) )
{
endIndex.increaseValue();
}
if (endIndex.isInvalid())
setEndIndexAfterStartIndex();
}
private String makeSubstringAccordToIndexes()
{
return text.substring(startIndex.getValue(), endIndex.getValue());
}
private void setStartIndexAtTheFirstNumber()
{
startIndex = finder.findFirstNumber(text);
}
private void setEndIndexAfterStartIndex()
{
endIndex = new Index(startIndex);
endIndex.increaseValue();
}
private boolean endOfTextNotReached()
{
return text.length() > endIndex.getValue();
}
}
IntegerFinder.java
package stringParser.integerParser;
import stringParser.LetterTypeArbiter;
public class IntegerFinder
{
private LetterTypeArbiter rater;
private Index index;
public IntegerFinder()
{
index = new Index();
rater = new LetterTypeArbiter();
}
public Index findFirstNumber ( String text )
{
for ( index.resetValue(); index.getValue() < text.length() ; index.increaseValue() )
if ( stringHasANumberAtIndex(text, index) )
return index;
return new InvalidIndex();
}
public boolean letterIsANumber ( String text, Index currentIndex )
{
index = new Index(currentIndex);
if ( stringHasANumberAtIndex(text, index) )
return true;
return false;
}
private boolean stringHasANumberAtIndex ( String text, Index index )
{
return rater.obtainsANumber( getLetterFromString(text, index) );
}
private int getLetterFromString ( String text, Index index )
{
return text.charAt( index.getValue() );
}
}
Index.java
package stringParser.integerParser;
public class Index
{
private int value;
public Index()
{
value = 0;
}
public Index( Index index )
{
value = index.getValue();
}
public int getValue()
{
return value;
}
public void resetValue()
{
value = 0;
}
public void increaseValue()
{
value++;
}
public boolean isInvalid()
{
return value < 0;
}
}
abc123
, it should return123
? This is really quite a complicated solution if that's the case... – h.j.k. May 4 at 0:52