diff --git a/test/pom.xml b/test/pom.xml index f3c4aa3..c7ca932 100644 --- a/test/pom.xml +++ b/test/pom.xml @@ -14,4 +14,16 @@ UTF-8 + + + + org.codehaus.mojo + exec-maven-plugin + 3.0.0 + + edu.uastw.App + + + + \ No newline at end of file diff --git a/test/src/main/java/edu/uastw/Test.java b/test/src/main/java/edu/uastw/App.java similarity index 55% rename from test/src/main/java/edu/uastw/Test.java rename to test/src/main/java/edu/uastw/App.java index 85e8ce6..99748ac 100644 --- a/test/src/main/java/edu/uastw/Test.java +++ b/test/src/main/java/edu/uastw/App.java @@ -1,6 +1,8 @@ package edu.uastw; -public class Test { +import java.util.Iterator; + +public class App { public static void main(String[] args) { LibraryItem book1 = new Book("The Great Gatsby", "F. Scott Fitzgerald"); LibraryItem book2 = new Book("To Kill a Mockingbird", "Harper Lee"); @@ -8,6 +10,7 @@ public class Test { LibraryItem magazine1 = new Magazine("National Geographic", "National Geographic Society"); Library library = new LibraryBuilder() + .setBooksCapacity(3) .addLibraryItem(book1) .addLibraryItem(book2) .addLibraryItem(book3) @@ -15,22 +18,32 @@ public class Test { .build(); library.displayLibraryItems(); + printSeparator(); LibraryDecorator increasedCapacityLibrary = new IncreaseBooksCapacityDecorator(library, 1); increasedCapacityLibrary.extendedFunctionality(); library.addLibraryItem(magazine1); library.displayLibraryItems(); - - System.out.println("Modified books capacity: " + library.getBooksCapacity()); + printSeparator(); LibraryDecorator decreasedCapacityLibrary = new DecreaseBooksCapacityDecorator(library, 2); decreasedCapacityLibrary.extendedFunctionality(); - System.out.println("Modified books capacity: " + library.getBooksCapacity()); - library.displayLibraryItems(); - library.addLibraryItem(magazine1); - library.displayLibraryItems(); + library.iterator().forEachRemaining(x -> System.out.println(x.getTitle() + " by " + x.getOwner())); + printSeparator(); + + Iterator bookIterator = library.customTypeIterator(Library.ItemType.BOOK); + System.out.println("Books available in the library:"); + bookIterator.forEachRemaining(x -> System.out.println(x.getTitle() + " by " + x.getOwner())); + + Iterator magazineIterator = library.customTypeIterator(Library.ItemType.MAGAZINE); + System.out.println("Magazines available in the library:"); + magazineIterator.forEachRemaining(x -> System.out.println(x.getTitle() + " by " + x.getOwner())); + } + + private static void printSeparator() { + System.out.println("-------------------"); } } \ No newline at end of file diff --git a/test/src/main/java/edu/uastw/DecreaseBooksCapacityDecorator.java b/test/src/main/java/edu/uastw/DecreaseBooksCapacityDecorator.java index 4974b6a..84fe2b2 100644 --- a/test/src/main/java/edu/uastw/DecreaseBooksCapacityDecorator.java +++ b/test/src/main/java/edu/uastw/DecreaseBooksCapacityDecorator.java @@ -1,6 +1,6 @@ package edu.uastw; -class DecreaseBooksCapacityDecorator extends LibraryDecorator { +public class DecreaseBooksCapacityDecorator extends LibraryDecorator { private final int reducedCapacity; public DecreaseBooksCapacityDecorator(Library library, int reducedCapacity) { diff --git a/test/src/main/java/edu/uastw/IncreaseBooksCapacityDecorator.java b/test/src/main/java/edu/uastw/IncreaseBooksCapacityDecorator.java index 3f07165..1580142 100644 --- a/test/src/main/java/edu/uastw/IncreaseBooksCapacityDecorator.java +++ b/test/src/main/java/edu/uastw/IncreaseBooksCapacityDecorator.java @@ -1,6 +1,6 @@ package edu.uastw; -class IncreaseBooksCapacityDecorator extends LibraryDecorator { +public class IncreaseBooksCapacityDecorator extends LibraryDecorator { private final int additionalCapacity; public IncreaseBooksCapacityDecorator(Library library, int additionalCapacity) { diff --git a/test/src/main/java/edu/uastw/Library.java b/test/src/main/java/edu/uastw/Library.java index 3ba2c69..a00fe4d 100644 --- a/test/src/main/java/edu/uastw/Library.java +++ b/test/src/main/java/edu/uastw/Library.java @@ -1,9 +1,10 @@ package edu.uastw; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; -public class Library { +public class Library implements Iterable { private static Library instance; private final List libraryItems; private int booksCapacity = 3; @@ -29,6 +30,7 @@ public class Library { public void addLibraryItem(LibraryItem libraryItem) { if (libraryItems.size() < booksCapacity) { libraryItems.add(libraryItem); + System.out.println("'" + libraryItem.getTitle() + "' was added."); } else { System.out.println("Library capacity reached. Cannot add more items."); } @@ -40,4 +42,39 @@ public class Library { System.out.println(libraryItem.getTitle() + " by " + libraryItem.getOwner()) ); } + + @Override + public Iterator iterator() { + return new LibraryItemIterator(); + } + + private class LibraryItemIterator implements Iterator { + private int currentIndex = 0; + + @Override + public boolean hasNext() { + return currentIndex < booksCapacity; + } + + @Override + public LibraryItem next() { + return libraryItems.get(currentIndex++); + } + } + + public enum ItemType { + BOOK, + MAGAZINE + } + + public Iterator customTypeIterator(ItemType type) { + List itemsOfType = new ArrayList<>(); + for (LibraryItem item : libraryItems) { + if ((type == ItemType.BOOK && item instanceof Book) || + (type == ItemType.MAGAZINE && item instanceof Magazine)) { + itemsOfType.add(item); + } + } + return itemsOfType.iterator(); + } } diff --git a/test/src/main/java/edu/uastw/LibraryBuilder.java b/test/src/main/java/edu/uastw/LibraryBuilder.java index 03e2827..3ad9669 100644 --- a/test/src/main/java/edu/uastw/LibraryBuilder.java +++ b/test/src/main/java/edu/uastw/LibraryBuilder.java @@ -12,6 +12,11 @@ public class LibraryBuilder { return this; } + public LibraryBuilder setBooksCapacity(int booksCapacity) { + library.setBooksCapacity(booksCapacity); + return this; + } + public Library build() { return library; } diff --git a/test/src/main/java/edu/uastw/LibraryDecorator.java b/test/src/main/java/edu/uastw/LibraryDecorator.java index e42a9b1..2e6eb2c 100644 --- a/test/src/main/java/edu/uastw/LibraryDecorator.java +++ b/test/src/main/java/edu/uastw/LibraryDecorator.java @@ -1,6 +1,6 @@ package edu.uastw; -abstract class LibraryDecorator extends Library { +public abstract class LibraryDecorator extends Library { protected Library library; public LibraryDecorator(Library library) { diff --git a/test/src/main/java/edu/uastw/LibraryItem.java b/test/src/main/java/edu/uastw/LibraryItem.java index c946ed9..4694f40 100644 --- a/test/src/main/java/edu/uastw/LibraryItem.java +++ b/test/src/main/java/edu/uastw/LibraryItem.java @@ -1,6 +1,6 @@ package edu.uastw; -interface LibraryItem { +public interface LibraryItem { String getTitle(); String getOwner(); }