diff --git a/src/tp05/Book.java b/src/tp05/Book.java new file mode 100644 index 0000000000000000000000000000000000000000..3897a3263165510abeee1c6a0eacb7d29bfd2c6f --- /dev/null +++ b/src/tp05/Book.java @@ -0,0 +1,5 @@ +package tp05; + +public class Book { + +} diff --git a/src/tp05/ComicBook.java b/src/tp05/ComicBook.java new file mode 100644 index 0000000000000000000000000000000000000000..b63c4c0e85b935af9b168b1f43e05aa5d2ea4876 --- /dev/null +++ b/src/tp05/ComicBook.java @@ -0,0 +1,5 @@ +package tp05; + +public class ComicBook { + +} diff --git a/src/tp05/Library.java b/src/tp05/Library.java new file mode 100644 index 0000000000000000000000000000000000000000..1110c1c824655aaa881c9651540194a57d08573e --- /dev/null +++ b/src/tp05/Library.java @@ -0,0 +1,5 @@ +package tp05; + +public class Library { + +} diff --git a/src/tp05/UseLibrary.java b/src/tp05/UseLibrary.java new file mode 100644 index 0000000000000000000000000000000000000000..e2550c25cabf28f48ea798ee18ae106fcfda4762 --- /dev/null +++ b/src/tp05/UseLibrary.java @@ -0,0 +1,5 @@ +package tp05; + +public class UseLibrary { + +} diff --git a/test/tp05/LibraryTest.java b/test/tp05/LibraryTest.java new file mode 100644 index 0000000000000000000000000000000000000000..a7542142a25c1048959dc45155202adbc5f1ebde --- /dev/null +++ b/test/tp05/LibraryTest.java @@ -0,0 +1,105 @@ +package tpOO.tp05; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.time.LocalDate; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.BeforeEach; + +public class LibraryTest { + private int p1, p2; + private Book b1, b2, b3; + private Library myLib; + private ComicBook c1, c2; + + @BeforeEach + public void initialization() { + // borrowers + p1 = 42; + p2 = 404; + // books + b1 = new Book("H2G2", "The Hitchhiker's Guide to the Galaxy", "D. Adams", 1979); + b2 = new Book("FLTL", "Flatland", "E.Abbott Abbott", 1884); + b3 = new Book("REU", "The Restaurant at the End of the Universe", "D. Adams", 1980); + c1 = new ComicBook("LeuG", "Léonard est un Génie", "Bob de Groot", 1977, "Turk"); + c2 = new ComicBook("GVV", "Génie, Vidi, Vici !", "Turk", 2020, "Zidrou"); + // library + myLib = new Library(); + } + + @Test + public void scenario1() { + myLib.addBook(b1); myLib.addBook(b2); myLib.addBook(b3); + assertEquals(b1, myLib.getBook("H2G2")); + assertEquals(b2, myLib.getBook("FLTL")); + assertEquals(b3, myLib.getBook("REU")); + assertNull(myLib.getBook("ERR")); + assertEquals(3, myLib.stockSize()); + assertEquals("[Book [H2G2:The Hitchhiker's Guide to the Galaxy->D. Adams,1979], Book [FLTL:Flatland->E.Abbott Abbott,1884], Book [REU:The Restaurant at the End of the Universe->D. Adams,1980]]", myLib.toString()); + } + + @Test + void scenario2() { + myLib.addBook(b1); myLib.addBook(b2); myLib.addBook(b3); + assertEquals("", myLib.borrowings()); + assertEquals(0, myLib.borrowedBookNumber()); + // p1 borrows b1 successfully + assertTrue(myLib.borrow(b1.getCode(), p1)); + assertEquals(1, myLib.borrowedBookNumber()); + // p2 borrows b1 failed + assertFalse(myLib.borrow(b1.getCode(), p2)); + assertEquals(1, myLib.borrowedBookNumber()); + // p2 borrows b3 successfully + assertTrue(myLib.borrow(b3.getCode(), p2)); + assertEquals(2, myLib.borrowedBookNumber()); + // p1 borrows b2 successfully + assertTrue(myLib.borrow(b2.getCode(), p1)); + assertEquals(3, myLib.borrowedBookNumber()); + } + + @Test + void scenario3() { + myLib.addBook(b1); myLib.addBook(c1); + assertEquals(2, myLib.stockSize()); + assertEquals(0, myLib.borrowedBookNumber()); + assertEquals("[Book [H2G2:The Hitchhiker's Guide to the Galaxy->D. Adams,1979], ComicBook[LeuG:Léonard est un Génie->Bob de Groot,1977,Turk]]", myLib.toString()); + // p1 borrows b1 successfully + assertTrue(myLib.borrow(b1.getCode(), p1)); + assertEquals(1, myLib.borrowedBookNumber()); + assertEquals("(H2G2)--42", myLib.borrowings()); + // p1 borrows c1 successfully + assertTrue(myLib.borrow(c1.getCode(), p1)); + assertEquals(2, myLib.borrowedBookNumber()); + assertEquals("(H2G2)--42(LeuG)--42", myLib.borrowings()); + } + + @Test + void scenario4() { + myLib.addBook(b1); myLib.addBook(c1); myLib.addBook(c2); + assertEquals(3, myLib.stockSize()); + assertEquals(0, myLib.borrowedBookNumber()); + assertEquals("[Book [H2G2:The Hitchhiker's Guide to the Galaxy->D. Adams,1979], ComicBook[LeuG:Léonard est un Génie->Bob de Groot,1977,Turk], ComicBook[GVV:Génie, Vidi, Vici !->Turk,2020,Zidrou]]", myLib.toString()); + // p1 borrows b1 successfully + assertTrue(b1.borrowAgain(p1)); + assertEquals(15, b1.getDurationMax()); + assertEquals(LocalDate.now().plusDays(b1.getDurationMax()), b1.getGiveBackDate()); + assertEquals(1, myLib.borrowedBookNumber()); + assertEquals("(H2G2)--42", myLib.borrowings()); + // p1 borrows c1 successfully + assertTrue(c1.borrowAgain(p1)); + assertEquals(15, c1.getDurationMax()); + assertEquals(LocalDate.now().plusDays(c1.getDurationMax()), c1.getGiveBackDate()); + assertEquals(2, myLib.borrowedBookNumber()); + assertEquals("(H2G2)--42(LeuG)--42", myLib.borrowings()); + // p1 borrows c2 successfully + assertTrue(c2.borrowAgain(p1)); + assertEquals(5, c2.getDurationMax()); + assertEquals(LocalDate.now().plusDays(c2.getDurationMax()), c2.getGiveBackDate()); + assertEquals(3, myLib.borrowedBookNumber()); + assertEquals("(H2G2)--42(LeuG)--42(GVV)--42", myLib.borrowings()); + } +} diff --git a/test/tp05/ShopTest.java b/test/tp05/ShopTest.java new file mode 100644 index 0000000000000000000000000000000000000000..dccaf258f420793c8ef088bb665ea1b9d4353cb6 --- /dev/null +++ b/test/tp05/ShopTest.java @@ -0,0 +1,158 @@ +package tpOO.tp05; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.time.LocalDate; +import java.util.ArrayList; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class ShopTest { + private String ref1, ref2, ref3, ref4; + private String label1, label2, label3, label4; + private double p5, p10, p15, p20; + private Article a1, a2, a3, a4, a5; + private ArrayList<Article> arts; + public Shop mag; + + @BeforeEach + public void initialization() { + // articles' references + ref1 = "refA"; + ref2 = "refB"; + ref3 = "refC"; + ref4 = "refD"; + // articles' labels + label1 = "a"; + label2 = "b"; + label3 = "c"; + label4 = "d"; + // articles' prices + p5 = 5.0; + p10 = 10.0; + p15 = 15.0; + p20 = 20.0; + // articles + a1 = new Article(ref1, label1, p10); + a2 = new Article(ref2, label2, p15); + a3 = new PerishableArticle(ref2, label2, p15); + a4 = new PerishableArticle(ref3, label3, p20, LocalDate.now().plusDays(5)); + a5 = new PerishableArticle(ref4, label4, p5, LocalDate.now().minusDays(1)); + arts = new ArrayList<Article>(); + arts.add(a1); + arts.add(a2); + // shop + mag = new Shop(); + } + + @Test + public void toStringArticleTest() { + assertEquals("Article [refA:a=10.0€/12.0€]", a1.toString()); + assertEquals("Article [refB:b=15.0€/18.0€]", a2.toString()); + } + + @Test + public void addArticleShopTest1() { + assertEquals(0, mag.getNbArticle()); + mag.addArticle(a1); + assertEquals(1, mag.getNbArticle()); + mag.addArticle(a2); + assertEquals(2, mag.getNbArticle()); + } + + @Test + public void addArticleShopTest2() { + assertEquals(0, mag.getNbArticle()); + mag.addArticle(arts); + assertEquals(2, mag.getNbArticle()); + } + + @Test + public void toStringPerishableArticleTest() { + assertEquals("PerishableArticle [refB:b=15.0€/18.0€-->"+LocalDate.now().plusDays(10)+"]", a3.toString()); + assertEquals("PerishableArticle [refC:c=20.0€/24.0€-->"+LocalDate.now().plusDays(5)+"]", a4.toString()); + assertEquals("PerishableArticle [refD:d=5.0€/6.0€-->"+LocalDate.now().minusDays(1)+"]", a5.toString()); + } + + @Test + public void addArticleShopTest3() { + assertEquals(0, mag.getNbArticle()); + assertEquals(0, mag.getNbPerishableArticle()); + mag.addArticle(a1); + assertEquals(1, mag.getNbArticle()); + assertEquals(0, mag.getNbPerishableArticle()); + mag.addArticle(a3); + assertEquals(2, mag.getNbArticle()); + assertEquals(1, mag.getNbPerishableArticle()); + } + + @Test + public void discountPerishableTest_none() { + // no perishable article + mag.addArticle(a1); + assertEquals(1, mag.getNbArticle()); + assertEquals(0, mag.getNbPerishableArticle()); + double oldPriceA1 = a1.getSalePrice(); + mag.discountPerishable(LocalDate.now().plusDays(10), 0.5); + assertEquals(oldPriceA1, a1.getSalePrice()); + } + @Test + public void discountPerishableTest_oneOverTwo() { + // one perishable article concerned + mag.addArticle(a1); + mag.addArticle(a3); + mag.addArticle(a4); + assertEquals(3, mag.getNbArticle()); + assertEquals(2, mag.getNbPerishableArticle()); + double oldPriceA1 = a1.getSalePrice(); + double oldPriceA3 = a3.getSalePrice(); + double oldPriceA4 = a4.getSalePrice(); + mag.discountPerishable(LocalDate.now().plusDays(8), 0.5); + assertEquals(oldPriceA1, a1.getSalePrice()); + assertEquals(oldPriceA3, a3.getSalePrice()); + assertEquals(oldPriceA4/2, a4.getSalePrice()); + } + @Test + public void discountPerishableTest_all() { + // all perishable article concerned + mag.addArticle(a1); + mag.addArticle(a3); + mag.addArticle(a4); + assertEquals(3, mag.getNbArticle()); + assertEquals(2, mag.getNbPerishableArticle()); + double oldPriceA1 = a1.getSalePrice(); + double oldPriceA3 = a3.getSalePrice(); + double oldPriceA4 = a4.getSalePrice(); + mag.discountPerishable(LocalDate.now().plusDays(15), 0.5); + assertEquals(oldPriceA1, a1.getSalePrice()); + assertEquals(oldPriceA3/2, a3.getSalePrice()); + assertEquals(oldPriceA4/2, a4.getSalePrice()); + } + + @Test + public void discountNotPerishableTest_none() { + // only perishable article + mag.addArticle(a3); + assertEquals(1, mag.getNbArticle()); + assertEquals(1, mag.getNbPerishableArticle()); + double oldPriceA3 = a3.getSalePrice(); + mag.discountNotPerishable(0.5); + assertEquals(oldPriceA3, a3.getSalePrice()); + } + + @Test + public void discountNotPerishableTest_some() { + // one perishable article and one not perishable article + mag.addArticle(a1); + mag.addArticle(a3); + assertEquals(2, mag.getNbArticle()); + assertEquals(1, mag.getNbPerishableArticle()); + double oldPriceA1 = a1.getSalePrice(); + double oldPriceA3 = a3.getSalePrice(); + mag.discountNotPerishable(0.5); + assertEquals(oldPriceA1/2, a1.getSalePrice()); + assertEquals(oldPriceA3, a3.getSalePrice()); + } + +}