From c7b56bf4f7ab44a5e3e634719e9314f1a012f7a8 Mon Sep 17 00:00:00 2001 From: Guilherme Dias Date: Mon, 3 Aug 2015 11:29:01 -0300 Subject: [PATCH] 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. --- src/com/twu/biblioteca/BibliotecaApp.java | 14 +++++ src/com/twu/biblioteca/Book.java | 12 ++++ src/com/twu/biblioteca/Menu.java | 16 ++++++ .../biblioteca/options/ListBooksOption.java | 17 ++++++ src/com/twu/biblioteca/options/Option.java | 27 +++++++++ test/com/twu/biblioteca/MenuTest.java | 13 +++++ .../options/ListBooksOptionTest.java | 20 +++++++ .../twu/biblioteca/options/OptionTest.java | 56 +++++++++++++++++++ 8 files changed, 175 insertions(+) create mode 100644 src/com/twu/biblioteca/options/ListBooksOption.java create mode 100644 src/com/twu/biblioteca/options/Option.java create mode 100644 test/com/twu/biblioteca/options/ListBooksOptionTest.java create mode 100644 test/com/twu/biblioteca/options/OptionTest.java diff --git a/src/com/twu/biblioteca/BibliotecaApp.java b/src/com/twu/biblioteca/BibliotecaApp.java index a6214c0..dd5f7bd 100755 --- a/src/com/twu/biblioteca/BibliotecaApp.java +++ b/src/com/twu/biblioteca/BibliotecaApp.java @@ -1,5 +1,9 @@ package com.twu.biblioteca; +import com.twu.biblioteca.options.Option; + +import java.util.Scanner; + public class BibliotecaApp { public static void main(String[] args) { @@ -9,5 +13,15 @@ public static void main(String[] args) { System.out.println(biblioteca.welcomeUser()); System.out.println(menu.listOptions()); + + String userInput = getUserInput(); + + Option selectedOption = menu.selectOption(Integer.parseInt(userInput)); + + System.out.println(selectedOption.execute(biblioteca)); + } + + private static String getUserInput() { + return new Scanner(System.in).nextLine(); } } diff --git a/src/com/twu/biblioteca/Book.java b/src/com/twu/biblioteca/Book.java index 00e5bd9..7ccd202 100644 --- a/src/com/twu/biblioteca/Book.java +++ b/src/com/twu/biblioteca/Book.java @@ -8,6 +8,7 @@ public class Book { public String author; public int year; + public Book(String name, String author, int year) { this.name = name; this.author = author; @@ -18,4 +19,15 @@ public String showDetails() { String separator = " - "; return name + separator + author + separator + Integer.toString(year); } + + @Override + public boolean equals(Object other) { + if (other instanceof Book) { + Book otherBook = (Book) other; + + return this.showDetails().equals(otherBook.showDetails()); + } + + return false; + } } diff --git a/src/com/twu/biblioteca/Menu.java b/src/com/twu/biblioteca/Menu.java index 5ca2da7..533b790 100644 --- a/src/com/twu/biblioteca/Menu.java +++ b/src/com/twu/biblioteca/Menu.java @@ -1,5 +1,8 @@ package com.twu.biblioteca; +import com.twu.biblioteca.options.ListBooksOption; +import com.twu.biblioteca.options.Option; + /** * Created by gdias on 7/31/15. */ @@ -7,4 +10,17 @@ 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; + } } diff --git a/src/com/twu/biblioteca/options/ListBooksOption.java b/src/com/twu/biblioteca/options/ListBooksOption.java new file mode 100644 index 0000000..70d007f --- /dev/null +++ b/src/com/twu/biblioteca/options/ListBooksOption.java @@ -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"; + } +} diff --git a/src/com/twu/biblioteca/options/Option.java b/src/com/twu/biblioteca/options/Option.java new file mode 100644 index 0000000..dbe4d64 --- /dev/null +++ b/src/com/twu/biblioteca/options/Option.java @@ -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); +} diff --git a/test/com/twu/biblioteca/MenuTest.java b/test/com/twu/biblioteca/MenuTest.java index 5d71e57..979af2d 100644 --- a/test/com/twu/biblioteca/MenuTest.java +++ b/test/com/twu/biblioteca/MenuTest.java @@ -1,5 +1,6 @@ package com.twu.biblioteca; +import com.twu.biblioteca.options.Option; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -12,4 +13,16 @@ public class MenuTest { public void listOptions_ShouldReturnTheMenuOptions() throws Exception { assertEquals("1 - List books", new Menu().listOptions()); } + + @Test + public void selectOption_ShouldReturnSelectedOption() throws Exception { + Option listBookOption= new Option(1, "List books") { + @Override + public String execute(Biblioteca biblioteca) { + return ""; + } + }; + + assertEquals(listBookOption, new Menu().selectOption(1)); + } } diff --git a/test/com/twu/biblioteca/options/ListBooksOptionTest.java b/test/com/twu/biblioteca/options/ListBooksOptionTest.java new file mode 100644 index 0000000..1459313 --- /dev/null +++ b/test/com/twu/biblioteca/options/ListBooksOptionTest.java @@ -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())); + } +} diff --git a/test/com/twu/biblioteca/options/OptionTest.java b/test/com/twu/biblioteca/options/OptionTest.java new file mode 100644 index 0000000..cb50f80 --- /dev/null +++ b/test/com/twu/biblioteca/options/OptionTest.java @@ -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; + } +}