commit 2d13eab9c42796a7272fc37679d97736b651450f Author: MHD Amin Bkdaliya <3008953@stud.hs-mannheim.de> Date: Wed Jan 10 23:20:38 2024 +0100 Dateien nach "/" hochladen diff --git a/Family.java b/Family.java new file mode 100644 index 0000000..4602e1c --- /dev/null +++ b/Family.java @@ -0,0 +1,258 @@ +package Testate; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Random; +import java.util.TreeMap; + +public class Family { + private String name; + private int memberNum; + private int address; + private String planet; + private static int letter = 'A'; + private ArrayList kids = new ArrayList<>(); + private static TreeMap famTree = new TreeMap(); + + Random random = new Random(); + + public Family() { + int letter = incrementLetter(); + int zufall = random.nextInt(3); + this.name = (char) letter + "-Familie"; + + if (zufall == 0) { + this.planet = "Mars"; + for (int i = 0; i < (2 * (letter - 'A')) + 1; i++) { + kids.add(new Kind(generateKidName())); + } + + this.address = random.nextInt('Z' - 'A' + 1) + 'A'; + + } else if (zufall == 1) { + this.planet = "Merkur"; + for (int i = 0; i < (2 * (letter - 'A')) + 2; i++) { + kids.add(new Kind(generateKidName())); + } + char[] name = this.name.toCharArray(); + int adress = 0; + for (char c : name) { + adress += c; + } + this.address = adress; + } else { + this.planet = "Pluto"; + int anzahlMale = random.nextInt(3) + 2; + int anzahlFem = random.nextInt(4) + 3; + for (int i = 0; i < anzahlMale; i++) { + kids.add(generateKid("male")); + + } + for (int i = 0; i < anzahlFem; i++) { + kids.add(generateKid("female")); + + } + this.address = random.nextInt('z' - 100 + 1) + 100; + } + setMemberNum(); + famTree.put(letter - 'A' + 1, this);// fügt die aktuelle Instanz direkt bei der Erstellung in den St ein + } + + private int incrementLetter() { + return letter++; + } + + public String toString() { + return String.format("Name: %s\nPlanet: %s\nAnzahl der Familienmitglieder: %d\nAdresse: %d\n", name, planet, + memberNum, address); + } + + public int getAdress() { + return address; + } + + public void setAddress(int address) { + this.address = address; + } + + public void setMemberNum() { + this.memberNum = kids.size() + 2;// (+2) damit wir die Eltern berücksichtigen + } + + public void incrementMemberNum() { + this.memberNum += 1; + } + + public String getName() { + return name; + } + + public String generateKidName() { + String name = ""; + int firstLetter = random.nextInt(26) + 65; + + name += (char) firstLetter; + for (int i = 0; i < 3; i++) { + int otherLetter = random.nextInt(26) + 97; + name += (char) otherLetter; + } + return name; + } + + public Kind generateKid(String sex) { + Kind neuKid = new Kind(generateKidName(), sex); + return neuKid; + } + + public String adress2binaer(int adress) { + if (adress == 0) { + return "0"; + } + int remainder = adress % 2; + int quotient = adress / 2; + return adress2binaer(quotient) + remainder; + } + + public void introduceFam(String name) { + Family family = getFamByName(name); + System.out.printf("%s begrößt euch, unsere Familie besteht aus dem Vater, der Mutter und folgenden Kindern:\n", + family.getName()); + for (Kind kind : family.kids) { + System.out.println(kind.toString()); + } + } + + public void addKinderAllFam(int numKids) { + for (Family family : famTree.values()) { + String name = family.getName(); + for (int i = 0; i < numKids; i++) { + addKindToFam(name); + } + } + + } + + public boolean addKindToFam(String famName) { + Family family = getFamByName(famName); + if (family != null) { + family.kids.add(new Kind(generateKidName())); + family.incrementMemberNum(); + return true; + } + return false; + } + + public void showAllFam() { + for (Family family : famTree.values()) { + System.out.println(family.toString()); + } +// FamTree.values().stream().forEach(System.out::println); + } + + public Family getFamByName(String name) { + for (Family fam : famTree.values()) { + if (fam.getName().equals(name)) { + return fam; + } + } + return null; + } + + public void showFamByName(String name) { + Family family = getFamByName(name); + System.out.println(family.toString()); + } + + private static void showCommands() { + System.out.println("Liste möglicher Befehle:(Bitte wählen Sie eine Zahl)"); + Family.printSlow( + "1.Zeig alle Familien\n2.Zeig eine Familie\n3.Eine Familie vorstellen \n4.Füge ein Kind zu einer Familie\n5.Füge n-Kinder zu allen Familien\n6.Kodiere die Adresse in binäre\n7.Programm beenden"); + } + + public static void printSlow(String output) { + output += "\n"; + try { + for (char x : output.toCharArray()) { + + System.out.print(x); + Thread.sleep(3); + } + + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + public void run() { + Family.printSlow("Willkommen im All:"); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { + while (true) { + showCommands(); + System.out.print("Input:"); + int eingabe = Integer.parseInt(reader.readLine()); + System.out.println(""); + switch (eingabe) { + case 1: { + showAllFam(); + break; + } + case 2: { + Family.printSlow("Bitte geben Sie den Buchstaben der Familie ein:"); + String buchstabe = reader.readLine().toUpperCase(); + showFamByName(buchstabe + "-Familie"); + break; + } + case 3: { + Family.printSlow("Bitte geben Sie den Buchstaben der Familie ein:"); + String buchstabe = reader.readLine().toUpperCase(); + String Name = buchstabe + "-Familie"; + introduceFam(Name); + break; + + } + case 4: { + Family.printSlow("Bitte geben Sie den Buchstaben der Familie ein:"); + String buchstabe = reader.readLine().toUpperCase(); + String name = buchstabe + "-Familie"; + if (addKindToFam(name)) { + Family.printSlow("Ein Kind wurde erfolgreich hinzugefügt."); + System.out.println(getFamByName(name).toString()); + } else { + Family.printSlow("Das Kind konnte nicht hinzugefügt wurde"); + } + break; + + } + case 5: { + Family.printSlow("Bitte geben Sie die Anzahl den Kindern:"); + int numKids = Integer.parseInt(reader.readLine()); + addKinderAllFam(numKids); + Family.printSlow("Einfügen war erfolgreich"); + showAllFam(); + break; + } + case 6: { + Family.printSlow("Encryption mode activated\n"); + for (Family family : famTree.values()) { + int oldAdress = family.getAdress(); + + int binaer = Integer.parseInt(adress2binaer(oldAdress)); + family.setAddress(binaer); + } + + break; + } + case 7: { + System.exit(0); + } + default: + throw new IllegalArgumentException("Unexpected value: " + eingabe); + } + } + } catch (Exception e) { + System.out.println("Beachten Sie das Format"); + } + } + +} diff --git a/Kind.java b/Kind.java new file mode 100644 index 0000000..8f903c6 --- /dev/null +++ b/Kind.java @@ -0,0 +1,89 @@ +package Testate; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Random; + +public class Kind { + private String name; + private String id; + private int birthDate; + private int birthMonth; + private int birthDay; + private String sex; + private static ArrayList sexList = new ArrayList(Arrays.asList("male","female")); + private static Random random = new Random(); + + public Kind(String name, String sex) { + this.sex = sex; + this.name = name; + this.id = name2id(); + this.birthDate = random.nextInt(366) + 1; + + int [] earthDate = num2birth(getBirthDate()); + this.birthMonth = earthDate[0]; + this.birthDay = earthDate[1]; + + if (!sexList.contains(sex.toLowerCase())) { + sexList.add(sex); + } + } + public Kind(String name) { + this(name, sexList.get(random.nextInt(sexList.size()))); + } + public String getId() { + return id; + } + public void setId(String id) { + this.id = id; + } + public int getBirthDate() { + return birthDate; + } + public void setBirthDate(int birthDate) { + this.birthDate = birthDate; + } + public String getName() { + return name; + } + + public String getSex() { + return sex; + } + public int[] num2birth(int date) { + int[] monthDays = {0,31,28,31,30,31,30,31,31,30,31,30,31}; + + if (date == 366) { + monthDays[2] = 29; + } + int [] earthDate = new int[2]; + for (int i = 1; i <= 12; i++) { + if (date <= monthDays[i]) { + earthDate[0] = i; + earthDate[1] = date; + break; + } else { + date -= monthDays[i]; + } + } + return earthDate; + } + + private String name2id() { + char[] name = getName().toCharArray(); + String id = ""; + for (char c : name) { + String hexValue = Integer.toHexString(c); + id += hexValue; + } + return id; + } + public String toString() { + return String.format("Mein Name ist: %s, ID: %s, Sex: %s, Birth Date: %s, Birthday: %d, Birthmonth: %d\n", + name, id, sex, birthDate, birthDay, birthMonth); + } + + + + +} \ No newline at end of file diff --git a/testing.java b/testing.java new file mode 100644 index 0000000..dc7d9ad --- /dev/null +++ b/testing.java @@ -0,0 +1,12 @@ +package Testate; + + +public class testing { + public static void main(String[] args) { + Family family1 = new Family(); + Family family2 = new Family(); + Family family3 = new Family(); + family3.run(); + } + +} \ No newline at end of file