diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/entity/Book.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/entity/Book.java index ad27bfb..500c09d 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/entity/Book.java +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/entity/Book.java @@ -1,6 +1,7 @@ package fr.iut_fbleau.but3.dev62.mylibrary.book.entity; +import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.IllegalBookStockException; import lombok.Builder; import lombok.Getter; @@ -24,4 +25,11 @@ public class Book { public void addStock(Integer copyToAdd){ this.stock += copyToAdd; } + + public void removeStock(Integer copyToRemomve) throws IllegalBookStockException { + if (copyToRemomve > this.stock){ + throw new IllegalBookStockException(copyToRemomve, this.stock); + } + this.stock -= copyToRemomve; + } } diff --git a/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/IllegalBookStockException.java b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/IllegalBookStockException.java new file mode 100644 index 0000000..6d7db41 --- /dev/null +++ b/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/IllegalBookStockException.java @@ -0,0 +1,13 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.book.exception; + +import java.text.MessageFormat; + +public class IllegalBookStockException extends Exception { + public static final String CANNOT_REMOVE_COPY = "Cannot remove {0} copy from {1} copy"; + + public IllegalBookStockException(Integer toremove, Integer actual ) { + + super(MessageFormat.format(CANNOT_REMOVE_COPY, toremove, + actual)); + } +} diff --git a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/entity/BookTest.java b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/entity/BookTest.java index db21e1d..14fe7da 100644 --- a/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/entity/BookTest.java +++ b/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/entity/BookTest.java @@ -1,7 +1,7 @@ package fr.iut_fbleau.but3.dev62.mylibrary.book.entity; -import fr.iut_fbleau.but3.dev62.mylibrary.book.BookDTO; -import fr.iut_fbleau.but3.dev62.mylibrary.book.converter.BookConverter; + +import fr.iut_fbleau.but3.dev62.mylibrary.book.exception.IllegalBookStockException; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @@ -9,7 +9,8 @@ import org.junit.jupiter.api.Test; import java.time.LocalDate; import java.util.ArrayList; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertTrue; public class BookTest { @@ -84,5 +85,66 @@ public class BookTest { assertEquals(copyExpected, book.getStock()); } + + @Test + @DisplayName("removeLoyaltyPoints should correctly decrement loyalty points") + void testRemoveCopy() throws IllegalBookStockException { + Book book =Book.builder() + .stock(5) + .build(); + Integer copyToRemove = 2; + Integer copyExpected = 3; + + book.removeStock(copyToRemove); + + assertEquals(copyExpected, book.getStock()); + } + + @Test + @DisplayName("removeLoyaltyPoints should correctly decrement loyalty points") + void testRemoveZeroToCopy() throws IllegalBookStockException { + Book book =Book.builder() + .stock(5) + .build(); + Integer copyToRemove = 0; + Integer copyExpected = 5; + + book.removeStock(copyToRemove); + + assertEquals(copyExpected, book.getStock()); + } + + @Test + @DisplayName("removeLoyaltyPoints should correctly decrement loyalty points") + void testRemoveAllToCopy() throws IllegalBookStockException { + Book book =Book.builder() + .stock(5) + .build(); + Integer copyToRemove = 5; + Integer copyExpected = 0; + + book.removeStock(copyToRemove); + + assertEquals(copyExpected, book.getStock()); + } + + @Test + @DisplayName("removeSTock should throw exception when trying to remove more copy than available") + void testRemoveTooManyCopy() { + Book book = Book.builder() + .stock(50) + .build(); + int copyToRemove = 75; + + IllegalBookStockException exception = assertThrows( + IllegalBookStockException.class, + () -> book.removeStock(copyToRemove) + ); + + assertEquals(50, book.getStock()); + + assertTrue(exception.getMessage().contains(String.valueOf(copyToRemove))); + assertTrue(exception.getMessage().contains(String.valueOf(book.getStock()))); + } } }