HUE 03 @ POS201920
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
2.3 KiB

  1. import java.io.*;
  2. import java.time.LocalDate;
  3. import java.time.format.DateTimeFormatter;
  4. import java.time.temporal.ChronoUnit;
  5. import java.util.ArrayList;
  6. public class Birthdays {
  7. public static void main(String[] args) throws IOException {
  8. File file = new File("C:\\Users\\David\\TechnikNews Cloud\\1 Schule\\0 HTL 201920\\POSE\\HUE_03_BirthdaysFile\\geburtstage.txt");
  9. ArrayList<Geburtstag> geburtstage = parseData(file);
  10. System.out.println("Als nächstes Geburtstag hat: ");
  11. System.out.println(calculateNextBirtday(geburtstage));
  12. }
  13. public static ArrayList parseData(File file) throws IOException {
  14. BufferedReader br = new BufferedReader(new FileReader(file));
  15. String read;
  16. String content[];
  17. ArrayList<Geburtstag> geburtstage = new ArrayList<Geburtstag>();
  18. while ((read = br.readLine()) != null) {
  19. if (!read.startsWith("#")) {
  20. content = read.split(",");
  21. if (content.length == 4) {
  22. Geburtstag g = new Geburtstag(content[0], Integer.parseInt(content[1]), Integer.parseInt(content[2]), Integer.parseInt(content[3]));
  23. geburtstage.add(g);
  24. } else {
  25. System.out.println("Fehler, diese Zeile kann nicht geparst werden: zu viele oder zu wenig Daten (" + read + ")");
  26. }
  27. } else {
  28. // do nothing, don't care about this line
  29. }
  30. }
  31. return geburtstage;
  32. }
  33. public static String calculateNextBirtday(ArrayList<Geburtstag> geburtstage) {
  34. DateTimeFormatter germanDate = DateTimeFormatter.ofPattern("dd.MM.YYYY");
  35. LocalDate today = LocalDate.now();
  36. long diff = ChronoUnit.DAYS.between(today, geburtstage.get(0).naechsteGeburtstagsfeier());
  37. int pos = 0;
  38. for (int i = 0; i < geburtstage.size(); i++) {
  39. if ((ChronoUnit.DAYS.between(today, geburtstage.get(i).naechsteGeburtstagsfeier())) < diff) {
  40. diff = ChronoUnit.DAYS.between(today, geburtstage.get(i).naechsteGeburtstagsfeier());
  41. pos = i;
  42. }
  43. }
  44. String output = "Name: " + geburtstage.get(pos).name + " | nächster Geburtstag: " + geburtstage.get(pos).naechsteGeburtstagsfeier().format(germanDate);
  45. return output;
  46. }
  47. }