-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option selection feature and refactoring
The following changes were made: Read user input to select option. Implement equals method in Book class. Create ListBooksOption (a concrete implementation of Option). Replace Option occurences for ListBooksOption.
- Loading branch information
1 parent
ca2b34b
commit c7b56bf
Showing
8 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,26 @@ | ||
package com.twu.biblioteca; | ||
|
||
import com.twu.biblioteca.options.ListBooksOption; | ||
import com.twu.biblioteca.options.Option; | ||
|
||
/** | ||
* Created by gdias on 7/31/15. | ||
*/ | ||
public class Menu { | ||
public String listOptions() { | ||
return "1 - List books"; | ||
} | ||
|
||
public Option selectOption(int selectedId) { | ||
Option option; | ||
switch (selectedId) { | ||
case 1: | ||
option = new ListBooksOption(1, "List books"); | ||
break; | ||
default: | ||
option = null; | ||
} | ||
|
||
return option; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.twu.biblioteca.options; | ||
|
||
import com.twu.biblioteca.Biblioteca; | ||
|
||
/** | ||
* Created by gdias on 8/3/15. | ||
*/ | ||
public class ListBooksOption extends Option { | ||
public ListBooksOption(int id, String name) { | ||
super(id, name); | ||
} | ||
|
||
@Override | ||
public String execute(Biblioteca biblioteca) { | ||
return "Harry Potter - J. K. Rowling - 1997\n"; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.twu.biblioteca.options; | ||
|
||
import com.twu.biblioteca.Biblioteca; | ||
|
||
/** | ||
* Created by gdias on 7/31/15. | ||
*/ | ||
public abstract class Option { | ||
private int id; | ||
private String name; | ||
|
||
public Option(int id, String name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other instanceof Option) { | ||
Option otherOption = (Option) other; | ||
return this.name.equals(otherOption.name) && this.id == otherOption.id; | ||
} | ||
return false; | ||
} | ||
|
||
public abstract String execute(Biblioteca biblioteca); | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.twu.biblioteca.options; | ||
|
||
import com.twu.biblioteca.Biblioteca; | ||
import com.twu.biblioteca.Book; | ||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* Created by gdias on 8/3/15. | ||
*/ | ||
public class ListBooksOptionTest { | ||
@Test | ||
public void execute_ShouldReturnAListOfBooksAsString() throws Exception { | ||
Option listBooksOption = new ListBooksOption(1, "List books"); | ||
assertEquals("Harry Potter - J. K. Rowling - 1997\n", listBooksOption.execute(new Biblioteca())); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.twu.biblioteca.options; | ||
|
||
import com.twu.biblioteca.Biblioteca; | ||
import com.twu.biblioteca.Book; | ||
import com.twu.biblioteca.options.Option; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Created by gdias on 7/31/15. | ||
*/ | ||
public class OptionTest { | ||
@Test | ||
public void equals_ShouldReturnFalse_WhenTheParameterIsNotAnOption() throws Exception { | ||
Option optionA = getConcreteOption(1, "List books"); | ||
String fakeOption = "AHuahaa"; | ||
|
||
assertFalse(optionA.equals(fakeOption)); | ||
} | ||
|
||
@Test | ||
public void equals_ShouldReturnTrue_WhenOptionsAreTheSame() throws Exception { | ||
Option optionA = getConcreteOption(1, "List books"); | ||
Option optionB = optionA; | ||
|
||
assertTrue(optionA.equals(optionB)); | ||
} | ||
|
||
@Test | ||
public void equals_ShouldReturnFalse_WhenOptionsAreDifferent() throws Exception { | ||
Option optionA = getConcreteOption(1, "List books"); | ||
|
||
Option optionB = getConcreteOption(2, "List books"); | ||
|
||
assertFalse(optionA.equals(optionB)); | ||
} | ||
|
||
@Test | ||
public void execute_ShouldOverrideExecuteMethod() throws Exception { | ||
Option overridenOption = getConcreteOption(0, ""); | ||
|
||
assertEquals("", overridenOption.execute(new Biblioteca())); | ||
} | ||
|
||
private Option getConcreteOption(int id, String name) { | ||
Option optionA = new Option(id, name) { | ||
@Override | ||
public String execute(Biblioteca biblioteca) { | ||
return ""; | ||
} | ||
}; | ||
|
||
return optionA; | ||
} | ||
} |