From e470ba64825be8d206514163d038d2796be7579b Mon Sep 17 00:00:00 2001 From: gle6a6y Date: Sat, 7 Feb 2026 14:07:46 +0300 Subject: [PATCH 1/3] bla bla bla --- .../java/lectures/lecture3/tasks/atm/Atm.java | 65 ++++++++++++++++++- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/src/main/java/hse/java/lectures/lecture3/tasks/atm/Atm.java b/src/main/java/hse/java/lectures/lecture3/tasks/atm/Atm.java index 08f551e4..2ccc1c72 100644 --- a/src/main/java/hse/java/lectures/lecture3/tasks/atm/Atm.java +++ b/src/main/java/hse/java/lectures/lecture3/tasks/atm/Atm.java @@ -16,6 +16,15 @@ public enum Denomination { this.value = value; } + static Denomination fromInt(int v) throws InvalidDepositException{ + for (Denomination d : values()) { + if (d.value == v) { + return d; + } + } + throw new InvalidDepositException("Invalid nominal value"); + } + int value() { return value; } @@ -30,16 +39,66 @@ public static Denomination fromInt(int value) { private final Map banknotes = new EnumMap<>(Denomination.class); public Atm() { + for(Denomination val : banknotes.keySet()) { + banknotes.put(val, 0); + } } - public void deposit(Map banknotes){} + public void deposit(Map banknotes_) { + try { + if (banknotes_.isEmpty()) { + throw new InvalidDepositException("There is no money"); + } + for (Map.Entry entry : banknotes_.entrySet()) { + Denomination val = Denomination.fromInt(entry.getKey()); + int oldValue = banknotes.get(val); + int amountOfNominal = entry.getValue(); + if (amountOfNominal < 0) { + throw new InvalidDepositException("Negative amount of " + entry.getKey().toString()); + } + int newValue = oldValue + amountOfNominal; + banknotes.replace(val, oldValue, newValue); + } + } + catch (InvalidDepositException e) { + System.err.println(e.getMessage()); + } + } public Map withdraw(int amount) { - return Map.of(); + Map mapka = new EnumMap<>(Denomination.class); + try { + if (amount <= 0) { + throw new InvalidAmountException("Amount is non positive"); + } + if (amount > getBalance()) { + throw new InsufficientFundsException("Amount > atm balance"); + } + for(int val : new int[]{5000, 1000, 500, 100, 50}) { + while (amount >= val) { + int oldValue = mapka.get(Denomination.fromInt(val)); + mapka.replace(Denomination.fromInt(val), oldValue, oldValue + 1); + amount -= val; + } + } + if (amount != 0) { + throw new CannotDispenseException("Can`t dicpense"); + } + return mapka; + } catch (InvalidAmountException e) { + System.err.println(e.getMessage()); + } catch (InsufficientFundsException e) { + System.err.println(e.getMessage()); + } + return mapka; } public int getBalance() { - return 0; + int sum = 0; + for(Map.Entry entry : banknotes.entrySet()) { + sum += entry.getKey().value() * entry.getValue(); + } + return sum; } } From cfaaa596a040197d45ec2d838cf97e4999f60ddd Mon Sep 17 00:00:00 2001 From: gle6a6y Date: Sat, 14 Mar 2026 13:18:48 +0300 Subject: [PATCH 2/3] queue: first try --- .../tasks/queue/BoundedBlockingQueue.java | 48 ++++++++++++++++--- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/src/main/java/hse/java/lectures/lecture6/tasks/queue/BoundedBlockingQueue.java b/src/main/java/hse/java/lectures/lecture6/tasks/queue/BoundedBlockingQueue.java index 5c686cff..844d2dee 100644 --- a/src/main/java/hse/java/lectures/lecture6/tasks/queue/BoundedBlockingQueue.java +++ b/src/main/java/hse/java/lectures/lecture6/tasks/queue/BoundedBlockingQueue.java @@ -1,25 +1,59 @@ package hse.java.lectures.lecture6.tasks.queue; +import java.util.LinkedList; + public class BoundedBlockingQueue { + final Object monitor; + int capacity; + LinkedList queue; + int curr_len; - public BoundedBlockingQueue(int capacity) { + public BoundedBlockingQueue(int capacity_) throws IllegalArgumentException { - } + if (capacity_ <= 0) { + throw new IllegalArgumentException(); + } - public void put(T item) { + monitor = new Object(); + capacity = capacity_; + queue = new LinkedList<>(); + curr_len = 0; + } + public void put(T item) throws InterruptedException { + if (item == null) { + throw new NullPointerException(); + } + synchronized (monitor) { + while (curr_len >= capacity) { + monitor.wait(); + } + queue.offer(item); + curr_len++; + monitor.notifyAll(); + } } - public T take() { - return null; + public T take() throws InterruptedException { + synchronized (monitor) { + while (curr_len <= 0) { + monitor.wait(); + } + curr_len--; + T item = queue.poll(); + monitor.notifyAll(); + return item; + } } public int size() { - return 0; + synchronized (monitor) { + return curr_len; + } } public int capacity() { - return 0; + return capacity; } } From 8b5f6460c9a2b523f3cce572ff7e53255eb6ed14 Mon Sep 17 00:00:00 2001 From: gle6a6y Date: Sat, 14 Mar 2026 13:50:29 +0300 Subject: [PATCH 3/3] queue: fix --- .../java/lectures/lecture3/tasks/atm/Atm.java | 65 +------------------ 1 file changed, 3 insertions(+), 62 deletions(-) diff --git a/src/main/java/hse/java/lectures/lecture3/tasks/atm/Atm.java b/src/main/java/hse/java/lectures/lecture3/tasks/atm/Atm.java index 2ccc1c72..08f551e4 100644 --- a/src/main/java/hse/java/lectures/lecture3/tasks/atm/Atm.java +++ b/src/main/java/hse/java/lectures/lecture3/tasks/atm/Atm.java @@ -16,15 +16,6 @@ public enum Denomination { this.value = value; } - static Denomination fromInt(int v) throws InvalidDepositException{ - for (Denomination d : values()) { - if (d.value == v) { - return d; - } - } - throw new InvalidDepositException("Invalid nominal value"); - } - int value() { return value; } @@ -39,66 +30,16 @@ public static Denomination fromInt(int value) { private final Map banknotes = new EnumMap<>(Denomination.class); public Atm() { - for(Denomination val : banknotes.keySet()) { - banknotes.put(val, 0); - } } - public void deposit(Map banknotes_) { - try { - if (banknotes_.isEmpty()) { - throw new InvalidDepositException("There is no money"); - } - for (Map.Entry entry : banknotes_.entrySet()) { - Denomination val = Denomination.fromInt(entry.getKey()); - int oldValue = banknotes.get(val); - int amountOfNominal = entry.getValue(); - if (amountOfNominal < 0) { - throw new InvalidDepositException("Negative amount of " + entry.getKey().toString()); - } - int newValue = oldValue + amountOfNominal; - banknotes.replace(val, oldValue, newValue); - } - } - catch (InvalidDepositException e) { - System.err.println(e.getMessage()); - } - } + public void deposit(Map banknotes){} public Map withdraw(int amount) { - Map mapka = new EnumMap<>(Denomination.class); - try { - if (amount <= 0) { - throw new InvalidAmountException("Amount is non positive"); - } - if (amount > getBalance()) { - throw new InsufficientFundsException("Amount > atm balance"); - } - for(int val : new int[]{5000, 1000, 500, 100, 50}) { - while (amount >= val) { - int oldValue = mapka.get(Denomination.fromInt(val)); - mapka.replace(Denomination.fromInt(val), oldValue, oldValue + 1); - amount -= val; - } - } - if (amount != 0) { - throw new CannotDispenseException("Can`t dicpense"); - } - return mapka; - } catch (InvalidAmountException e) { - System.err.println(e.getMessage()); - } catch (InsufficientFundsException e) { - System.err.println(e.getMessage()); - } - return mapka; + return Map.of(); } public int getBalance() { - int sum = 0; - for(Map.Entry entry : banknotes.entrySet()) { - sum += entry.getKey().value() * entry.getValue(); - } - return sum; + return 0; } }