From c1d759b710bdac48bf29cda640c91642c80b6159 Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Tue, 27 Feb 2024 11:35:48 +0100 Subject: [PATCH] Generify iterator based on ItemType --- src/main/java/edu/uastw/App.java | 5 +++-- src/main/java/edu/uastw/Book.java | 6 ++++++ src/main/java/edu/uastw/ItemType.java | 6 ++++++ src/main/java/edu/uastw/Library.java | 9 ++------- src/main/java/edu/uastw/LibraryItem.java | 1 + src/main/java/edu/uastw/Magazine.java | 6 ++++++ 6 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 src/main/java/edu/uastw/ItemType.java diff --git a/src/main/java/edu/uastw/App.java b/src/main/java/edu/uastw/App.java index 99748ac..d7913eb 100644 --- a/src/main/java/edu/uastw/App.java +++ b/src/main/java/edu/uastw/App.java @@ -34,11 +34,12 @@ public class App { library.iterator().forEachRemaining(x -> System.out.println(x.getTitle() + " by " + x.getOwner())); printSeparator(); - Iterator bookIterator = library.customTypeIterator(Library.ItemType.BOOK); + Iterator bookIterator = library.customTypeIterator(ItemType.BOOK); System.out.println("Books available in the library:"); bookIterator.forEachRemaining(x -> System.out.println(x.getTitle() + " by " + x.getOwner())); + printSeparator(); - Iterator magazineIterator = library.customTypeIterator(Library.ItemType.MAGAZINE); + Iterator magazineIterator = library.customTypeIterator(ItemType.MAGAZINE); System.out.println("Magazines available in the library:"); magazineIterator.forEachRemaining(x -> System.out.println(x.getTitle() + " by " + x.getOwner())); } diff --git a/src/main/java/edu/uastw/Book.java b/src/main/java/edu/uastw/Book.java index e95852d..552cecd 100644 --- a/src/main/java/edu/uastw/Book.java +++ b/src/main/java/edu/uastw/Book.java @@ -9,6 +9,7 @@ public class Book implements LibraryItem { this.author = author; } + @Override public String getTitle() { return title; } @@ -17,4 +18,9 @@ public class Book implements LibraryItem { public String getOwner() { return author; } + + @Override + public ItemType getType() { + return ItemType.BOOK; + } } diff --git a/src/main/java/edu/uastw/ItemType.java b/src/main/java/edu/uastw/ItemType.java new file mode 100644 index 0000000..52556f5 --- /dev/null +++ b/src/main/java/edu/uastw/ItemType.java @@ -0,0 +1,6 @@ +package edu.uastw; + +public enum ItemType { + BOOK, + MAGAZINE +} \ No newline at end of file diff --git a/src/main/java/edu/uastw/Library.java b/src/main/java/edu/uastw/Library.java index a00fe4d..bea5277 100644 --- a/src/main/java/edu/uastw/Library.java +++ b/src/main/java/edu/uastw/Library.java @@ -19,6 +19,7 @@ public class Library implements Iterable { } return instance; } + public void setBooksCapacity(int capacity) { this.booksCapacity = capacity; } @@ -62,16 +63,10 @@ public class Library implements Iterable { } } - 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)) { + if (item.getType() == type) { itemsOfType.add(item); } } diff --git a/src/main/java/edu/uastw/LibraryItem.java b/src/main/java/edu/uastw/LibraryItem.java index 4694f40..adcce09 100644 --- a/src/main/java/edu/uastw/LibraryItem.java +++ b/src/main/java/edu/uastw/LibraryItem.java @@ -3,4 +3,5 @@ package edu.uastw; public interface LibraryItem { String getTitle(); String getOwner(); + ItemType getType(); } diff --git a/src/main/java/edu/uastw/Magazine.java b/src/main/java/edu/uastw/Magazine.java index dfd40ff..a8a79d8 100644 --- a/src/main/java/edu/uastw/Magazine.java +++ b/src/main/java/edu/uastw/Magazine.java @@ -9,6 +9,7 @@ public class Magazine implements LibraryItem { this.publisher = publisher; } + @Override public String getTitle() { return title; } @@ -17,4 +18,9 @@ public class Magazine implements LibraryItem { public String getOwner() { return publisher; } + + @Override + public ItemType getType() { + return ItemType.MAGAZINE; + } }