@@ -29,6 +29,9 @@ trait Product {
29
29
self . price ( ) - self . apply_discount ( )
30
30
}
31
31
}
32
+
33
+ //____________________________________
34
+ // Estructuras concretas
32
35
struct ElectronicsProduct {
33
36
name : String ,
34
37
price : f64 ,
@@ -42,13 +45,9 @@ impl Product for ElectronicsProduct {
42
45
}
43
46
}
44
47
45
- fn name ( & self ) -> & str {
46
- & self . name
47
- }
48
+ fn name ( & self ) -> & str { & self . name }
48
49
49
- fn price ( & self ) -> f64 {
50
- self . price
51
- }
50
+ fn price ( & self ) -> f64 { self . price }
52
51
53
52
fn apply_discount ( & self ) -> f64 {
54
53
self . price * 0.05 // 5% discount
@@ -69,13 +68,9 @@ impl Product for ClothingProduct {
69
68
}
70
69
}
71
70
72
- fn name ( & self ) -> & str {
73
- & self . name
74
- }
71
+ fn name ( & self ) -> & str { & self . name }
75
72
76
- fn price ( & self ) -> f64 {
77
- self . price
78
- }
73
+ fn price ( & self ) -> f64 { self . price }
79
74
80
75
fn apply_discount ( & self ) -> f64 {
81
76
if self . price > 50.0 {
@@ -86,16 +81,6 @@ impl Product for ClothingProduct {
86
81
}
87
82
}
88
83
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
-
99
84
/*
100
85
_______________
101
86
* EJERCICIO #2:
@@ -112,90 +97,90 @@ _______________
112
97
// Trait base
113
98
trait Calculator {
114
99
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 ) ;
126
101
}
127
102
103
+ // __________________________________________
128
104
// Estructuras concretas
129
105
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 }
134
106
135
107
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 ! ( " \n Suma:" ) ;
112
+ println ! ( "{} + {} = {}" , self . a , self . b , self . a + self . b ) ;
113
+ }
142
114
}
143
115
116
+ // __________________________________________
117
+ struct Subtraction { a : f64 , b : f64 }
118
+
144
119
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 ! ( " \n Resta:" ) ;
124
+ println ! ( "{} - {} = {}" , self . a , self . b , self . a - self . b ) ;
125
+ }
151
126
}
152
127
128
+ // __________________________________________
129
+ struct Multiplication { a : f64 , b : f64 }
130
+
153
131
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 ! ( " \n Multiplicación:" ) ;
136
+ println ! ( "{} * {} = {}" , self . a , self . b , self . a * self . b ) ;
137
+ }
160
138
}
161
139
140
+ // __________________________________________
141
+ struct Division { a : f64 , b : f64 }
142
+
162
143
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 ! ( " \n División:" ) ;
148
+ println ! ( "{} / {} = {}" , self . a , self . b , self . a / self . b ) ;
149
+ }
169
150
}
170
151
152
+ // __________________________________________
153
+ struct Pow { a : f64 , b : f64 }
154
+
171
155
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 ! ( " \n Potencia:" ) ;
160
+ println ! ( "{} ^ {} = {}" , self . a , self . b , self . a . powf ( self . b ) ) ;
161
+ }
178
162
}
179
163
180
164
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
+ }
183
173
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 ) ) ;
186
176
187
177
// _______________________________________________
188
178
// exs 2
189
179
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 ( ) ;
197
185
198
- for calc in calculators. iter ( ) {
199
- calc. print_result ( ) ;
200
- }
201
186
}
0 commit comments