From 9c06665ca25de0677fabca587fe901b7f9b4c559 Mon Sep 17 00:00:00 2001 From: Aelita4 Date: Tue, 10 Jan 2023 09:15:36 +0100 Subject: [PATCH] kolos --- .gitignore | 29 +++++++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ .idea/workspace.xml | 92 ++++++++++++++++++++ drzewa.txt | 3 + kolos.iml | 11 +++ src/Drzewo.java | 30 +++++++ src/DrzewoIglaste.java | 14 +++ src/DrzewoLisciaste.java | 26 ++++++ src/Las.java | 138 ++++++++++++++++++++++++++++++ src/Main.java | 5 ++ src/NieMoznaZrzucicException.java | 5 ++ 13 files changed, 373 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 drzewa.txt create mode 100644 kolos.iml create mode 100644 src/Drzewo.java create mode 100644 src/DrzewoIglaste.java create mode 100644 src/DrzewoLisciaste.java create mode 100644 src/Las.java create mode 100644 src/Main.java create mode 100644 src/NieMoznaZrzucicException.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..07115cd --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..0a07fc4 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..d44db51 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,92 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1673333769196 + + + + + + + \ No newline at end of file diff --git a/drzewa.txt b/drzewa.txt new file mode 100644 index 0000000..0e9735b --- /dev/null +++ b/drzewa.txt @@ -0,0 +1,3 @@ +iglaste;sosna;14 +lisciaste;dab;123;15 +grzyb;przemek;1234;5678 \ No newline at end of file diff --git a/kolos.iml b/kolos.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/kolos.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/Drzewo.java b/src/Drzewo.java new file mode 100644 index 0000000..8eb49a5 --- /dev/null +++ b/src/Drzewo.java @@ -0,0 +1,30 @@ +public abstract class Drzewo { + protected String gatunek; + protected int wiek; + + Drzewo(String gatunek, int wiek) { + this.gatunek = gatunek; + this.wiek = wiek; + } + + // metoda abstrakcyjna, bo to co ona robi zalezy od klasy dziedziczacej + // i nie ma potrzeby odgornie wypelniac metody + public abstract void zrzucLiscie() throws NieMoznaZrzucicException; + + @Override + public String toString() { + return this.gatunek + ";" + this.wiek; + } + + public int getWiek() { + return wiek; + } + + public void setWiek(int wiek) { + this.wiek = wiek; + } + + public String getGatunek() { + return gatunek; + } +} diff --git a/src/DrzewoIglaste.java b/src/DrzewoIglaste.java new file mode 100644 index 0000000..a8a8315 --- /dev/null +++ b/src/DrzewoIglaste.java @@ -0,0 +1,14 @@ +public class DrzewoIglaste extends Drzewo { + public DrzewoIglaste(String gatunek, int wiek) { + super(gatunek, wiek); + } + + @Override + public String toString() { + return "iglaste;" + super.toString(); + } + + public void zrzucLiscie() throws NieMoznaZrzucicException { + throw new NieMoznaZrzucicException("Nie mozna zrzucic lisci z iglaka"); + } +} diff --git a/src/DrzewoLisciaste.java b/src/DrzewoLisciaste.java new file mode 100644 index 0000000..e46a8a3 --- /dev/null +++ b/src/DrzewoLisciaste.java @@ -0,0 +1,26 @@ +public class DrzewoLisciaste extends Drzewo { + int wysokosc; + + public DrzewoLisciaste(String gatunek, int wiek, int wysokosc) { + super(gatunek, wiek); + this.wysokosc = wysokosc; + } + + @Override + public String toString() { + return "lisciaste;" + super.toString() + ";" + this.wysokosc; + } + + public int getWysokosc() { + return wysokosc; + } + + public void setWysokosc(int wysokosc) { + this.wysokosc = wysokosc; + } + + public void zrzucLiscie() { + System.out.println("zrzucam liscie"); + } + +} diff --git a/src/Las.java b/src/Las.java new file mode 100644 index 0000000..6d9a827 --- /dev/null +++ b/src/Las.java @@ -0,0 +1,138 @@ +import java.io.*; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Scanner; + +public class Las { + List drzewa; + + public Las() { + drzewa = new ArrayList<>(); + + drzewa.add(new DrzewoLisciaste("brzoza", 16, 23)); + drzewa.add(new DrzewoIglaste("modrzew", 10)); + + System.out.println("test"); + wczytaj("drzewa.txt"); + wyswietlWszystkie(); + System.out.println(); + zrzucWszystkieLiscie(); + System.out.println(); + + Drzewo tmp = drzewa.get(1); + tmp.setWiek(166); + + System.out.println("gatunek " + tmp.getGatunek()); + System.out.println("wiek " + tmp.getWiek()); + // jezeli klasa obiektu to DrzewoLisciaste, wyswietl dodatkowo wysokosc + if(tmp.getClass().getSimpleName() == "DrzewoLisciaste") { + DrzewoLisciaste a = (DrzewoLisciaste) tmp; + System.out.println("wysokosc " + a.getWysokosc()); + a.setWysokosc(666); + System.out.println("wysokosc " + a.getWysokosc()); + } + + Drzewo tmp2 = drzewa.get(0); + tmp2.setWiek(170); + + System.out.println("gatunek " + tmp2.getGatunek()); + System.out.println("wiek " + tmp2.getWiek()); + // jezeli klasa obiektu to DrzewoLisciaste, wyswietl dodatkowo wysokosc + if(tmp2.getClass().getSimpleName() == "DrzewoLisciaste") { + DrzewoLisciaste a = (DrzewoLisciaste) tmp2; + System.out.println("wysokosc " + a.getWysokosc()); + a.setWysokosc(666); + System.out.println("wysokosc " + a.getWysokosc()); + } + + System.out.println(); + wyswietlWszystkie(); + System.out.println(); + + try { + wyswietlPosortowaneIZapisz(); + } catch(IOException e) { + System.out.println("Nie mozna zapisac pliku: "); + e.printStackTrace(); + } + } + + public void wyswietlWszystkie() { + for (Drzewo drzewo : drzewa) { + System.out.println(drzewo.toString()); + } + } + + public void zrzucWszystkieLiscie() { + for (Drzewo drzewo : drzewa) { + try { + drzewo.zrzucLiscie(); + } catch(NieMoznaZrzucicException e) { + System.out.println("nie mozna zrzucic lisci"); + } + } + } + + public void wczytaj(String path) { + try { + File plik = new File(path); + Scanner scanner = new Scanner(plik); + + while(scanner.hasNextLine()) { + String line = scanner.nextLine(); + String[] elements = line.split(";"); + // 0 typ; 1 gatunek; 2 wiek; 3 wysokosc + Drzewo temp; + switch(elements[0]) { + case "iglaste": + temp = new DrzewoIglaste(elements[1], Integer.parseInt(elements[2])); + break; + case "lisciaste": + temp = new DrzewoLisciaste(elements[1], Integer.parseInt(elements[2]), Integer.parseInt(elements[3])); + break; + default: // domyslnie zakladamy ze to iglak (mniej pol danych) + // poniewaz Drzewo jest abstrakcyjne, + // nie mozna utworzyc obiektu klasy Drzewo + temp = new DrzewoIglaste("nieznany", 0); + break; + } + drzewa.add(temp); + } + scanner.close(); + } catch (FileNotFoundException e) { + System.out.println("Nie znaleziono pliku"); + } + } + + public void wyswietlPosortowaneIZapisz() throws IOException { + List sorted = new ArrayList(drzewa); + sorted.sort(new DrzewaComparator()); + + for (Drzewo drzewo : sorted) { + System.out.println(drzewo.toString()); + } + + System.out.print("Podaj nazwe pliku do zapisu: "); + Scanner scanner = new Scanner(System.in); + String nazwaPliku = scanner.next(); + + String str = ""; + for (Drzewo drzewo : drzewa) { + str += drzewo.toString(); + str += "\n"; + } + BufferedWriter w = new BufferedWriter(new FileWriter(nazwaPliku)); + w.write(str); + w.close(); + } +} + +class DrzewaComparator implements Comparator { + @Override + public int compare(Drzewo d1, Drzewo d2) { + if(d1.getWiek() == d2.getWiek()) return 0; + if(d1.getWiek() > d2.getWiek()) return 1; + return -1; + } +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..0222bec --- /dev/null +++ b/src/Main.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args) { + Las las = new Las(); + } +} \ No newline at end of file diff --git a/src/NieMoznaZrzucicException.java b/src/NieMoznaZrzucicException.java new file mode 100644 index 0000000..0bb5ce8 --- /dev/null +++ b/src/NieMoznaZrzucicException.java @@ -0,0 +1,5 @@ +public class NieMoznaZrzucicException extends Exception { + public NieMoznaZrzucicException(String message) { + super(message); + } +}