Tags | loops functions foundations data structures |
Your directory structure should look like this:
└── src
└──shopping_cart.js
In Javascript it is best practice to use destructuring when passing arguments to a function. You can learn about destructuring here: destructuring.
Please make sure you use destructuring for all your functions.
Let’s say you have a function called getCustomerBaskets that takes an email address and data-structure containing all the shopping baskets as input.
// DON'T DEFINE YOUR FUNCTION LIKE THIS
function getCustomerBaskets(shoppingBaskets, "sine@umuzi.org"){...}
// DON'T CALL YOUR FUNCTION LIKE THIS
getCustomerBaskets("sine@umuzi.org", shoppingBaskets);
// The problem with the above code is that if you get the order of the parameters wrong, then things will break. Your functions should work like this instead:
// DEFINE YOUR FUNCTION LIKE THIS
function getCustomerBaskets({ shoppingBaskets, email }) {...}
// YOU CAN CALL IT LIKE THIS
getCustomerBaskets({shoppingBaskets, email: "sine@umuzi.org"}) //DO THIS
Make sure you export all your functions using the following format:
module.exports = {
YOUR_FIRST_FUNCTION_NAME,
YOUR_SECOND_FUNCTION_NAME,...
};
Please make use of Gradle from the command line to set up your project. You can learn more about Gradle here:
[TODO] TOPIC: African Coding Network SyllabusWhen you use gradle to create your project, give your project the following name: shopping_cart
Your directory structure should look like this:
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── app
├── build.gradle
└── src
└── main
└── java
└── shopping_cart
└── ShoppingCartCalculations.java <------ names are important
If the instructions tell you to return a list or array then the correct thing to return is a List
.
Your directory structure should look like this:
└── shopping_cart
└── shopping_cart.py
A lot of new programmers don’t know why understanding is important. They think they can just copy code from people and that’s enough to get by. BUT as soon as you get your first job you’ll need to solve some serious problems on your own.
The problems you’ll be solving in this project are designed to be realistic. As a professional, you’ll likely need to do things similar to this. Please make sure you 100% understand!
Imagine you are working for a hot new online shopping startup. You’ve seen online shopping, right? Go poke around on Takealot or Amazon if you haven’t.
On the online shopping site, users (people who want to buy stuff) navigate around and then add items to their shopping baskets. Once they are happy with their baskets, then they “checkout”, pay for their goodies, and await their delivery.
As a techie, data will be a part of your life. You’re going to need to learn how to make this stuff dance.
For data scientists and data engineers, it’s pretty obvious why this is the case. But seriously, for any kind of developer, it’s friggin’ important.
Generally, developers need to work on programs that talk to other programs. And programs talk to each other by passing data structures back and forth.
Take a look at this to see how you can see Tilde’s data structures:
Ideally, you will be able to build stuff at least as complicated as that. But we’re going to start with some simpler things.
First of all, please download the data in this file. You can use it for testing out your functions.
If you look at the data you’ll see that what we have is a list or array of elements. Each element represents a single shopping basket.
Each shopping basket has a few different pieces:
Each item in the shopping basket has a few properties of its own:
For example, if there is an item in the shopping cart that looks like this:
{"name": "hamster", "quantity": 2, "price": 20}
Then it means that the person is buying 2 hamsters at a price of R20 each. So that’s R40 in total.
Please note that one person can have multiple baskets. If you look at the data, you’ll see that tshepo@umuzi.org has 4 baskets. Two have been delivered, one has been paid for (so he’s awaiting delivery) and one is open (so he’s setting up his next order).
JSON is a lovely data format because it is easy for humans to read, and machines like it too.
If you are working in Python then you can turn the JSON file into a data structure in your code by making use of the json package.
In JavaScript you can simply require
a JSON file and then it’ll get converted into the appropriate data-structure.
In Java you can turn the JSON file into a data structure in your code by making use of json for java, here is a tutorial
Write a function called get customer baskets
that takes in two arguments:
The function must return a list/array of all the shopping baskets that belong to the customer with that email address.
If the customer has no shopping baskets then return an empty list/array.
Different languages have different requirements:
getCustomerBaskets({ email, shoppingBaskets }); // javascript. This should return an array
getCustomerBaskets(email, shoppingBaskets); // java. This should return a List
get_customer_baskets(email, shopping_baskets) // python. This should return a list
This kind of data should be returned if someone@umuzi.org
has only one basket.
[
{
email: 'someone@umuzi.org',
status: 'PAID',
items: [{
"name": "hamster",
"quantity": 2,
"price": 20
}]
}
]
Write a function called get all customers
, the function should take the data array as an argument and should return a list of sorted customer email addresses. The list must have no duplicates.
e.g.
getAllCustomers({ shoppingBaskets }); //javascript
getAllCustomers(shoppingBaskets); //java
get_all_customers(shopping_baskets) // python
Write a function called get required stock
, your function should take the data array as an argument and should return all the items that need to be sent out for delivery.
You need to return data in the correct format. Just include the names and quantities of the items.
e.g.
getRequiredStock({ shoppingBaskets }); //javascript
getRequiredStock(shoppingBaskets); //java
get_required_stock(shopping_baskets) //python
For example, if one customer paid for 2 hamsters and another customer paid for one hamster and a bag of sawdust then your function should return the following data structure:
[
{"name": "hamster", "quantity": 3},
{"name": "bag of sawdust", "quantity": 1}
]
Write a function called get total spent
that takes an email address as an argument and the data array.
The function must return the total amount that the customer has spent up until this time.
e.g.
getTotalSpent({ email, shoppingBaskets }); //javascript
getTotalSpent(email, shoppingBaskets); //java
get_total_spent(email, shopping_baskets) //python
Note that if a basket has been delivered then it has been paid for.
Write a function called get top customers
that takes the data array as an argument and returns a list/array of all the customers. The result should be ordered according to the total amount spent.
The returned data structure should be an array/list of dictionaries/objects showing the email addresses and the total amounts spent per customer.
e.g.
getTopCustomers({ shoppingBaskets }); // javascript
getTopCustomers(shoppingBaskets); // java
get_top_customers(shopping_baskets) //python
Make sure the returned value matches the following structure:
[
{"email": "tshepo@umuzi.org", "total": 1023.10},
{"email": "maru@email.com", "total": 950},
... etc
]
Hint: You have already defined some functions that would be useful in finding this result. Call those functions.
Write a function called get customers with open baskets
that takes in the data array as an argument and returns a sorted list/array of email addresses for customers who have baskets that are open.
e.g.
getCustomersWithOpenBaskets({ shoppingBaskets }); //javascript
getCustomersWithOpenBaskets(shoppingBaskets); //java
get_customers_with_open_baskets(shopping_baskets) //python
const DELIVERED="DELIVERED"
.