From 0cfcce1869d26576715c532ca72df9da4ac5004b Mon Sep 17 00:00:00 2001 From: DragoonXVIII <99823997+DragoonXVIII@users.noreply.github.com> Date: Tue, 21 Oct 2025 11:05:21 +0200 Subject: [PATCH] 2nd initial commit xD --- .gitignore | 38 ++++++++ .idea/.gitignore | 3 + .idea/encodings.xml | 7 ++ .idea/misc.xml | 14 +++ .idea/vcs.xml | 6 ++ pom.xml | 17 ++++ src/main/java/org/example/Main.java | 145 ++++++++++++++++++++++++++++ 7 files changed, 230 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 pom.xml create mode 100644 src/main/java/org/example/Main.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..001e756 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..4fce1d8 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..e094e33 --- /dev/null +++ b/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + org.example + lab1 + 1.0-SNAPSHOT + + + 23 + 23 + UTF-8 + + + \ No newline at end of file diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java new file mode 100644 index 0000000..1f96828 --- /dev/null +++ b/src/main/java/org/example/Main.java @@ -0,0 +1,145 @@ +package org.example; + +import java.io.*; +import java.util.*; +import java.util.stream.*; + +public class Main { + + public static void zad1() { + List przedmioty = Arrays.asList( + "Procesy wytwarzania oprogramowania", + "Seminarium dyplomowe", + "Architektura i programowanie w .NET", + "Projekt zespołowy - implementacja", + "Projekt zespołowy - projektowanie", + "Zarządzanie bazami SQL i NoSQL", + "Integracja systemów", + "Interakcja człowiek - komputer", + "Programowanie aplikacji mobilnych na platformę Android", + "Programowanie aplikacji mobilnych na platformę iOS", + "Programowanie aplikacji w chmurze obliczeniowej", + "Komponentowe podejscie do wytwarzania aplikacji", + "Zaawansowane programowanie w Javie", + "Szkielety programistyczne w aplikacjach internetowych" + ); + + // filtr + boolean zawieraZaaw = przedmioty.stream() + .anyMatch(p -> p.toLowerCase().contains("zaaw")); // lub filter bez znaczenia + System.out.println("Czy w liście są przedmioty z 'zaaw'? " + (zawieraZaaw ? "Tak" : "Nie")); + + // nowy strumie + Map przedmiotOcena = przedmioty.stream() + .collect(Collectors.toMap(p -> p, p -> new Random().nextInt(4) + 2)); // oceny 2–5 + + System.out.println("\nLista przedmiotów i ocen:"); + przedmiotOcena.forEach((p, o) -> System.out.println(p + " -> " + o)); + + // powtorzenia check + Map wystapienia = przedmiotOcena.values().stream() + .collect(Collectors.groupingBy(o -> o, Collectors.counting())); + + System.out.println("\nPowtarzające się oceny:"); + wystapienia.forEach((ocena, liczba) -> { + if (liczba > 1) + System.out.println("Ocena " + ocena + " wystąpiła " + liczba + " razy"); + }); + } + + public static void zad2() { + Map przedmiotOcena = wczytajZPliku("oceny.txt"); + if (przedmiotOcena.isEmpty()) { + System.out.println("Brak danych w pliku."); + return; + } + + List> sortOceny = przedmiotOcena.entrySet().stream() + .sorted(Map.Entry.comparingByValue()) + .collect(Collectors.toList()); + + // Zliczenie częstości występowania ocen + Map freq = przedmiotOcena.values().stream() + .collect(Collectors.groupingBy(o -> o, Collectors.counting())); + + // Sortowanie po częstości występowania (rosnąco) + List> sortPowtarzalnosc = freq.entrySet().stream() + .sorted(Map.Entry.comparingByValue()) + .collect(Collectors.toList()); + + try (FileWriter writer = new FileWriter("oceny_posortowane.txt")) { + writer.write("=== Przedmioty z najniższymi ocenami (rosnąco) ===\n"); + for (Map.Entry e : sortOceny) { + writer.write(e.getKey() + " -> " + e.getValue() + "\n"); + } + writer.write("\n=== Oceny najrzadziej występujące (rosnąco) ===\n"); + for (Map.Entry e : sortPowtarzalnosc) { + writer.write("Ocena " + e.getKey() + " wystąpiła " + e.getValue() + " razy\n"); + } + } catch (IOException e) { + System.err.println("Błąd zapisu: " + e.getMessage()); + } + + System.out.println("Dane zapisano do pliku: oceny_posortowane.txt"); + } + + // === Zadanie 1.3: Odczyt i obliczenia na danych z pliku === + public static void zad3() { + Map przedmiotOcena = wczytajZPliku("oceny.txt"); + if (przedmiotOcena.isEmpty()) { + System.out.println("Brak danych w pliku."); + return; + } + + double srednia = przedmiotOcena.values().stream() + .mapToInt(Integer::intValue) + .average() + .orElse(0.0); + + Map.Entry max = przedmiotOcena.entrySet().stream() + .max(Map.Entry.comparingByValue()) + .orElse(null); + + Map.Entry min = przedmiotOcena.entrySet().stream() + .min(Map.Entry.comparingByValue()) + .orElse(null); + + System.out.println("Średnia ocen: " + srednia); + if (max != null) System.out.println("Najlepszy przedmiot: " + max.getKey() + " (" + max.getValue() + ")"); + if (min != null) System.out.println("Najgorszy przedmiot: " + min.getKey() + " (" + min.getValue() + ")"); + } + + // === Funkcje pomocnicze === + private static void zapiszDoPliku(Map map, String filename) { + try (FileWriter writer = new FileWriter(filename)) { + for (Map.Entry e : map.entrySet()) { + writer.write(e.getKey() + ";" + e.getValue() + "\n"); + } + } catch (IOException e) { + System.err.println("Błąd zapisu do pliku: " + e.getMessage()); + } + } + + private static Map wczytajZPliku(String filename) { + Map map = new LinkedHashMap<>(); + try (BufferedReader reader = new BufferedReader(new FileReader(filename))) { + String linia; + while ((linia = reader.readLine()) != null) { + if (!linia.contains(";")) continue; + String[] parts = linia.split(";"); + if (parts.length == 2) { + map.put(parts[0], Integer.parseInt(parts[1])); + } + } + } catch (IOException e) { + System.err.println("Błąd odczytu pliku: " + e.getMessage()); + } + return map; + } + + public static void main(String[] args) { + //zad1(); + zad2(); + //zad3(); + } +}