- Build a domain model with class relations using JavaScript iterator methods
- Use JavaScript to write query methods on our classes to answer questions about data in the store
In the following lab, we will be modeling all the models for The Daily Deli. The Daily Deli delivers daily lunches to different offices around US Cities. A customer orders a meal and it is delivered to the office of the customer's employer. So The Daily Deli relates a customer to a meal based on whether that meal was delivered to a customer. The employer of a customer is associated with the deliveries and meals ordered by its employees.
You will be modeling the following:
Customer
class:
new Customer()
— initialized with both name, and an instance of anemployer
; returns a JavaScript object that has attributes ofid
,employerId
, andname
meals()
- returns all of the meals that a customer has had delivereddeliveries()
— returns all of the deliveries that customer has receivedtotalSpent()
- returns the total amount that the customer has spent, as a function of the cost of the meals he has had delivered
Meal
class:
new Meal()
— initialized withtitle
andprice
; returns an object that has attributes oftitle
,price
, andid
deliveries()
- returns all of the deliveries that delivered the particular meal.customers()
- returns all of the customers who have had the meal delivered.byPrice()
- A class method that orders the meals by their price. Use thestatic
keyword to write a class method.
Class methods are methods that are not called on an instance of the class, but on the class itself, for example Meal.byPrice() is a class method as it is called on the
Meal
class. You can write a class method simply by preceding a method on a class with JavaScript'sstatic
keyword. You can read more about class methods in JavaScript here.
Delivery
class:
new Delivery()
— initialized withmeal
andcustomer
; returns an object that has attributes ofmealId
,customerId
, andid
meal()
- returns the meal associated with the deliverycustomer()
- returns the customer associated with the delivery
Employer
class:
new Employer()
— initialized withname
; returns an object that has attributes ofname
andid
employees()
- returns a list of customers employed by the employerdeliveries()
- returns a list of deliveries ordered by the employer's employeesmeals()
- returns a list of meals ordered by the employer's employees. The method is to not return the same meal multiple times.mealTotals()
- returns a JavaScript object displaying each respective meal id ordered by the employer's employees. The keys of the JavaScript object are the meal ids and associated with each meal id is a value. For example,employerOne.mealTotals()
returning an object of{1: 4, 2: 3}
would mean that the meal with id of 1 was ordered by employerOne's employees four times, and the meal with id of 2 was ordered by employerOne's employees three times.