Semsteraufgabe/Kind.java

89 lines
2.0 KiB
Java
Raw Normal View History

2024-01-10 23:20:38 +01:00
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);
}
}