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
public class App {
/***************** Resilience variables *****************/
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 RATE_LIMIT = 2;
private static final int TIMEOUT = 1000;
@ -57,21 +56,29 @@ public class App {
try {
library.addLibraryItem(book1);
printSeparator();
library.addLibraryItem(book2);
printSeparator();
library.addLibraryItem(book3);
printSeparator();
library.addLibraryItem(magazine1);
printSeparator();
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
printSeparator();
library.displayLibraryItems();
printSeparator();
library.displayLibraryItems();
printSeparator();
library.displayLibraryItems();
printSeparator();
try {
Thread.sleep(INTERVAL + 5);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
library.displayLibraryItems();
@ -79,42 +86,46 @@ public class App {
LibraryDecorator increasedCapacityLibrary = new IncreaseBooksCapacityDecorator(library, 1);
increasedCapacityLibrary.extendedFunctionality();
printSeparator();
try {
library.addLibraryItem(magazine1);
} catch (Exception e) {
System.out.println("Add library item error");
e.printStackTrace();
System.out.println(e.getMessage());
}
printSeparator();
library.displayLibraryItems();
printSeparator();
LibraryDecorator decreasedCapacityLibrary = new DecreaseBooksCapacityDecorator(library, 2);
decreasedCapacityLibrary.extendedFunctionality();
printSeparator();
try {
library.addLibraryItem(magazine1);
printSeparator();
} catch (Exception e) {
System.out.println("Add library item error");
e.printStackTrace();
System.out.println(e.getMessage());
}
library.iterator().forEachRemaining(x -> System.out.println(x.getTitle() + " by " + x.getOwner()));
library.iterator().forEachRemaining(System.out::println);
printSeparator();
Iterator<LibraryItem> bookIterator = library.customTypeIterator(ItemType.BOOK);
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();
Iterator<LibraryItem> magazineIterator = library.customTypeIterator(ItemType.MAGAZINE);
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 {
library.removeLibraryItem(book1);
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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