Skip to content
Snippets Groups Projects
Commit e7d8a77d authored by Baptiste Royer's avatar Baptiste Royer
Browse files

housekeeping

parent e4a2714d
No related branches found
No related tags found
No related merge requests found
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
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
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;
}
}
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
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());
}
}
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());
}
}
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
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
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();
}
}
}
*/
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);
}
}
tp06 @ a3a413dd
Subproject commit a3a413ddd7a02a4055d86a2a8452438bba4ab392
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment