-
Notifications
You must be signed in to change notification settings - Fork 12
Task 2 #17
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
South12309
wants to merge
5
commits into
FAANG-School:master
Choose a base branch
from
South12309:task_2
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
Task 2 #17
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
03a6462
task2 is done without main
South12309 0916b0e
task2 is done with main
South12309 81eee79
task3 classes and logical are done. Need add exception
South12309 1aedc2b
task2 fix bags, add in constructors adding field by set method
South12309 121f275
task2 is done. Fixed by comments
South12309 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
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 message) { | ||
| super(message); | ||
| } | ||
| } |
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 message) { | ||
| super(message); | ||
| } | ||
| } |
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,70 @@ | ||
| package task_2; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| @Slf4j | ||
| public class OlivandersShop { | ||
| private List<WandOrder> orders = new ArrayList<>(); | ||
| private Map<String, Integer> woodTypeCount; | ||
| private Map<String, Integer> coreMaterialCount; | ||
|
|
||
| public static void main(String[] args) { | ||
| Map<String, Integer> woodTypeCount = new HashMap<>(); | ||
| woodTypeCount.put("Дуб", 10); | ||
| woodTypeCount.put("Липа", 15); | ||
| woodTypeCount.put("Береза", 13); | ||
| Map<String, Integer> coreMaterialCount = new HashMap<>(); | ||
| coreMaterialCount.put("Перо", 15); | ||
| coreMaterialCount.put("ЧтоТо", 15); | ||
| coreMaterialCount.put("ЧтоТоЕще", 15); | ||
| OlivandersShop olivandersShop = new OlivandersShop(woodTypeCount, coreMaterialCount); | ||
| WandOrder order = new WandOrder("Ivan", new Wand("Дуб", 10, "Перо", 10), 5); | ||
| olivandersShop.placeOrder(order); | ||
| try { | ||
| System.out.println(olivandersShop.findMostPowerfulWand()); | ||
| } catch (OrdersListIsEmptyException e) { | ||
| log.info(e.getStackTrace()); | ||
| } | ||
| } | ||
|
|
||
| public OlivandersShop(Map<String, Integer> woodTypeCount, Map<String, Integer> coreMaterialCount) { | ||
| this.woodTypeCount = woodTypeCount; | ||
| this.coreMaterialCount = coreMaterialCount; | ||
| } | ||
|
|
||
| public void placeOrder(WandOrder order) throws NotEnoughWoodException { | ||
| String woodType = order.getWand().getWoodType(); | ||
| Integer woodTypeCnt = woodTypeCount.get(woodType); | ||
| checkQuantity(order, woodTypeCnt, woodType); | ||
| String coreMaterial = order.getWand().getCoreMaterial(); | ||
| Integer coreMaterialCnt = coreMaterialCount.get(coreMaterial); | ||
| checkQuantity(order, coreMaterialCnt, coreMaterial); | ||
| woodTypeCount.put(woodType, woodTypeCnt - order.getQuantity()); | ||
| coreMaterialCount.put(coreMaterial, coreMaterialCnt - order.getQuantity()); | ||
| orders.add(order); | ||
| } | ||
|
|
||
| private void checkQuantity(WandOrder order, Integer typeCnt, String type) { | ||
| if (typeCnt == null || typeCnt < order.getQuantity()) { | ||
| throw new NotEnoughWoodException(type); | ||
| } | ||
| } | ||
|
|
||
| public Wand findMostPowerfulWand() throws OrdersListIsEmptyException { | ||
| if (orders.isEmpty()) { | ||
| throw new OrdersListIsEmptyException("Список заказов пуст"); | ||
| } | ||
| WandOrder maxQualityWand = new WandOrder(); | ||
| for (WandOrder order : orders) { | ||
| if (order.getQuantity() > maxQualityWand.getQuantity()) { | ||
| maxQualityWand = order; | ||
| } | ||
| } | ||
| return maxQualityWand.getWand(); | ||
| } | ||
| } | ||
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 Exception{ | ||
| public OrdersListIsEmptyException(String message) { | ||
| super(message); | ||
| } | ||
| } |
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,71 @@ | ||
| package task_2; | ||
|
|
||
| public class Wand { | ||
| private String woodType; | ||
| private int length; | ||
| private String coreMaterial; | ||
| private int powerLevel; | ||
|
|
||
| public Wand(String woodType, int length, String coreMaterial, int powerLevel) { | ||
| setWoodType(woodType); | ||
| setLength(length); | ||
| setCoreMaterial(coreMaterial); | ||
| setPowerLevel(powerLevel); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Wand{" + | ||
| "woodType='" + woodType + '\'' + | ||
| ", length=" + length + | ||
| ", coreMaterial='" + coreMaterial + '\'' + | ||
| ", powerLevel=" + powerLevel + | ||
| '}'; | ||
| } | ||
|
|
||
| public String getWoodType() { | ||
| return woodType; | ||
| } | ||
|
|
||
| public void setWoodType(String woodType) { | ||
| if (woodType.isBlank()) { | ||
| throw new IllegalArgumentException("wood type not be empty"); | ||
| } | ||
| this.woodType = woodType; | ||
| } | ||
|
|
||
| public int getLength() { | ||
| return length; | ||
| } | ||
|
|
||
| public void setLength(int length) { | ||
| if (length <= 0) { | ||
| throw new IllegalArgumentException("wand length must be ,ore than 0"); | ||
| } | ||
| this.length = length; | ||
| } | ||
|
|
||
| public String getCoreMaterial() { | ||
| return coreMaterial; | ||
| } | ||
|
|
||
| public void setCoreMaterial(String coreMaterial) { | ||
| if (woodType.isBlank()) { | ||
| throw new IllegalArgumentException("core material type not be empty"); | ||
| } | ||
| this.coreMaterial = coreMaterial; | ||
| } | ||
|
|
||
| public int getPowerLevel() { | ||
| return powerLevel; | ||
| } | ||
|
|
||
| public void setPowerLevel(int powerLevel) { | ||
| if (length <= 0) { | ||
| throw new IllegalArgumentException("wand power must be ,ore than 0"); | ||
| } | ||
| this.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,46 @@ | ||
| package task_2; | ||
|
|
||
| public class WandOrder { | ||
| private String customerName; | ||
| private Wand wand; | ||
| private int quantity; | ||
|
|
||
| public WandOrder() { | ||
| } | ||
|
|
||
| public WandOrder(String customerName, Wand wand, int quantity) { | ||
| setCustomerName(customerName); | ||
| setWand(wand); | ||
| setQuantity(quantity); | ||
| } | ||
|
|
||
| public String getCustomerName() { | ||
| return customerName; | ||
| } | ||
|
|
||
| public void setCustomerName(String customerName) { | ||
| if (customerName.isBlank()) { | ||
| throw new IllegalArgumentException("Name customer must be not empty"); | ||
| } | ||
| this.customerName = customerName; | ||
| } | ||
|
|
||
| public Wand getWand() { | ||
| return wand; | ||
| } | ||
|
|
||
| public void setWand(Wand wand) { | ||
| this.wand = wand; | ||
| } | ||
|
|
||
| public int getQuantity() { | ||
| return quantity; | ||
| } | ||
|
|
||
| public void setQuantity(int quantity) { | ||
| if (quantity <= 0) { | ||
| throw new IllegalArgumentException("Count must be more than 0"); | ||
| } | ||
| this.quantity = 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.
Можно использовать пустой Optional
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.
По подсказке 1 должно выбрасываться исключение с этим именем.