Compare commits
2 Commits
e26eab2f97
...
4343517646
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4343517646 | ||
|
|
3bcd1a1e9a |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
build/
|
||||
*class
|
||||
.vscode
|
||||
|
||||
26
Main.java
26
Main.java
@ -1,5 +1,29 @@
|
||||
import src.Person;
|
||||
import src.Country;
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello, World!");
|
||||
Person p = new Person();
|
||||
|
||||
p.setFirstName("John");
|
||||
p.setLastName("Doe");
|
||||
System.out.println("Full Name: " + p.getFullName());
|
||||
|
||||
p.updateBirthday("15.04.2000");
|
||||
System.out.println("Birthday: " + p.getBirthdayString());
|
||||
System.out.println("Age: " + p.getAge());
|
||||
System.out.println("Is Adult (USA): " + p.isAdult(Country.US));
|
||||
System.out.println("Is Minor (USA): " + p.isMinor(Country.US));
|
||||
|
||||
p.setEmail("john.doe@example.com");
|
||||
p.setPhoneNumber("+1234567890");
|
||||
System.out.println("Email: " + p.getEmail());
|
||||
System.out.println("Phone Number: " + p.getPhoneNumber());
|
||||
|
||||
System.out.println("\nPerson Details:\n" + p);
|
||||
|
||||
String state = "survive";
|
||||
while (state == "survive") {
|
||||
state = p.playGame();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
30
src/Country.java
Normal file
30
src/Country.java
Normal file
@ -0,0 +1,30 @@
|
||||
package src;
|
||||
|
||||
public enum Country {
|
||||
JP(20),
|
||||
KR(19),
|
||||
DE(18),
|
||||
IN(18),
|
||||
UK(18),
|
||||
US(18);
|
||||
|
||||
private final int legalAge;
|
||||
|
||||
Country(int legalAge) {
|
||||
this.legalAge = legalAge;
|
||||
}
|
||||
|
||||
public int getLegalAge() {
|
||||
return legalAge;
|
||||
}
|
||||
|
||||
public static int getLegalAgeByCode(String code) {
|
||||
for (Country country : Country.values()) {
|
||||
if (country.name().equalsIgnoreCase(code)) {
|
||||
return country.getLegalAge();
|
||||
}
|
||||
}
|
||||
|
||||
return 18;
|
||||
}
|
||||
}
|
||||
39
src/DateUtil.java
Normal file
39
src/DateUtil.java
Normal file
@ -0,0 +1,39 @@
|
||||
package src;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
public class DateUtil {
|
||||
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("dd.MM.yyyy");
|
||||
|
||||
public static LocalDate parse(String input) {
|
||||
return LocalDate.parse(input, FORMATTER);
|
||||
}
|
||||
|
||||
public static long yearsSince(String start) {
|
||||
LocalDate d1 = parse(start);
|
||||
return ChronoUnit.YEARS.between(d1, LocalDate.now());
|
||||
}
|
||||
|
||||
public static long yearsSince(LocalDate start) {
|
||||
return ChronoUnit.YEARS.between(start, LocalDate.now());
|
||||
}
|
||||
|
||||
public static LocalDate today() {
|
||||
return LocalDate.now();
|
||||
}
|
||||
|
||||
public static String format(LocalDate date) {
|
||||
return date.format(FORMATTER);
|
||||
}
|
||||
|
||||
public static boolean isValidDate(String input) {
|
||||
try {
|
||||
LocalDate.parse(input, FORMATTER);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
14
src/EmailValidator.java
Normal file
14
src/EmailValidator.java
Normal file
@ -0,0 +1,14 @@
|
||||
package src;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class EmailValidator {
|
||||
private static final Pattern EMAIL_PATTERN = Pattern.compile(
|
||||
"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}$",
|
||||
Pattern.CASE_INSENSITIVE
|
||||
);
|
||||
|
||||
public static boolean isValid(String email) {
|
||||
return email != null && EMAIL_PATTERN.matcher(email).matches();
|
||||
}
|
||||
}
|
||||
173
src/Person.java
Normal file
173
src/Person.java
Normal file
@ -0,0 +1,173 @@
|
||||
package src;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Person {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private LocalDate birthday;
|
||||
private String email;
|
||||
private String phoneNumber;
|
||||
private final boolean[] bullets;
|
||||
private int revolverIndex = 0;
|
||||
|
||||
public Person() {
|
||||
this.bullets = genBullets();
|
||||
}
|
||||
|
||||
public Person(String firstName, String lastName) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.bullets = genBullets();
|
||||
}
|
||||
|
||||
public Person(String firstName, String lastName, String birthday, String email, String phoneNumber) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.updateBirthday(birthday);
|
||||
this.setEmail(email);
|
||||
this.setPhoneNumber(phoneNumber);
|
||||
this.bullets = genBullets();
|
||||
}
|
||||
private boolean[] genBullets() {
|
||||
boolean[] chamber = {false, false, false, false, false, true};
|
||||
|
||||
for (int i = chamber.length - 1; i > 0; i--) {
|
||||
int j = (int) (Math.random() * (i + 1));
|
||||
boolean temp = chamber[i];
|
||||
chamber[i] = chamber[j];
|
||||
chamber[j] = temp;
|
||||
}
|
||||
return chamber;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public String getFullName() {
|
||||
return firstName + " " + lastName;
|
||||
}
|
||||
|
||||
public LocalDate getBirthday() {
|
||||
return birthday;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public String getPhoneNumber() {
|
||||
return phoneNumber;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
if (birthday == null) {
|
||||
return -1;
|
||||
}
|
||||
return (int) DateUtil.yearsSince(birthday);
|
||||
}
|
||||
|
||||
public boolean isAdult(Country country) {
|
||||
int age = getAge();
|
||||
int legalAge = country.getLegalAge();
|
||||
if (age == -1) {
|
||||
return false;
|
||||
}
|
||||
return age >= legalAge;
|
||||
}
|
||||
|
||||
public boolean isMinor(Country country) {
|
||||
return !isAdult(country);
|
||||
}
|
||||
|
||||
public String getBirthdayString() {
|
||||
if (birthday == null) {
|
||||
return "unknown";
|
||||
}
|
||||
return DateUtil.format(birthday);
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
if (!EmailValidator.isValid(email)) {
|
||||
throw new IllegalArgumentException("Invalid email format");
|
||||
}
|
||||
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public void setPhoneNumber(String phoneNumber) {
|
||||
if (!PhoneValidator.isValid(phoneNumber)) {
|
||||
throw new IllegalArgumentException("Invalid phone number format");
|
||||
}
|
||||
|
||||
this.phoneNumber = phoneNumber;
|
||||
}
|
||||
|
||||
public void updateBirthday(String birthday) {
|
||||
if (birthday == null) {
|
||||
throw new IllegalArgumentException("Birthday cannot be null");
|
||||
}
|
||||
|
||||
if (!DateUtil.isValidDate(birthday)) {
|
||||
throw new IllegalArgumentException("Invalid birthday format");
|
||||
}
|
||||
|
||||
LocalDate newBirthday = DateUtil.parse(birthday);
|
||||
|
||||
if (newBirthday.isAfter(DateUtil.today())) {
|
||||
throw new IllegalArgumentException("Birthday cannot be in the future");
|
||||
}
|
||||
|
||||
if (DateUtil.yearsSince(newBirthday) > 150) {
|
||||
throw new IllegalArgumentException("Age cannot be greater than 150 years");
|
||||
}
|
||||
this.birthday = newBirthday;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format(
|
||||
"Full Name: %s\nBirthday: %s\nAge: %d\nIs Adult (Germany): %b\nIs Minor (Germany): %b\nEmail: %s\nPhone Number: %s",
|
||||
getFullName(),
|
||||
getBirthdayString(),
|
||||
getAge(),
|
||||
isAdult(Country.DE),
|
||||
isMinor(Country.DE),
|
||||
email,
|
||||
phoneNumber
|
||||
);
|
||||
}
|
||||
|
||||
public String playGame() {
|
||||
revolverIndex++;
|
||||
int shot = revolverIndex % bullets.length;
|
||||
if (shot == bullets.length - 1) {
|
||||
System.out.println(getFullName() + " reached the end and wins!");
|
||||
return "win";
|
||||
}
|
||||
|
||||
System.out.println(getFullName() + " pulls the trigger...");
|
||||
|
||||
if (bullets[shot]) {
|
||||
System.out.println("💀 " + getFullName() + " lost!");
|
||||
return "lose";
|
||||
} else {
|
||||
System.out.println("😅 " + getFullName() + " survives.");
|
||||
return "survive";
|
||||
}
|
||||
}
|
||||
}
|
||||
13
src/PhoneValidator.java
Normal file
13
src/PhoneValidator.java
Normal file
@ -0,0 +1,13 @@
|
||||
package src;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class PhoneValidator {
|
||||
private static final Pattern PHONE_PATTERN = Pattern.compile(
|
||||
"^\\+?[0-9][0-9\\- ]{6,14}$"
|
||||
);
|
||||
|
||||
public static boolean isValid(String phone) {
|
||||
return phone != null && PHONE_PATTERN.matcher(phone).matches();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user