diff --git a/commander/src/main/java/hse/java/commander/CommanderApplication.java b/commander/src/main/java/hse/java/commander/CommanderApplication.java index 471f6fd5..efe291c1 100644 --- a/commander/src/main/java/hse/java/commander/CommanderApplication.java +++ b/commander/src/main/java/hse/java/commander/CommanderApplication.java @@ -8,6 +8,8 @@ import java.io.IOException; import java.nio.file.Paths; + + public class CommanderApplication extends Application { @Override public void start(Stage stage) throws IOException { diff --git a/src/main/java/hse/java/Task.java b/src/main/java/hse/java/Task.java new file mode 100644 index 00000000..1a12f298 --- /dev/null +++ b/src/main/java/hse/java/Task.java @@ -0,0 +1,65 @@ +package hse.java; + +import javax.swing.plaf.nimbus.State; +import java.util.List; +import java.util.Optional; +import java.util.OptionalLong; +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.IntBinaryOperator; + +import static java.util.Collections.copy; +import static java.util.Collections.max; + +public class Task { + /** + * The operator combines all values in the given range into one value + * using combiner and initial value (seed) + */ + public static final BiFunction reduceIntOperator = new BiFunction() { + @Override + public IntBinaryOperator apply(Integer integer, IntBinaryOperator intBinaryOperator) { + return (start, end) -> { + Integer sum = integer; + for (int x = start; x <= end; x++) { + sum = intBinaryOperator.applyAsInt(x, sum); + } + return sum; + }; + } + }; + + /** + * The operator calculates the sum in the given range (inclusively) + */ + static IntBinaryOperator multiply = (x, y) -> x * y; + static IntBinaryOperator sum = (x, y) -> x + y; + public static final IntBinaryOperator sumOperator = reduceIntOperator.apply(0, sum); + + /** + * The operator calculates the product in the given range (inclusively) + */ + public static final IntBinaryOperator productOperator = reduceIntOperator.apply(1, multiply);// write your code here +} +class Transaction { + enum State {CANCELED;} + State state; + public Long getSum() {return new Long(0);} +} +class Account { + List transactions; +} + +class Task2 { + public static long calcSumOfCanceledTransOnNonEmptyAccounts(List accounts) { + OptionalLong vall = accounts.stream() + .filter((account) -> {return account.balance > 0;}) + .map((account) -> { + OptionalLong val = account.transactions.stream().filter((t) -> {return t.state == State.CANCELED;}) + .map((tr) -> {return tr.getSum();}).mapToLong(Number::longValue).reduce((g, h) -> {return g + h;}); + return val.isPresent() ? val.getAsLong() : 0; + }) + .reduce((g, h) -> {return g + h;}); + return vall.isPresent() ? vall.getAsLong() : 0; + } +} diff --git a/src/main/java/hse/java/lectures/lecture3/practice/randomSet/Debub.java b/src/main/java/hse/java/lectures/lecture3/practice/randomSet/Debub.java new file mode 100644 index 00000000..ed17fec7 --- /dev/null +++ b/src/main/java/hse/java/lectures/lecture3/practice/randomSet/Debub.java @@ -0,0 +1,20 @@ +package hse.java.lectures.lecture3.practice.randomSet; + +public class Debub { + public static void main(String[] args) { + RandomSet rs = new RandomSet(); + + rs.insert(10); + rs.insert(20); + + for (int i = 0; i < 20; i++) { + System.out.println(rs.getRandom()); + } + + rs.remove(10); + for (int i = 0; i < 20; i++) { + System.out.println(rs.getRandom()); + } + System.out.println(rs.remove(10)); + } +} diff --git a/src/main/java/hse/java/lectures/lecture3/tasks/html/HtmlDocument.java b/src/main/java/hse/java/lectures/lecture3/tasks/html/HtmlDocument.java index 1ddf27bf..43eb1e08 100644 --- a/src/main/java/hse/java/lectures/lecture3/tasks/html/HtmlDocument.java +++ b/src/main/java/hse/java/lectures/lecture3/tasks/html/HtmlDocument.java @@ -14,6 +14,13 @@ public HtmlDocument(String filePath) { this(Path.of(filePath)); } + static void foo() {} + + static void bar() { + foo(); + + } + public HtmlDocument(Path filePath) { String content = readFile(filePath); validate(content); @@ -27,6 +34,41 @@ private String readFile(Path filePath) { } } - private void validate(String content){} + private class Pair { + public T first; + public U second; + public Pair(T t, U u) { + first = t; + second = u; + } + } + + private Pair nextTag(String content, int current) { + content.s + } + + public class Tag {} + + enum OTag extends HtmlDocument.Tag { + html, + head, + body, + p, + div + } + enum CTag { + html, + head, + body, + p, + div + } + private void validate(String content){ + + + + + boolean was_ + } } 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..5e88fb24 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,18 +1,46 @@ package hse.java.lectures.lecture6.tasks.queue; -public class BoundedBlockingQueue { +import java.util.Queue; +public class BoundedBlockingQueue { + Queue queue; + private int capacity; + private int size = 0; + Object monitor; public BoundedBlockingQueue(int capacity) { - + this.capacity = capacity; } public void put(T item) { - + synchronized (monitor) { + if (size == capacity) { + try { + monitor.wait(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + + queue.add(item); + size++; + monitor.notifyAll(); + } } public T take() { - return null; + synchronized (monitor) { + if (size == 0) { + try { + monitor.wait(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + size--; + monitor.notifyAll(); + return queue.poll(); + } } public int size() { diff --git a/src/main/java/hse/java/practice/task1/Debug.java b/src/main/java/hse/java/practice/task1/Debug.java new file mode 100644 index 00000000..e69de29b