File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ abstract class DeliveryItem {
2+ items : DeliveryItem [ ]
3+
4+ addItem ( item : DeliveryItem ) {
5+ this . items . push ( item )
6+ }
7+
8+ getItemPrices ( ) : number {
9+ return this . items . reduce ( ( acc : number , i : DeliveryItem ) => acc += i . getItemPrices ( ) , 0 )
10+ }
11+
12+ abstract getPrice ( ) : number
13+ }
14+
15+ class DeliveryShop extends DeliveryItem {
16+ constructor ( private delivaryFee : number ) {
17+ super ( )
18+ }
19+
20+ getPrice ( ) : number {
21+ return this . items . reduce ( ( acc : number , i : DeliveryItem ) => acc += i . getItemPrices ( ) , 0 ) + this . delivaryFee
22+ }
23+ }
24+
25+ class Package extends DeliveryItem { s
26+ getPrice ( ) : number {
27+ return this . getItemPrices ( )
28+ }
29+ }
30+
31+ class Product extends DeliveryItem {
32+ constructor ( private price : number ) {
33+ super ( )
34+ }
35+
36+ getPrice ( ) : number {
37+ return this . price
38+ }
39+ }
40+
41+ const shop = new DeliveryShop ( 100 )
42+ shop . addItem ( new Product ( 1000 ) )
43+ const pack1 = new Package ( )
44+ pack1 . addItem ( new Product ( 200 ) )
45+ pack1 . addItem ( new Product ( 300 ) )
46+ shop . addItem ( pack1 )
47+ const pack2 = new Package ( )
48+ pack2 . addItem ( new Product ( 30 ) )
You can’t perform that action at this time.
0 commit comments