From 0a7c23b4892d64bd85bca219136aaed519b13d2a Mon Sep 17 00:00:00 2001 From: David Wurm Date: Tue, 22 Oct 2019 09:22:24 +0200 Subject: [PATCH] import files --- .idea/misc.xml | 6 +++++ .idea/modules.xml | 8 ++++++ HUE_03_BirthdaysFile.iml | 11 +++++++++ geburtstage.txt | 10 ++++++++ src/Birthdays.java | 53 ++++++++++++++++++++++++++++++++++++++++ src/Geburtstag.java | 49 +++++++++++++++++++++++++++++++++++++ 6 files changed, 137 insertions(+) create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 HUE_03_BirthdaysFile.iml create mode 100644 geburtstage.txt create mode 100644 src/Birthdays.java create mode 100644 src/Geburtstag.java diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..e0844bc --- /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..d339209 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/HUE_03_BirthdaysFile.iml b/HUE_03_BirthdaysFile.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/HUE_03_BirthdaysFile.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/geburtstage.txt b/geburtstage.txt new file mode 100644 index 0000000..c5cdf99 --- /dev/null +++ b/geburtstage.txt @@ -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 diff --git a/src/Birthdays.java b/src/Birthdays.java new file mode 100644 index 0000000..1489d96 --- /dev/null +++ b/src/Birthdays.java @@ -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 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 geburtstage = new ArrayList(); + 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 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; + } +} + diff --git a/src/Geburtstag.java b/src/Geburtstag.java new file mode 100644 index 0000000..0640edd --- /dev/null +++ b/src/Geburtstag.java @@ -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; + } +} + + +