Java - problemas con agregar a una cola de prioridad

 
Vista:

problemas con agregar a una cola de prioridad

Publicado por Ricardo (2 intervenciones) el 06/09/2014 14:56:23
Buenas , estoy intentando agregar unos datos de una clase a una cola de prioridad declarada en otra , el caso es que no me permite agregar mas de 1 elemento , ya que la segunda vez que intento agregar un elemento este
me salta al hilo, os dejo el codigo , darme ideas porfa
public class Agenda {

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
54
55
56
57
58
//Do stuff with the user
 
 
//Do stuff with user
 
 
//Do stuff with user
	////////////////////////////// ATRIBUTOS //////////////////////////////
        //LISTA DE PRIORIDAD
       PriorityQueue<Tarea> colaTareas;
        //ARRAY DE OBJETOS
       //private ArrayList<Tarea> colaTareas;
	////////////////////////////// CONSTRUCTORES //////////////////////////////
	public Agenda(){
		colaTareas =  new PriorityQueue<Tarea>();
 
                //ARRAY DE OBJETOS
                //colaTareas= new ArrayList<>();
	}
 
	////////////////////////////// MÉTODOS PÚBLICOS //////////////////////////////
/*	public Tarea addTimeToFirst( int seconds ){
		Tarea first = colaTareas.poll();
		if ( first != null ){
			first.retrasar( seconds );
			colaTareas.add( first );
		}
		
		return getFirst();
	}*/
	public void addTarea(String tareaName, String date, String hour) throws FechaException {
                Calendar today =  Calendar.getInstance();
                String other = date+" "+hour;
                Calendar otherday = Calendar.getInstance();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		//HAY QUE SACAR UNA ECEPCIóN
                try {
			otherday.setTime( sdf.parse(other) );
		} catch (ParseException e) {
			//throw new FechaException( tareaName, date, hour );
		}
                if (today.compareTo(otherday)<0){
                    Calendar newCalendar2 = Calendar.getInstance();
 
 
 
                    //ARRAY DE OBJETOS
                    //colaTareas.add( newTarea );
                    //LISTA DE PRIORIDAD
                      Tarea newTarea = new  Tarea(tareaName);
                      colaTareas.offer(newTarea);
 
                }else{
                //  throw new FechaException( tareaName, date, hour );
                  //Escepcion que pasa de mi culo!!!!!!!!!!!!!!!!!!!!!!
                   throw new FechaException("Números fuera del intervalo");
                }
        }
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

problemas con agregar a una cola de prioridad

Publicado por Pedro (81 intervenciones) el 06/09/2014 16:52:48
¿Podrías pegar el código de la clase Tarea?
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

problemas con agregar a una cola de prioridad

Publicado por Ricardo (2 intervenciones) el 06/09/2014 20:15:52
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agend;
import java.text.SimpleDateFormat;
 
import java.util.Calendar;
import java.util.Date;
/**
 *
 * @author Ricardo
 */
public class Tarea{
///////////ATRIBUTOS/////////////////    
private String Nombre;
private Calendar Fecha;
 
//////////CONSTRUCTORES//////////////
//sobreargo la clase 
public Tarea(String nombre, Calendar fecha){
this.Nombre=nombre;
this.Fecha=fecha;
}
 
public Tarea(String nombre){
this.Nombre=nombre;
}
 
    Tarea(String tareaName, String date, String hour) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
///////////METODOS PÚBLICOS/////////
public void retrasar(int seconds){
 
    Fecha.add(Calendar.SECOND,-seconds);
}
 
//sobreescritura paraetriza los metodos BASE
@Override
public String toString(){
    		return String.format("%1$s - %2$s - %3$s", getNombre(), getDia(), getHora());
}
/*@Override
	public int compareTo(Tarea o) {
		int compare = 0;

	    Calendar c1 = getFecha();
	    Calendar c2 = o.getFecha();

	    if ( c1.before(c2) )
	    	compare = -1;
	    else if ( c1.after(c2) )
	    	compare = 1;

	    return compare;
	}
*/
 
 
 
 
/////GETTERS Y SETTERS ///
 
    public String getNombre() {
        return Nombre;
    }
 
    public void setNombre(String Nombre) {
        this.Nombre = Nombre;
    }
 
    public Calendar getFecha() {
        return Fecha;
    }
 
    public void setFecha(Calendar Fecha) {
        this.Fecha = Fecha;
    }
 
    public String getDia(){
     Date date= Fecha.getTime();
     SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy");
     return sdf.format(date);
    }
 
    public String getHora(){
        Date date = Fecha.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        return sdf.format(date);
    }
 
    int compareTo(Tarea newTarea) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
 
 
 
 
 
}
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
sin imagen de perfil

problemas con agregar a una cola de prioridad

Publicado por Pedro (81 intervenciones) el 06/09/2014 20:22:48
No has implementado la interfaz Comparable aunque si has puesto los métodos, es decir:

1
2
3
public class Tarea implements Comparable<Tarea>{
 
}
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