GJPassos

Console.java

Aug 14th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.11 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class Console{
  4.     // método que lê uma String do teclado
  5.     public static String leString(){
  6.         BufferedReader teclado= new BufferedReader (new InputStreamReader (System.in));
  7.         try{
  8.             return teclado.readLine();
  9.         } catch (IOException e){
  10.             System.out.println(e.getMessage ());
  11.         }
  12.         return null;           
  13.     }
  14.    
  15.     // método que mostra uma mensagem na tela e lê uma String do teclado.
  16.     // caso a String seja nula ou em branco, repete a amostragem e a leitura.
  17.     public static String leString (String txt){
  18.         String str="";
  19.         while (true){
  20.             System.out.print(txt);
  21.             str=leString();
  22.             if (str!=null && !str.trim().equals(""))
  23.                 return str.trim();
  24.         }
  25.     }
  26.    
  27.     // método que lê um int do teclado
  28.     public static int leInt(){
  29.         return Integer.parseInt (leString());
  30.     }  
  31.    
  32.     // método que mostra uma mensagem na tela e lê um int do teclado.
  33.     // caso o valor digitado não seja um número, repete a amostragem e a leitura
  34.     public static int leInt(String txt){
  35.         while (true){
  36.             try{
  37.                 System.out.print(txt);
  38.                 return leInt();
  39.             }catch (NumberFormatException e){
  40.                 System.out.println("Numero Invalido");
  41.             }
  42.         }
  43.     }
  44.    
  45.     // método que lê um double do teclado
  46.     public static double leDouble(){
  47.         return Double.parseDouble(leString());
  48.     }  
  49.    
  50.     // método que mostra uma mensagem e lê um double do teclado
  51.     // caso o valor digitado não seja um número, repete a amostragem e a leitura 
  52.     public static double leDouble(String txt){
  53.         while (true){
  54.             try{
  55.                 System.out.print(txt);
  56.                 return leDouble();
  57.             }catch (NumberFormatException e){
  58.                 System.out.println("Numero Invalido");
  59.             }
  60.         }
  61.     }  
  62.    
  63.     // método que apresenta um menu na tela e retorna o número da opção selecionada
  64.     public static int menu(String titulo, String[] opcoes){
  65.         int op = 0;
  66.          
  67.         while(true){
  68.             System.out.println(titulo);
  69.             for(int i = 0; i < opcoes.length; i++){
  70.                 System.out.println((i + 1) + ". " + opcoes[i]);
  71.             }
  72.             op = leInt("Opção: ");
  73.            
  74.             if (op > 0 && op <= opcoes.length)
  75.                 return op;
  76.             else
  77.                 System.out.println("Opção inválida");
  78.                
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment