In this exercise we're going to design the objects for an on-demand ice cream delivery platform called Bareed.
The objects have the following properties and methods:
-
Point
:x
- x coordinate.y
- y coordinate.distanceTo(point)
- takes aPoint
, calculates the distance to that point from the current point.
-
Wallet
:money
- defaults to 0.credit(amount)
- addsamount
tomoney
.debit(amount)
- subtractsamount
frommoney
.
-
Person
:name
location
- aPoint
.wallet
- aWallet
initially with 0.moveTo(point)
- moves the person to the newpoint
.
-
Vendor
:name
location
- aPoint
.wallet
- aWallet
initially with 0.range
- the maximum distance this vendor can travel - initially 5.price
- the cost of a single ice cream - initially 1.moveTo(point)
- moves the customer to the newpoint
.sellTo(customer, numberOfIceCreams)
- sells a specific number of ice creams tocustomer
by doing the following:- Move to the
customer
'slocation
. - Transfer money from the
customer
'swallet
to the vendor'swallet
.
NOTE: You might have to partially build theCustomer
class first before you can get this to work!
- Move to the
-
Customer
:name
location
- aPoint
.wallet
- aWallet
initially with 10.moveTo(point)
- moves the customer to the newpoint
._isInRange(vendor)
- checks if the customer is in range ofvendor
._haveEnoughMoney(vendor, numberOfIceCreams)
- checks if the customer has enough money to buy a specific number of ice creams fromvendor
.requestIceCream(vendor, numberOfIceCreams)
- sends a request tovendor
to buy a specific number of ice creams but ONLY IF the customer is in the vendor's range AND they have enough money to buy that much ice cream. ONLY THEN is a request sent.
BONUS: Log a helpful message to let the customer know why they can't have ice cream.
NOTE: You might have to partially build theVendor
class first before you can get this to work!
let vendorAsis = new Vendor("Asis", 10, 10); // create a new vendor named Asis at location (10,10)
let nearbyCustomer = new Customer("MishMish", 11, 11); // create a new customer named MishMish at location (11,11)
let distantCustomer = new Customer("Hamsa", 1000, 1000); // create a new customer named Hamsa at location (1000,1000)
let brokeCustomer = new Customer("Maskeen", 12, 12); // create a new customer named Maskeen at location (12,12)
brokeCustomer.wallet.money = 0; // steal all of Maskeen's money
nearbyCustomer.requestIceCream(vendorAsis, 10); // ask to buy 10 ice creams from Asis
// money was transferred from MishMish to Asis
nearbyCustomer.wallet.money; // 0 left
vendorAsis.wallet.money; // 10
// Asis moved to MishMish's location
vendorAsis.location; // { x: 11, y: 11 }
distantCustomer.requestIceCream(vendorAsis, 10); // ask to buy 10 ice creams from Asis
// no money was transferred because the request failed - Hamsa is too far away
distantCustomer.wallet.money; // 10 left
vendorAsis.wallet.money; // still only 10
// Asis didn't move
vendorAsis.location; // { x: 11, y: 11 }
brokeCustomer.requestIceCream(vendorAsis, 1); // ask to buy 1 ice creams from Asis
// no money was transferred because the request failed - Maskeen doesn't have enough money to buy even one ice cream :(
brokeCustomer.wallet.money; // 0
vendorAsis.wallet.money; // still only 10
// Asis didn't move
vendorAsis.location; // { x: 11, y: 11 }
- Install
node
:$ brew install node
- Install
yarn
:$ brew install yarn --without-node
Fork this repository and clone your fork (make sure you clone it into your development
directory):
$ git clone https://github.com/<your_username>/JSFoundations-Classes.git
Inside you'll find the following:
bareed.js
- this is the file you'll be editingtests/
- this is a directory of testing files. These files check that your application has the features discussed above.
DO NOT EDIT THIS FILE
Install all the requirements:
- Navigate to the project root (you'll find a file called
package.json
there). - Install the requirements using
yarn install
.
Run the tests:
$ yarn test
This command will run the testing file and test your pairs()
function to make sure it has all the required features.
You'll know when you're done when your code passes all the tests.