diff --git a/src/exercises/ex_q3/Book.java b/src/exercises/ex_q3/Book.java new file mode 100644 index 0000000..39d6118 --- /dev/null +++ b/src/exercises/ex_q3/Book.java @@ -0,0 +1,8 @@ +package exercises.ex_q3; + +public record Book(String ISBN, String title, String authorFirstName, String authorLastName, int year) implements Comparable { + @Override + public int compareTo(Book o) { + return title.compareTo(o.title); + } +} diff --git a/src/exercises/ex_q3/BookGenerator.java b/src/exercises/ex_q3/BookGenerator.java new file mode 100644 index 0000000..bc89237 --- /dev/null +++ b/src/exercises/ex_q3/BookGenerator.java @@ -0,0 +1,67 @@ +package exercises.ex_q3; + +import java.util.ArrayList; +import java.util.Random; + +public class BookGenerator { + private static final String[] adjectives = { + "big", "red", "small", "weird", "amazing", "fantastic", "friendly", "curious", "frightening", + }; + private static final String[] nouns = { + "apple", "car", "gingerbread man", "octopus", "planet", "volcano", "bottle", "adventure" + }; + private static final String[] determinants = {"the", "a"}; + private static final String vowels = "aeiou"; + + private static final String[] firstnames = { + "John", "Patrick", "Gérard", "Jean-Michel", "Ronswalle", "Guilherme", "Jean-Claude", "Perceval", "Hector", "Ferguson", + "Azénor", "Clémentine", "Ailizabette", "Bérangère", "Clothilde", "Mélissandre" + }; + + private static final String[] lastnames = { + "de Mont-Castel", "Smith", "Martin", "Ferguson", "Lassalle", "Berset", "Constantin" + }; + + private static final Random random = new Random(); + + private static String getRandom(String[] list) { + int i = random.nextInt(list.length); + return list[i]; + } + + public static Book generateRandomBook() { + String noun = getRandom(nouns); + String adj = getRandom(adjectives); + String det = getRandom(determinants); + if (det.equals("a") && vowels.contains(adj.substring(0, 1))) { + det += "n"; + } + String title = det + " " + adj + " " + noun; + title = title.substring(0, 1).toUpperCase() + title.substring(1); + + String isbn = ""; + int checksum = 0; + for (int i = 0; i < 9; i++) { + int digit = random.nextInt(10); + isbn += String.valueOf(digit); + if (i % 4 == 0) { + isbn += "-"; + } + checksum += (10 - i) * digit; + } + checksum = checksum % 11; + if (checksum != 0) { + checksum = 11 - checksum; + } + if (checksum == 10) { + isbn += "X"; + } else { + isbn += String.valueOf(checksum); + } + + String firstname = getRandom(firstnames); + String lastname = getRandom(lastnames); + + return new Book(isbn, title, firstname, lastname, random.nextInt(1000, 2025)); + } +} diff --git a/src/exercises/ex_q3/Library.java b/src/exercises/ex_q3/Library.java new file mode 100644 index 0000000..ab6d177 --- /dev/null +++ b/src/exercises/ex_q3/Library.java @@ -0,0 +1,20 @@ +package exercises.ex_q3; + +import java.util.Arrays; + +public class Library { + private static final int SIZE = 100_000; + private static final Book[] books = new Book[SIZE]; + + public static void main(String[] args) { + for (int i = 0; i < SIZE; i++) { + Book book = BookGenerator.generateRandomBook(); + books[i] = book; + } + + Arrays.parallelSort(books); + for (int i = 0; i < Math.min(15, SIZE); i++) { + System.out.println(books[i]); + } + } +}