Skip to content

Commit 87f7528

Browse files
authored
Merge pull request mouredev#7046 from victor-Casta/[email protected]
#12 - TypeScript
2 parents 6e1304b + 6fb5710 commit 87f7528

File tree

2 files changed

+218
-0
lines changed

2 files changed

+218
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
4+
/*
5+
* EJERCICIO:
6+
* Desarrolla un programa capaz de crear un archivo XML y JSON que guarde los
7+
* siguientes datos (haciendo uso de la sintaxis correcta en cada caso):
8+
* - Nombre
9+
* - Edad
10+
* - Fecha de nacimiento
11+
* - Listado de lenguajes de programación
12+
* Muestra el contenido de los archivos.
13+
* Borra los archivos.
14+
*/
15+
16+
async function saveData(): Promise<void> {
17+
const JSONdata = {
18+
name: 'Victor',
19+
age: 21,
20+
dateOfBirth: new Date(2002, 11, 17).toDateString(),
21+
programmingLanguages: ['JavaScript', 'TypeScript', 'Python', 'PHP']
22+
}
23+
24+
const xmlData = `
25+
<root>
26+
<name>Victor</name>
27+
<age>21</age>
28+
<dateOfBirth>2002-11-17</dateOfBirth>
29+
<programmingLanguages>
30+
<language>JavaScript</language>
31+
<language>TypeScript</language>
32+
<language>Python</language>
33+
<language>PHP</language>
34+
</programmingLanguages>
35+
</root>`
36+
37+
38+
await fs.writeFileSync('data.json', JSON.stringify(JSONdata))
39+
fs.writeFileSync('data.xml', xmlData, 'utf8')
40+
console.log(fs.readFileSync('data.xml', 'utf8'))
41+
console.log(fs.readFileSync('data.json', 'utf8'))
42+
// fs.unlinkSync('data.json')
43+
// fs.unlinkSync('data.xml')
44+
}
45+
46+
saveData()
47+
48+
/*
49+
* DIFICULTAD EXTRA (opcional):
50+
* Utilizando la lógica de creación de los archivos anteriores, crea un
51+
* programa capaz de leer y transformar en una misma clase custom de tu
52+
* lenguaje los datos almacenados en el XML y el JSON.
53+
* Borra los archivos.
54+
*/
55+
56+
class customData {
57+
file: string
58+
constructor(file: string) {
59+
this.file = file
60+
}
61+
62+
async readData(): Promise<any> {
63+
if (path.extname(this.file) === '.json') {
64+
return JSON.parse(fs.readFileSync(this.file, 'utf8'))
65+
}
66+
}
67+
68+
async updateData(): Promise<any> {
69+
if (path.extname(this.file) === '.xml') {
70+
return fs.readFileSync('data.xml', 'utf8')
71+
}
72+
}
73+
74+
async deleteFile(): Promise<void> {
75+
fs.unlinkSync(this.file)
76+
}
77+
}
78+
79+
const dataJson = new customData('data.json')
80+
console.log(dataJson.readData())
81+
82+
const dataXml = new customData('data.xml')
83+
console.log(dataXml.updateData())
84+
85+
dataJson.deleteFile()
86+
dataXml.deleteFile()
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* EJERCICIO:
3+
* Explora el "Principio SOLID de Sustitución de Liskov (Liskov Substitution Principle, LSP)"
4+
* y crea un ejemplo simple donde se muestre su funcionamiento
5+
* de forma correcta e incorrecta.
6+
*/
7+
8+
class Worker {
9+
work() {
10+
return 'Realizando tareas generales'
11+
}
12+
}
13+
14+
class Developer extends Worker {
15+
work() {
16+
return 'Escribiendo código'
17+
}
18+
}
19+
20+
class Designer extends Worker {
21+
work() {
22+
return 'Creando diseños'
23+
}
24+
}
25+
26+
let worker = new Worker()
27+
console.log(worker.work())
28+
29+
let developer = new Developer()
30+
console.log(developer.work())
31+
32+
let designer = new Designer()
33+
console.log(designer.work())
34+
35+
36+
worker = new Developer()
37+
console.log(worker.work())
38+
39+
worker = new Designer()
40+
console.log(worker.work())
41+
42+
43+
/*
44+
* DIFICULTAD EXTRA (opcional):
45+
* Crea una jerarquía de vehículos. Todos ellos deben poder acelerar y frenar, así como
46+
* cumplir el LSP.
47+
* Instrucciones:
48+
* 1. Crea la clase Vehículo.
49+
* 2. Añade tres subclases de Vehículo.
50+
* 3. Implementa las operaciones "acelerar" y "frenar" como corresponda.
51+
* 4. Desarrolla un código que compruebe que se cumple el LSP.
52+
*/
53+
console.log('--Extra--')
54+
55+
class Vehicle {
56+
speed = 0
57+
constructor(speed) {
58+
this.speed = speed
59+
}
60+
61+
speedUp() {
62+
this.speed += 1
63+
return `Velocidad: ${this.speed}`
64+
}
65+
66+
slowDown() {
67+
if (this.speed > 0) {
68+
this.speed -= 1
69+
return `Velocidad: ${this.speed}`
70+
} else {
71+
this.speed = 0
72+
return `Velocidad: ${this.speed}`
73+
}
74+
}
75+
}
76+
77+
class Car extends Vehicle {
78+
speedUp() {
79+
this.speed += 4
80+
return `Velocidad: ${this.speed}`
81+
}
82+
}
83+
84+
class Plane extends Vehicle {
85+
speedUp() {
86+
this.speed += 10
87+
return `Velocidad: ${this.speed}`
88+
}
89+
90+
slowDown() {
91+
if (this.speed > 0) {
92+
this.speed -= 5
93+
return `Velocidad: ${this.speed}`
94+
} else {
95+
return `El avión ya está detenido`
96+
}
97+
}
98+
}
99+
100+
101+
class Bus extends Vehicle {
102+
speedUp() {
103+
this.speed += 3
104+
return `Velocidad: ${this.speed}`
105+
}
106+
}
107+
108+
let vehicle = new Vehicle(0)
109+
110+
let car = new Car(10)
111+
console.log(car.speedUp())
112+
console.log(car.slowDown())
113+
114+
let plane = new Plane(50)
115+
console.log(plane.speedUp())
116+
console.log(plane.slowDown())
117+
118+
let bus = new Bus(20)
119+
console.log(bus.speedUp())
120+
console.log(bus.slowDown())
121+
122+
vehicle = new Car(0)
123+
console.log(vehicle.speedUp())
124+
console.log(vehicle.slowDown())
125+
126+
vehicle = new Plane(0)
127+
console.log(vehicle.speedUp())
128+
console.log(vehicle.slowDown())
129+
130+
vehicle = new Bus(0)
131+
console.log(vehicle.speedUp())
132+
console.log(vehicle.slowDown())

0 commit comments

Comments
 (0)