Advertisement
fabi2295

POO

Sep 29th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | None | 0 0
  1. public class Pessoa{
  2.     private String[] telefones = new String[3];
  3.     private int cont = 0;
  4.  
  5.     public void inserirTelefone(String numero){
  6.         /**for(int i = 0; i < this.telefones.length; i++){
  7.             if(this.telefones[i] == null){
  8.                 this.telefones[i] = numero;
  9.                 break;
  10.             }
  11.         } **/
  12.         if(cont < this.telefones.length){
  13.             if(this.telefones[cont] == null){
  14.                 this.telefones[cont] = numero;
  15.                 cont++;
  16.             }
  17.         }else{
  18.             System.out.println("ERRO! Só é permitido 3 telefones");
  19.         }
  20.     }
  21.  
  22.     public void listarTelefone(){
  23.         int i;
  24.  
  25.         for(i = 0; i < this.telefones.length; i++){
  26.             if(this.telefones[i] != null){
  27.                 System.out.println("["+ i+"]"+this.telefones[i]);
  28.             }
  29.         }
  30.     }
  31.  
  32.    
  33.     //
  34.     public void apagarTelefone(String numero){
  35.                
  36.         for(int i = 0; i < this.telefones.length; i++){
  37.             if(this.telefones[i] == numero){
  38.                 if(i == 0){
  39.                     this.telefones[i] = this.telefones[i + 1];
  40.                     this.telefones[i + 1] = this.telefones[i + 2];
  41.                     this.telefones[i + 2] = null;
  42.                     break;
  43.                    
  44.                 }else if(i == 1){
  45.                     this.telefones[i] = this.telefones[i + 1];
  46.                     this.telefones[i + 1] = null;
  47.                     break;
  48.                    
  49.                 }else{
  50.                     this.telefones[i] = null;
  51.                 }
  52.                
  53.             }
  54.         }
  55.        
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement