enhance Main and Person classes with detailed validation and game logic; add new countries to Country enum
This commit is contained in:
parent
4343517646
commit
84f435a8ce
108
Main.java
108
Main.java
@ -4,26 +4,94 @@ public class Main {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Person p = new Person();
|
Person p = new Person();
|
||||||
|
|
||||||
p.setFirstName("John");
|
// Test setters and print results
|
||||||
p.setLastName("Doe");
|
try {
|
||||||
System.out.println("Full Name: " + p.getFullName());
|
p.setFirstName("John");
|
||||||
|
p.setLastName("Doe");
|
||||||
p.updateBirthday("15.04.2000");
|
System.out.println("Full Name: " + p.getFullName());
|
||||||
System.out.println("Birthday: " + p.getBirthdayString());
|
} catch (Exception e) {
|
||||||
System.out.println("Age: " + p.getAge());
|
System.out.println("Error setting name: " + e.getMessage());
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
p.updateBirthday("15.04.2000");
|
||||||
|
System.out.println("Birthday: " + p.getBirthdayString());
|
||||||
|
System.out.println("Age: " + p.getAge());
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Error setting birthday: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
System.out.println("Is Adult (USA): " + p.isAdult(Country.US));
|
||||||
|
System.out.println("Is Minor (USA): " + p.isMinor(Country.US));
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Error checking age: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
p.setEmail("john.doe@example.com");
|
||||||
|
System.out.println("Email: " + p.getEmail());
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Error setting email: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
p.setPhoneNumber("+1234567890");
|
||||||
|
System.out.println("Phone Number: " + p.getPhoneNumber());
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Error setting phone: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\nPerson Details:\n" + p + "\n");
|
||||||
|
|
||||||
|
// Test invalid email
|
||||||
|
try {
|
||||||
|
p.setEmail("not-an-email");
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Expected email error: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test invalid phone
|
||||||
|
try {
|
||||||
|
p.setPhoneNumber("not-a-phone");
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Expected phone error: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test invalid birthday
|
||||||
|
try {
|
||||||
|
p.updateBirthday("99.99.9999");
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Expected birthday error: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Play game in Austria
|
||||||
|
String state = "survive";
|
||||||
|
System.out.println("\nPlaying Russian Roulette in Austria:");
|
||||||
|
while (state.equals("survive")) {
|
||||||
|
state = p.playGame(Country.AT);
|
||||||
|
System.out.println("Result: " + state + ", Alive: " + p.isAlive());
|
||||||
|
if (!p.isAlive()) {
|
||||||
|
System.out.println("Time of Death: " + p.getTimeOfDeath());
|
||||||
|
System.out.println("Dead Since: " + p.getDeadSince());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\nFinal Person Details after Austria:\n" + p + "\n");
|
||||||
|
|
||||||
|
// Reset for Russia game
|
||||||
|
Person p2 = new Person("Jane", "Roe", "01.01.1995", "jane.roe@example.com", "+1987654321");
|
||||||
|
System.out.println("\nNew Person for Russia game:\n" + p2 + "\n");
|
||||||
|
state = "survive";
|
||||||
|
System.out.println("Playing Russian Roulette in Russia:");
|
||||||
|
while (state.equals("survive")) {
|
||||||
|
state = p2.playGame(Country.RU);
|
||||||
|
System.out.println("Result: " + state + ", Alive: " + p2.isAlive());
|
||||||
|
if (!p2.isAlive()) {
|
||||||
|
System.out.println("Time of Death: " + p2.getTimeOfDeath());
|
||||||
|
System.out.println("Dead Since: " + p2.getDeadSince());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("\nFinal Person Details after Russia:\n" + p2 + "\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,12 @@ public enum Country {
|
|||||||
DE(18),
|
DE(18),
|
||||||
IN(18),
|
IN(18),
|
||||||
UK(18),
|
UK(18),
|
||||||
US(18);
|
US(18),
|
||||||
|
RU(18),
|
||||||
|
FR(18),
|
||||||
|
CH(18),
|
||||||
|
IT(18),
|
||||||
|
AT(18);
|
||||||
|
|
||||||
private final int legalAge;
|
private final int legalAge;
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
package src;
|
package src;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.Duration;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class Person {
|
public class Person {
|
||||||
@ -11,6 +13,21 @@ public class Person {
|
|||||||
private String phoneNumber;
|
private String phoneNumber;
|
||||||
private final boolean[] bullets;
|
private final boolean[] bullets;
|
||||||
private int revolverIndex = 0;
|
private int revolverIndex = 0;
|
||||||
|
private boolean alive = true;
|
||||||
|
private LocalDateTime timeOfDeath = null;
|
||||||
|
public LocalDateTime getTimeOfDeath() {
|
||||||
|
return timeOfDeath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDeadSince() {
|
||||||
|
if (timeOfDeath == null) return "-";
|
||||||
|
Duration d = Duration.between(timeOfDeath, LocalDateTime.now());
|
||||||
|
long days = d.toDays();
|
||||||
|
long hours = d.toHours() % 24;
|
||||||
|
long minutes = d.toMinutes() % 60;
|
||||||
|
long seconds = d.getSeconds() % 60;
|
||||||
|
return String.format("%d days, %d hours, %d min, %d sec", days, hours, minutes, seconds);
|
||||||
|
}
|
||||||
|
|
||||||
public Person() {
|
public Person() {
|
||||||
this.bullets = genBullets();
|
this.bullets = genBullets();
|
||||||
@ -138,21 +155,80 @@ public class Person {
|
|||||||
this.birthday = newBirthday;
|
this.birthday = newBirthday;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isAlive() {
|
||||||
|
return this.alive;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void decease() {
|
||||||
|
if (alive) {
|
||||||
|
alive = false;
|
||||||
|
timeOfDeath = LocalDateTime.now();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format(
|
return String.format(
|
||||||
"Full Name: %s\nBirthday: %s\nAge: %d\nIs Adult (Germany): %b\nIs Minor (Germany): %b\nEmail: %s\nPhone Number: %s",
|
"Full Name: %s\nBirthday: %s\nAge: %d\nIs Adult (Germany): %b\nIs Minor (Germany): %b\nEmail: %s\nPhone Number: %s\nIs Alive: %b\nTime of Death: %s\nDead Since: %s",
|
||||||
getFullName(),
|
getFullName(),
|
||||||
getBirthdayString(),
|
getBirthdayString(),
|
||||||
getAge(),
|
getAge(),
|
||||||
isAdult(Country.DE),
|
isAdult(Country.DE),
|
||||||
isMinor(Country.DE),
|
isMinor(Country.DE),
|
||||||
email,
|
email,
|
||||||
phoneNumber
|
phoneNumber,
|
||||||
|
isAlive(),
|
||||||
|
(timeOfDeath == null ? "-" : timeOfDeath.toString()),
|
||||||
|
getDeadSince()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String playGame() {
|
public String playGame(Country country) {
|
||||||
|
String result = null;
|
||||||
|
switch (country) {
|
||||||
|
case DE:
|
||||||
|
System.out.println("🇩🇪 In Germany, we don't play games. We work. *Arbeit macht frei!*");
|
||||||
|
result = "no games";
|
||||||
|
break;
|
||||||
|
case FR:
|
||||||
|
System.out.println("🇫🇷 En France, on ne joue pas à ça. On fait la *bagoute* !");
|
||||||
|
result = "no games";
|
||||||
|
break;
|
||||||
|
case AT:
|
||||||
|
System.out.println("🇦🇹 Geh schleich di mit deim Spiel!");
|
||||||
|
result = "no games";
|
||||||
|
break;
|
||||||
|
case IT:
|
||||||
|
System.out.println("🇮🇹 Mamma mia! Too busy eating pizza, no time for roulette!");
|
||||||
|
result = "no games";
|
||||||
|
break;
|
||||||
|
case US:
|
||||||
|
System.out.println("🇺🇸 Pew pew! This isn’t roulette, it’s just a regular Tuesday!");
|
||||||
|
result = "no games";
|
||||||
|
break;
|
||||||
|
case UK:
|
||||||
|
System.out.println("🇬🇧 Fancy a cuppa instead, mate? Russian Roulette is far too uncivilised.");
|
||||||
|
result = "no games";
|
||||||
|
break;
|
||||||
|
case CH:
|
||||||
|
System.out.println("🇨🇭 Neutral as always. No games, just cheese and banking.");
|
||||||
|
result = "no games";
|
||||||
|
break;
|
||||||
|
case JP:
|
||||||
|
System.out.println("🇯🇵 Too polite to shoot. Let’s just play Pachinko instead.");
|
||||||
|
result = "no games";
|
||||||
|
break;
|
||||||
|
case RU:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.out.println("no games");
|
||||||
|
result = "no games";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (result != null) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
revolverIndex++;
|
revolverIndex++;
|
||||||
int shot = revolverIndex % bullets.length;
|
int shot = revolverIndex % bullets.length;
|
||||||
if (shot == bullets.length - 1) {
|
if (shot == bullets.length - 1) {
|
||||||
@ -164,6 +240,7 @@ public class Person {
|
|||||||
|
|
||||||
if (bullets[shot]) {
|
if (bullets[shot]) {
|
||||||
System.out.println("💀 " + getFullName() + " lost!");
|
System.out.println("💀 " + getFullName() + " lost!");
|
||||||
|
decease();
|
||||||
return "lose";
|
return "lose";
|
||||||
} else {
|
} else {
|
||||||
System.out.println("😅 " + getFullName() + " survives.");
|
System.out.println("😅 " + getFullName() + " survives.");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user