Skip to content

Instantly share code, notes, and snippets.

@nachivpn
Last active July 30, 2017 13:59
Show Gist options
  • Save nachivpn/ad5d25e2e2aac7c61121b9986a7fd672 to your computer and use it in GitHub Desktop.
Save nachivpn/ad5d25e2e2aac7c61121b9986a7fd672 to your computer and use it in GitHub Desktop.
InventorySample.java
public static void main(String args[]){
//bike product
Map<String,List<String>> productDetails = new HashMap<>();
List<String> bikeComponentNames = new ArrayList<>();
bikeComponentNames.add("wheel");
bikeComponentNames.add("wheel");
bikeComponentNames.add("frame");
productDetails.put("bike", bikeComponentNames);
//componets
Map<String,Component> components = new HashMap<>();
components.put("wheel", new BikeComponent("mrf#123"));
components.put("frame", new BikeComponent("cf#52"));
//component prices
Map<String,Double> prices = new HashMap<>();
prices.put("mrf#123", 100D);
prices.put("cf#52", 1000D);
Double discount = 0.10;
Either<Exception, Double> ep = new InventorySample(productDetails, components, prices, discount).getProductPrice("bike");
ep.match((Exception e) -> {System.out.println(e.getMessage()); return null;},
(Double price) -> {System.out.println(price); return null;});
}
/*
Output with various changes to code above:
---------------
(No change)
-> 1080.0
---------------
//remove price of "cf#52" from inventory
Map<String,Double> prices = new HashMap<>();
prices.put("mrf#123", 100D);
// prices.put("cf#52", 1000D);
-> Price not found for cf#52
-----------------
//remove frame componenet from inventory
Map<String,Component> components = new HashMap<>();
components.put("wheel", new BikeComponent("mrf#123"));
// components.put("frame", new BikeComponent("cf#52"));
-> Component not found: frame
-----------------
//no discount
Either<Exception, Double> ep = new InventorySample(productDetails, components, prices, null).getProductPrice("bike");
-> 1200.00
------------------
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment