The shop is the backend system of an online store like eBay or Amazon. The part of the store we are creating gets instructions from a customer that wants information from the database and gives back the information requested.
I have started by creating the database, exceptions and necessary classes. I’ve also created the add, remove and update methods + tests for you so you can see the workflow.
I recommend you install the Lombok plugin in IntelliJ (you can install via IntelliJ plugins menu) so you can use snacks like checkNotNull() or annotations if you should choose to use them. SonarLint is also a useful code quality plugin, but quite aggressive.
- The point of this exercise is to use test driven development and functional programming in Java 8 to improve performance, create clean code and generally make life a little easier for the jvm and whoever takes over the code after you.
- Start by creating the methods you think you will need by looking at the test requirements and workflow.
- Make sure all methods return null unless they are void.
- Create a test (move the caret to the class name and press ALT+ENTER and “create test”).
- Finish the tests and run them. Almost all should fail.
- Implement the body of the methods in the actual classes, remember to use the Stream api where applicable.
Follow the steps above for both components, you should only need to work in the ShopRepository and ShopService classes, however, test driven development promotes the use of more methods and classes, so create as many classes and methods as you think you will need (for example an input validator class?/method? Or maybe it’s logical to have more methods in the repository layer?).
Good luck!
- Receive instructions from the service layer
- Perform CRUD operations
- Return an object or list based on the instructions
- If there are no results, proceed with the error handling.
The repository should return
- An item by id
- A list of all items
- A list of items by ids from range x to y
- A list per item for location x
- A list per item for type x
- A list per item for producer x (eg hugo_boss)
- Assume the input will be “hugo boss” (no underscore)
The repository should be able to
- Remove an item (using the itemsID)
- Add an item
- Update an item
No items match the given criteria | Return null or empty list
- Validate search values to ensure they are not null or empty
- If a value is missing, throw an InvalidCriteriaException with message “Input was null, empty or lower than 0.”.
- Proceed with the call
- Validate the object returned from the repository.
- If the reply is null, throw a NoItemsFoundForCriteriaException with message “No items were found for the given search criteria.”.
- If not null, return the object.
- Return the information requested
The service should have methods for
- Getting a Map<ItemLocation, List> per ItemLocation
- Getting a Map<ItemType, List> per ItemType
- Getting a Map<String, List> per producer
- Getting a Map<Boolean, List> where one list has items under 1500 in stock and one over.
- Getting an item by id (hint: Optional)
- Getting a String of all the producers separated by x
- Getting a list of all locations with more than x in stock
- Getting a list of all locations with less than x in stock
- Getting a list of items for x location with more than y in stock
- Getting a list of items for x location with less than y in stock
- Getting a list of all items where the name (not producer) starts with x
- Finding the average item stock for location x
- Finding which item is most in stock
- Finding which item is least in stock
- Finding all items for location x that have more than y in stock
- Finding all items sorted alphabetically by producer
- Finding all items sorted alphabetically by name (not producer)
- Finding all items sorted by stock from high to low
- Finding all items without any duplicates
- Creating a single list from two lists where the first list gets items from range a to b and the second list gets items from range x to y (hint, see Stream.of, use the repository methods you created earlier)
- Creating a single list from three lists where the first list is of item location a, the second list is of item type b, the third list is of item producer c, no duplicates
- Get the total stock for all items
- Create more methods in the service layer, experiment with the Stream api and perhaps even parallel streams.
- Create a provider (or app) layer that has a class with a main method which can run the methods in the service class on the larger database in the common area.
There is a simple App class that has been implemented. This will allow the user to easily search for information in the database. To se the whole man page type "man -search" the following prompt will appear.
SEARCH OPTIONS
----------------------------------------------------------------------------------------------------
EXTENSION EXAMPLE DESCRIPTION
----------------------------------------------------------------------------------------------------
-id search -id 1 Will print the single item with that id
-l search -l oslo Will print a list of items from a given location
-t search -t clothing Will print a list of items with the given type
-s search -s 1500 Will print a list of items with stock higher then the set parameter
-n search -p iphone Will print a list of items with the given name
-p search -s apple Will print a list of items with the given producer
----------------------------------------------------------------------------------------------------