diff --git a/Main.java b/Main.java index 887b19a..b335db5 100644 --- a/Main.java +++ b/Main.java @@ -4,26 +4,94 @@ public class Main { public static void main(String[] args) { 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(); + // Test setters and print results + try { + p.setFirstName("John"); + p.setLastName("Doe"); + System.out.println("Full Name: " + p.getFullName()); + } catch (Exception e) { + System.out.println("Error setting name: " + e.getMessage()); } + + 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"); } } diff --git a/src/Country.java b/src/Country.java index 0873bec..fd23956 100644 --- a/src/Country.java +++ b/src/Country.java @@ -6,7 +6,12 @@ public enum Country { DE(18), IN(18), UK(18), - US(18); + US(18), + RU(18), + FR(18), + CH(18), + IT(18), + AT(18); private final int legalAge; diff --git a/src/Person.java b/src/Person.java index e1161df..532b14f 100644 --- a/src/Person.java +++ b/src/Person.java @@ -1,6 +1,8 @@ package src; import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.Duration; import java.io.IOException; public class Person { @@ -11,6 +13,21 @@ public class Person { private String phoneNumber; private final boolean[] bullets; 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() { this.bullets = genBullets(); @@ -138,21 +155,80 @@ public class Person { this.birthday = newBirthday; } + public boolean isAlive() { + return this.alive; + } + + public void decease() { + if (alive) { + alive = false; + timeOfDeath = LocalDateTime.now(); + } + } + @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", + "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(), getBirthdayString(), getAge(), isAdult(Country.DE), isMinor(Country.DE), 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++; int shot = revolverIndex % bullets.length; if (shot == bullets.length - 1) { @@ -164,6 +240,7 @@ public class Person { if (bullets[shot]) { System.out.println("๐Ÿ’€ " + getFullName() + " lost!"); + decease(); return "lose"; } else { System.out.println("๐Ÿ˜… " + getFullName() + " survives.");