commit 21f62c4e8fa874103d5198fdefe06dfefa8ca83d Author: Lukas Martin Date: Wed Sep 4 19:31:13 2024 +0200 [LM] new git. Signed-off-by: Lukas Martin diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..919ce1f --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/material_theme_project_new.xml b/.idea/material_theme_project_new.xml new file mode 100644 index 0000000..647b14c --- /dev/null +++ b/.idea/material_theme_project_new.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..d583332 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ 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/pom.xml b/pom.xml new file mode 100644 index 0000000..68cfeea --- /dev/null +++ b/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + de.kaabp + wichteln + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + com.sun.mail + javax.mail + 1.6.2 + + + + \ No newline at end of file diff --git a/src/main/java/de/kaabp/Main.java b/src/main/java/de/kaabp/Main.java new file mode 100644 index 0000000..c0fa254 --- /dev/null +++ b/src/main/java/de/kaabp/Main.java @@ -0,0 +1,23 @@ +package de.kaabp; + +import de.kaabp.util.Person; +import de.kaabp.parser.ReadCsv; +import de.kaabp.util.Matching; +import de.kaabp.parser.WriteCsv; +import de.kaabp.mail.sendMail; + +import java.io.File; +import java.util.*; + +public class Main { + public static void main(String[] args) { + ReadCsv readCsv = new ReadCsv(); + List personList = readCsv.csvToList("src/main/resources/entries.csv"); + List matchedPeople = new ArrayList<>(); + Person[] matches = new Person[personList.size()]; + + Matching.matchPeople(personList, matchedPeople, matches); + WriteCsv.updateLastMatch(new File("src/main/resources/entries.csv"),matches, readCsv.csvToList("src/main/resources/entries.csv")); + sendMail.sendMail(readCsv.csvToList("src/main/resources/entries.csv")); + } +} \ No newline at end of file diff --git a/src/main/java/de/kaabp/mail/sendMail.java b/src/main/java/de/kaabp/mail/sendMail.java new file mode 100644 index 0000000..a4b6d82 --- /dev/null +++ b/src/main/java/de/kaabp/mail/sendMail.java @@ -0,0 +1,64 @@ +package de.kaabp.mail; + +import de.kaabp.util.Person; + +import javax.mail.*; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; +import java.util.List; +import java.util.Properties; + +public class sendMail { + + + /** + * @author Lukas Martin + * @param {@code String[][]} + * @param {@code String[]} + * @info Uses the params to send mails to the chosen people. + */ + public static void sendMail(List personList) { + //authentication info + final String username = "weihnachtswichteln0@gmail.com"; + final String password = "fmrvjhogruskvjkj\n"; + String fromEmail = "weihnachtswichteln0@gmail.com"; + String toEmail = "lukas.martin155@outlook.de"; + + Properties properties = new Properties(); + properties.put("mail.smtp.auth", "true"); + properties.put("mail.smtp.starttls.enable", "true"); + properties.put("mail.smtp.host", "smtp.gmail.com"); + properties.put("mail.smtp.port", "587"); + properties.put("mail.smtp.ssl.trust", "smtp.gmail.com"); + + Session session = Session.getInstance(properties, new javax.mail.Authenticator() { + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + //Start our mail message + for(int i = 0; i < personList.size(); i++) { + MimeMessage msg = new MimeMessage(session); + try { + msg.setFrom(new InternetAddress(fromEmail)); + msg.addRecipient(Message.RecipientType.TO, new InternetAddress(personList.get(i).getEmail())); + msg.setSubject("Weihnachtswichteln 2023"); + msg.setText("Hallo liebe/r " + personList.get(i).getName()+ "," + "\n" + + "\n" + + "Dein Match für das diesjährige Wichteln ist " + personList.get(personList.get(i).getLastMatch()).getName() + "." + "\n" + + "\n" + + "Mit freundlichen Grüßen \n" + + "Dein Wichtelsystem" + "\n" + + "\n" + "\n" + + + "--Bitte beachtet, dass ungefähr für 25€ gewichtelt wird--"); + Transport.send(msg); + } catch (MessagingException e) { + e.printStackTrace(); + } + } + + + + } +} \ No newline at end of file diff --git a/src/main/java/de/kaabp/parser/ReadCsv.java b/src/main/java/de/kaabp/parser/ReadCsv.java new file mode 100644 index 0000000..9a9a817 --- /dev/null +++ b/src/main/java/de/kaabp/parser/ReadCsv.java @@ -0,0 +1,49 @@ +package de.kaabp.parser; + + +import de.kaabp.util.Person; +import de.kaabp.util.personCreate; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class ReadCsv { + + public List csvToList(String csv) { + List persons = new ArrayList<>(); + try (BufferedReader br = new BufferedReader(new FileReader(csv))) { + + // read the first line from the text file + String line = br.readLine(); + + // loop until all lines are read + while (line != null) { + + // use string.split to load a string array with the values from + // each line of + // the file, using a comma as the delimiter + String[] attributes = line.split(","); + + Person person = personCreate.createPerson(attributes); + + // adding book into ArrayList + persons.add(person); + + // read next line before looping + // if end of file reached, line would be null + line = br.readLine(); + } + + } catch (IOException ioe) { + ioe.printStackTrace(); + } + + return persons; + } + + +} + diff --git a/src/main/java/de/kaabp/parser/ReadIni.java b/src/main/java/de/kaabp/parser/ReadIni.java new file mode 100644 index 0000000..02878ed --- /dev/null +++ b/src/main/java/de/kaabp/parser/ReadIni.java @@ -0,0 +1,4 @@ +package de.kaabp.parser; + +public class ReadIni { +} diff --git a/src/main/java/de/kaabp/parser/WriteCsv.java b/src/main/java/de/kaabp/parser/WriteCsv.java new file mode 100644 index 0000000..d8a4860 --- /dev/null +++ b/src/main/java/de/kaabp/parser/WriteCsv.java @@ -0,0 +1,39 @@ +package de.kaabp.parser; + +import de.kaabp.util.Person; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.List; + +public class WriteCsv { + + public static void updateLastMatch(File file, Person[] matches, List personList) { + if (matches.length == personList.size()) { + for (int i = 0; i < matches.length; i++) { + personList.get(i).setLastMatch(matches[i].getID()); + } + } + writePersonListToCSV(file.toString(), personList); + } + + + private static void writePersonListToCSV(String csvFileName, List personList) { + try (FileWriter writer = new FileWriter(csvFileName)) { + for (Person person : personList) { + //System.out.println(person); + // Create a CSV row for each Person object + String csvRow = String.format("%d,%s,%s,%s,%d%n", + person.getID(), person.getName(), person.getEmail(), person.getGroups(), person.getLastMatch()); + + writer.write(csvRow); + } + + System.out.println("Data has been written to " + csvFileName); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/src/main/java/de/kaabp/util/Matching.java b/src/main/java/de/kaabp/util/Matching.java new file mode 100644 index 0000000..0c9dc91 --- /dev/null +++ b/src/main/java/de/kaabp/util/Matching.java @@ -0,0 +1,49 @@ +package de.kaabp.util; + +import java.util.*; + +public class Matching { + + public static Person[] matchPeople(List personList, List matchedPeople, Person[] matches) { + List cpPersonalList = new ArrayList<>(personList); + Collections.shuffle(cpPersonalList); + Collections.shuffle(personList); + + while (!personList.isEmpty()) { + Person p = personList.get(0); + boolean foundMatch = false; + + for (Person m : cpPersonalList) { + if (isMatchValid(p, m, matchedPeople)) { + matchedPeople.add(m); + matches[p.getID()] = m; + personList.remove(p); // Remove the matched person from the list + System.out.println(p.getName() + "->" + m.getName()); + foundMatch = true; + break; + } + } + + if (!foundMatch) { + System.out.println("Error: No valid match found for " + p.getName()); + Arrays.fill(matches, null); + matchedPeople.clear(); + personList = new ArrayList<>(cpPersonalList); + Collections.shuffle(personList); + } + + } + return matches; + } + + + + + + private static boolean isMatchValid(Person p1, Person p2, List matchedPeople) { + return p1.getID() != p2.getID() && !Objects.equals(p1.getGroups(), p2.getGroups()) && !matchedPeople.contains(p2); + } + + + +} diff --git a/src/main/java/de/kaabp/util/Person.java b/src/main/java/de/kaabp/util/Person.java new file mode 100644 index 0000000..ada248d --- /dev/null +++ b/src/main/java/de/kaabp/util/Person.java @@ -0,0 +1,68 @@ +package de.kaabp.util; + +public class Person { + private int ID; + private String name; + private String email; + private String groups; + private int lastMatch; + + public Person(int ID, String name, String email, String groups, int lastMatch) { + this.ID = ID; + this.name = name; + this.email = email; + this.groups = groups; + this.lastMatch = lastMatch; + } + + public int getID() { + return ID; + } + + public void setID(int ID) { + this.ID = ID; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getGroups() { + return groups; + } + + public void setGroups(String groups) { + this.groups = groups; + } + + public int getLastMatch() { + return lastMatch; + } + + public void setLastMatch(int lastMatch) { + this.lastMatch = lastMatch; + } + + @Override + public String toString() { + return "Person{" + + "ID=" + ID + + ", name='" + name + '\'' + + ", email='" + email + '\'' + + ", groups=" + groups + + ", lastMatch=" + lastMatch + + '}'; + } +} diff --git a/src/main/java/de/kaabp/util/personCreate.java b/src/main/java/de/kaabp/util/personCreate.java new file mode 100644 index 0000000..6b09181 --- /dev/null +++ b/src/main/java/de/kaabp/util/personCreate.java @@ -0,0 +1,15 @@ +package de.kaabp.util; + +public class personCreate { + + public static Person createPerson(String[] metadata) { + int ID = Integer.parseInt(metadata[0]); + String name = metadata[1]; + String email = metadata[2]; + String groups = metadata[3]; + int lastMatch = Integer.parseInt(metadata[4]); + + return new Person(ID, name, email, groups, lastMatch); + } + +} diff --git a/src/main/resources/entries.csv b/src/main/resources/entries.csv new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/settings.ini b/src/main/resources/settings.ini new file mode 100644 index 0000000..e69de29