-
Notifications
You must be signed in to change notification settings - Fork 12
Сompleted task 1, task 2 #22
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
Open
AlexIJdev
wants to merge
4
commits into
FAANG-School:master
Choose a base branch
from
AlexIJdev:borovik
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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!"); | ||
| } | ||
| 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); | ||
|
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. Добавить catch |
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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!"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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!"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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!"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 + "]."; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Добавить контекст возникновения ошибки