From afc2fbfa8ae7e82a371c53899bff0c3f44cfa129 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Mon, 13 Jan 2025 18:18:14 +0100 Subject: [PATCH] started ex Z4 --- .idea/uiDesigner.xml | 124 ++++++++++++++++++ src/exercises/ex_k_bis1/Customer.java | 17 +++ src/exercises/ex_k_bis1/TestServiceDesk.java | 22 ++++ src/exercises/ex_n_bis/LibraryCatalog.java | 75 +++++++++++ src/exercises/ex_w/Car.java | 19 +++ src/exercises/ex_w/Customer.java | 18 +++ src/exercises/ex_w/Garage.java | 33 +++++ .../ex_w/TestingExchanger_Garage.java | 12 ++ src/exercises/ex_z4/AuctionMain.java | 23 ++++ src/exercises/ex_z4/Auctioneer.java | 81 ++++++++++++ src/exercises/ex_z4/Bid.java | 5 + src/exercises/ex_z4/Bidder.java | 44 +++++++ src/exercises/ex_z4/Item.java | 5 + 13 files changed, 478 insertions(+) create mode 100644 .idea/uiDesigner.xml create mode 100644 src/exercises/ex_k_bis1/Customer.java create mode 100644 src/exercises/ex_k_bis1/TestServiceDesk.java create mode 100644 src/exercises/ex_n_bis/LibraryCatalog.java create mode 100644 src/exercises/ex_w/Car.java create mode 100644 src/exercises/ex_w/Customer.java create mode 100644 src/exercises/ex_w/Garage.java create mode 100644 src/exercises/ex_w/TestingExchanger_Garage.java create mode 100644 src/exercises/ex_z4/AuctionMain.java create mode 100644 src/exercises/ex_z4/Auctioneer.java create mode 100644 src/exercises/ex_z4/Bid.java create mode 100644 src/exercises/ex_z4/Bidder.java create mode 100644 src/exercises/ex_z4/Item.java diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/exercises/ex_k_bis1/Customer.java b/src/exercises/ex_k_bis1/Customer.java new file mode 100644 index 0000000..b3e0e36 --- /dev/null +++ b/src/exercises/ex_k_bis1/Customer.java @@ -0,0 +1,17 @@ +package exercises.ex_k_bis1; + +public class Customer implements Runnable { + /*private String name; + private ServiceDesk serviceDesk; + + public Customer(String name, ServiceDesk serviceDesk) { + this.name = name; + this.serviceDesk = serviceDesk; + } + */ + + @Override + public void run() { + + } +} diff --git a/src/exercises/ex_k_bis1/TestServiceDesk.java b/src/exercises/ex_k_bis1/TestServiceDesk.java new file mode 100644 index 0000000..648fa6a --- /dev/null +++ b/src/exercises/ex_k_bis1/TestServiceDesk.java @@ -0,0 +1,22 @@ +package exercises.ex_k_bis1; + +public class TestServiceDesk { + public static void main(String[] args) { + /* + ServiceDesk serviceDesk = new ServiceDesk(2); + for (int j = 0; j<3; j++) { + new Thread(new ServiceRepresentative(serviceDesk), "Employee-"+ j).start(); + } + // Start customer threads + for (int i = 0; i < 10; i++) { // 30 customers + String name = "Customer-" + i; + new Thread(new Customer(name, serviceDesk)).start(); + try { + Thread.sleep((int) (Math.random() * 1000)); // Random arrival + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + */ + } +} diff --git a/src/exercises/ex_n_bis/LibraryCatalog.java b/src/exercises/ex_n_bis/LibraryCatalog.java new file mode 100644 index 0000000..bdb4e29 --- /dev/null +++ b/src/exercises/ex_n_bis/LibraryCatalog.java @@ -0,0 +1,75 @@ +package exercises.ex_n_bis; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +public class LibraryCatalog { + private final Map catalog = new HashMap<>(); + private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + private final Lock readLock = lock.readLock(); + private final Lock writeLock = lock.writeLock(); + + public LibraryCatalog() { + + } + + public ArrayList getBooks() { + ArrayList books = new ArrayList<>(); + try { + readLock.lock(); + catalog.forEach((title, author) -> { + books.add(new Book(title, author)); + }); + } finally { + readLock.unlock(); + } + return books; + } + + public String findBook(String title) { + String author; + try { + readLock.lock(); + author = catalog.get(title); + } finally { + readLock.unlock(); + } + if (author == null) { + System.out.println("The book '" + title + "' is not in the catalog"); + return null; + } + return author; + } + + public void addBook(String title, String author) { + try { + writeLock.lock(); + catalog.put(title, author); + } finally { + writeLock.unlock(); + } + } + + public void removeBook(String title) { + try { + writeLock.lock(); + catalog.remove(title); + } finally { + writeLock.unlock(); + } + } + + public void updateBook(String title, String newAuthor) { + try { + writeLock.lock(); + catalog.replace(title, newAuthor); + } finally { + writeLock.unlock(); + } + } + + public record Book(String title, String author) {} +} diff --git a/src/exercises/ex_w/Car.java b/src/exercises/ex_w/Car.java new file mode 100644 index 0000000..daa55c4 --- /dev/null +++ b/src/exercises/ex_w/Car.java @@ -0,0 +1,19 @@ +package exercises.ex_w; + +public class Car { + private String name; + private String licensePlateNumber; + public Car(String name, String licensePlateNumber) { + this.name = name; + this.licensePlateNumber = licensePlateNumber; + } + + public String getLicensePlateNumber() { + return licensePlateNumber; + } + + @Override + public String toString() { + return name; + } +} diff --git a/src/exercises/ex_w/Customer.java b/src/exercises/ex_w/Customer.java new file mode 100644 index 0000000..57b23dd --- /dev/null +++ b/src/exercises/ex_w/Customer.java @@ -0,0 +1,18 @@ +package exercises.ex_w; + +public class Customer implements Runnable { + private final String name; + private final String licensePlateNumber; + private Car car; + + public Customer(String name, Car car) { + this.name = name; + this.car = car; + licensePlateNumber = car.getLicensePlateNumber(); + } + + @Override + public void run() { + + } +} diff --git a/src/exercises/ex_w/Garage.java b/src/exercises/ex_w/Garage.java new file mode 100644 index 0000000..6c9643c --- /dev/null +++ b/src/exercises/ex_w/Garage.java @@ -0,0 +1,33 @@ +package exercises.ex_w; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Exchanger; + +public class Garage implements Runnable { + private List courtesyCars = new ArrayList<>(); + private Exchanger receiveExchanger = new Exchanger<>(); + + public Garage() { + + } + + public Car leaveForRepair(Car car) throws InterruptedException { + return receiveExchanger.exchange(car); + } + + @Override + public void run() { + while (true) { + Car courtesyCar = courtesyCars.removeFirst(); + try { + Car toRepair = receiveExchanger.exchange(courtesyCar); + + + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + } + } +} diff --git a/src/exercises/ex_w/TestingExchanger_Garage.java b/src/exercises/ex_w/TestingExchanger_Garage.java new file mode 100644 index 0000000..14743ae --- /dev/null +++ b/src/exercises/ex_w/TestingExchanger_Garage.java @@ -0,0 +1,12 @@ +package exercises.ex_w; + +public class TestingExchanger_Garage { + String[] carTypes = {"Seat Alhambra", "BMW Mini", "Tesla 3"}; + public static void main(String[] args) { + // Create a Customer thread and a Garage thread + // that use an exchanger for replacing the customer car + // with a courtesy car during the reparation. At the end of the day, + // the cars are exchanged back. + Garage garage = new Garage(); + } +} diff --git a/src/exercises/ex_z4/AuctionMain.java b/src/exercises/ex_z4/AuctionMain.java new file mode 100644 index 0000000..8f25ac8 --- /dev/null +++ b/src/exercises/ex_z4/AuctionMain.java @@ -0,0 +1,23 @@ +package exercises.ex_z4; + +import java.util.Random; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.Semaphore; + +public class AuctionMain { + public static final String[] names = { + "Sabrina", "José", "Patrick", "Salomé", "Lisa", "Alfred", "Annna", "Alex", "Kevin" + }; + + public static void main(String[] args) { + Semaphore mutex = new Semaphore(1); + ConcurrentLinkedQueue bids = new ConcurrentLinkedQueue<>(); + Auctioneer auctioneer = new Auctioneer("Paul", bids, mutex); + + new Thread(auctioneer).start(); + + for (int i = 0; i < names.length; i++) { + new Thread(new Bidder(names[i])).start(); + } + } +} diff --git a/src/exercises/ex_z4/Auctioneer.java b/src/exercises/ex_z4/Auctioneer.java new file mode 100644 index 0000000..a9ff427 --- /dev/null +++ b/src/exercises/ex_z4/Auctioneer.java @@ -0,0 +1,81 @@ +package exercises.ex_z4; + +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.Semaphore; + +public class Auctioneer implements Runnable { + private String name; + private ConcurrentLinkedQueue bids; + private Semaphore mutex; + private Bid maximumBid; + private Thread countdownThread = null; + + public Auctioneer(String name, ConcurrentLinkedQueue bids, Semaphore mutex) { + this.name = name; + this.bids = bids; + this.mutex = mutex; + } + + public void placeBid(Bid bid) { + bids.add(bid); + notify(); + } + + private void countdown() { + try { + Thread.sleep(1000); + System.out.println("Three"); + Thread.sleep(1000); + System.out.println("Two"); + Thread.sleep(1000); + System.out.println("One"); + Thread.sleep(1000); + System.out.println("End of the Auction !"); + notify(); + } catch (InterruptedException _) {} + } + + public void startCountdown() { + countdownThread = new Thread(this::countdown); + countdownThread.start(); + } + + public Bid getMaximumBid() { + return maximumBid; + } + + public void endAuction(){ + System.out.println("End of the auction ! "); + notifyAll(); + } + + @Override + public void run() { + while (true) { + try { + mutex.acquire(); + Bid current_bid = bids.poll(); + if (current_bid == null) { + startCountdown(); + wait(); + if (bids.isEmpty()) { + endAuction(); + } + continue; + } + if (!bids.isEmpty()) { + if (current_bid.amount() > maximumBid.amount()) { + maximumBid = current_bid; + System.out.println("New bid : " + maximumBid.amount() + " by " + maximumBid.bidder()); + mutex.release(); + return; + } + } else { + mutex.release(); + } + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + } +} diff --git a/src/exercises/ex_z4/Bid.java b/src/exercises/ex_z4/Bid.java new file mode 100644 index 0000000..9dc1ce8 --- /dev/null +++ b/src/exercises/ex_z4/Bid.java @@ -0,0 +1,5 @@ +package exercises.ex_z4; + +public record Bid(Bidder bidder, double amount) { + +} diff --git a/src/exercises/ex_z4/Bidder.java b/src/exercises/ex_z4/Bidder.java new file mode 100644 index 0000000..4417fc9 --- /dev/null +++ b/src/exercises/ex_z4/Bidder.java @@ -0,0 +1,44 @@ +package exercises.ex_z4; + +import java.util.Random; +import java.util.ArrayList; +import java.util.List; + +public class Bidder implements Runnable { + private static int nextId = 0; + private final String name; + private final int id; + private final List items = new ArrayList<>(); + private Double lastBid = null; + + public Bidder(String name) { + this.name = name; + this.id = nextId++; + } + + @Override + public void run() { + while(true){ + Random rand = new Random(); + double addon = rand.nextDouble(500); + lastBid += addon; + } + } + + public String getName() { + return name; + } + + public int getId() { + return id; + } + + public Double getLastBid() { + return lastBid; + } + + @Override + public String toString() { + return "(" + id + ") " + name; + } +} diff --git a/src/exercises/ex_z4/Item.java b/src/exercises/ex_z4/Item.java new file mode 100644 index 0000000..ccb22e5 --- /dev/null +++ b/src/exercises/ex_z4/Item.java @@ -0,0 +1,5 @@ +package exercises.ex_z4; + +public record Item(String name) { + +}