fork download
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5.  
  6. class Pizza {
  7. private static final Map<String, Integer> quantidadesPorIngrediente = new HashMap<>();
  8.  
  9. private final List<String> ingredientes;
  10.  
  11. public Pizza() {
  12. ingredientes = new ArrayList<>();
  13. }
  14.  
  15. private static void contabilizaIngrediente(String ingrediente) {
  16. quantidadesPorIngrediente.compute(ingrediente, (k, v) -> v == null ? 1 : v + 1);
  17. }
  18.  
  19. public void adicionaIngrediente(String ingrediente) {
  20. ingredientes.add(ingrediente);
  21. contabilizaIngrediente(ingrediente);
  22. }
  23.  
  24. public boolean temIngredientes() {
  25. return !ingredientes.isEmpty();
  26. }
  27.  
  28. public int getPreco() {
  29. int quantidadeIngredientes = ingredientes.size();
  30. return quantidadeIngredientes <= 2 ? 15 : quantidadeIngredientes <= 5 ? 20 : 23;
  31. }
  32.  
  33. public static Map<String, Integer> getQuantidadesPorIngrediente() {
  34. return quantidadesPorIngrediente;
  35. }
  36. }
  37.  
  38. class CarrinhoDeCompras {
  39. private final List<Pizza> pizzas;
  40.  
  41. public CarrinhoDeCompras() {
  42. pizzas = new ArrayList<>();
  43. }
  44.  
  45. public void adiciona(Pizza p) {
  46. if (!p.temIngredientes()) throw new IllegalArgumentException();
  47. pizzas.add(p);
  48. }
  49.  
  50. public int getPreco() {
  51. int total = 0;
  52. for (Pizza p : pizzas) {
  53. total += p.getPreco();
  54. }
  55. return total;
  56. }
  57. }
  58.  
  59. class Principal {
  60. public static void main(String[] args) {
  61. Pizza marguerita = new Pizza();
  62. marguerita.adicionaIngrediente("Queijo");
  63. marguerita.adicionaIngrediente("Tomate");
  64. marguerita.adicionaIngrediente("Orégano");
  65.  
  66. Pizza portuguesa = new Pizza();
  67. portuguesa.adicionaIngrediente("Queijo");
  68. portuguesa.adicionaIngrediente("Presunto");
  69. portuguesa.adicionaIngrediente("Ovo");
  70. portuguesa.adicionaIngrediente("Calabresa");
  71. portuguesa.adicionaIngrediente("Cebola");
  72. portuguesa.adicionaIngrediente("Tomate");
  73.  
  74. Pizza brigadeiro = new Pizza();
  75. brigadeiro.adicionaIngrediente("Brigadeiro derretido");
  76.  
  77. CarrinhoDeCompras c1 = new CarrinhoDeCompras();
  78. c1.adiciona(marguerita);
  79. c1.adiciona(portuguesa);
  80. c1.adiciona(brigadeiro);
  81. System.out.println("Preço do carrinho: " + c1.getPreco());
  82.  
  83. for (Map.Entry<String, Integer> entry : Pizza.getQuantidadesPorIngrediente().entrySet()) {
  84. System.out.println("O ingrediente " + entry.getKey() + " foi usado " + entry.getValue() + " vezes.");
  85. }
  86. }
  87. }
Success #stdin #stdout 0.15s 33112KB
stdin
Standard input is empty
stdout
Preço do carrinho: 58
O ingrediente Queijo foi usado 2 vezes.
O ingrediente Cebola foi usado 1 vezes.
O ingrediente Tomate foi usado 2 vezes.
O ingrediente Ovo foi usado 1 vezes.
O ingrediente Orégano foi usado 1 vezes.
O ingrediente Calabresa foi usado 1 vezes.
O ingrediente Presunto foi usado 1 vezes.
O ingrediente Brigadeiro derretido foi usado 1 vezes.