-
Notifications
You must be signed in to change notification settings - Fork 12
Task 2 #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Task 2 #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package task_1.solution; | ||
|
|
||
| public class Main { | ||
| 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()); | ||
| } | ||
| } | ||
| } |
| 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."); | ||
| } | ||
|
|
||
| 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."); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package task_1.solution; | ||
|
|
||
| public class TimeTravelException extends Exception { | ||
| public TimeTravelException(String message) { | ||
| super(message); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package task_1.solution; | ||
|
|
||
| public class TimeTraveler { | ||
| 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; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package task_2.solution; | ||
|
|
||
| import task_2.solution.exceptions.NoOrdersException; | ||
| import task_2.solution.exceptions.NotEnoughMaterialException; | ||
| import task_2.solution.exceptions.NotEnoughWoodException; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| try { | ||
| Wand wand1 = new Wand("Дуб", 12, "-", 10); | ||
| WandOrder order1 = new WandOrder("Петр", wand1, 3); | ||
|
|
||
| Wand wand2 = new Wand("Ель", 11, "-", 12); | ||
| WandOrder order2 = new WandOrder("Шавкат", wand2, 2); | ||
|
|
||
| OlivandersShop olivanders = new OlivandersShop(); | ||
| olivanders.placeOrder(order1); | ||
| olivanders.placeOrder(order2); | ||
|
|
||
| Wand mostPowerfulWand = olivanders.findMostPowerfulWand(); | ||
| System.out.println("Самая сильная палочка : " + mostPowerfulWand.getWoodType() + " " + mostPowerfulWand.getCoreMaterial()); | ||
| } catch (NotEnoughWoodException | NotEnoughMaterialException | NoOrdersException e) { | ||
| System.out.println("Ошибка: " + e.getMessage()); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package task_2.solution; | ||
|
|
||
| import task_2.solution.exceptions.NoOrdersException; | ||
| import task_2.solution.exceptions.NotEnoughMaterialException; | ||
| import task_2.solution.exceptions.NotEnoughWoodException; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class OlivandersShop { | ||
| private List<WandOrder> orders; | ||
| private Map<String, Integer> woodTypeCount; | ||
| private Map<String, Integer> coreMaterialCount; | ||
|
|
||
| public OlivandersShop() { | ||
| orders = new ArrayList<>(); | ||
| woodTypeCount = new HashMap<>(); | ||
| coreMaterialCount = new HashMap<>(); | ||
| } | ||
| public void placeOrder(WandOrder order) throws NotEnoughMaterialException, NotEnoughWoodException { | ||
| String woodType = order.getWand().getWoodType(); | ||
| int requiredWoodCount = order.getQuantity(); | ||
|
|
||
| if (woodTypeCount.containsKey(woodType)) { | ||
| int availableWoodCount = woodTypeCount.get(woodType); | ||
| if (availableWoodCount < requiredWoodCount) { | ||
| throw new NotEnoughWoodException(woodType); | ||
| } | ||
| woodTypeCount.put(woodType, availableWoodCount - requiredWoodCount); | ||
| } else { | ||
| throw new NotEnoughWoodException(woodType); | ||
| } | ||
|
|
||
| String coreMaterial = order.getWand().getCoreMaterial(); | ||
| int requiredMaterialCount = order.getQuantity(); | ||
|
|
||
| if (coreMaterialCount.containsKey(coreMaterial)) { | ||
| int availableMaterialCount = coreMaterialCount.get(coreMaterial); | ||
| if (availableMaterialCount < requiredMaterialCount) { | ||
| throw new NotEnoughMaterialException(coreMaterial); | ||
| } | ||
| coreMaterialCount.put(coreMaterial, availableMaterialCount - requiredMaterialCount); | ||
| } else { | ||
| throw new NotEnoughMaterialException(coreMaterial); | ||
| } | ||
|
|
||
| orders.add(order); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Отступ |
||
| public Wand findMostPowerfulWand() throws NoOrdersException{ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional |
||
| if (orders.isEmpty()) { | ||
| throw new NoOrdersException("Заказы недоступны"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Возвращать пустой Optional, а не кидать исключение |
||
| } | ||
|
|
||
| Wand mostPowerfulWand = orders.get(0).getWand(); | ||
| int maxPowerLevel = mostPowerfulWand.getPowerLevel(); | ||
|
|
||
| for (WandOrder order : orders) { | ||
| if (order.getWand().getPowerLevel() > maxPowerLevel) { | ||
| mostPowerfulWand = order.getWand(); | ||
| maxPowerLevel = mostPowerfulWand.getPowerLevel(); | ||
| } | ||
| } | ||
|
|
||
| return mostPowerfulWand; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package task_2.solution; | ||
|
|
||
| import task_2.solution.exceptions.NotEnoughMaterialException; | ||
| import task_2.solution.exceptions.NotEnoughWoodException; | ||
|
|
||
| public class Wand { | ||
| private String woodType; | ||
| private double length; | ||
| private String coreMaterial; | ||
| private int powerLevel; | ||
|
|
||
| public Wand(String woodType, int length, String coreMaterial, int powerLevel) throws NotEnoughWoodException, | ||
| NotEnoughMaterialException { | ||
| if (woodType == null || woodType.isEmpty()) { | ||
| throw new NotEnoughWoodException("Тип дерева не может быть пустым"); | ||
| } | ||
| if (length <= 0) { | ||
| throw new IllegalArgumentException("Длина должна быть больше 0"); | ||
| } | ||
| if (coreMaterial == null || coreMaterial.isEmpty()) { | ||
| throw new NotEnoughMaterialException("Материал не может быть пустым"); | ||
| } | ||
| if (powerLevel <= 0) { | ||
| throw new IllegalArgumentException("Уровень мощности должен быть больше 0"); | ||
| } | ||
| this.woodType = woodType; | ||
| this.length = length; | ||
| this.coreMaterial = coreMaterial; | ||
| this.powerLevel = powerLevel; | ||
| } | ||
|
|
||
| public String getWoodType() { | ||
| return woodType; | ||
| } | ||
|
|
||
| public double getLength() { | ||
| return length; | ||
| } | ||
|
|
||
| public String getCoreMaterial() { | ||
| return coreMaterial; | ||
| } | ||
|
|
||
| public int getPowerLevel() { | ||
| return powerLevel; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package task_2.solution; | ||
|
|
||
| public class WandOrder { | ||
| private String customerName; | ||
| private Wand wand; | ||
| private int quantity; | ||
| public WandOrder(String customerName, Wand wand, int quantity) { | ||
| if (customerName == null || customerName.isEmpty()) { | ||
| throw new IllegalArgumentException("Имя клиента не может быть пустым"); | ||
| } | ||
| if (quantity <= 0) { | ||
| throw new IllegalArgumentException("Количество должно быть больше 0"); | ||
| } | ||
|
|
||
| this.customerName = customerName; | ||
| this.wand = wand; | ||
| this.quantity = quantity; | ||
| } | ||
|
|
||
| public String getCustomerName() { | ||
| return customerName; | ||
| } | ||
|
|
||
| public Wand getWand() { | ||
| return wand; | ||
| } | ||
|
|
||
| public int getQuantity() { | ||
| return quantity; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package task_2.solution.exceptions; | ||
|
|
||
| public class NoOrdersException extends Exception{ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. RuntimeException |
||
| public NoOrdersException(String message) { | ||
| super(message); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package task_2.solution.exceptions; | ||
|
|
||
| public class NotEnoughMaterialException extends Exception{ | ||
| public NotEnoughMaterialException(String material) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. RuntimeException |
||
| super("Недостаточно материала для сердца палочки: " + material); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package task_2.solution.exceptions; | ||
|
|
||
| public class NotEnoughWoodException extends Exception{ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. RuntimeException |
||
| public NotEnoughWoodException(String woodType) { | ||
| super("Недостаточно дерева типа " + woodType + " для изготовления палочек."); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Дубликация кода. Вынести в приватный метод проверку наличия материалов в достаточном количестве