Advertisement
LaCaraDeLaVerga

TAD agenda

May 20th, 2016
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.07 KB | None | 0 0
  1. package Agenda;   // todo dentro del pack agenda
  2.  
  3. public class Contacto {
  4.     String nombre;
  5.     int ID;
  6.    
  7.     public Contacto (String nombre, int ID){
  8.         this.nombre = nombre;
  9.         this.ID = ID;
  10.     }
  11.    
  12.     public String toString(){
  13.         return (this.nombre+" "+this.ID);
  14.     }
  15.    
  16. }
  17.  
  18. //####################################################################################################################################
  19. //package Agenda;
  20.  
  21. import java.util.ArrayList;
  22.  
  23. public class Agenda {
  24.     ArrayList<Contacto> agenda;
  25.    
  26.     public Agenda(){
  27.         this.agenda = new ArrayList<Contacto>();
  28.     }
  29.    
  30.     public Contacto tope(){
  31.         return (this.agenda.get(this.agenda.size()-1));
  32.     }
  33.    
  34.     public void quitar(){
  35.         this.agenda.remove(this.agenda.size()-1);
  36.     }
  37.     public boolean vacio(){
  38.         return( this.agenda.isEmpty());
  39.     }
  40.    
  41.     public void agregar(Contacto c){
  42.         this.agenda.add(c);
  43.     }
  44.    
  45.    
  46.     public void agregarOrd(Contacto c){
  47.         if( (this.vacio()) || (this.tope().ID>c.ID) ){
  48.             this.agregar(c);
  49.         }
  50.         else{
  51.             ArrayList<Contacto> aux = new ArrayList<Contacto>();
  52.             boolean ordenado = false;
  53.             while (!ordenado){
  54.                 if (this.tope().ID > c.ID){
  55.                     aux.add(this.tope());
  56.                     this.quitar();              }
  57.                 else{
  58.                     this.agregar(c);
  59.                    
  60.                    
  61.                     while(!ordenado){
  62.                         this.agregar(aux.remove(aux.size()-1));
  63.                         if(aux.isEmpty()){
  64.                             ordenado = true;
  65.                         }
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.     }
  71.    
  72.     public String toString (){
  73.        
  74.         String ordenados = "";
  75.         for (int i = this.agenda.size()-1; i>=0;i--){
  76.             ordenados += agenda.get(i)+" ";
  77.         }
  78.          return ordenados;
  79.     }
  80.    
  81.    
  82.    
  83.    
  84. }
  85. //########################################################################################################################
  86. //package Agenda;
  87.  
  88. public class Main {
  89.  
  90.     public static void main(String[] args) {
  91.        
  92.         Agenda agenda = new Agenda();
  93.        
  94.         Contacto a = new Contacto ("Jere", 92);
  95.         Contacto b = new Contacto ("Luca", 23);
  96.         Contacto c = new Contacto ("Joge", 21);
  97.        
  98.         agenda.agregarOrd(a);
  99.         agenda.agregarOrd(b);
  100.         agenda.agregarOrd(c);
  101.  
  102.        
  103.         System.out.println(agenda.toString());
  104.     }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement