-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy path1-17-23.js
161 lines (132 loc) · 3.77 KB
/
1-17-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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
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
- username
- status
- orderHistory
- cart: Cart
- Methods:
- login
- logout
- getRecommendations
PrimeUser extends User
GuestUser extends User
Cart
- Properties
- items: array
- wishlist: array
- Methods:
- addItem(item, quantity)
- removeItem(item, quantity)
- checkout
cart.items = [ Item {name: 'banana', price; 0.5}, Item, Item]
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 Cart {
constructor() {
this.items = [];
}
// user will call this method, passing in the itemName
addToCart(item) {
this.items.push(item)
}
}
class Item {
constructor(name, price) {
this.name = name;
this.price = price;
this.serialCode = Math.random();
}
toString() { // method override
return `${this.name} costs ${this.price}`
}
}
class Inventory {
constructor(itemQuantities) {
this.items = {}
for (const itemName in itemQuantities) {
this.items[itemName] = []
const { quantity, price } = itemQuantities[itemName];
// const itemInfo = itemQuantities[itemName];
// const quantity = itemInfo.quantity;
// const price = itemInfo.price
for (let i = 0; i < quantity; i++) {
const newItem = new Item(itemName, price);
this.items[itemName].push(newItem)
}
}
}
getItem(itemName) {
// itemsArray = [Item, Item, Item]
const itemsArray = this.items[itemName]
const itemToGet = itemsArray.pop();
return itemToGet;
}
returnItem() {
}
}
class User {
constructor(username, email, address, isPrime) {
this.username = username;
this.email = email;
this.address = address;
this.isPrime = isPrime;
this.cart = new Cart()
}
}
// Test Driven Development
// Write the tests first -> build implementation to pass the tests
const itemsToAddToInventory = {
banana: {quantity: 4, price: .75}, // => this.items.banana = []
battery: {quantity: 3, price: .5}, // => this.items.battery = []
macbookCharger: { quantity: 3, price: 50 }, // => this.items.macbookCharger = []
PS5: {quantity: 2, price: 500} // => this.items.PS5 = []
}
const inventory = new Inventory(itemsToAddToInventory);
console.log(inventory);
const staceyann = new User("staceykg", "staceykg@gmail.com", "100 main st, Brooklyn, NY", true)
staceyann.cart.addToCart(inventory.getItem("banana"));
staceyann.cart.addToCart(inventory.getItem("PS5"));
const ben = new User("bspex", "ben@gmail.com", "100 main st, Brooklyn, NY", true)
ben.cart.addToCart(inventory.getItem("battery"));
ben.cart.addToCart(inventory.getItem("PS5"));
// console.log(ben, staceyann, inventory);