File tree Expand file tree Collapse file tree 1 file changed +101
-0
lines changed
Roadmap/08 - CLASES/typescript Expand file tree Collapse file tree 1 file changed +101
-0
lines changed Original file line number Diff line number Diff line change
1
+ // * EdiedRamos
2
+
3
+ // * Exercise
4
+
5
+ class Car {
6
+ constructor (
7
+ private brand : string ,
8
+ private doors : number ,
9
+ private color : string
10
+ ) { }
11
+
12
+ set setBrand ( brand : string ) {
13
+ this . brand = brand ;
14
+ }
15
+
16
+ set setDoors ( doors : number ) {
17
+ this . doors = doors ;
18
+ }
19
+
20
+ set setColor ( color : string ) {
21
+ this . color = color ;
22
+ }
23
+
24
+ get toString ( ) : string {
25
+ return `Marca: ${ this . brand } \nPuertas: ${ this . doors } \nColor: ${ this . color } ` ;
26
+ }
27
+ }
28
+
29
+ // * Extra exercise
30
+
31
+ class Stack < T > {
32
+ constructor ( private data : T [ ] ) { }
33
+
34
+ push ( item : T ) {
35
+ this . data . unshift ( item ) ;
36
+ }
37
+
38
+ pop ( ) {
39
+ if ( ! this . data . length ) throw new Error ( "La pila está vacía" ) ;
40
+ this . data . shift ( ) ;
41
+ }
42
+
43
+ size ( ) {
44
+ return this . data . length ;
45
+ }
46
+
47
+ show ( ) {
48
+ if ( ! this . data . length ) throw new Error ( "La pila está vacía" ) ;
49
+ console . log ( `[${ this . data . join ( "," ) } ]` ) ;
50
+ }
51
+ }
52
+
53
+ class Queue < T > {
54
+ constructor ( private data : T [ ] ) { }
55
+
56
+ push ( item : T ) {
57
+ this . data . push ( item ) ;
58
+ }
59
+
60
+ pop ( ) {
61
+ if ( ! this . data . length ) throw new Error ( "La cola está vacía" ) ;
62
+ this . data . pop ( ) ;
63
+ }
64
+
65
+ size ( ) {
66
+ return this . data . length ;
67
+ }
68
+
69
+ show ( ) {
70
+ if ( ! this . data . length ) throw new Error ( "La cola está vacía" ) ;
71
+ console . log ( `[${ this . data . join ( "," ) } ]` ) ;
72
+ }
73
+ }
74
+
75
+ // * Main
76
+ ( ( ) => {
77
+ // * First exercise
78
+ const car = new Car ( "Porshe" , 2 , "black" ) ;
79
+ console . log ( car . toString ) ;
80
+ car . setBrand = "Mazda" ;
81
+ car . setDoors = 4 ;
82
+ car . setColor = "red" ;
83
+ console . log ( car . toString ) ;
84
+
85
+ // * Second exercise
86
+ console . log ( "== PILA ==" ) ;
87
+ const stack = new Stack < number > ( [ 1 ] ) ;
88
+ stack . push ( 33 ) ;
89
+ stack . push ( 12 ) ;
90
+ stack . show ( ) ;
91
+ stack . pop ( ) ;
92
+ stack . show ( ) ;
93
+
94
+ console . log ( "== COLA ==" ) ;
95
+ const queue = new Queue < number > ( [ 1 ] ) ;
96
+ queue . push ( 33 ) ;
97
+ queue . push ( 12 ) ;
98
+ queue . show ( ) ;
99
+ queue . pop ( ) ;
100
+ queue . show ( ) ;
101
+ } ) ( ) ;
You can’t perform that action at this time.
0 commit comments