1
My doubt may seem simple to someone, but to me it’s kind of killing me because I’m trying to find the mistake and I’m not getting it.
At first, I know very little about object-oriented programming in Java, since most of the time in my course I only use C or C++. So I decided to take a course in Java to learn a little more, but apparently it’s very different from what I know how to do.
So I’m going to pass the question here and my code plus I’m not able to calculate the total price.
Create a class
Pizza
which has the methodadicionaIngrediente()
who receives aString
with the ingredient to be added. This class must also have the methodgetPreco()
which calculates as follows: 2 ingredients or less cost 15 real, 3 to 5 ingredients cost 20 real and more than 5 ingredients costs 23 real.You need to account for the ingredients spent on all pizzas! Use a static variable in the class
Pizza
to store this type of information (tip: use the classHashMap
to keep the ingredient as key and aInteger
as value). Create static methodcontabilizaIngrediente()
to be called withinadicionaIngrediente()
and make that record.Create a new class called
CarrinhoDeCompras
which can receive class objectsPizza
. It must have a method that returns the total value of all pizzas added. Cart cannot accept adding a pizza without ingredients.Create a class
Principal
with the methodmain()
which does the following:
Creates 3 pizzas with different ingredients.
Add these
Pizza
s in aCarrinhoDeCompra
.Prints the total of
CarrinhoDeCompra
.Prints the amount of each ingredient used.
Then, just below I tried to make the calculation of the total price but it returns me the value as zero. I spent a few hours trying to find what could be the error that it has to return me only zero, then I decided to come here in stackoverflow where I have already solved many my doubts in C++.
import java.util.*;
public class Pizza {
static HashMap<String, Integer> ingrediente = new HashMap<String, Integer>();
ArrayList<String> qntdadeingre;
static int totalingre=0;
static int total=0;
static int preco=0;
public Pizza()
{
this.qntdadeingre = new ArrayList<String>();
}
public static void contabilizaIngrediente(String ingredientes, Integer qntidade)
{
ingrediente.put(ingredientes,qntidade);
}
public void adicionaIngrediente(String ingredientes)
{
this.qntdadeingre.add(ingredientes);
if(Pizza.ingrediente.containsKey(ingredientes)) {
Pizza.contabilizaIngrediente(ingredientes, Pizza.ingrediente.get(ingredientes) + 1);
} else {
Pizza.contabilizaIngrediente(ingredientes, 1);
}
}
public int getPreco()
{
if(totalingre<=2)
{
preco=15;
}else
{
if(totalingre <= 5)
{
preco=20;
}else
{
preco=23;
}
}
total +=preco;
return total;
}
public int contemingr()
{
if(qntdadeingre.size() > 0)
return 1;
else
return 0;
}
public HashMap<String, Integer> getIngrediente()
{
return ingrediente;
}
static void printatotal()
{
System.out.println("Preco Final: " + totalingre);
}
}
It’s this kind of answer that I like, even though I disagree punctually.
– Maniero
Thank you so much for clearing up my doubts. Because as I said I’m trying to learn another language outside of what I’ve learned over the course. I met this object-oriented course through a friend so I wouldn’t know if I’m learning correctly or not.
– HazComp
To store ingredients without taking into account the quantities the exercise could have suggested a
Set
/HashSet
. If you take them into account there is a collection of Guava that fits well there, if I’m not mistaken it’s calledMultiSet
, although of course leaving the collections provided by Java is "advanced". And a point about exposing the variablequantidadesPorIngrediente
through the methodgetQuantidadesPorIngrediente()
: it is subject to improper modification outside the class. Perhaps it would be the case to expose a copy of it in place.– Piovezan
@Piovezan Yes, I didn’t even get into the details about exposing the
Map
. This violates class encapsulation, but would require more advanced approaches to solve. Areturn Collections.unmodifiableMap(quantidadesPorIngrediente);
would solve part of it, but the best thing would be to make that method have aBiConsumer<String, Integer>
as a parameter.– Victor Stafusa