This project is a hands-on assignment to practice Kotlin functions and basic logic in a small “shopping cart” scenario. You will implement functions to manage store inventory, a shopping cart, item totals, and product filtering.
You will:
- Fork this repository to your own GitHub account.
- Clone your fork locally.
- Implement the required functions in
Main.kt(found insrc/shoppingCart/). - Push your changes to your fork.
- Create a Pull Request (PR) back to the original repository.
All tests are located in the tests folder. They will indicate what functionality is missing or incorrect until they all pass successfully.
- On the original GitHub repo, click “Fork” (top-right corner) and choose your own GitHub account.
- Copy the fork’s clone URL.
- In a terminal or command prompt:
git clone https://github.com/<YOUR_USERNAME>/kotlin-shopping-cart.git
cd kotlin-shopping-cart - Open the project in IntelliJ (File > Open).
Open Main.kt in src/shoppingCart/. You’ll find 5 functions with TODO(...) calls. For each function, replace the TODO(...) code with your logic:
-
createStoreInventory()
Returns aMutableMap<Int, MutableMap<String, Any>>with exactly 5 items (IDs 1..5).
Each product must have"name","price", and"stock"keys. -
addToCart(...)
Checks if the product ID exists and if the requested quantity ≤ current stock.
If valid, reduce the stock and increase the cart’s quantity.
Returns true on success, false otherwise. -
removeFromCart(...)
Checks if the cart has enough of the product to remove.
If valid, remove items from the cart and restore them to the store stock.
Returns true on success, false otherwise. -
calculateTotal(...)
Sums the cost of items in the cart by (price * quantity). -
filterProductsByName(...)
Returns a list of product IDs whose"name"contains a given keyword.
May be case-insensitive, depending on the tests.
- Locate the
testsfolder in your project tool window. - Right-click the folder or an individual
...Tests.ktfile. - Select “Run Tests” or “Run 'AddToCartTests'”, etc.
Initially, the tests will fail due to the TODO(...) lines. As you implement each function, re-run the tests until they pass.
Once tests pass (or partially pass):
git add .
git commit -m "Implement functions"
git push origin main
Adjust if you’re on a different branch.
- Go to your fork on GitHub.
- You’ll see a button or prompt to create a Pull Request (PR) to the original repository.
- Provide a short description of your changes. The repository owners or instructors can then review your work.
- NotImplementedError: Each function currently calls
TODO(...), which throwsNotImplementedError. Remove that and implement real logic. - Test Failures: Read the error messages carefully — they usually explain what was expected vs. what you returned.
- Don’t Modify Tests: The tests guide you on what’s needed. Keep them as is.
Good luck and have fun coding!