Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3.  
  4. public class Pacote<E> implements Iterable<E>, Cloneable {
  5.  
  6.  
  7.     public static final int NUM_MAX_ITEMS = 3;
  8.     private ArrayList<E> pacote;
  9.     private double capcidade;
  10.     private double capUsada;
  11.  
  12.  
  13.     public Pacote(double capcidade) {
  14.         pacote = new ArrayList<>();
  15.         this.capcidade = capcidade;
  16.         this.capUsada= 0.0;
  17.     }
  18.  
  19.  
  20.     public double obtemCapacidadeOcupada() {
  21.         return this.capUsada;
  22.     }
  23.  
  24.     public int obtemNumItems() {
  25.         return this.pacote.size();
  26.     }
  27.  
  28.  
  29.     public boolean estaCheio() {
  30.         return obtemNumItems() == NUM_MAX_ITEMS;
  31.     }
  32.  
  33.  
  34.     public boolean cabe(double tamanho) {
  35.         return (tamanho + capUsada <= capcidade) && !estaCheio();
  36.     }
  37.  
  38.  
  39.     public boolean empacota(E item, double dimensaoDoItem){
  40.         boolean emp = true;
  41.         if(cabe(dimensaoDoItem)){
  42.             this.pacote.add(item);
  43.             this.capUsada += dimensaoDoItem;
  44.         }
  45.         else {
  46.             emp = false;
  47.         }
  48.         return emp;
  49.     }
  50.  
  51.  
  52.  
  53.     public String toString(){
  54.         StringBuilder sb = new StringBuilder();
  55.         if(this.pacote.isEmpty()) {
  56.             sb.append("[]");
  57.         }else {
  58.             sb.append("[");
  59.             for(int i = 0; i < this.pacote.size(); i++) {
  60.                 sb.append(this.pacote.get(i).toString());
  61.                 sb.append(", ");
  62.             }
  63.             sb.deleteCharAt(sb.length()-1);
  64.             sb.deleteCharAt(sb.length()-1);
  65.             sb.append("]");
  66.         }
  67.         return sb.toString();
  68.     }
  69.  
  70.  
  71.     public Iterator<E> iterator() {
  72.         //Deve implementar um iterador em vez de delegar num ja existente de um dos atributos
  73.         //(esse apenas serve parque deve usar para confirmar resultados)
  74.         return new Iterador();
  75.     }
  76.  
  77.  
  78.     public Pacote<E> clone () {
  79.         Pacote<E> copia = new Pacote<E>(this.capcidade);
  80.         Iterator<E> it = this.iterator();
  81.         int i = 0;
  82.         while(it.hasNext() && i < this.pacote.size()) {
  83.             copia.pacote.add(it.next());
  84.             i++;
  85.         }
  86.         copia.capUsada = this.capUsada;
  87.         return copia;
  88.     }
  89.  
  90.  
  91.     @Override
  92.     public boolean equals(Object obj) {
  93.         boolean b = true;
  94.         Pacote<E> a = (Pacote <E>) obj;
  95.         if(a.capcidade != this.capcidade) {
  96.             b =  false;
  97.         }else if(a.capUsada != this.capUsada) {
  98.             b = false;
  99.         }else {
  100.             Iterator<E> it = this.iterator();
  101.             int i = 0;
  102.             while(it.hasNext() && i < this.pacote.size()) {
  103.                 if(!a.pacote.get(i).equals(this.pacote.get(i))){
  104.                     b = false;
  105.                 }
  106.                 i++;
  107.             }
  108.         }
  109.         return b;
  110.     }
  111.  
  112.  
  113.     private class Iterador implements Iterator<E>{
  114.  
  115.         private int indice;
  116.  
  117.         private Iterador(){
  118.             this.indice = 0;
  119.         }
  120.         @Override
  121.         public boolean hasNext() {
  122.             return this.indice < pacote.size();
  123.         }
  124.  
  125.         @Override
  126.         public E next() {
  127.             if(hasNext()) {
  128.                 return pacote.get(indice++);
  129.             }else {
  130.                 return null;
  131.             }
  132.  
  133.         }
  134.  
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement