Implement more iterators and refactor

This commit is contained in:
Santiago Lo Coco 2024-02-26 13:49:07 +01:00
parent 10eab5a997
commit 00e8919617
8 changed files with 79 additions and 12 deletions

View File

@ -14,4 +14,16 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</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>

View File

@ -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<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("-------------------");
}
}

View File

@ -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) {

View File

@ -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) {

View File

@ -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<LibraryItem> {
private static Library instance;
private final List<LibraryItem> 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<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();
}
}

View File

@ -12,6 +12,11 @@ public class LibraryBuilder {
return this;
}
public LibraryBuilder setBooksCapacity(int booksCapacity) {
library.setBooksCapacity(booksCapacity);
return this;
}
public Library build() {
return library;
}

View File

@ -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) {

View File

@ -1,6 +1,6 @@
package edu.uastw;
interface LibraryItem {
public interface LibraryItem {
String getTitle();
String getOwner();
}