Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Listener feature completed #3

Closed
wants to merge 5 commits into from

2 participants

@nathanh89

Listener is open for basic dependency injection for tests and the reader is closed at program exit. Updated To-Do and feature lists to reflect changes.

@leadVisionary

This is incorrect. You're creating a new instance and then closing the reader that belongs to that one, not the one you used to populate the String userInput. If you explore some profiling tools, you should be able to diagnose a memory leak and see that you've nicely written a virus. :)

What you need to do is retain an instance of the original listener you create to get the user's input string (as opposed to getting a new one every time) and then call closeListener on that original instance.

Remember that object creation is expensive and needs to be carefully managed.
http://oreilly.com/catalog/javapt/chapter/ch04.html
http://en.wikipedia.org/wiki/Flyweight_pattern

@leadVisionary

You should hang on to the reference of the Listener you create and keep calling getInput() on that one, not create a new one every time.

Listener inputGrabber = Listener.create();
String firstInput = inputGrabber.getInput();
String secondInput = inputGrabber.getInput();

@leadVisionary

This isn't quite there. Clean up the issues in the comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 15, 2013
  1. @nathanh89
  2. @nathanh89

    Renamed Listener to TakeInput, added RequiredInputs interface, added …

    nathanh89 authored
    …test for TakeInput, need to fix broken classes as a result of Listener refactor
Commits on Jan 16, 2013
  1. @nathanh89
Commits on Jan 18, 2013
  1. @nathanh89
  2. @nathanh89
