I have to read from a file, so made:
cod_order (int)
cod_cliente (int)
cod_pizza (int)
num_pizza (int)
cod_pizza
num_pizza
...
$FINE
here is an example:
1
1107
02
1
01
5
03
2
$FINE
I created this method to read from this text file:
private void loadOrdini(String fname){
try{
BufferedReader reader = new BufferedReader(new FileReader(fname));
String cod_ordine = reader.readLine();
while(cod_ordine!=null){
String cod_cliente=reader.readLine();
Cliente cl=listaclienti.get(Integer.parseInt(cod_cliente));
String cod_pizza=reader.readLine();
while(!cod_pizza.equals("$FINE")){
String n_pizze=reader.readLine();
Ordine ord = new Ordine(Integer.parseInt(cod_pizza),Integer.parseInt(n_pizze));
cl.getListaOrdini().put(Integer.parseInt(cod_ordine), ord);
cod_pizza=reader.readLine();
}
cod_ordine=reader.readLine();
}
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
the problem is the method load into the HashMap listaordini only the numbers 03 and 2 while the numbers before aren’t considerer.why?
import java.util.*;
public class Cliente{
private String name;
private String address;
private Map<Integer,Ordine> listaordini;
public Cliente(String n,String addr){
name=n;
address=addr;
listaordini=new HashMap<Integer,Ordine>();
}
public String getName(){
return name;
}
public String getAddr(){
return address;
}
public Map<Integer,Ordine> getListaOrdini(){
return listaordini;
}
public String toString(){
String temp="";
temp+="Nome Cliente: "+name;
temp+="\nIndirizzo: "+address;
return temp;
}
}
even after this suggets the problem remain the same
I think the other answers have missed the point: you want a mapping from order IDs to pizzas.
The problem is, a map (such as a HashMap) must have a unique mapping for each key. What that means is, you can only map a single
Ordineto the order ID, a map doesn’t support having multipleOrdines for the same order ID.There’s a couple of ways around this. You could make a new object that contains a list of
Ordersand put that into the map, or you could change your map to map an order ID to aListof orders.For example, doing it the second way you would have: