Skip to content

Commit c20f2a6

Browse files
authored
Merge pull request mouredev#4961 from Kenysdev/27.rs
#27 - rust -fix
2 parents e8cfc1a + 11a283e commit c20f2a6

File tree

1 file changed

+66
-81
lines changed

1 file changed

+66
-81
lines changed

Roadmap/27 - SOLID OCP/rust/kenysdev.rs

Lines changed: 66 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ trait Product {
2929
self.price() - self.apply_discount()
3030
}
3131
}
32+
33+
//____________________________________
34+
// Estructuras concretas
3235
struct ElectronicsProduct {
3336
name: String,
3437
price: f64,
@@ -42,13 +45,9 @@ impl Product for ElectronicsProduct {
4245
}
4346
}
4447

45-
fn name(&self) -> &str {
46-
&self.name
47-
}
48+
fn name(&self) -> &str {&self.name}
4849

49-
fn price(&self) -> f64 {
50-
self.price
51-
}
50+
fn price(&self) -> f64 {self.price}
5251

5352
fn apply_discount(&self) -> f64 {
5453
self.price * 0.05 // 5% discount
@@ -69,13 +68,9 @@ impl Product for ClothingProduct {
6968
}
7069
}
7170

72-
fn name(&self) -> &str {
73-
&self.name
74-
}
71+
fn name(&self) -> &str {&self.name}
7572

76-
fn price(&self) -> f64 {
77-
self.price
78-
}
73+
fn price(&self) -> f64 {self.price}
7974

