Skip to content
Snippets Groups Projects
Commit fb57f2ea authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Reversed toString() default behavior

Now by default pads the string with newlines to fill the StringBox
parent 8f8c9c98
No related branches found
No related tags found
No related merge requests found
......@@ -32,10 +32,12 @@ public class StringBox {
* is printed with <code>System.out.println()</code> then it will leave the cursor on the 24th line, where the
* user can enter their input without scrolling the top of the string off the screen.</p>
*
* <p>Alternatively, you might create a 24x80 StringBox using {@link #StringBox(int, int)}. If the string is printed
* <p>Alternatively, you might create a StringBox using <code>StringBox(24,80)</code>. If the string is printed
* with <code>System.out.print()</code> then it will leave the cursor at the end of the 24th line without scrolling
* the top of the string off of the screen. This particular style would be useful if you place the prompt on the
* 24th line.</p>
*
* @see #StringBox(int, int)
*/
public StringBox() {
this(23, 80); // standard terminal is 24x80, but leave room for the user's input
......@@ -73,12 +75,13 @@ public class StringBox {
* and the lines will be left-justified. Any portions of the string that would be placed outside the StringBox's
* defined boundaries will be silently truncated.</p>
*
* <p>Equivalent to {@link #placeStringAlignTopLeft(String, int, int)}</p>
* <p>Equivalent to <code>placeStringAlignTopLeft(string, topRow, leftColumn)</code>.</p>
*
* @param string the string to be placed in the StringBox
* @param topRow the row on which the first line of the string should be placed
* @param leftColumn the column in which the first character of each row should be placed
* @return the current StringBox object, suitable for chained calls
* @see #placeStringAlignTopLeft(String, int, int)
*/
public StringBox placeString(String string, int topRow, int leftColumn) {
return placeStringAlignTopLeft(string, topRow, leftColumn);
......@@ -162,17 +165,27 @@ public class StringBox {
return placeStringAlignTopRight(string, bottomRow - strings.length + 1, rightColumn);
}
/**
* <p>Generates the string that the client code produced by calling {@link #placeString(String, int, int)} and
* its related methods. Any unused lines between the last line of text and the bottom of the StringBox will be
* filled with newLines so that when the string is printed, the previous string will fully scroll off of the screen.
*
* <p>Equivalent to <code>toString(true)</code>.</p>
*
* @return the string built by calls to {@link #placeString(String, int, int)} and its related methods
* @see #toString(boolean)
*/
@Override
public String toString() {
return toString(false);
return toString(true);
}
/**
* <p>Generates the string that the client code produced by calling {@link #placeString(String, int, int)} and
* Generates the string that the client code produced by calling {@link #placeString(String, int, int)} and
* its related methods. If this method's argument is <code>true</code>, then any unused lines between the last line
* of text and the bottom of the StringBox will be filled with newLines so that when the string is printed, the
* previous string will fully scroll off of the screen. If the argument is <code>false</code>, then the returned
* string will stop after the last line of text.</p>
* string will stop after the last line of text.
*
* @param padToHeight indicates whether newlines should be placed after the last line of text
* @return the string built by calls to {@link #placeString(String, int, int)} and its related methods
......@@ -238,7 +251,7 @@ public class StringBox {
stringBuilder.setLength(rightEdge);
}
return this;
} // TODO: handle leftColumn < 0
}
public StringRow placeSubstringAlignRight(String string, int rightColumn) {
String modifiedString = string
......
......@@ -197,7 +197,7 @@ public class StringBoxTest {
int column = 10;
String expectedOutput = "\n\n foo";
// Act
String actualOutput = stringBox.placeString(input, row, column).toString();
String actualOutput = stringBox.placeString(input, row, column).toString(false);
// Assert
assertEquals(expectedOutput, actualOutput);
}
......@@ -239,7 +239,7 @@ public class StringBoxTest {
.placeStringAlignBottomLeft(inputs[1], rows[1], columns[1])
.placeStringAlignTopRight(inputs[2], rows[2], columns[2])
.placeStringAlignBottomRight(inputs[3], rows[3], columns[3])
.toString(true);
.toString();
// Assert
assertEquals(expectedOutput, actualOutput);
}
......@@ -262,7 +262,7 @@ public class StringBoxTest {
int column = 10;
String expectedOutput = " bar";
// Act
String actualOutput = stringBox.placeString(input, row, column).toString();
String actualOutput = stringBox.placeString(input, row, column).toString(false);
// Assert
assertEquals(expectedOutput, actualOutput);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment