From 5b93be53831975a962cecb10eef2e9f2ff6c0084 Mon Sep 17 00:00:00 2001 From: Milanya Date: Sun, 15 Mar 2026 02:27:34 +0300 Subject: [PATCH 1/3] commander: first try --- .../hse/java/commander/MainController.java | 142 ++++++++++++++++-- 1 file changed, 129 insertions(+), 13 deletions(-) diff --git a/commander/src/main/java/hse/java/commander/MainController.java b/commander/src/main/java/hse/java/commander/MainController.java index 95d2eda9..87dfd197 100644 --- a/commander/src/main/java/hse/java/commander/MainController.java +++ b/commander/src/main/java/hse/java/commander/MainController.java @@ -3,19 +3,11 @@ import javafx.fxml.FXML; import javafx.scene.control.ListView; -public class MainController { +import java.lang.Throwable; +import java.nio.file.*; +import java.util.stream.Stream; - public void initialize() { - left.getItems().add("Kek"); - left.setOnMouseClicked(event -> { - if (event.getClickCount() == 2) { - int index = left.getSelectionModel().getSelectedIndex(); - if (index >= 0) { - left.getItems().set(index, "clicked"); - } - } - }); - } +public class MainController { @FXML public ListView left; @@ -23,5 +15,129 @@ public void initialize() { @FXML public ListView right; + private Path leftPath; + private Path rightPath; + private ListView Panel; + + public void init() { + leftPath = Paths.get(System.getProperty("user.dir")); + rightPath = Paths.get(System.getProperty("user.dir")); + + Panel = left; + dir(left, leftPath); + dir(right, rightPath); + + left.setOnMouseClicked(e -> { + Panel = left; + if (e.getClickCount() == 2) { + open(left); + } + }); + + right.setOnMouseClicked(e -> { + Panel = right; + if (e.getClickCount() == 2) { + open(right); + } + }); + } + + private void update() { + dir(left, leftPath); + dir(right, rightPath); + } + + private void dir(ListView panel, Path path) { + panel.getItems().clear(); + panel.getItems().add("..."); + try (Stream f = Files.list(path)) { + f.forEach(p -> panel.getItems().add(p.getFileName().toString())); + } catch (Throwable e) {} + } + + private void open(ListView panel) { + String name = panel.getSelectionModel().getSelectedItem(); + if (name == null) return; + + Path curr = panel == left ? leftPath : rightPath; + + if (name.equals("...")) { + Path par = curr.getParent(); + if (par != null) { + if (panel == left) { + leftPath = par; + dir(left, leftPath); + } else { + rightPath = par; + dir(right, rightPath); + } + } + return; + } + + Path touch = curr.resolve(name); + if (Files.isDirectory(touch)) { + if (panel == left) { + leftPath = touch; + dir(left, leftPath); + } else { + rightPath = touch; + dir(right, rightPath); + } + } + } + + @FXML + public void copy() { + ListView srcPanel = Panel; + + Path first_path = (Panel == left) ? leftPath : rightPath; + Path snd_path = (Panel == left) ? rightPath : leftPath; + + String name = srcPanel.getSelectionModel().getSelectedItem(); + if (name == null || name.equals("...")) return; + + Path src = first_path.resolve(name); + Path dst = snd_path.resolve(name); + + try { + Files.copy(src, dst, StandardCopyOption.REPLACE_EXISTING); + } catch (Throwable e) {} + + update(); + } + + @FXML + public void move() { + ListView srcPanel = Panel; + + Path first_path = (Panel == left) ? leftPath : rightPath; + Path snd_path = (Panel == left) ? rightPath : leftPath; + + String name = srcPanel.getSelectionModel().getSelectedItem(); + if (name == null || name.equals("...")) return; + + Path src = first_path.resolve(name); + Path dst = snd_path.resolve(name); + + try { + Files.move(src, dst, StandardCopyOption.REPLACE_EXISTING); + } catch (Throwable e) {} + update(); + } + + @FXML + public void delete() { + ListView panel = Panel; + Path path = (panel == left) ? leftPath : rightPath; + + String name = panel.getSelectionModel().getSelectedItem(); + if (name == null || name.equals("...")) return; -} + Path file = path.resolve(name); + try { + Files.deleteIfExists(file); + } catch (Throwable e) {} + update(); + } +} \ No newline at end of file From 5f1b5d3cb8d8ed8a77ae52d1093a5f03089c9cb8 Mon Sep 17 00:00:00 2001 From: Milanya Date: Tue, 24 Mar 2026 17:28:39 +0300 Subject: [PATCH 2/3] atm: first try --- .../java/lectures/lecture3/tasks/atm/Atm.java | 60 ++++++++++++++++++- 1 file changed, 57 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..e6e5b1c6 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 @@ -32,14 +32,68 @@ public static Denomination fromInt(int value) { public Atm() { } - public void deposit(Map banknotes){} + public void deposit(Map banknotes) { + if (banknotes == null || banknotes.isEmpty()) { + throw new InvalidDepositException("Deposit is empty"); + } + + for (Map.Entry entry : banknotes.entrySet()) { + if (entry.getKey() == null) { + throw new InvalidDepositException("Invalid denomination"); + } + if (entry.getValue() == null || entry.getValue() <= 0) { + throw new InvalidDepositException("Invalid banknote count: " + entry.getValue()); + } + } + + for (Map.Entry entry : banknotes.entrySet()) { + this.banknotes.merge(entry.getKey(), entry.getValue(), Integer::sum); + } + } public Map withdraw(int amount) { - return Map.of(); + if (amount <= 0) { + throw new InvalidAmountException("Amount must be positive"); + } + int balance = getBalance(); + if (amount > balance) { + throw new InsufficientFundsException("Not enough money in ATM"); + } + + Map temp = new EnumMap<>(banknotes); + Map res = new EnumMap<>(Denomination.class); + + List denoms = new ArrayList<>(Arrays.asList(Denomination.values())); + denoms.sort((a, b) -> Integer.compare(b.value(), a.value())); + + int ost = amount; + + for (Denomination denom : denoms) { + int able = temp.getOrDefault(denom, 0); + int need = ost / denom.value(); + int take = Math.min(able, need); + + if (take > 0) { + res.put(denom, take); + ost -= take * denom.value(); + } + } + if (ost != 0) { + throw new CannotDispenseException("Not able to process"); + } + for (Map.Entry entry : res.entrySet()) { + banknotes.merge(entry.getKey(), -entry.getValue(), Integer::sum); + } + + return res; } public int getBalance() { - return 0; + int sum = 0; + for (Map.Entry entry : banknotes.entrySet()) { + sum += entry.getKey().value() * entry.getValue(); + } + return sum; } } From 59ec3394cac4c94b5d13d03ff74003291148044c Mon Sep 17 00:00:00 2001 From: Milanya Date: Tue, 24 Mar 2026 18:18:53 +0300 Subject: [PATCH 3/3] html: first try --- .../lecture3/tasks/html/HtmlDocument.java | 104 +++++++++++++++++- 1 file changed, 101 insertions(+), 3 deletions(-) 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..cb841760 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 @@ -9,7 +9,6 @@ import java.util.Set; public class HtmlDocument { - public HtmlDocument(String filePath) { this(Path.of(filePath)); } @@ -27,6 +26,105 @@ private String readFile(Path filePath) { } } - private void validate(String content){} + private void validate(String content) { + Deque stack = new ArrayDeque<>(); + Set tags = Set.of("html", "head", "body", "div", "p"); + boolean opened = false; + boolean closed = false; + boolean head = false; + boolean body = false; + + int i = 0; + while (i < content.length()) { + if (content.charAt(i) == '<') { + int j = content.indexOf('>', i); + if (j == -1) { + throw new RuntimeException(); + } + + String tag = content.substring(i + 1, j).trim(); + + boolean closing_tag = tag.startsWith("/"); + String tag_name; + if (closing_tag) { + tag_name = tag.substring(1).trim().toLowerCase(); + } else { + String[] parts = tag.split("\\s+"); + tag_name = parts[0].toLowerCase(); + } + + if (!tags.contains(tag_name)) { + throw new UnsupportedTagException("Unsupported tag: " + tag_name); + } + + if (closing_tag) { + if (stack.isEmpty()) { + throw new UnexpectedClosingTagException("Unexpected closing tag: " + tag_name); + } + + String expected = stack.pop(); + if (!expected.equals(tag_name)) { + throw new MismatchedClosingTagException("Mismatched closing tag"); + } + + if (tag_name.equals("html")) { + closed = true; + } + + } else { + if (closed) { + throw new InvalidStructureException("Invalid structure"); + } -} + if (tag_name.equals("html")) { + if (opened) { + throw new InvalidStructureException("Invalid structure"); + } + if (!stack.isEmpty()) { + throw new InvalidStructureException("Invalid structure"); + } + opened = true; + } else { + if (!opened) { + throw new InvalidStructureException("Invalid structure"); + } + } + if (tag_name.equals("head") || tag_name.equals("body")) { + if (stack.isEmpty() || !stack.peek().equals("html")) { + throw new InvalidStructureException("Invalid structure"); + } + } + + if (tag_name.equals("head")) { + if (head) { + throw new InvalidStructureException("Invalid structure"); + } + if (body) { + throw new InvalidStructureException("Invalid structure"); + } + head = true; + } + + if (tag_name.equals("body")) { + if (body) { + throw new InvalidStructureException("Invalid structure"); + } + body = true; + } + stack.push(tag_name); + } + i = j + 1; + } else { + i++; + } + } + + if (!stack.isEmpty()) { + throw new UnclosedTagException("Unclosed tags"); + } + + if (!opened || !closed) { + throw new InvalidStructureException("Invalid structure"); + } + } +} \ No newline at end of file