Advertisement
wellison1

Untitled

Oct 21st, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. package ip;
  2. import java.util.*;
  3.  
  4. class Lista {
  5.     // ATRIBUTOS
  6.     private long ano;
  7.     private String acontecimento;
  8.     private Lista proximo;
  9.  
  10.     // CONSTRUTOR
  11.     public Lista() {
  12.         this.ano = 0;
  13.         this.acontecimento = null;
  14.         this.proximo = null;
  15.     }
  16.     //METODO PARA INSERIR ELEMENTOS
  17.     public void inserir(long ano, String acontecimento) {
  18.         if (this.proximo == null) {
  19.             this.ano = ano;
  20.             this.acontecimento = acontecimento;
  21.             this.proximo = new Lista();
  22.         }
  23.  
  24.         else {
  25.             //colocando em ordem crescente
  26.             if (this.ano > ano){
  27.                 //mudando posição
  28.                 this.proximo.inserir(this.ano,this.acontecimento);
  29.                 this.ano = ano;
  30.                 this.acontecimento = acontecimento;
  31.             }
  32.             // colocanco em ordem alfabetica
  33.             else if (this.ano == ano){
  34.                 int aux =  this.acontecimento.compareTo(acontecimento);
  35.                 if (aux > 0 ){
  36.                     this.proximo.inserir(this.ano,this.acontecimento);
  37.                     this.ano = ano;
  38.                     this.acontecimento = acontecimento;
  39.                 }else {
  40.                     this.proximo.inserir(ano,acontecimento);
  41.                 }
  42.             }
  43.             else {
  44.                 //mudando posição
  45.                 this.proximo.inserir(ano,acontecimento);
  46.             }
  47.  
  48.         }
  49.  
  50.  
  51.     }
  52.  
  53.     public  String imprimirLista (){
  54.         String aux = "";
  55.         if (this.ano != 0){
  56.             aux += this.ano + " "+  "-" + " " + this.acontecimento + "\n" + this.proximo.imprimirLista();
  57.  
  58.         } else {
  59.             aux += "";
  60.         }
  61.         return aux;
  62.     }
  63.  
  64.  
  65. }
  66.  
  67.  
  68.  
  69. public class teste {
  70.     public static void main(String[] args) {
  71.         Scanner in = new Scanner(System.in);
  72.  
  73.  
  74.         long n = 0;
  75.         String acontecimento = "";
  76.         Lista lista = new Lista();
  77.         while (in.hasNext()) {
  78.             acontecimento = in.nextLine();
  79.             n = Long.parseLong(in.nextLine());
  80.             lista.inserir(n, acontecimento);
  81.  
  82.  
  83.         }
  84.         System.out.println(lista.imprimirLista());
  85.  
  86.     }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement