Fix bugs and refactor code

Co-authored-by: LucasCatolino <lcatolino@fi.uba.ar>
Co-authored-by: KuJo7 <joel.k2010@gmail.com>
This commit is contained in:
Santiago Lo Coco 2024-03-10 17:21:03 +01:00
parent 61bef1210e
commit 62f333e6cf
7 changed files with 45 additions and 25 deletions

View File

@ -14,10 +14,9 @@ import java.util.Iterator;
@IgnoreCoverage @IgnoreCoverage
public class App { public class App {
/***************** Resilience variables *****************/ /***************** Resilience variables *****************/
private static final int RETRY_ATTEMPTS = 2; private static final int RETRY_ATTEMPTS = 2;
private static final double LIBRARY_OPEN_CONDITION = 0.5; private static final double LIBRARY_OPEN_CONDITION = 0;
private static final int TIME_MULTIPLIER = 3000; private static final int TIME_MULTIPLIER = 3000;
private static final int RATE_LIMIT = 2; private static final int RATE_LIMIT = 2;
private static final int TIMEOUT = 1000; private static final int TIMEOUT = 1000;
@ -57,21 +56,29 @@ public class App {
try { try {
library.addLibraryItem(book1); library.addLibraryItem(book1);
printSeparator();
library.addLibraryItem(book2); library.addLibraryItem(book2);
printSeparator();
library.addLibraryItem(book3); library.addLibraryItem(book3);
printSeparator();
library.addLibraryItem(magazine1); library.addLibraryItem(magazine1);
printSeparator();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); System.out.println(e.getMessage());
} }
printSeparator();
library.displayLibraryItems(); library.displayLibraryItems();
printSeparator();
library.displayLibraryItems(); library.displayLibraryItems();
printSeparator();
library.displayLibraryItems(); library.displayLibraryItems();
printSeparator();
try { try {
Thread.sleep(INTERVAL + 5); Thread.sleep(INTERVAL + 5);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); System.out.println(e.getMessage());
} }
library.displayLibraryItems(); library.displayLibraryItems();
@ -79,42 +86,46 @@ public class App {
LibraryDecorator increasedCapacityLibrary = new IncreaseBooksCapacityDecorator(library, 1); LibraryDecorator increasedCapacityLibrary = new IncreaseBooksCapacityDecorator(library, 1);
increasedCapacityLibrary.extendedFunctionality(); increasedCapacityLibrary.extendedFunctionality();
printSeparator();
try { try {
library.addLibraryItem(magazine1); library.addLibraryItem(magazine1);
} catch (Exception e) { } catch (Exception e) {
System.out.println("Add library item error"); System.out.println(e.getMessage());
e.printStackTrace();
} }
printSeparator();
library.displayLibraryItems(); library.displayLibraryItems();
printSeparator(); printSeparator();
LibraryDecorator decreasedCapacityLibrary = new DecreaseBooksCapacityDecorator(library, 2); LibraryDecorator decreasedCapacityLibrary = new DecreaseBooksCapacityDecorator(library, 2);
decreasedCapacityLibrary.extendedFunctionality(); decreasedCapacityLibrary.extendedFunctionality();
printSeparator();
try { try {
library.addLibraryItem(magazine1); library.addLibraryItem(magazine1);
printSeparator();
} catch (Exception e) { } catch (Exception e) {
System.out.println("Add library item error"); System.out.println(e.getMessage());
e.printStackTrace();
} }
library.iterator().forEachRemaining(x -> System.out.println(x.getTitle() + " by " + x.getOwner()));
library.iterator().forEachRemaining(System.out::println);
printSeparator(); printSeparator();
Iterator<LibraryItem> bookIterator = library.customTypeIterator(ItemType.BOOK); Iterator<LibraryItem> bookIterator = library.customTypeIterator(ItemType.BOOK);
System.out.println("Books available in the library:"); System.out.println("Books available in the library:");
bookIterator.forEachRemaining(x -> System.out.println(x.getTitle() + " by " + x.getOwner())); bookIterator.forEachRemaining(System.out::println);
printSeparator(); printSeparator();
Iterator<LibraryItem> magazineIterator = library.customTypeIterator(ItemType.MAGAZINE); Iterator<LibraryItem> magazineIterator = library.customTypeIterator(ItemType.MAGAZINE);
System.out.println("Magazines available in the library:"); System.out.println("Magazines available in the library:");
magazineIterator.forEachRemaining(x -> System.out.println(x.getTitle() + " by " + x.getOwner())); magazineIterator.forEachRemaining(System.out::println);
printSeparator();
try { try {
library.removeLibraryItem(book1); library.removeLibraryItem(book1);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); System.out.println(e.getMessage());
} }
} }

View File

@ -107,12 +107,12 @@ public class Library implements Iterable<LibraryItem> {
try { try {
Thread.sleep(random); Thread.sleep(random);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); System.out.println(e.getMessage());
} }
if (libraryItems.size() > 0) { if (libraryItems.size() > 0) {
libraryItems.remove(item); libraryItems.remove(item);
} else { } else {
System.out.println("Library is full"); System.out.println("Library is empty");
} }
}, timeout); }, timeout);
} }
@ -137,9 +137,7 @@ public class Library implements Iterable<LibraryItem> {
if (tokens > 0) { if (tokens > 0) {
System.out.println("Items available in the library:"); System.out.println("Items available in the library:");
libraryItems.forEach(libraryItem -> libraryItems.forEach(System.out::println);
System.out.println(libraryItem.getTitle() + " by " + libraryItem.getOwner())
);
tokens--; tokens--;
lastAccessTime = currentTime; lastAccessTime = currentTime;
} else { } else {

View File

@ -1,8 +1,7 @@
package edu.uastw.library.exceptions; package edu.uastw.library.exceptions;
public class LibraryClosedException extends Exception { public class LibraryClosedException extends Exception {
public LibraryClosedException(String message) {
public LibraryClosedException(String string) { super(message);
//TODO Auto-generated constructor stub
} }
} }

View File

@ -1,9 +1,7 @@
package edu.uastw.library.exceptions; package edu.uastw.library.exceptions;
public class LibraryFullException extends Exception { public class LibraryFullException extends Exception {
public LibraryFullException(String message) {
public LibraryFullException(String string) { super(message);
//TODO Auto-generated constructor stub
} }
} }

View File

@ -6,4 +6,8 @@ public interface LibraryItem {
String getTitle(); String getTitle();
String getOwner(); String getOwner();
ItemType getType(); ItemType getType();
default String defaultToString() {
return getTitle() + " by " + getOwner();
}
} }

View File

@ -26,6 +26,11 @@ public class Book implements LibraryItem {
return ItemType.BOOK; return ItemType.BOOK;
} }
@Override
public String toString() {
return defaultToString();
}
public static class Builder { public static class Builder {
private String title; private String title;
private String author; private String author;

View File

@ -26,6 +26,11 @@ public class Magazine implements LibraryItem {
return ItemType.MAGAZINE; return ItemType.MAGAZINE;
} }
@Override
public String toString() {
return defaultToString();
}
public static class Builder { public static class Builder {
private String title; private String title;
private String publisher; private String publisher;