From 34f458138a932b6a16a94fbe71639532cf85b943 Mon Sep 17 00:00:00 2001 From: Samuel Turpin <samuel.turpin.etu@univ-lille.fr> Date: Wed, 27 Mar 2024 17:27:57 +0100 Subject: [PATCH] vivaitalia --- src/td05/Calculatrice.java | 1 - src/tp05/Book.java | 91 +++++++++++++++++++++++++++++++++++++ src/tp05/Library.java | 92 ++++++++++++++++++++++++++++++++++++++ src/tp05/Main.java | 8 ---- src/tp05/UseLibrary.java | 15 +++++++ 5 files changed, 198 insertions(+), 9 deletions(-) create mode 100644 src/tp05/Book.java create mode 100644 src/tp05/Library.java delete mode 100644 src/tp05/Main.java create mode 100644 src/tp05/UseLibrary.java diff --git a/src/td05/Calculatrice.java b/src/td05/Calculatrice.java index e5bfd63..48e3aed 100644 --- a/src/td05/Calculatrice.java +++ b/src/td05/Calculatrice.java @@ -1,6 +1,5 @@ package td05; -import java.util.ArrayList; import java.util.Scanner; public class Calculatrice diff --git a/src/tp05/Book.java b/src/tp05/Book.java new file mode 100644 index 0000000..64f6e27 --- /dev/null +++ b/src/tp05/Book.java @@ -0,0 +1,91 @@ +package tp05; + +public class Book +{ + private String code; + private final String title; + private final String author; + private final int publicationYear; + private int user; // 0 = default + + public Book(String c, String t, String a, int p, int u) + { + this.code = c; + this.title = t; + this.author = a; + this.publicationYear = p; + this.user = u; + } + + public String getCode() + { + return code; + } + + public String getTitle() + { + return title; + } + + public String getAuthor() + { + return author; + } + + public int getPubliYear() + { + return publicationYear; + } + + public int getUser() + { + return user; + } + + public boolean borrow(int borrower) + { + this.user = borrower; + if(this.getUser() != 0) + { + return true; + } + return false; + } + + public boolean giveBack() + { + if(this.isAvailable()) + { + return false; + } + else + { + this.user = 0; + return true; + } + } + + public boolean equals(Book obj) + { + if(this == obj) { return true; } + if(this.getCode().equals(obj.getCode())) { return true; } + if((this.getAuthor().equals(obj.getAuthor())) && (this.getTitle().equals(obj.getTitle()))) { return true; } + if(this == null || obj == null) { return false; } + return false; + } + + public boolean isAvailable() + { + if(this.getUser() == 0) + { + return true; + } + return false; + } + + @Override + public String toString() + { + return "Code : "+getCode()+" | Nom : "+getTitle()+" | Auteur : "+getAuthor()+" | Publication : "+getPubliYear()+'\n'; + } +} \ No newline at end of file diff --git a/src/tp05/Library.java b/src/tp05/Library.java new file mode 100644 index 0000000..bb072f4 --- /dev/null +++ b/src/tp05/Library.java @@ -0,0 +1,92 @@ +package tp05; + +import java.util.ArrayList; + +public class Library +{ + private ArrayList<Book> books = new ArrayList<>(); + + public Book getBook(String code) + { + for(int i = 0; i <= books.size() - 1; i++) + { + if(code.equals(books.get(i).getCode())) + { + return books.get(i); + } + } + return null; + } + + public boolean addBook(Book b) + { + return books.add(b); + } + + public boolean removeBook(String aCode) + { + for(int i = 0; i <= books.size() - 1; i++) + { + if(aCode.equals(books.get(i).getCode())) + { + return books.remove(i) != null; + } + } + return false; + } + + public boolean removeBook(Book b) + { + return books.remove(b); + } + + public String borrowings() + { + String str = ""; + for(int i = 0; i <= books.size() - 1; i++) + { + if(books.get(i).getUser() == 0) + { + str=str+books.get(i); + } + } + return str; + } + + public boolean borrow(String code, int borrower) + { + for(int i = 0; i <= books.size() - 1; i++) + { + if(code.equals(books.get(i).getCode())) + { + return books.get(i).borrow(borrower); + } + } + return false; + } + + public boolean giveBack(String code) + { + for(int i = 0; i <= books.size() - 1; i++) + { + if(code.equals(books.get(i).getCode())) + { + return books.get(i).giveBack(); + } + } + return false; + } + + @Override + public String toString() + { + String str = ""; + + for(int i = 0; i <= books.size() - 1; i++) + { + Book b = books.get(i); + str=str+"Code : "+b.getCode()+" | Nom : "+b.getTitle()+" | Auteur : "+b.getAuthor()+" | Publication : "+b.getPubliYear()+'\n'; + } + return str; + } +} \ No newline at end of file diff --git a/src/tp05/Main.java b/src/tp05/Main.java deleted file mode 100644 index afa2add..0000000 --- a/src/tp05/Main.java +++ /dev/null @@ -1,8 +0,0 @@ -package tp05; - -public class Main { - public static void main(String[] args) { - System.out.println("Ok"); - System.out.println("Oui"); - } -} diff --git a/src/tp05/UseLibrary.java b/src/tp05/UseLibrary.java new file mode 100644 index 0000000..9556d37 --- /dev/null +++ b/src/tp05/UseLibrary.java @@ -0,0 +1,15 @@ +package tp05; + +public class UseLibrary +{ + public static void main(String[] args) + { + Library lib = new Library(); + lib.addBook(new Book("mc", "Minecaft", "None", 2010, 0)); + lib.addBook(new Book("nes", "Nees 1985", "N", 2000, 0)); + lib.addBook(new Book("fr", "Ma France", "Geaul", 1956, 0)); + + System.out.println(lib.getBook("nes")); + System.out.println(lib); + } +} \ No newline at end of file -- GitLab