diff --git a/bin/tp09/DicoJava.class b/bin/tp09/DicoJava.class new file mode 100644 index 0000000000000000000000000000000000000000..7598ffddb1b78e518d77c4caad951af791445461 Binary files /dev/null and b/bin/tp09/DicoJava.class differ diff --git a/lib/tp04-res.jar b/lib/tp04-res.jar new file mode 100755 index 0000000000000000000000000000000000000000..4088fda2baee4ed5f221ddd3c9999890d7f2637f Binary files /dev/null and b/lib/tp04-res.jar differ diff --git a/lib/tp07-res.jar b/lib/tp07-res.jar new file mode 100644 index 0000000000000000000000000000000000000000..6926c42ee68ba1e984e142fe33e8dd818aae5833 Binary files /dev/null and b/lib/tp07-res.jar differ diff --git a/lib/tp09-res.jar b/lib/tp09-res.jar new file mode 100644 index 0000000000000000000000000000000000000000..11bab3bbfd189ba434befcd2bf9940689f047fc8 Binary files /dev/null and b/lib/tp09-res.jar differ diff --git a/res/tp09/DicoJava.txt b/res/tp09/DicoJava.txt new file mode 100755 index 0000000000000000000000000000000000000000..fa57b1e6ad6af7e98de7392223755dfe5364b4aa --- /dev/null +++ b/res/tp09/DicoJava.txt @@ -0,0 +1,53 @@ +abstract Mot-clé Déclaration d'une méthode ou d'une classe abstraite. +assert Mot-clé Assertion +break Mot-clé Interrompre une boucle ou un choix multiple. +case Mot-clé Cas dans un choix multiple. +catch Mot-clé Capture d'un type d'exception. +class Mot-clé Déclaration d'une classe. +continue Mot-clé Continuer une boucle en allant à l'itération suivante. +default Mot-clé Cas par défaut dans un choix multiple. +do Mot-clé Boucle itérative. +else Mot-clé Exécution conditionnelle. +enum Mot-clé Déclaration d'une énumération. +extends Mot-clé Héritage : déclaration de la classe mère, ou pour une interface de toutes les interfaces mères. +final Mot-clé Déclarer un membre comme final. +finally Mot-clé Code exécuté quoi qu'il se passe dans un bloc de capture d'exception. +for Mot-clé Boucle itérative. +if Mot-clé Exécution conditionnelle. +implements Mot-clé Déclaration des interfaces implémentées par une classe. +import Mot-clé Déclaration des packages utilisés par une classe. +instanceof Mot-clé Tester si un objet est de la classe indiquée (voir Transtypage). +interface Mot-clé Déclaration d'une interface. +native Mot-clé Déclaration d'une méthode native. +new Mot-clé Allocation d'une instance de classe. +package Mot-clé Déclaration du package de la classe. +private Mot-clé Déclaration d'un membre privé de la classe. +protected Mot-clé Déclaration d'un membre protégé de la classe. +public Mot-clé Déclaration d'un membre public de la classe. +return Mot-clé Retourner une valeur depuis une méthode. +static Mot-clé Déclaration d'un membre statique de la classe. +strictfp Mot-clé Déclaration d'une méthode ou classe où les opérations en virgule flottante doivent être évaluées strictement de gauche à droite selon la spécification Java. +super Mot-clé Référence à l'instance de la classe mère. +switch Mot-clé Début d'un choix multiple. +synchronized Mot-clé Voir Processus légers et synchronisation. +this Mot-clé Référence à l'instance de la classe englobante. +throw Mot-clé Lever une exception. +throws Mot-clé Déclaration des exceptions levées par une méthode. +transient Mot-clé Déclaration d'un attribut à exclure de la sérialisation. +try Mot-clé Capture d'un type d'exception. +void Mot-clé Déclaration d'une méthode ne retournant aucune valeur. +volatile Mot-clé Déclaration d'un attribut volatile, c'est à dire dont la valeur ne doit pas être mise en cache car elle est accédée par différents threads. +while Mot-clé Boucle itérative. +const Réservé Inutilisé actuellement. +goto Réservé Inutilisé actuellement. +boolean Type de données Valeur booléenne (vrai ou faux). +byte Type de données Entier signé de -128 à +127. +char Type de données Caractères Unicode (UTF-16, donc sur 16 bits). +double Type de données Nombre à virgule flottante, double précision. +float Type de données Nombre à virgule flottante, simple précision. +int Type de données Entier signé de -2 147 483 648 à +2 147 483 647. +long Type de données Entier signé de -9 223 372 036 854 775 808 à +9 223 372 036 854 775 807. +short Type de données Entier signé de -32 768 à +32 767. +false Valeur littérale Valeur booléenne fausse. +null Valeur littérale Référence nulle. +true Valeur littérale Valeur booléenne vraie. diff --git a/res/tp09/directory.txt b/res/tp09/directory.txt new file mode 100755 index 0000000000000000000000000000000000000000..b78227092e7e10819aa1fdb472fd5add268b04b2 --- /dev/null +++ b/res/tp09/directory.txt @@ -0,0 +1,5 @@ +Alice 2015 +Bruno 1942 +Clément 2019 +Dorothée 1980 +Élise 1954 diff --git a/res/tp09/testScan.csv b/res/tp09/testScan.csv new file mode 100755 index 0000000000000000000000000000000000000000..003bf1d15a367332ba0a3d3e134bee11339ccfb7 --- /dev/null +++ b/res/tp09/testScan.csv @@ -0,0 +1,4 @@ +1;3;true;false;true +1;2;false;true +2;1;42 +42;2;poo;coo diff --git a/src/tp03/Card.java b/src/tp03/Card.java new file mode 100644 index 0000000000000000000000000000000000000000..b732c43cb0b6b13ce1978a498c419c63f59ced55 --- /dev/null +++ b/src/tp03/Card.java @@ -0,0 +1,44 @@ +package tp03; + +class Card { + private Color color; + private Rank rank; + + public Card(Color color, Rank rank) { + this.color = color; + this.rank = rank; + } + + public Card(String color, String rank) { + this.color = Color.valueOf(color); + this.rank = Rank.valueOf(rank); + } + + public Color getColor() { + return this.color; + } + + public Rank getRank() { + return this.rank; + } + + public int compareRank(Card card) { + return this.rank.getValeur() - card.rank.getValeur(); + } + + public int compareColor(Card card) { + return this.color.compareTo(card.getColor()); + } + + public boolean isBefore(Card card) { + return this.rank.getValeur() - card.rank.getValeur() < 0; + } + + public boolean equals(Card card) { + return this.compareColor(card) == 0 && this.compareRank(card) == 0; + } + + public String toString() { + return this.rank.toString() + " of " + this.color.toString(); + } +} \ No newline at end of file diff --git a/src/tp03/Color.java b/src/tp03/Color.java new file mode 100644 index 0000000000000000000000000000000000000000..47cb94859ba593c492d12126d344a9548077f5b5 --- /dev/null +++ b/src/tp03/Color.java @@ -0,0 +1,5 @@ +package tp03; + +public enum Color { + CLUB, DIAMOND, HEART, SPADE; +} \ No newline at end of file diff --git a/src/tp03/Rank.java b/src/tp03/Rank.java new file mode 100644 index 0000000000000000000000000000000000000000..43511252d32c61d4e22b3a700c19e979c319c6c0 --- /dev/null +++ b/src/tp03/Rank.java @@ -0,0 +1,15 @@ +package tp03; + +public enum Rank { + SEVEN(7), EIGHT(8), NINE(9), TEN(10), JACK(11), QUEEN(12), KING(13), ACE(14); + + private final int valeur; + + private Rank(int valeur) { + this.valeur = valeur; + } + + public int getValeur() { + return this.valeur; + } +} \ No newline at end of file diff --git a/src/tp03/UseCard.java b/src/tp03/UseCard.java new file mode 100644 index 0000000000000000000000000000000000000000..f2876c0db4f4a5cdfcebddd3ab70b35e44e659ac --- /dev/null +++ b/src/tp03/UseCard.java @@ -0,0 +1,13 @@ +package tp03; + +class UseCard { + public static void main(String[] args) { + Card carte1 = new Card(Color.SPADE, Rank.SEVEN); + Card carte2 = new Card("HEART", "KING"); + System.out.println(carte1.getColor()); + System.out.println(carte2.getRank()); + System.out.println(carte1.isBefore(carte2)); + System.out.println(carte1.equals(carte2)); + System.out.println(carte1.toString() + "\n" + carte2.toString()); + } +} \ No newline at end of file diff --git a/src/tp03/UseLocalDate.java b/src/tp03/UseLocalDate.java new file mode 100644 index 0000000000000000000000000000000000000000..e418ac38675bddd7c60e7aa39891a383922e48cd --- /dev/null +++ b/src/tp03/UseLocalDate.java @@ -0,0 +1,19 @@ +package tp03; +import java.time.LocalDate; +import java.util.Scanner; + +class UseLocalDate { + private Scanner scan = new Scanner(System.in); + + public static String dateOfTheDay() { + return "Today's date is " + LocalDate.now() + "."; + } + + public static void main(String[] args) { + System.out.println(dateOfTheDay()); + } + + public static LocalDate inputDate() { + return null; + } +} \ No newline at end of file diff --git a/src/tp04/NumeroAutomatique.java b/src/tp04/NumeroAutomatique.java new file mode 100644 index 0000000000000000000000000000000000000000..4427b3cdbd5bd3a87868dda4fe7f718f65662f39 --- /dev/null +++ b/src/tp04/NumeroAutomatique.java @@ -0,0 +1,9 @@ +package tp04; + +public class NumeroAutomatique { + private static int numero = 0; + + public static int getNumber() { + return ++numero; + } +} \ No newline at end of file diff --git a/src/tp04/Person.java b/src/tp04/Person.java new file mode 100644 index 0000000000000000000000000000000000000000..b6e4a6ee464ea66fed436cda11235ea1f7b31772 --- /dev/null +++ b/src/tp04/Person.java @@ -0,0 +1,44 @@ +package tp04; + +import tp04.NumeroAutomatique; + +public class Person { + private int ID; + private String forename; + private String name; + + public Person(String forename, String name) { + this.forename = forename; + this.name = name; + NumeroAutomatique temp = new NumeroAutomatique(); + this.ID = temp.getNumber(); + } + + public int getID() { + return this.ID; + } + + public String getForename() { + return this.forename; + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public void setForename(String forename) { + this.forename = forename; + } + + public String toString() { + return ID + " : " + forename + " " + name; + } + + public boolean equals(Person other) { + return this.ID == other.ID; + } +} \ No newline at end of file diff --git a/src/tp04/Student.java b/src/tp04/Student.java new file mode 100644 index 0000000000000000000000000000000000000000..84b1305f7c3f9e50151531118297bf8697b4ae57 --- /dev/null +++ b/src/tp04/Student.java @@ -0,0 +1,90 @@ +package tp04; + +public class Student{ + private Person person; + private double[] grades; + + public Student(Person pers, double[] grades) { + this.person = pers; + this.grades = grades; + } + + public Student(String forename, String name, double[] grades) { + this.person = new Person(forename, name); + this.grades = grades; + } + + public Student(String forename, String name) { + this.person = new Person(forename, name); + this.grades = new double[0]; + } + + public String getForename() { + return this.person.getForename(); + } + + public String getName() { + return this.person.getName(); + } + + public int getID() { + return this.person.getID(); + } + + public double[] getGrades() { + return this.grades; + } + + public String getStringGrades() { + String result = "["; + for (int i = 0; i<this.grades.length; i++) { + result += this.grades[i] + ";"; + } + result = result.substring(0, result.length() - 1); + return result + "]"; + } + + public void setForename(String forename) { + this.person.setForename(forename); + } + + public void setName(String name) { + this.person.setName(name); + } + + public void setGrades(double[] grades) { + this.grades = grades; + } + + public String toString() { + return "Student [" + this.person.getID() + " : " + this.person.getForename() + " " + this.person.getName() + " = " + getStringGrades() + "; avg = " + getAverage() + "]"; + } + + public boolean equals(Student other) { + return (this.person == other.person && this.grades == other.grades); + } + + public double getAverage() { + if (grades.length == 0) { + return 0; + } else { + int somme = 0; + for (int i = 0; i<grades.length; i++) { + somme += grades[i]; + } + return somme/grades.length; + } + } + + public void addGrade(double grade) { + double[] temp = this.grades; + this.grades = new double[temp.length + 1]; + for (int i = 0; i<grades.length; i++) { + if (i<temp.length) { + this.grades[i] = temp[i]; + } else { + this.grades[i] = grade; + } + } + } +} \ No newline at end of file diff --git a/src/tp04/StudentAbs.java b/src/tp04/StudentAbs.java new file mode 100644 index 0000000000000000000000000000000000000000..09f736bf7bd49c90383536c4aba77ee28d1e87a1 --- /dev/null +++ b/src/tp04/StudentAbs.java @@ -0,0 +1,38 @@ +package tp04; + +public class StudentAbs { + private Student stud; + private int nbAbsence = 0; + + public StudentAbs(Student stud) {this.stud = stud;} + public StudentAbs(String forename, String name, double[] grades) {this.stud = new Student(forename, name, grades);} + public StudentAbs(String forename, String name) {this.stud = new Student(forename, name);} + + public Student getStud() { + return this.stud; + } + + public int getNbAbsences() { + return this.nbAbsence; + } + + public void increaseAbs() { + this.nbAbsence++; + } + + public String toString() { + return this.stud.toString() + ", nbAbs = " + nbAbsence; + } + + public boolean equals(StudentAbs other) { + return (this.stud.equals(other.stud) && this.nbAbsence == other.nbAbsence); + } + + public boolean warning(int thresholdAbs, double thresholdAvg) { + return (this.nbAbsence >= thresholdAbs || this.stud.getAverage() <= thresholdAvg); + } + + public boolean validation(int thresholdAbs, double thresholdAvg) { + return (this.nbAbsence <= thresholdAbs && this.stud.getAverage() >= thresholdAvg); + } +} \ No newline at end of file diff --git a/src/tp04/UsePendingCaseQueue.java b/src/tp04/UsePendingCaseQueue.java new file mode 100755 index 0000000000000000000000000000000000000000..05bc89220d280594566c8b7f42f5a2b62b89c2b5 --- /dev/null +++ b/src/tp04/UsePendingCaseQueue.java @@ -0,0 +1,29 @@ +package tp04; + +public class UsePendingCaseQueue { + public static void main(String[] args) { + PendingCase pc1 = new PendingCase("Alice", "AAA", 1234.56); + PendingCase pc2 = new PendingCase("Bruno", "BBB", 0.42); + PendingCase pc3 = new PendingCase("Chloé", "CCC", 745.99); + PendingCase pc4 = new PendingCase("Denis", "DDD", 125.0); + PendingCaseQueue.CAPACITY = 3; + PendingCaseQueue pcq = new PendingCaseQueue(); + System.out.println("Before anything: " + pcq); + System.out.println("Empty? " + pcq.isEmpty() + "\tFull? " + pcq.isFull()); + pcq.addOne(pc1); + pcq.addOne(pc2); + System.out.println("After addition of pc1 and pc2: " + pcq); + System.out.println("Empty? " + pcq.isEmpty() + "\tFull? " + pcq.isFull()); + pcq.addOne(pc3); + System.out.println("After addition of pc3: " + pcq); + System.out.println("Empty? " + pcq.isEmpty() + "\tFull? " + pcq.isFull()); + pcq.addOne(pc4); + System.out.println("After addition of pc4: \t" + pcq); + pcq.removeOne(); + System.out.println("After a removal: " + pcq); + pcq.addOne(pc1); + System.out.println("After addition of pc1: " + pcq); + pcq.clear(); + System.out.println("After clearing: " + pcq); + } +} diff --git a/src/tp04/UsePerson.java b/src/tp04/UsePerson.java new file mode 100644 index 0000000000000000000000000000000000000000..aaf0940b6992750d3a04a452c724321088aee4f9 --- /dev/null +++ b/src/tp04/UsePerson.java @@ -0,0 +1,28 @@ +package tp04; + +public class UsePerson { + public static void main(String[] args) { + Person alice = new Person("Alice", "A"); + Person bruno = new Person("Bruno", "B"); + Person autre = alice; + boolean testsOK = true; + // test des getters + if(!alice.getForename().equals("Alice") || !alice.getName().equals("A") || !bruno.getForename().equals("Bruno") || !bruno.getName().equals("B") || !autre.getForename().equals("Alice") || !autre.getName().equals("A")) { + System.out.println("Erreur de méthodes GET"); + testsOK = false; + } + // test des setters + alice.setForename("Anatole"); + bruno.setName("BB"); + if(!alice.getForename().equals("Anatole") || !alice.getName().equals("A") || !bruno.getForename().equals("Bruno") || !bruno.getName().equals("BB") || !autre.getForename().equals("Anatole") || !autre.getName().equals("A")) { + System.out.println("Erreur de méthodes SET"); + testsOK = false; + } + // test du equals + if(alice.equals(bruno) || !alice.equals(autre)) { + System.out.println("Erreur de méthode equals"); + testsOK = false; + } + if(testsOK) System.out.println("Tests de UsePerson réussis !"); + } +} diff --git a/src/tp04/UseStudent.java b/src/tp04/UseStudent.java new file mode 100644 index 0000000000000000000000000000000000000000..c3e7fd8994b55bbdab150dff74780f623dcf6c78 --- /dev/null +++ b/src/tp04/UseStudent.java @@ -0,0 +1,20 @@ +package tp04; + +public class UseStudent { + public static void main(String[] args) { + double[] grades = new double[] {19.25, 15.2, 0.01}; + double[] grades2 = new double[] {5, 2849, 9}; + Person gens1 = new Person("AAAAAAAAAAAAAAAAAAA", "BBBBB"); + Student gars1 = new Student(gens1, grades); + Student gars2 = new Student("Jean-Marie", "Lepen", grades2); + Student gars3 = new Student("Gicé", "Védé"); + gars3.addGrade(5.49); + gars3.addGrade(5.49); + gars3.addGrade(5.49); + System.out.println(gars1.equals(gars2)); + System.out.println(gars2.equals(gars3)); + System.out.println(gars1.toString()); + System.out.println(gars2.toString()); + System.out.println(gars3.toString()); + } +} \ No newline at end of file diff --git a/src/tp04/UseYearGroup.java b/src/tp04/UseYearGroup.java new file mode 100644 index 0000000000000000000000000000000000000000..6b6319de50561d94c3450a8b8d4ad13df6757309 --- /dev/null +++ b/src/tp04/UseYearGroup.java @@ -0,0 +1,20 @@ +package tp04; + +public class UseYearGroup { + public static void main(String[] args) { + Student gars1 = new Student("Clara", "Oké", new double[] {19.25, 15.2, 17.3}); + Student gars2 = new Student("Will", "Kinson", new double[] {5, 13, 2}); + Student gars3 = new Student("Titouan", "Ladopté", new double[] {17.6, 14.3, 15.9}); + Student gars4 = new Student("Gicé", "Védé", new double[] {0, 0.1, 0.2}); + StudentAbs abs1 = new StudentAbs(gars1); + StudentAbs abs2 = new StudentAbs(gars2); + StudentAbs abs3 = new StudentAbs(gars3); + StudentAbs abs4 = new StudentAbs(gars4); + for (int i = 0; i < 10; i++) { + abs3.increaseAbs(); + abs4.increaseAbs(); + } + YearGroup BUT1 = new YearGroup(new StudentAbs[]{abs1, abs2, abs3, abs4}); + BUT1.validation(5, 14); + } +} \ No newline at end of file diff --git a/src/tp04/YearGroup.java b/src/tp04/YearGroup.java new file mode 100644 index 0000000000000000000000000000000000000000..8efdbe4085361aaf46b9ef3f3f63ad53cc7d2aca --- /dev/null +++ b/src/tp04/YearGroup.java @@ -0,0 +1,40 @@ +package tp04; + +public class YearGroup { + private StudentAbs[] listeEtu; + + public YearGroup(StudentAbs[] liste) {this.listeEtu = liste;} + public YearGroup() {this.listeEtu = new StudentAbs[0];} + + public StudentAbs[] getListeEtu() { + return listeEtu; + } + + public String getStringListeEtu() { + String result = "["; + for (int i = 0; i<this.listeEtu.length; i++) { + result+=listeEtu[i].toString() + "\n"; + } + return result + "]"; + } + + public void setListeEtu(StudentAbs[] liste) { + this.listeEtu = liste; + } + + public void addGrade(double[] grades) { + for (int i = 0; i < this.listeEtu.length; i++) { + this.listeEtu[i].getStud().addGrade(grades[i]); + } + } + + public void validation(int thresholdAbs, int thresholdAvg) { + String lesGensQuiPassent = ""; + for (int i = 0; i < this.listeEtu.length; i++) { + if (this.listeEtu[i].validation(thresholdAbs, thresholdAvg)) { + lesGensQuiPassent+=listeEtu[i].toString() + "\n"; + } + } + System.out.println(lesGensQuiPassent.substring(0, lesGensQuiPassent.length() - 1)); + } +} \ No newline at end of file diff --git a/src/tp05/Book.java b/src/tp05/Book.java new file mode 100644 index 0000000000000000000000000000000000000000..280ed8f71c5c4da9623fdf698f9c83f554f28d15 --- /dev/null +++ b/src/tp05/Book.java @@ -0,0 +1,95 @@ +package tp05; + +import java.time.LocalDateTime; + +public class Book { + private String code; + private String title; + private String author; + private int publicationYear; + private int borrower = 0; + private LocalDateTime borrowingDate = null; + + public Book(String code, String title, String author, int publicationYear) { + this.code = code; + this.title = title; + this.author = author; + this.publicationYear = publicationYear; + } + + public String getCode() { + return this.code; + } + + public String getTitle() { + return this.title; + } + + public String getAuthor() { + return this.author; + } + + public int getPublicationYear() { + return this.publicationYear; + } + + public int getBorrower() { + return this.borrower; + } + + public LocalDateTime getBorrowingDate() { + return this.borrowingDate; + } + + public void setCode(String code) { + this.code = code; + } + + public void setTitle(String title) { + this.title = title; + } + + public void setAuthor(String author) { + this.author = author; + } + + public void setPublicationYear(int publicationYear) { + this.publicationYear = publicationYear; + } + + public boolean borrow(int borrower) { + if (this.borrower == 0) { + this.borrower = borrower; + this.borrowingDate = LocalDateTime.now(); + return true; + } else { + return false; + } + } + + public boolean giveBack() { + if (this.borrower != 0) { + this.borrower = 0; + this.borrowingDate = null; + return true; + } else { + return false; + } + } + + public LocalDateTime getGiveBackDate() { + return this.borrowingDate.plusDays(10); + } + + public boolean isAvailable() { + return this.borrower == 0; + } + + public String toString() { + String borrowed = ""; + if (this.borrowingDate != null) { + borrowed = " borrowed the : " + this.borrowingDate.toString(); + } + return "Book [" + this.getCode() + ":" + this.title + " -> " + this.author + ", " + this.publicationYear + "]" + borrowed; + } +} \ No newline at end of file diff --git a/src/tp05/ComicBook.java b/src/tp05/ComicBook.java new file mode 100644 index 0000000000000000000000000000000000000000..70841601af04419c731a7266bd0f7b670a583132 --- /dev/null +++ b/src/tp05/ComicBook.java @@ -0,0 +1,18 @@ +package tp05; + +public class ComicBook extends Book { + private String illustrator; + + public ComicBook(String code, String title, String author, String illustrator, int publicationYear) { + super(code, title, author, publicationYear); + this.illustrator = illustrator; + } + + public String toString() { + String borrowed = ""; + if (super.getBorrowingDate() != null) { + borrowed = " borrowed the : " + super.getBorrowingDate().toString(); + } + return "ComicBook [" + super.getCode() + ":" + super.getTitle() + " -> " + super.getAuthor() + ", " + super.getPublicationYear() + ", " + this.illustrator + "]" + borrowed; + } +} diff --git a/src/tp05/Library.java b/src/tp05/Library.java new file mode 100644 index 0000000000000000000000000000000000000000..58d5597558c9945280e5f4a57e8a919662b4e327 --- /dev/null +++ b/src/tp05/Library.java @@ -0,0 +1,89 @@ +package tp05; + +public class Library { + private Book[] catalog = new Book[0]; + + public Book getBook(String code) { + int i = 0; + while (i<this.catalog.length && this.catalog[i].getCode() != code) { + i++; + } + return catalog[i]; + } + + public boolean addBook(Book book) { + Book[] temp = catalog; + this.catalog = new Book[temp.length+1]; + for (int i = 0; i < temp.length; i++) { + this.catalog[i] = temp[i]; + } + this.catalog[temp.length] = book; + return true; + } + + public boolean removeBook(String aCode) { + Book remove = getBook(aCode); + int indice = getIndice(remove); + Book[] temp = this.catalog; + this.catalog = new Book[temp.length-1]; + int index = 0; + for (int i = 0; i<this.catalog.length; i++) { + if(i != indice) { + this.catalog[index] = temp[i]; + index++; + } + } + return true; + } + + public boolean removeBook(Book b) { + int indice = getIndice(b); + Book[] temp = this.catalog; + this.catalog = new Book[temp.length-1]; + int index = 0; + for (int i = 0; i<this.catalog.length; i++) { + if(i != indice) { + this.catalog[index] = temp[i]; + index++; + } + } + return true; + } + + public int getIndice(Book book) { + int i = 0; + while (i<this.catalog.length && this.catalog[i] != book) { + i++; + } + return i; + } + + public String borrowings() { + String result = ""; + for (int i = 0; i<this.catalog.length;i++) { + if (this.catalog[i].getBorrower() != 0) { + result += "(" + this.catalog[i].getCode() + ")--" + this.catalog[i].getBorrower() + " "; + } + } + return result; + } + + public boolean borrow(String code, int borrower) { + Book book = getBook(code); + return (book.borrow(borrower)); + } + + public boolean giveBack(String code) { + Book book = getBook(code); + return book.giveBack(); + } + + public String toString() { + String result = "["; + for (int i = 0; i<this.catalog.length; i++) { + result += this.catalog[i].toString() + "\n"; + } + return result.substring(0, result.length()-1) + "]"; + } + +} \ No newline at end of file diff --git a/src/tp05/LibraryTest.java b/src/tp05/LibraryTest.java new file mode 100644 index 0000000000000000000000000000000000000000..a7542142a25c1048959dc45155202adbc5f1ebde --- /dev/null +++ b/src/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/src/tp05/ShopTest.java b/src/tp05/ShopTest.java new file mode 100644 index 0000000000000000000000000000000000000000..dccaf258f420793c8ef088bb665ea1b9d4353cb6 --- /dev/null +++ b/src/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()); + } + +} diff --git a/src/tp05/Test.java b/src/tp05/Test.java new file mode 100644 index 0000000000000000000000000000000000000000..56f69a0c65cdbe1405b84b5ef48363dc811c9e6b --- /dev/null +++ b/src/tp05/Test.java @@ -0,0 +1,9 @@ +package tp05; + +public class Test { + public static void main(String[] args) { + Object test = new Book("A", "A", "Jean", 200); + + System.out.println(test.getClass() == Book.class); + } +} \ No newline at end of file diff --git a/src/tp05/UseLibrary.java b/src/tp05/UseLibrary.java new file mode 100644 index 0000000000000000000000000000000000000000..bd642508accab220c4ca2e6409b8d7a23ea293e9 --- /dev/null +++ b/src/tp05/UseLibrary.java @@ -0,0 +1,50 @@ +package tp05; + +public class UseLibrary { + public static void main(String[] args) { + Library bookstore = new Library(); + Book book1 = new Book("LGDC", "La Guerre Des Clans", "Erin Hunter", 2003); + Book book2 = new Book("ERGN", "Eragon", "Christopher Paolini", 2002); + Book book3 = new Book("LMDO", "Les Métamprphoses D'Ovide", "Ovide", 100); + ComicBook book4 = new ComicBook("TAT", "Tintin Au Tibet", "Hergé", "Hergé", 1960); + int borrower1 = 42; + int borrower2 = 404; + + boolean add1 = bookstore.addBook(book1); + boolean add2 = bookstore.addBook(book2); + boolean add3 = bookstore.addBook(book3); + boolean add4 = bookstore.addBook(book4); + + Book search = bookstore.getBook("ERGN"); + + String script = bookstore.toString(); + + System.out.println(add1); + System.out.println(add2); + System.out.println(add3); + System.out.println(add4); + + newLine(); + + System.out.println(search.toString()); + + newLine(); + + System.out.println(script); + + newLine(); + + System.out.println(bookstore.borrow("ERGN", borrower1) + " --> " + bookstore.borrowings()); + System.out.println(bookstore.borrow("ERGN", borrower2) + " --> " + bookstore.borrowings()); + System.out.println(bookstore.borrow("LGDC", borrower2) + " --> " + bookstore.borrowings()); + System.out.println(bookstore.borrow("LMDO", borrower1) + " --> " + bookstore.borrowings()); + + newLine(); + + System.out.println(bookstore.toString()); + } + + public static void newLine() { + System.out.println(); + } +} \ No newline at end of file diff --git a/src/tp09/DicoJava.java b/src/tp09/DicoJava.java new file mode 100644 index 0000000000000000000000000000000000000000..f4477c93b4597f9d3c2d596e5c7a1c81ad289a83 --- /dev/null +++ b/src/tp09/DicoJava.java @@ -0,0 +1,67 @@ +package tp09; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.io.File; // Import the File class +import java.io.FileWriter; // Import the FileWriter class + +public class DicoJava { + + public static int getIndiceTabulation(String line) { + int i = 0; + while (i < line.length() && line.charAt(i) != '\t') { + i++; + } + return i; + } + + public static void main(String[] args) { + + try { + // Création d'un fileReader pour lire le fichier + FileReader fileReader = new FileReader("res/tp09/DicoJava.txt"); + + // Création d'un fichier + FileWriter myWriter = new FileWriter("MotsJava.txt"); + + // Création d'un bufferedReader qui utilise le fileReader + BufferedReader reader = new BufferedReader(fileReader); + + // une fonction à essayer pouvant générer une erreur + String line = reader.readLine(); + + while (line != null) { + // affichage de la ligne + line = line.substring(0, getIndiceTabulation(line)); + myWriter.write(line + "\n"); + // lecture de la prochaine ligne + line = reader.readLine(); + } + reader.close(); + myWriter.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} +/* +import java.io.File; // Import the File class +import java.io.IOException; // Import the IOException class to handle errors + +public class CreateFile { + public static void main(String[] args) { + try { + File myObj = new File("filename.txt"); + if (myObj.createNewFile()) { + System.out.println("File created: " + myObj.getName()); + } else { + System.out.println("File already exists."); + } + } catch (IOException e) { + System.out.println("An error occurred."); + e.printStackTrace(); + } + } +} +*/ diff --git a/src/tp09/HierarchyCreation.java b/src/tp09/HierarchyCreation.java new file mode 100755 index 0000000000000000000000000000000000000000..3aaa161a71a45257f8d8606c2f9072c463770996 --- /dev/null +++ b/src/tp09/HierarchyCreation.java @@ -0,0 +1,62 @@ +package util; + +import java.io.File; +import java.io.IOException; + +public class HierarchyCreation { + public static void hierarchyCreation(String path) throws IOException { + File f = new File(path); + if(!f.exists()) { + path = System.getProperty("user.home"); + } + path = path + File.separator + "hierarchy" + File.separator; + // directory dir1 and dir11 + String path_tmp = path + File.separator + "dir1" + File.separator + "dir11"; + File f_tmp = new File(path_tmp); + f_tmp.mkdirs(); + // directory dir2 + path_tmp = path + File.separator + "dir2"; + f_tmp = new File(path_tmp); + f_tmp.mkdir(); + // content of hierarchy + f = new File(path + "file1"); + f.createNewFile(); + f.setExecutable(true); + f = new File(path + "file2"); + f.createNewFile(); + f.setExecutable(false); + f = new File(path + ".file3"); + f.createNewFile(); + f.setExecutable(true); + // content of dir1 + f = new File(path + File.separator + "dir1" + File.separator + "file11"); + f.createNewFile(); + f.setExecutable(true); + f = new File(path + File.separator + "dir1" + File.separator + "file12"); + f.createNewFile(); + f.setExecutable(false); + f = new File(path + File.separator + "dir1" + File.separator + ".file13"); + f.createNewFile(); + f.setExecutable(true); + // content of dir2 + f = new File(path + File.separator + "dir2" + File.separator + "file21"); + f.createNewFile(); + f.setExecutable(true); + f = new File(path + File.separator + "dir2" + File.separator + "file22"); + f.createNewFile(); + f.setExecutable(false); + f = new File(path + File.separator + "dir2" + File.separator + ".file23"); + f.createNewFile(); + f.setExecutable(true); + // content of dir11 + f = new File(path + File.separator + "dir1" + File.separator + "dir11" + File.separator + "file111"); + f.createNewFile(); + f.setExecutable(true); + f = new File(path + File.separator + "dir1" + File.separator + "dir11" + File.separator + "file112"); + f.createNewFile(); + f.setExecutable(false); + f = new File(path + File.separator + "dir1" + File.separator + "dir11" + File.separator + ".file113"); + f.createNewFile(); + f.setExecutable(true); + } +} diff --git a/tp06 b/tp06 deleted file mode 160000 index a3a413ddd7a02a4055d86a2a8452438bba4ab392..0000000000000000000000000000000000000000 --- a/tp06 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit a3a413ddd7a02a4055d86a2a8452438bba4ab392