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"); } } }