Created
February 13, 2012 11:53
-
-
Save rodrigomanhaes/1816289 to your computer and use it in GitHub Desktop.
Códigos em Ruby sobre herança feitos na aula de POO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Conta | |
def initialize(nome_correntista, numero) | |
@nome_correntista, @numero = nome_correntista, numero | |
@saldo = 0 | |
end | |
attr_reader :saldo, :nome_correntista, :numero | |
def depositar(valor) | |
@saldo += valor | |
end | |
def sacar(valor) | |
raise if @saldo - valor < 0 | |
@saldo -= valor | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './conta' | |
class ContaEspecial < Conta | |
def initialize(limite) | |
super('', '') | |
@limite = limite | |
end | |
def sacar(valor) | |
raise if valor > saldo + limite | |
@saldo -= valor | |
end | |
attr_reader :limite | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
require './conta_especial' | |
describe ContaEspecial do | |
let(:conta) { ContaEspecial.new(1_000) } | |
it 'possui limite' do | |
conta.limite.should == 1_000 | |
end | |
it 'permite sacar até o limite' do | |
conta.saldo.should == 0 | |
conta.sacar(500) | |
conta.sacar(500) | |
conta.saldo.should == -1_000 | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
require './conta' | |
describe Conta do | |
before(:each) { @conta = Conta.new('Linus', '12345') } | |
it 'possui nome do correntista e número' do | |
@conta.nome_correntista.should == 'Linus' | |
@conta.numero.should == '12345' | |
end | |
it 'possui saldo inicial zero' do | |
@conta.saldo.should == 0 | |
end | |
it 'permite realizar depósitos' do | |
@conta.depositar(110) | |
@conta.saldo.should == 110 | |
@conta.depositar(10.5) | |
@conta.saldo.should == 120.5 | |
end | |
it 'permite realizar saques' do | |
@conta.depositar(100) | |
@conta.sacar(75) | |
@conta.saldo.should == 25 | |
@conta.sacar(10.5) | |
@conta.saldo.should == 14.5 | |
end | |
it 'permite sacar até zerar o saldo' do | |
@conta.depositar(100) | |
@conta.sacar(75) | |
@conta.sacar(25) | |
@conta.saldo.should == 0 | |
end | |
it 'dispara exceção para saque maior que saldo' do | |
@conta.depositar(100) | |
expect { @conta.sacar(100.01) }.should raise_error | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Figura | |
def desenhar | |
"%s(area=%.2f)" % [self.class.name.downcase, area] | |
end | |
end | |
class Circulo < Figura | |
def initialize(raio) | |
@raio = raio | |
end | |
attr_reader :raio | |
def area | |
2 * Math::PI * raio | |
end | |
end | |
class Triangulo < Figura | |
def initialize(base, altura) | |
@base, @altura = base, altura | |
end | |
attr_reader :base, :altura | |
def area | |
base * altura / 2 | |
end | |
end | |
class Surrealista < Figura | |
def desenhar | |
'===~~~^' | |
end | |
end | |
class Coisa | |
def desenhar | |
42 | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
require './figura' | |
describe Circulo do | |
let(:circulo) { Circulo.new(2) } | |
it 'possui um raio' do | |
circulo.raio.should == 2 | |
end | |
it 'calcula sua área' do | |
circulo.area.should be_within(0.01).of(12.56) | |
end | |
it 'se desenha' do | |
circulo.desenhar.should == 'circulo(area=12.57)' | |
end | |
end | |
describe Triangulo do | |
let(:triangulo) { Triangulo.new(3, 4) } | |
it 'possui base e altura' do | |
triangulo.base.should == 3 | |
triangulo.altura.should == 4 | |
end | |
it 'calcula sua área' do | |
triangulo.area.should == 6 | |
end | |
it 'se desenha' do | |
triangulo.desenhar.should == 'triangulo(area=6.00)' | |
end | |
end | |
describe 'figuras' do | |
it 'desenhar todas as figuras' do | |
figuras = [Triangulo.new(3, 4), Circulo.new(5), Circulo.new(1), | |
Surrealista.new, Coisa.new] | |
saida = [] | |
for figura in figuras | |
saida << figura.desenhar | |
end | |
saida.should == [ | |
'triangulo(area=6.00)', 'circulo(area=31.42)', | |
'circulo(area=6.28)', '===~~~^', 42] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment