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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
5. Git создаст локальную копию удаленного репозитория в текущем каталоге. Локальную копию вы можете изменять и синхронизировать с удалённым репозиторием по мере необходимости.

# Как создать Pull Request
Идём в НАШ удаленный репозиторий на GitHub (не в вашу копию, а именно в НАШ репозиторий). Так как мы только что сделали push с решением задачи в свою ветку, то сразу же видим сообщение с предложением создать Pull Request.
Идём в НАШ удаленный репозиторий на GitHub (в свою копию или в наш, по идее не важно). Так как мы только что сделали push с решением задачи в свою ветку, то сразу же видим сообщение с предложением создать Pull Request.
Жмём на зеленую кнопку.
![Image1](https://github.com/FAANG-School/exceptions_workshop/blob/master/images/image1.png)
Если не видите эту жёлтую всплывашку, то просто перейдите во вкладку Pull requests сверху, и нажмите зеленую кнопку там.
Expand Down
Binary file added build/classes/java/main/task_1/TimeMachine.class
Binary file not shown.
Binary file not shown.
Binary file added build/classes/java/main/task_1/TimeTraveler.class
Binary file not shown.
Binary file added build/tmp/compileJava/previous-compilation-data.bin
Binary file not shown.
46 changes: 46 additions & 0 deletions src/main/java/task_1/TimeMachine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package task_1;

public class TimeMachine {

private int currentYear;
private boolean isWorking;

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

public void travelInTime(TimeTraveler timeTraveler, int yearInTheFuture) {
if (!isWorking) {
throw new TimeTravelException("The time machine is not working at the moment!");
}
if (yearInTheFuture < timeTraveler.getBirthYear()) {
throw new TimeTravelException("The traveler has not yet been born!");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Добавить контекст возникновения ошибки

}
if (yearInTheFuture > timeTraveler.getDeathYear()) {
throw new TimeTravelException("The traveler has already died!");
}
System.out.println("The trip was successful! The traveler " + timeTraveler.getName() + " is in " + yearInTheFuture + ".");
}

public static void main(String[] args) {
TimeMachine timeMachineOne = new TimeMachine(2023, true);
TimeMachine timeMachineTwo = new TimeMachine(2000, false);

TimeTraveler timeTravelerOne = new TimeTraveler("Alex", 1996, 2060);
TimeTraveler timeTravelerTwo = new TimeTraveler("Mike", 2025, 2080);
TimeTraveler timeTravelerThree = new TimeTraveler("Kate", 2080, 2150);

// исключение "The traveler has already died!"
timeMachineOne.travelInTime(timeTravelerOne, 2070);

// успешное выполнение программы
timeMachineOne.travelInTime(timeTravelerTwo, 2070);

// исключение "The traveler has not yet been born!"
timeMachineOne.travelInTime(timeTravelerThree, 2070);

// исключение "The time machine is not working at the moment!"
timeMachineTwo.travelInTime(timeTravelerOne, 2070);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Добавить catch

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

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

public class TimeTraveler {

private final String name;
private final int birthYear;
private final 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;
}
}
1 change: 1 addition & 0 deletions src/main/java/task_1/clues/clue_3.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
а затем проверьте год, в который нужно путешествовать.
Для этого сравните год путешествия с годом рождение и смерти.
В случае не выполнения условий выкиньте исключение с
помощью ключевого слова `throw`.
помощью ключевого слова `throw`.
7 changes: 7 additions & 0 deletions src/main/java/task_2/NotEnoughMaterialException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package task_2;

public class NotEnoughMaterialException extends RuntimeException {
public NotEnoughMaterialException(String coreMaterial) {
super("There is not enough \"" + coreMaterial + "\" for the production of sticks!");
}
}
7 changes: 7 additions & 0 deletions src/main/java/task_2/NotEnoughWoodException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package task_2;

public class NotEnoughWoodException extends RuntimeException {
public NotEnoughWoodException(String woodType) {
super("There is not enough \"" + woodType + "\" for the production of sticks!");
}
}
102 changes: 102 additions & 0 deletions src/main/java/task_2/OlivandersShop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package task_2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OlivandersShop {

private Map<String, Integer> woodTypeCount;
private Map<String, Integer> coreMaterialCount;
private List<WandOrder> orders = new ArrayList<>();

public Map<String, Integer> getWoodTypeCount() {
return woodTypeCount;
}

public Map<String, Integer> getCoreMaterialCount() {
return coreMaterialCount;
}

public void setWoodTypeCount(Map<String, Integer> woodTypeCount) {
this.woodTypeCount = woodTypeCount;
}

public void setCoreMaterialCount(Map<String, Integer> coreMaterialCount) {
this.coreMaterialCount = coreMaterialCount;
}

public void placeOrder(WandOrder order) {
String woodType = order.getWand().getWoodType();
String coreMaterial = order.getWand().getCoreMaterial();

for (Map.Entry<String, Integer> woodTypeCnt : woodTypeCount.entrySet()) {
if (woodTypeCnt.getKey().equals(woodType)) {
if (woodTypeCnt.getValue() >= order.getQuantity()) {
for (Map.Entry<String, Integer> coreMaterialCnt : coreMaterialCount.entrySet()) {
if (coreMaterialCnt.getKey().equals(coreMaterial)) {
if (coreMaterialCnt.getValue() >= order.getQuantity()) {
orders.add(order);
} else {
throw new NotEnoughMaterialException(coreMaterial);
}
}
}
} else {
throw new NotEnoughWoodException(woodType);
}
}
}
}

public Wand findMostPowerfulWand() {
if (orders.isEmpty()) {
throw new OrdersListIsEmptyException();
}
Wand mostPowerfulWand = orders.get(0).getWand();
for (WandOrder wandOrder : orders) {
if (wandOrder.getWand().getPowerLevel() > mostPowerfulWand.getPowerLevel()) {
mostPowerfulWand = wandOrder.getWand();
}
}
return mostPowerfulWand;
}

public static void main(String[] args) {
OlivandersShop olivandersShop = new OlivandersShop();

olivandersShop.setWoodTypeCount(new HashMap<>());

olivandersShop.getWoodTypeCount().put("Acacia", 30);
olivandersShop.getWoodTypeCount().put("Beech", 25);
olivandersShop.getWoodTypeCount().put("Elder", 40);
olivandersShop.getWoodTypeCount().put("Vine", 10);
olivandersShop.getWoodTypeCount().put("Willow", 15);
olivandersShop.getWoodTypeCount().put("Cedar", 35);
olivandersShop.getWoodTypeCount().put("Yew", 20);

olivandersShop.setCoreMaterialCount(new HashMap<>());

olivandersShop.getCoreMaterialCount().put("Phoenix feather", 30);
olivandersShop.getCoreMaterialCount().put("Thestral tail hair", 40);
olivandersShop.getCoreMaterialCount().put("Unicorn hair", 25);
olivandersShop.getCoreMaterialCount().put("Dragon heartstring", 10);

Wand wandOne = new Wand("Acacia", 15.3, "Unicorn hair", 100);
Wand wandTwo = new Wand("Cedar", 13.8, "Thestral tail hair", 150);
Wand wandThree = new Wand("Willow", 14.8, "Dragon heartstring", 90);

WandOrder wandOrderOne = new WandOrder("CustomerOne", wandOne, 10);
WandOrder wandOrderTwo = new WandOrder("CustomerOne", wandTwo, 20);
WandOrder wandOrderThree = new WandOrder("CustomerTwo", wandTwo, 5);
WandOrder wandOrderFour = new WandOrder("CustomerTwo", wandThree, 9);

olivandersShop.placeOrder(wandOrderOne);
olivandersShop.placeOrder(wandOrderTwo);
olivandersShop.placeOrder(wandOrderThree);
olivandersShop.placeOrder(wandOrderFour);

System.out.println(olivandersShop.findMostPowerfulWand());
}
}
7 changes: 7 additions & 0 deletions src/main/java/task_2/OrdersListIsEmptyException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package task_2;

public class OrdersListIsEmptyException extends RuntimeException {
public OrdersListIsEmptyException() {
super("The list of orders is empty!");
}
}
47 changes: 47 additions & 0 deletions src/main/java/task_2/Wand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package task_2;

public class Wand {

private String woodType;
private double length;
private String coreMaterial;
private int powerLevel;

public Wand(String woodType, double length, String coreMaterial, int powerLevel) {
if (woodType.isEmpty()) {
throw new IllegalArgumentException("The \"woodType\" field should not be empty!");
}
if (length <= 0) {
throw new IllegalArgumentException("The \"length\" field should not be <= 0!");
}
if (coreMaterial.isEmpty()) {
throw new IllegalArgumentException("The \"coreMaterial\" field should not be empty!");
}
if (powerLevel <= 0) {
throw new IllegalArgumentException("The \"powerLevel\" field should not be <= 0!");
}
this.woodType = woodType;
this.length = length;
this.coreMaterial = coreMaterial;
this.powerLevel = powerLevel;

}

public String getWoodType() {
return woodType;
}

public String getCoreMaterial() {
return coreMaterial;
}

public int getPowerLevel() {
return powerLevel;
}

@Override
public String toString() {
return "Wand [" + "woodType: " + woodType + "; length: " + length + "; coreMaterial: " +
coreMaterial + "; powerLevel: " + powerLevel + "].";
}
}
28 changes: 28 additions & 0 deletions src/main/java/task_2/WandOrder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package task_2;

public class WandOrder {

private final String customerName;
private Wand wand;
private int quantity;

public WandOrder(String customerName, Wand wand, int quantity) {
if (customerName.isEmpty()) {
throw new IllegalArgumentException("The \"customerName\" field should not be empty!");
}
if (quantity <= 0) {
throw new IllegalArgumentException("The \"quantity\" field should not be <= 0!");
}
this.customerName = customerName;
this.wand = wand;
this.quantity = quantity;
}

public Wand getWand() {
return wand;
}

public int getQuantity() {
return quantity;
}
}