@@ -0,0 +1,6 @@ | |||||
<?xml version="1.0" encoding="UTF-8"?> | |||||
<project version="4"> | |||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK"> | |||||
<output url="file://$PROJECT_DIR$/out" /> | |||||
</component> | |||||
</project> |
@@ -0,0 +1,8 @@ | |||||
<?xml version="1.0" encoding="UTF-8"?> | |||||
<project version="4"> | |||||
<component name="ProjectModuleManager"> | |||||
<modules> | |||||
<module fileurl="file://$PROJECT_DIR$/HUE_03_BirthdaysFile.iml" filepath="$PROJECT_DIR$/HUE_03_BirthdaysFile.iml" /> | |||||
</modules> | |||||
</component> | |||||
</project> |
@@ -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> |
@@ -0,0 +1,10 @@ | |||||
# Einige Namen und Geburtstage | |||||
Pete Townshend,1945,5,19 | |||||
Roger Daltrey,1944,3,1 | |||||
John Entwistle,1944,10,9 | |||||
Keith Moon,1946,8,23 | |||||
Syd Barrett,1946,1,6 | |||||
Roger Waters,1943,9,6 | |||||
Nick Mason,1944,1,27 | |||||
Richard Wright,1945,7,28 | |||||
# eof |
@@ -0,0 +1,53 @@ | |||||
import java.io.*; | |||||
import java.time.LocalDate; | |||||
import java.time.format.DateTimeFormatter; | |||||
import java.time.temporal.ChronoUnit; | |||||
import java.util.ArrayList; | |||||
public class Birthdays { | |||||
public static void main(String[] args) throws IOException { | |||||
File file = new File("C:\\Users\\David\\TechnikNews Cloud\\1 Schule\\0 HTL 201920\\POSE\\HUE_03_BirthdaysFile\\geburtstage.txt"); | |||||
ArrayList<Geburtstag> geburtstage = parseData(file); | |||||
System.out.println("Als nächstes Geburtstag hat: "); | |||||
System.out.println(calculateNextBirtday(geburtstage)); | |||||
} | |||||
public static ArrayList parseData(File file) throws IOException { | |||||
BufferedReader br = new BufferedReader(new FileReader(file)); | |||||
String read; | |||||
String content[]; | |||||
ArrayList<Geburtstag> geburtstage = new ArrayList<Geburtstag>(); | |||||
while ((read = br.readLine()) != null) { | |||||
if (!read.startsWith("#")) { | |||||
content = read.split(","); | |||||
if (content.length == 4) { | |||||
Geburtstag g = new Geburtstag(content[0], Integer.parseInt(content[1]), Integer.parseInt(content[2]), Integer.parseInt(content[3])); | |||||
geburtstage.add(g); | |||||
} else { | |||||
System.out.println("Fehler, diese Zeile kann nicht geparst werden: zu viele oder zu wenig Daten (" + read + ")"); | |||||
} | |||||
} else { | |||||
// do nothing, don't care about this line | |||||
} | |||||
} | |||||
return geburtstage; | |||||
} | |||||
public static String calculateNextBirtday(ArrayList<Geburtstag> geburtstage) { | |||||
DateTimeFormatter germanDate = DateTimeFormatter.ofPattern("dd.MM.YYYY"); | |||||
LocalDate today = LocalDate.now(); | |||||
long diff = ChronoUnit.DAYS.between(today, geburtstage.get(0).naechsteGeburtstagsfeier()); | |||||
int pos = 0; | |||||
for (int i = 0; i < geburtstage.size(); i++) { | |||||
if ((ChronoUnit.DAYS.between(today, geburtstage.get(i).naechsteGeburtstagsfeier())) < diff) { | |||||
diff = ChronoUnit.DAYS.between(today, geburtstage.get(i).naechsteGeburtstagsfeier()); | |||||
pos = i; | |||||
} | |||||
} | |||||
String output = "Name: " + geburtstage.get(pos).name + " | nächster Geburtstag: " + geburtstage.get(pos).naechsteGeburtstagsfeier().format(germanDate); | |||||
return output; | |||||
} | |||||
} | |||||
@@ -0,0 +1,49 @@ | |||||
import java.time.LocalDate; | |||||
import java.time.Period; | |||||
public class Geburtstag { | |||||
String name; | |||||
int jahr; | |||||
int monat; | |||||
int tag; | |||||
LocalDate datum; | |||||
public Geburtstag(LocalDate datum) { | |||||
this.datum = datum; | |||||
} | |||||
public Geburtstag(String name, int jahr, int monat, int tag) { | |||||
this.name = name; | |||||
this.datum = LocalDate.of(jahr, monat, tag); | |||||
} | |||||
public LocalDate naechsteGeburtstagsfeier() { | |||||
Geburtstag temp = new Geburtstag(this.datum); | |||||
LocalDate today = LocalDate.now(); | |||||
if (temp.datum.isAfter(today) || temp.datum.isEqual(today)) { | |||||
// do nothing, birthday wasn't already or is today | |||||
} else if (temp.datum.isBefore(today)) { | |||||
for (int i = 0; temp.datum.isBefore(today) && !temp.datum.isEqual(today); i++) { | |||||
temp.datum = temp.datum.plusYears(1); | |||||
} | |||||
} | |||||
return temp.datum; | |||||
} | |||||
public int alter() { | |||||
LocalDate today = LocalDate.now(); | |||||
int years = 0; | |||||
try { | |||||
years = Period.between(this.datum, today).getYears(); | |||||
if (this.datum.isEqual(today)) { | |||||
years++; | |||||
} | |||||
} catch (Exception NullPointerException) { | |||||
// date was invalid, do nothing | |||||
} | |||||
return years; | |||||
} | |||||
} | |||||