Java - Error al trabajar con listas y archivos....

 
Vista:
sin imagen de perfil

Error al trabajar con listas y archivos....

Publicado por Alex (1 intervención) el 15/12/2013 21:22:45
Estoy creando un proyecto que consiste en una emresa que brinda transporte terrestre interprovincial y que posee una clase Ruta() que tiene como atributos ademas de codigo, nombre, una lista de Vehiculos y un tipo de Servicio(si es en modo ,economico, ejecutivo , etc) , donde Vehiculo y Tipo_Servicio son otras clases que ya cree y que funcionan bien por si solas..
Como van a haber varias rutas las estoy ordenando en un ArrayList y las estoy guardando en un archivo , lo mismo estoy haciendo con la clase vehiculo y tipo_servicio. Mi codigo es este
Clase Vehiculo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public class Vehiculo {
    private String codigo,modelo,color, placa,año;
    Asiento[] asientos;
    private int nasientos;
 
    public Vehiculo(String s){
        List<String>lisVehiculo = Arrays.asList(s.split(","));
        if(lisVehiculo.size() != 6){
            throw new IllegalArgumentException("Deben ser 6 valores " + lisVehiculo.size());
        }
        this.codigo=lisVehiculo.get(0);
        this.modelo=lisVehiculo.get(1);
        this.color=lisVehiculo.get(2);
        this.placa=lisVehiculo.get(3);
        this.año =lisVehiculo.get(4);
        this.nasientos = Integer.parseInt(lisVehiculo.get(5));
        asientos = new Asiento[this.nasientos];
        for(int i=0;i<nasientos;i++){
            asientos[0]=new Asiento(i+1);
        }
 
    }//constructor
 
    public static List<Vehiculo> LeeVehiculos(String nomFich)throws FileNotFoundException{
        List<Vehiculo> Id = new LinkedList<>();
        Scanner sc = new Scanner(new File(nomFich));
        while(sc.hasNextLine()){
            Vehiculo v = new Vehiculo(sc.nextLine());
            Id.add(v);
        }//fin while
 
        sc.close();
        return Id;
    }// fin LeeDocentes
 
    public static int EscribirVehiculo(String nomFich, List<Vehiculo> listVehiculo){
        File archivo = new File(nomFich);
        PrintWriter ps;
        try{
            ps = new PrintWriter(archivo);
        }
        catch(FileNotFoundException e){
            return 0;
        }
        for(Vehiculo objVehiculo : listVehiculo){
            ps.println(objVehiculo);
        }
        ps.close();
        return 1;
    }
      .
      ..
      ...

Para la clase Ruta

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Ruta {
        private String codigo;
	private String nombre;
	private String inicio;
	private String destino;
	private double costo;
        private Categoria cat;
        private ArrayList<Vehiculo> vehiculos= new ArrayList<>();
        private int nvehiculos;
        //Tramo[] tramo= new Tramo[100];
        //private String ntramo = String.valueOf(tramo.length);
 
   public Ruta(String s){
        List<String>lisRuta = Arrays.asList(s.split(","));
        this.codigo =lisRuta.get(0);
        this.nombre =lisRuta.get(1);
        this.inicio =lisRuta.get(2);
        this.destino =lisRuta.get(3);
        this.cat=(Categoria)lisRuta.get(4);  //ERROR inconvertibles types ='/img/emoticons/down.gif' width='22' height='22' border='0' />
        this.costo =Double.parseDouble(lisRuta.get(4));
        this.nvehiculos =Integer.parseInt(lisRuta.get(6));
        this.cat=((ArrayList<Vehiculo>)lisRuta).get(6);  //ERROR   incovertibles type ='/img/emoticons/down.gif' width='22' height='22' border='0' />
        //this.ntramo =lisRuta.get(6);
    }
  //get y set
Aparte del error de codigo
Esta es la forma correcta de hacerlo o tambien esta mal la idea que tengo??
RUTA
-String codigo
-String Nombre
-ArrayList Vehiculos
-Tipo Servicio servicio
-double precio

GRACIAS de antemano ...
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Error al trabajar con listas y archivos....

Publicado por UnoPorAhi (128 intervenciones) el 16/12/2013 09:20:58
Buenas,

Te falla porque estas asignando un String como valor una referencia de tipo Categoria.


Un saludo
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar