Implement more iterators and refactor
This commit is contained in:
parent
10eab5a997
commit
00e8919617
12
test/pom.xml
12
test/pom.xml
|
@ -14,4 +14,16 @@
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
|
<version>3.0.0</version>
|
||||||
|
<configuration>
|
||||||
|
<mainClass>edu.uastw.App</mainClass>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
</project>
|
</project>
|
|
@ -1,6 +1,8 @@
|
||||||
package edu.uastw;
|
package edu.uastw;
|
||||||
|
|
||||||
public class Test {
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
public class App {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
LibraryItem book1 = new Book("The Great Gatsby", "F. Scott Fitzgerald");
|
LibraryItem book1 = new Book("The Great Gatsby", "F. Scott Fitzgerald");
|
||||||
LibraryItem book2 = new Book("To Kill a Mockingbird", "Harper Lee");
|
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");
|
LibraryItem magazine1 = new Magazine("National Geographic", "National Geographic Society");
|
||||||
|
|
||||||
Library library = new LibraryBuilder()
|
Library library = new LibraryBuilder()
|
||||||
|
.setBooksCapacity(3)
|
||||||
.addLibraryItem(book1)
|
.addLibraryItem(book1)
|
||||||
.addLibraryItem(book2)
|
.addLibraryItem(book2)
|
||||||
.addLibraryItem(book3)
|
.addLibraryItem(book3)
|
||||||
|
@ -15,22 +18,32 @@ public class Test {
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
library.displayLibraryItems();
|
library.displayLibraryItems();
|
||||||
|
printSeparator();
|
||||||
|
|
||||||
LibraryDecorator increasedCapacityLibrary = new IncreaseBooksCapacityDecorator(library, 1);
|
LibraryDecorator increasedCapacityLibrary = new IncreaseBooksCapacityDecorator(library, 1);
|
||||||
increasedCapacityLibrary.extendedFunctionality();
|
increasedCapacityLibrary.extendedFunctionality();
|
||||||
|
|
||||||
library.addLibraryItem(magazine1);
|
library.addLibraryItem(magazine1);
|
||||||
library.displayLibraryItems();
|
library.displayLibraryItems();
|
||||||
|
printSeparator();
|
||||||
System.out.println("Modified books capacity: " + library.getBooksCapacity());
|
|
||||||
|
|
||||||
LibraryDecorator decreasedCapacityLibrary = new DecreaseBooksCapacityDecorator(library, 2);
|
LibraryDecorator decreasedCapacityLibrary = new DecreaseBooksCapacityDecorator(library, 2);
|
||||||
decreasedCapacityLibrary.extendedFunctionality();
|
decreasedCapacityLibrary.extendedFunctionality();
|
||||||
|
|
||||||
System.out.println("Modified books capacity: " + library.getBooksCapacity());
|
|
||||||
library.displayLibraryItems();
|
|
||||||
|
|
||||||
library.addLibraryItem(magazine1);
|
library.addLibraryItem(magazine1);
|
||||||
library.displayLibraryItems();
|
library.iterator().forEachRemaining(x -> System.out.println(x.getTitle() + " by " + x.getOwner()));
|
||||||
|
printSeparator();
|
||||||
|
|
||||||
|
Iterator<LibraryItem> 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<LibraryItem> 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("-------------------");
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
package edu.uastw;
|
package edu.uastw;
|
||||||
|
|
||||||
class DecreaseBooksCapacityDecorator extends LibraryDecorator {
|
public class DecreaseBooksCapacityDecorator extends LibraryDecorator {
|
||||||
private final int reducedCapacity;
|
private final int reducedCapacity;
|
||||||
|
|
||||||
public DecreaseBooksCapacityDecorator(Library library, int reducedCapacity) {
|
public DecreaseBooksCapacityDecorator(Library library, int reducedCapacity) {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package edu.uastw;
|
package edu.uastw;
|
||||||
|
|
||||||
class IncreaseBooksCapacityDecorator extends LibraryDecorator {
|
public class IncreaseBooksCapacityDecorator extends LibraryDecorator {
|
||||||
private final int additionalCapacity;
|
private final int additionalCapacity;
|
||||||
|
|
||||||
public IncreaseBooksCapacityDecorator(Library library, int additionalCapacity) {
|
public IncreaseBooksCapacityDecorator(Library library, int additionalCapacity) {
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
package edu.uastw;
|
package edu.uastw;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Library {
|
public class Library implements Iterable<LibraryItem> {
|
||||||
private static Library instance;
|
private static Library instance;
|
||||||
private final List<LibraryItem> libraryItems;
|
private final List<LibraryItem> libraryItems;
|
||||||
private int booksCapacity = 3;
|
private int booksCapacity = 3;
|
||||||
|
@ -29,6 +30,7 @@ public class Library {
|
||||||
public void addLibraryItem(LibraryItem libraryItem) {
|
public void addLibraryItem(LibraryItem libraryItem) {
|
||||||
if (libraryItems.size() < booksCapacity) {
|
if (libraryItems.size() < booksCapacity) {
|
||||||
libraryItems.add(libraryItem);
|
libraryItems.add(libraryItem);
|
||||||
|
System.out.println("'" + libraryItem.getTitle() + "' was added.");
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Library capacity reached. Cannot add more items.");
|
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())
|
System.out.println(libraryItem.getTitle() + " by " + libraryItem.getOwner())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterator<LibraryItem> iterator() {
|
||||||
|
return new LibraryItemIterator();
|
||||||
|
}
|
||||||
|
|
||||||
|
private class LibraryItemIterator implements Iterator<LibraryItem> {
|
||||||
|
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<LibraryItem> customTypeIterator(ItemType type) {
|
||||||
|
List<LibraryItem> 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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,11 @@ public class LibraryBuilder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LibraryBuilder setBooksCapacity(int booksCapacity) {
|
||||||
|
library.setBooksCapacity(booksCapacity);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Library build() {
|
public Library build() {
|
||||||
return library;
|
return library;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package edu.uastw;
|
package edu.uastw;
|
||||||
|
|
||||||
abstract class LibraryDecorator extends Library {
|
public abstract class LibraryDecorator extends Library {
|
||||||
protected Library library;
|
protected Library library;
|
||||||
|
|
||||||
public LibraryDecorator(Library library) {
|
public LibraryDecorator(Library library) {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package edu.uastw;
|
package edu.uastw;
|
||||||
|
|
||||||
interface LibraryItem {
|
public interface LibraryItem {
|
||||||
String getTitle();
|
String getTitle();
|
||||||
String getOwner();
|
String getOwner();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue