Class: Plato_2

Inherits:
Plato show all
Defined in:
lib/practica/plato_herencia.rb

Overview

Clase Plato_2 que hereda de Plato

Direct Known Subclasses

Plato_3

Instance Attribute Summary collapse

Attributes inherited from Plato

#calorias, #cantidad, #carbohidratos, #lipidos, #lista, #nombre, #p_carbohidratos, #p_lipidos, #p_proteinas, #proteinas

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Plato

#calc_calorias, #calc_carbohidrato, #calc_lipido, #calc_porcentajes, #calc_proteina, #calcular_equivalente, #get_this

Constructor Details

#initialize(lista, cantidad, nombre) ⇒ Plato_2

constructor que recibe dos listas, de cantidad y de alientos, ademas del nombre del plato



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/practica/plato_herencia.rb', line 14

def initialize (lista, cantidad,nombre) #constructor que recibe dos listas, de cantidad y de alientos, ademas del nombre del plato
	@gei = @terreno = @eficiencia = 0
	super(lista,cantidad,nombre)		#instanciamos la clase padre con los parametros dados		
	self.calc_gei
	self.calc_terreno
	self.calc_eficiencia

	#creamos diferentes metodos lambda 
	@Huella = ->{ #para  calcular solamente el INDICE nutricional 

		(@Indice_energia.call + @Indice_Carbono.call)/2
	}

	@Indice_energia = -> { 	# Para calcular el indice de Energia 
		if @calorias<670 then
			1
		elsif @calorias >= 670 && @calorias <= 830 then
			 2
		else
			 3
		end
	}
	@Indice_Carbono = -> { 	#para calcular el indice de Carbono
		if @gei < 800  then
			1
		elsif @gei >= 800 && @gei <= 1200 then

			2
		else
			3
		end
	}
	@Indice_huella = ->(huella){ 	#para obtener la huella segun el indice de la misma
		if huella == 1 then
			"Baja"
		elsif huella == 2 then
			"Regular"
		else
			"Alta"
		end

	}


end

Instance Attribute Details

#eficienciaObject (readonly)

Returns the value of attribute eficiencia.



3
4
5
# File 'lib/practica/plato_herencia.rb', line 3

def eficiencia
  @eficiencia
end

#geiObject (readonly)

Returns the value of attribute gei.



3
4
5
# File 'lib/practica/plato_herencia.rb', line 3

def gei
  @gei
end

#terrenoObject (readonly)

Returns the value of attribute terreno.



3
4
5
# File 'lib/practica/plato_herencia.rb', line 3

def terreno
  @terreno
end

Class Method Details

.huella(valor) ⇒ Object



77
78
79
# File 'lib/practica/plato_herencia.rb', line 77

def self.huella(valor)
	@Indice_huella.call(valor)
end

Instance Method Details

#+(other) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/practica/plato_herencia.rb', line 117

def +(other)

	@aux_list=lista
	@aux_list.insertar_list(other.lista)
			
	@aux_cantidad=cantidad
	@aux_cantidad.insertar_list(other.cantidad)

	@Nuevo_plato= Plato_2.new(@aux_list,@aux_cantidad, self.nombre)
end

#<=>(other) ⇒ Object

operador <=>



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/practica/plato_herencia.rb', line 128

def <=> (other) 		# operador <=>
	
	if other.instance_of? Plato_2 	# para una instancia de Plato_2
	
	self.get_huella <=> other.get_huella # lo hacemos segun la huella nutricional
	elsif other.is_a? Plato  		# en caso de que sea una instancia de la clase Plato
		self.calorias <=> other.calorias	#lo hacemos segun las calorias totales de ambos objetos
	else
		return nil
	end

end

#==(other) ⇒ Object

operador de comparacion



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/practica/plato_herencia.rb', line 100

def == (other)		#operador de comparacion
	if other.instance_of? Plato_2 #en caso de que el objeto a comparar sea una instancia de Plato_2
	
	self.to_s == other.to_s		#comparamos ambos to_s
	elsif other.is_a? Plato  #en caso de que sea una instancia de Plato
		s = ""
		s<< super.to_s

		return (s == other.to_s) 	#comnparamos segun el to_s de la clase super

	else
		
		false
	end

end

#calc_eficienciaObject

metodo que calcula la eficiencia energetica del plato



91
92
93
# File 'lib/practica/plato_herencia.rb', line 91

def calc_eficiencia		# metodo que calcula la eficiencia energetica del plato
	@eficiencia= ((@calorias / @gei )+ @terreno).round(2)
end

#calc_geiObject

calculamos el GEI total del plato



60
61
62
63
64
65
66
67
68
# File 'lib/practica/plato_herencia.rb', line 60

def calc_gei		#calculamos el GEI total del plato
	contador=0
	while( @lista[contador] != nil && @cantidad[contador] != nil)  do
		
		@gei=@gei + calcular_equivalente(@lista[contador].get_valor.gei , @cantidad[contador].get_valor) 
		contador = contador +1
	end
	
end

#Calc_huella_nutriObject

metodo que llama a lambda para obtener la huella nutricional



73
74
75
76
# File 'lib/practica/plato_herencia.rb', line 73

def Calc_huella_nutri	#metodo que llama a lambda para obtener la huella nutricional
	
	return @Indice_huella.call((@Indice_energia.call + @Indice_Carbono.call)/2)
end

#calc_terrenoObject

metodo que obtiene el terreno total del plato



81
82
83
84
85
86
87
88
89
# File 'lib/practica/plato_herencia.rb', line 81

def calc_terreno		# metodo que obtiene el terreno total del plato
	contador=0
	while( @lista[contador] != nil && @cantidad[contador] != nil)  do
		
		@terreno=@terreno + calcular_equivalente(@lista[contador].get_valor.terreno , @cantidad[contador].get_valor)
		contador = contador +1
	end
	
end

#get_huellaObject

metodo que llama al lambda para obtener el indice de la huella nutricional



69
70
71
72
# File 'lib/practica/plato_herencia.rb', line 69

def get_huella	#metodo que llama al lambda para obtener el indice de la huella nutricional

	@Huella.call
end

#to_sObject

metodo que da formato a al plato en un string



95
96
97
98
99
# File 'lib/practica/plato_herencia.rb', line 95

def to_s		#metodo que da formato a al plato en un string
	s =""
	s << super.to_s		# llamamos al to_s de la clase super
	s = s+",#{@gei},#{@terreno}"
end