-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy path1-13-23.js
123 lines (106 loc) · 2.89 KB
/
1-13-23.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
Designing an Object-Oriented System
1. What are the key objects that exist in this system?
- What does the object know about itself?
- What can the object do?
2. How do the objects in your system interact?
3. What inheritance exists (if any)? Are there any "is a" relationships?
Designing Amazon Shopping Experience
- Design at least 2 (ideally 3) classes
class ClassName:
- Properties:
- prop1: dataType
- prop2: dataType
- Methods:
- method1(param): description
- method2(param): description
User
- Properties
- login
- logout
- status
- orderHistory
- cart: Cart
- Methods:
- getRecommendations
PrimeUser extends User
GuestUser extends User
Cart
- Properties
- items: array
- wishlist: array
- Methods:
- addItem(item, quantity)
- removeItem(item, quantity)
- checkout
Item
- Properties
- name
- price
- Methods:
Inventory
- Properties
- items: object
- Methods
- getItem(itemName)
- returnItem(Item)
this.items = {
banana: [Item, Item, Item, Item],
battery: [Item, Item, Item],
macbookCharger: [Item, Item, Item],
PS5: [Item, Item, Item, Item]
}
class ClassName {
constructor(prop) {
this.prop = prop;
}
method() {
console.log(this.prop)
}
}
*/
class Item {
constructor(name, price) {
this.name = name;
this.price = price;
}
toString() { // method override
return `${this.name} costs ${this.price}`
}
}
const banana = new Item("banana", 0.75);
const PS5 = new Item("Playstation 5", 500);
const battery = new Item("AAA Battery", .5);
const macbookCharger = new Item("Macbook Charger", 50);
class Inventory {
// itemQuantities is an object of objects
constructor(itemQuantities) {
// This will be an object of arrays
this.items = {}
// Each key in itemQuantities is an itemName
// Each value is an object: { quantity, price }
for (const itemName in itemQuantities) {
// make an array for each itemName
this.items[itemName] = []
// pull out the quantity and price from the value
const { quantity, price } = itemQuantities[itemName];
// based on the quantity, make that many new Items
for (let i = 0; i < quantity; i++) {
const newItem = new Item(itemName, price);
this.items[itemName].push(newItem)
}
}
}
getItem() {}
returnItem() {}
}
// Test Driven Development
// Write the tests first -> build implementation to pass the tests
const itemsToAddToInventory = {
banana: {quantity: 4, price: .75},
battery: {quantity: 3, price: .5},
macbookCharger: { quantity: 3, price: 50 },
PS5: {quantity: 2, price: 500}
}
const inventory = new Inventory(itemsToAddToInventory);
console.log(inventory);