This page is out of date. Refresh to see the latest.
View
24 features/By Class/Listener.md
@@ -0,0 +1,24 @@
+<table>
+ <thead>
+ <tr>
+ <th>Feature</th>
+ <th>Story</th>
+ <th>Status</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>Listener can take a String input from the console</td>
+ <td>
+ <ul>
+ <li>Given: A Listener is needed to take input from the console</li>
+ <li>When: The input is provided by a user</li>
+ <li>Then: Listener will get the String with a reader for use in BudgetBoss</li>
+ </td>
+ <td>Implemented</td>
+ </tr>
+
+ </tbody>
+ <tfoot>
+ </tfoot>
+</table>
View
2  src/com/visionarysoftwaresolutions/budgetboss/app/BudgetBoss.java
@@ -59,7 +59,7 @@ public static void main(String args[]){
defaultDirectory = Finder.pathFinder();
mainMenu = new MainMenu(currentBudget);
Printer.print("welcome");
- Listener.getInput();
+ //TakeInput.create().getStringConsole();
while (stillUsingBudgetBoss){
mainMenu.getOption(mainMenu);
View
1  src/com/visionarysoftwaresolutions/budgetboss/cli/InputValidator.java
@@ -161,6 +161,7 @@ public static boolean itsTimeToExit(String toCheck){
BudgetBoss.endLoadSavedBudget();
BudgetBoss.endNeedNewBudget();
BudgetBoss.doneUsingBudgetBoss();
+ Listener.create().closeListener();
return true;
}else
return false;
View
43 src/com/visionarysoftwaresolutions/budgetboss/cli/Listener.java
@@ -2,20 +2,37 @@
import java.io.BufferedReader;
import java.io.IOException;
-import java.io.InputStreamReader;
+import java.io.InputStreamReader;
-public class Listener {
+public class Listener{
- private Listener(){}
-
- public static String getInput(){
- String userInput = null;
- BufferedReader listener = new BufferedReader(new InputStreamReader(System.in));
- try {
- userInput = listener.readLine();
- } catch (IOException e) {
- System.out.println("You fucked up. I mean, it wasn't me. Un-Bosslike.");
+ private String userInput = null;
+ private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
+
+ private Listener(){}
+
+ public void setReader(BufferedReader reader){
+ this.reader = reader;
+ }
+
+ public static Listener create(){
+ return new Listener();
+ }
+
+ public String getInput(){
+ try {
+ userInput = reader.readLine();
+ }catch (IOException e){
+ System.out.println("I/O ERROR!");
}
return userInput;
- }
-}
+ }
+
+ public void closeListener(){
+ try {
+ reader.close();
+ } catch (IOException e) {
+ System.out.println("I/O ERROR!");
+ }
+ }
+}
View
8 src/com/visionarysoftwaresolutions/budgetboss/fileops/Opener.java
@@ -17,9 +17,9 @@ public static void getLoadDirectory(){
Printer.print("savedInDefault");
System.out.println(BudgetBoss.getDefaultDirectory());
Printer.print("whereSaved");
- String toCheck = Listener.getInput();
+ String toCheck = Listener.create().getInput();
while(InputValidator.pathIsInvalid(toCheck))
- toCheck = Listener.getInput();
+ toCheck = Listener.create().getInput();
if(!(toCheck.equals("exit"))){
if(toCheck.equalsIgnoreCase("y")){
searchDirectory(BudgetBoss.getDefaultDirectory());
@@ -51,10 +51,10 @@ private static void chooseBudget(File[] foundBudgets){
}
private static int getChoiceNumber(File[] foundBudgets){
- String toCheck = Listener.getInput();
+ String toCheck = Listener.create().getInput();
int highestChoice = foundBudgets.length;
while(InputValidator.inputNotABudget(toCheck, highestChoice))
- toCheck = Listener.getInput();
+ toCheck = Listener.create().getInput();
if(!(toCheck.equals("exit")))
return (Integer.valueOf(toCheck) - 1);
else
View
4 src/com/visionarysoftwaresolutions/budgetboss/fileops/Savior.java
@@ -13,9 +13,9 @@ private Savior(){}
private static String getSaveDirectory(){
Printer.print("getSaveDirectoryPath");
System.out.println("The default is: " + BudgetBoss.getDefaultDirectory());
- String toCheck = Listener.getInput();
+ String toCheck = Listener.create().getInput();
while(InputValidator.pathIsInvalid(toCheck))
- toCheck = Listener.getInput();
+ toCheck = Listener.create().getInput();
if(!(toCheck.equalsIgnoreCase("exit"))){
if(toCheck.equalsIgnoreCase("y"))
return BudgetBoss.getDefaultDirectory();
View
2  src/com/visionarysoftwaresolutions/budgetboss/fileops/TheCreator.java
@@ -10,7 +10,7 @@ private TheCreator(){}
public static void createBudget(){
Printer.print("getBudgetName");
- String desiredName = Listener.getInput();
+ String desiredName = Listener.create().getInput();
if(!(desiredName.equals("exit"))){
System.out.println("Creating budget " + desiredName + "...\n");
Budget newBudget = new Budget(desiredName);
View
15 src/com/visionarysoftwaresolutions/budgetboss/menu/EditorMenu.java
@@ -60,11 +60,11 @@ public void displayMenu(){
public void getOption(MasterMenu menu){
displayMenu();
System.out.println(errorMessage);
- String userInput = Listener.getInput();
+ String userInput = Listener.create().getInput();
while(InputValidator.menuChoiceIsInvalid(userInput, this)){
displayMenu();
System.out.println(errorMessage);
- userInput = Listener.getInput();
+ userInput = Listener.create().getInput();
}
if(!(userInput.equalsIgnoreCase("exit"))){
int optionChose = Integer.valueOf(userInput);
@@ -79,7 +79,7 @@ private void reportToDisk(){
private void getNewName(){
Printer.print("getNewName");
- String userInput = Listener.getInput();
+ String userInput = Listener.create().getInput();
if(!(userInput.equalsIgnoreCase("exit")))
toEdit.setName(userInput);
else{
@@ -87,23 +87,24 @@ private void getNewName(){
BudgetBoss.endLoadSavedBudget();
BudgetBoss.endNeedNewBudget();
BudgetBoss.doneUsingBudgetBoss();
+ Listener.create().closeListener();
}
}
private void getNewStartDate(){
Printer.print("getNewStartDate");
- String userInput = Listener.getInput();
+ String userInput = Listener.create().getInput();
while(InputValidator.dateIsInvalid(userInput))
- userInput = Listener.getInput();
+ userInput = Listener.create().getInput();
if(!(userInput.equalsIgnoreCase("exit")))
toEdit.setStartDate(userInput);
}
private void getNewEndDate(){
Printer.print("getNewEndDate");
- String userInput = Listener.getInput();
+ String userInput = Listener.create().getInput();
while(InputValidator.dateIsInvalid(userInput))
- userInput = Listener.getInput();
+ userInput = Listener.create().getInput();
if(!(userInput.equalsIgnoreCase("exit")))
toEdit.setEndDate(userInput);
}
View
5 src/com/visionarysoftwaresolutions/budgetboss/menu/MainMenu.java
@@ -58,11 +58,11 @@ public void displayMenu(){
public void getOption(MasterMenu menu){
displayMenu();
System.out.println(errorMessage);
- String userInput = Listener.getInput();
+ String userInput = Listener.create().getInput();
while (InputValidator.menuChoiceIsInvalid(userInput, menu)){
displayMenu();
System.out.println(errorMessage);
- userInput = Listener.getInput();
+ userInput = Listener.create().getInput();
}
if(!(userInput.equalsIgnoreCase("exit"))){
int optionChose = Integer.valueOf(userInput);
@@ -122,6 +122,7 @@ private void exitProgram(){
BudgetBoss.doneUsingBudgetBoss();
BudgetBoss.endLoadSavedBudget();
BudgetBoss.endNeedNewBudget();
+ Listener.create().closeListener();
}
public void chooseOption(int optionChose){
View
20 tests/com/VSSBudgetBoss/tests/InputValidatorTests.java
@@ -7,23 +7,15 @@
import com.visionarysoftwaresolutions.budgetboss.cli.InputValidator;
public class InputValidatorTests {
-
- InputValidator validator;
-
- @Before
- public void setup(){
- //Given: An input is required from a user
- validator = new InputValidator();
- }
@Test
public void testNonIntegerRejection(){
//And: The input must be an integer (for selecting menu options numerically)
//When: The user inputs something
//Then: The input will only validate if it is an integer
- assertFalse(validator.inputNotAnInteger("8"));
- assertFalse(validator.inputNotAnInteger("1"));
- assertTrue(validator.inputNotAnInteger("toCheck"));
+ assertFalse(InputValidator.inputNotAnInteger("8"));
+ assertFalse(InputValidator.inputNotAnInteger("1"));
+ assertTrue(InputValidator.inputNotAnInteger("toCheck"));
}
@Test
@@ -32,9 +24,9 @@ public void testValidatePathLinux(){
//When: The user inputs something
//Then: The input will only validate if it is a valid path, ending in "/"
String currentUser = System.getProperty("user.name");
- assertTrue(validator.pathIsInvalid("/home/" + currentUser + "/Documents"));
- assertFalse(validator.pathIsInvalid("/home/" + currentUser + "/Documents/"));
- assertTrue(validator.pathIsInvalid("/home/" + currentUser + "/notevenarealdirectory/"));
+ assertTrue(InputValidator.pathIsInvalid("/home/" + currentUser + "/Documents"));
+ assertFalse(InputValidator.pathIsInvalid("/home/" + currentUser + "/Documents/"));
+ assertTrue(InputValidator.pathIsInvalid("/home/" + currentUser + "/notevenarealdirectory/"));
}
}
View
38 tests/com/visionarysoftwaresolutions/budgetboss/cli/tests/ListenerTests.java
@@ -0,0 +1,38 @@
+package com.visionarysoftwaresolutions.budgetboss.cli.tests;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+import com.visionarysoftwaresolutions.budgetboss.cli.*;
+
+public class ListenerTests {
+
+ @Test
+ public void testGetInput() {
+ //Given: A String is needed. The source is the directly from the user via the console.
+ String isNeeded;
+ Listener listener = Listener.create();
+ BufferedReader mockReader = Mockito.mock(BufferedReader.class);
+ listener.setReader(mockReader);
+ try{
+ Mockito.when(mockReader.readLine()).thenReturn("Input from console!");
+ } catch (IOException e1) {
+ e1.printStackTrace();
+ }
+ //When: Listener prompts for the input from the console
+ isNeeded = listener.getInput();
+ //Then: TakeInput will get the correct String from the user
+ Assert.assertTrue(isNeeded.equals("Input from console!"));
+ }
+
+ @Test
+ public void testMulitpleInstantiationsRealInput(){
+ //This ensures that the reader doesn't close before the end of the program
+ String firstInput = Listener.create().getInput();
+ String secondInput = Listener.create().getInput();
+ }
+}
Something went wrong with that request. Please try again.