8075
fn apply_discount(&self) -> f64 {
8176
if self.price > 50.0 {
@@ -86,16 +81,6 @@ impl Product for ClothingProduct {
8681
}
8782
}
8883

89-
90-
//____________________________________
91-
fn process_product<T: Product>(product: T) {
92-
println!(
93-
"Producto: {}, Precio final: {}",
94-
product.name(),
95-
product.final_price()
96-
);
97-
}
98-
9984
/*
10085
_______________
10186
* EJERCICIO #2:
@@ -112,90 +97,90 @@ _______________
11297
// Trait base
11398
trait Calculator {
11499
fn new(a: f64, b: f64) -> Self where Self: Sized;
115-
fn math_operation(&self) -> f64;
116-
fn a(&self) -> f64;
117-
fn b(&self) -> f64;
118-
fn operation_name(&self) -> &'static str;
119-
fn operation_symbol(&self) -> &'static str;
120-
121-
fn print_result(&self) {
122-
println!("\n{} de {} {} {}:",
123-
self.operation_name(), self.a(), self.operation_symbol(), self.b());
124-
println!("Es: {}", self.math_operation());
125-
}
100+
fn operation_result(&self);
126101
}
127102

103+
// __________________________________________
128104
// Estructuras concretas
129105
struct Sum { a: f64, b: f64 }
130-
struct Subtraction { a: f64, b: f64 }
131-
struct Multiplication { a: f64, b: f64 }
132-
struct Division { a: f64, b: f64 }
133-
struct Pow { a: f64, b: f64 }
134106

135107
impl Calculator for Sum {
136-
fn new(a: f64, b: f64) -> Self { Self { a, b } }
137-
fn a(&self) -> f64 { self.a }
138-
fn b(&self) -> f64 { self.b }
139-
fn operation_name(&self) -> &'static str { "Suma" }
140-
fn operation_symbol(&self) -> &'static str { "+" }
141-
fn math_operation(&self) -> f64 { self.a + self.b }
108+
fn new(a: f64, b: f64) -> Sum {Sum { a, b }}
109+
110+
fn operation_result(&self) {
111+
println!("\nSuma:");
112+
println!("{} + {} = {}", self.a, self.b, self.a + self.b);
113+
}
142114
}
143115

116+
// __________________________________________
117+
struct Subtraction { a: f64, b: f64 }
118+
144119
impl Calculator for Subtraction {
145-
fn new(a: f64, b: f64) -> Self { Self { a, b } }
146-
fn a(&self) -> f64 { self.a }
147-
fn b(&self) -> f64 { self.b }
148-
fn operation_name(&self) -> &'static str { "Resta" }
149-
fn operation_symbol(&self) -> &'static str { "-" }
150-
fn math_operation(&self) -> f64 { self.a - self.b }
120+
fn new(a: f64, b: f64) -> Subtraction {Subtraction { a, b }}
121+
122+
fn operation_result(&self) {
123+
println!("\nResta:");
124+
println!("{} - {} = {}", self.a, self.b, self.a - self.b);
125+
}
151126
}
152127

128+
// __________________________________________
129+
struct Multiplication { a: f64, b: f64 }
130+
153131
impl Calculator for Multiplication {
154-
fn new(a: f64, b: f64) -> Self { Self { a, b } }
155-
fn a(&self) -> f64 { self.a }
156-
fn b(&self) -> f64 { self.b }
157-
fn operation_name(&self) -> &'static str { "Multiplicación" }
158-
fn operation_symbol(&self) -> &'static str { "*" }
159-
fn math_operation(&self) -> f64 { self.a * self.b }
132+
fn new(a: f64, b: f64) -> Multiplication {Multiplication { a, b }}
133+
134+
fn operation_result(&self) {
135+
println!("\nMultiplicación:");
136+
println!("{} * {} = {}", self.a, self.b, self.a * self.b);
137+
}
160138
}
161139

140+
// __________________________________________
141+
struct Division { a: f64, b: f64 }
142+
162143
impl Calculator for Division {
163-
fn new(a: f64, b: f64) -> Self { Self { a, b } }
164-
fn a(&self) -> f64 { self.a }
165-
fn b(&self) -> f64 { self.b }
166-
fn operation_name(&self) -> &'static str { "División" }
167-
fn operation_symbol(&self) -> &'static str { "/" }
168-
fn math_operation(&self) -> f64 { self.a / self.b }
144+
fn new(a: f64, b: f64) -> Division {Division { a, b }}
145+
146+
fn operation_result(&self) {
147+
println!("\nDivisión:");
148+
println!("{} / {} = {}", self.a, self.b, self.a / self.b);
149+
}
169150
}
170151

152+
// __________________________________________
153+
struct Pow { a: f64, b: f64 }
154+
171155
impl Calculator for Pow {
172-
fn new(a: f64, b: f64) -> Self { Self { a, b } }
173-
fn a(&self) -> f64 { self.a }
174-
fn b(&self) -> f64 { self.b }
175-
fn operation_name(&self) -> &'static str { "Potencia" }
176-
fn operation_symbol(&self) -> &'static str { "^" }
177-
fn math_operation(&self) -> f64 { self.a.powf(self.b) }
156+
fn new(a: f64, b: f64) -> Pow {Pow { a, b }}
157+
158+
fn operation_result(&self) {
159+
println!("\nPotencia:");
160+
println!("{} ^ {} = {}", self.a, self.b, self.a.powf(self.b));
161+
}
178162
}
179163

180164
fn main() {
181-
let laptop = ElectronicsProduct::new("Laptop", 700.0);
182-
let pants = ClothingProduct::new("Pants", 55.0);
165+
//____________________________________
166+
fn process_product<T: Product>(product: T) {
167+
println!(
168+
"Producto: {}, Precio final: {}",
169+
product.name(),
170+
product.final_price()
171+
);
172+
}
183173

184-
process_product(laptop); // Output: Producto: Laptop, Precio final: 665
185-
process_product(pants); // Output: Producto: Pants, Precio final: 45
174+
process_product(ElectronicsProduct::new("Laptop", 700.0));
175+
process_product(ClothingProduct::new("Pants", 55.0));
186176

187177
// _______________________________________________
188178
// exs 2
189179

190-
let calculators: Vec<Box<dyn Calculator>> = vec![
191-
Box::new(Sum::new(2.0, 2.0)),
192-
Box::new(Subtraction::new(2.0, 2.0)),
193-
Box::new(Multiplication::new(2.0, 2.0)),
194-
Box::new(Division::new(2.0, 2.0)),
195-
Box::new(Pow::new(2.0, 2.0)),
196-
];
180+
Sum::new(2.0, 2.0).operation_result();
181+
Subtraction::new(2.0, 2.0).operation_result();
182+
Multiplication::new(2.0, 2.0).operation_result();
183+
Division::new(2.0, 2.0).operation_result();
184+
Pow::new(2.0, 2.0).operation_result();
197185

198-
for calc in calculators.iter() {
199-
calc.print_result();
200-
}
201186
}

0 commit comments

Comments
 (0)