Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added build/tmp/compileJava/previous-compilation-data.bin
Binary file not shown.
17 changes: 17 additions & 0 deletions src/main/java/task_1/solution/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package task_1.solution;

public class Main {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@slf4j из lombok даст log объект

public static void main(String[] args) {
TimeMachine timeMachine = new TimeMachine(2023);

TimeTraveler traveler1 = new TimeTraveler("Travis", 1990, 2100);
TimeTraveler traveler2 = new TimeTraveler("Ja", 1985, 2050);

try {
timeMachine.travelInTime(traveler1, 2000);
timeMachine.travelInTime(traveler2, 2075);
} catch (TimeTravelException e) {
System.out.println("Произошла ошибка: " + e.getMessage());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не логируется stacktrace

}
}
}
29 changes: 29 additions & 0 deletions src/main/java/task_1/solution/TimeMachine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package task_1.solution;

public class TimeMachine {
private int currentYear;
private boolean isWorking;

public TimeMachine(int currentYear) {
this.currentYear = currentYear;
this.isWorking = true;
}

public void travelInTime(TimeTraveler timeTraveler, int year) throws TimeTravelException {
if (!isWorking) {
throw new TimeTravelException("The time machine is not working at the moment.");
}

if (year < timeTraveler.getBirthYear()) {
throw new TimeTravelException("The year of travel to the past is less than the year of the traveler's " +
"birth.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Добавить информацию о путешественнике во времени

}

if (year > timeTraveler.getDeathYear()) {
throw new TimeTravelException("The year of the travel into the future is longer than the year of the " +
"traveler's death.");
}

System.out.println(timeTraveler.getName() + " travels through time in " + year + " year.");
}
}
7 changes: 7 additions & 0 deletions src/main/java/task_1/solution/TimeTravelException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package task_1.solution;

public class TimeTravelException extends Exception {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше unchecked
extendes RuntimeException

public TimeTravelException(String message) {
super(message);
}
}
25 changes: 25 additions & 0 deletions src/main/java/task_1/solution/TimeTraveler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package task_1.solution;

public class TimeTraveler {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Рассмотреть использования lombok

private String name;
private int birthYear;
private int deathYear;

public TimeTraveler(String name, int birthYear, int deathYear) {
this.name = name;
this.birthYear = birthYear;
this.deathYear = deathYear;
}

public String getName() {
return name;
}

public int getBirthYear() {
return birthYear;
}

public int getDeathYear() {
return deathYear;
}
}