Created
December 21, 2011 10:51
-
-
Save rafaelchagasb/1505593 to your computer and use it in GitHub Desktop.
Decorator - Exercício 1
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
public abstract class Imposto { | |
protected final Imposto outroImposto; | |
public Imposto(Imposto outroImposto){ | |
this.outroImposto = outroImposto; | |
} | |
public Imposto() | |
{ | |
this.outroImposto = null; | |
} | |
protected double calculoDoOutroImposto(Orcamento orcamento){ | |
if (outroImposto == null) return 0; | |
return outroImposto.calcula(orcamento); | |
} | |
public abstract double calcula(Orcamento orcamento); | |
} | |
public class Item { | |
private String nome; | |
private double valor; | |
public Item(String nome,double valor) { | |
this.nome = nome; | |
this.valor = valor; | |
} | |
public String getNome() { | |
return nome; | |
} | |
public void setNome(String nome) { | |
this.nome = nome; | |
} | |
public double getValor() { | |
return valor; | |
} | |
public void setValor(double valor) { | |
this.valor = valor; | |
} | |
} | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.Collections; | |
import java.util.List; | |
public class Orcamento { | |
private double valor; | |
private List<Item> itens; | |
public Orcamento(double valor) | |
{ | |
this.setValor(valor); | |
this.itens = new ArrayList<Item>(); | |
} | |
public double getValor() { | |
return valor; | |
} | |
public void setValor(double valor) { | |
this.valor = valor; | |
} | |
public List<Item> getItens() | |
{ | |
return Collections.unmodifiableList(itens); | |
} | |
public void adicionaItem(Item item) | |
{ | |
itens.add(item); | |
} | |
} | |
public class ISS extends Imposto{ | |
public ISS(Imposto outroImposto){ | |
super(outroImposto); | |
} | |
public ISS() { | |
} | |
@Override | |
public double calcula(Orcamento orcamento) { | |
return orcamento.getValor() * 0.6 + calculoDoOutroImposto(orcamento); | |
} | |
} | |
public class IKVC extends TemplateImposto{ | |
@Override | |
public boolean usarMaximaTaxacao(Orcamento orcamento) { | |
return orcamento.getValor() > 500 && valorDeItemSuperior(orcamento); | |
} | |
@Override | |
public double maximaTaxacao(Orcamento orcamento) { | |
return orcamento.getValor()*0.1; | |
} | |
@Override | |
public double minimaTaxacao(Orcamento orcamento) { | |
return orcamento.getValor()*0.06; | |
} | |
private boolean valorDeItemSuperior(Orcamento orcamento) | |
{ | |
for(Item item:orcamento.getItens()) | |
if(item.getValor() > 100) | |
return true; | |
return false; | |
} | |
@Override | |
public double calcula(Orcamento orcamento) { | |
return this.calculaImposto(orcamento)+ calculoDoOutroImposto(orcamento); | |
} | |
} | |
public class ICPP extends Imposto{ | |
public ICPP(Imposto outroImposto){ | |
super(outroImposto); | |
} | |
public ICPP() { | |
} | |
@Override | |
public double calcula(Orcamento orcamento) { | |
return 0; | |
} | |
} | |
public class ICMS extends Imposto { | |
public ICMS(Imposto outroImposto){ | |
super(outroImposto); | |
} | |
public ICMS() { | |
} | |
@Override | |
public double calcula(Orcamento orcamento) { | |
return orcamento.getValor() * 0.2 + calculoDoOutroImposto(orcamento); | |
} | |
} | |
public class TestarImposto { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
Orcamento orcamento = new Orcamento(700.0); | |
Imposto impostoComposto = new ICMS(new ISS()); | |
System.out.println(impostoComposto.calcula(orcamento)); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment