This commit is contained in:
Aelita4 2023-01-10 09:15:36 +01:00
commit 81c4779231
Signed by: Aelita4
GPG Key ID: C217320B9C5FD53B
13 changed files with 371 additions and 0 deletions

29
.gitignore vendored Normal file
View File

@ -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

6
.idea/misc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/kolos.iml" filepath="$PROJECT_DIR$/kolos.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

92
.idea/workspace.xml Normal file
View File

@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="922e9e1c-9810-4e1a-a7ac-7aa4905cea80" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/drzewa.txt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/kolos.iml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/Drzewo.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/DrzewoIglaste.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/DrzewoLisciaste.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/Las.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/Main.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/NieMoznaZrzucicException.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectId" id="2K7lzayfhJ29MbslP9RBouCeH8h" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true">
<ConfirmationsSetting value="2" id="Add" />
</component>
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true"
}
}]]></component>
<component name="RecentsManager">
<key name="CreateClassDialog.RecentsKey">
<recent name="" />
</key>
</component>
<component name="RunManager">
<configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="Main" />
<module name="kolos" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="Application.Main" />
</list>
</recent_temporary>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="922e9e1c-9810-4e1a-a7ac-7aa4905cea80" name="Changes" comment="" />
<created>1673333769196</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1673333769196</updated>
</task>
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</component>
</project>

3
drzewa.txt Normal file
View File

@ -0,0 +1,3 @@
iglaste;sosna;14
lisciaste;dab;123;15
grzyb;przemek;1234;5678

11
kolos.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

30
src/Drzewo.java Normal file
View File

@ -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;
}
}

14
src/DrzewoIglaste.java Normal file
View File

@ -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");
}
}

26
src/DrzewoLisciaste.java Normal file
View File

@ -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");
}
}

136
src/Las.java Normal file
View File

@ -0,0 +1,136 @@
import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Las {
List<Drzewo> 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<Drzewo> sorted = new ArrayList<Drzewo>(drzewa);
sorted.sort(new DrzewaComparator());
String str = "";
for (Drzewo drzewo : sorted) {
System.out.println(drzewo.toString());
str += drzewo.toString();
str += "\n";
}
System.out.print("Podaj nazwe pliku do zapisu: ");
Scanner scanner = new Scanner(System.in);
String nazwaPliku = scanner.next();
BufferedWriter w = new BufferedWriter(new FileWriter(nazwaPliku));
w.write(str);
w.close();
}
}
class DrzewaComparator implements Comparator<Drzewo> {
@Override
public int compare(Drzewo d1, Drzewo d2) {
if(d1.getWiek() == d2.getWiek()) return 0;
if(d1.getWiek() > d2.getWiek()) return 1;
return -1;
}
}

5
src/Main.java Normal file
View File

@ -0,0 +1,5 @@
public class Main {
public static void main(String[] args) {
Las las = new Las();
}
}

View File

@ -0,0 +1,5 @@
public class NieMoznaZrzucicException extends Exception {
public NieMoznaZrzucicException(String message) {
super(message);
}
}