Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Agenda; // todo dentro del pack agenda
- public class Contacto {
- String nombre;
- int ID;
- public Contacto (String nombre, int ID){
- this.nombre = nombre;
- this.ID = ID;
- }
- public String toString(){
- return (this.nombre+" "+this.ID);
- }
- }
- //####################################################################################################################################
- //package Agenda;
- import java.util.ArrayList;
- public class Agenda {
- ArrayList<Contacto> agenda;
- public Agenda(){
- this.agenda = new ArrayList<Contacto>();
- }
- public Contacto tope(){
- return (this.agenda.get(this.agenda.size()-1));
- }
- public void quitar(){
- this.agenda.remove(this.agenda.size()-1);
- }
- public boolean vacio(){
- return( this.agenda.isEmpty());
- }
- public void agregar(Contacto c){
- this.agenda.add(c);
- }
- public void agregarOrd(Contacto c){
- if( (this.vacio()) || (this.tope().ID>c.ID) ){
- this.agregar(c);
- }
- else{
- ArrayList<Contacto> aux = new ArrayList<Contacto>();
- boolean ordenado = false;
- while (!ordenado){
- if (this.tope().ID > c.ID){
- aux.add(this.tope());
- this.quitar(); }
- else{
- this.agregar(c);
- while(!ordenado){
- this.agregar(aux.remove(aux.size()-1));
- if(aux.isEmpty()){
- ordenado = true;
- }
- }
- }
- }
- }
- }
- public String toString (){
- String ordenados = "";
- for (int i = this.agenda.size()-1; i>=0;i--){
- ordenados += agenda.get(i)+" ";
- }
- return ordenados;
- }
- }
- //########################################################################################################################
- //package Agenda;
- public class Main {
- public static void main(String[] args) {
- Agenda agenda = new Agenda();
- Contacto a = new Contacto ("Jere", 92);
- Contacto b = new Contacto ("Luca", 23);
- Contacto c = new Contacto ("Joge", 21);
- agenda.agregarOrd(a);
- agenda.agregarOrd(b);
- agenda.agregarOrd(c);
- System.out.println(agenda.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement