Dateien nach "/" hochladen

main
MHD Amin Bkdaliya 2024-01-10 23:20:38 +01:00
commit 2d13eab9c4
3 changed files with 359 additions and 0 deletions

258
Family.java 100644
View File

@ -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<Kind> kids = new ArrayList<>();
private static TreeMap<Integer, Family> famTree = new TreeMap<Integer, Family>();
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");
}
}
}

89
Kind.java 100644
View File

@ -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<String> sexList = new ArrayList<String>(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);
}
}

12
testing.java 100644
View File

@ -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();
}
}