Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. package edu.lp2.opencm.application.cli;
  2.  
  3. import java.io.PrintStream;
  4. import java.math.BigDecimal;
  5. import java.util.Scanner;
  6.  
  7. import edu.lp2.opencm.application.Bootstrap;
  8. import edu.lp2.opencm.application.service.PlanoSaudeService;
  9. import edu.lp2.opencm.domain.model.PlanoSaude;
  10.  
  11. public final class CadastrarPlanoSaude {
  12.  
  13.     private final PrintStream out;
  14.     private final Scanner in;
  15.     private final PlanoSaudeService planoSaudeService;
  16.  
  17.     private abstract class ValueReader {
  18.         abstract void set(String value);
  19.        
  20.         public void read(String prompt, boolean required) {
  21.             while(true) {
  22.                 out.printf(prompt);
  23.                 try {
  24.                     set(in.nextLine());
  25.                     break;
  26.                 } catch (IllegalArgumentException e) {
  27.                     out.printf("%s%n", e.getMessage());
  28.                 } catch (NullPointerException e) {
  29.                     if (required) {
  30.                         out.printf("%s%n", e.getMessage());
  31.                     } else {
  32.                         break;
  33.                     }
  34.                 }
  35.             }
  36.         }
  37.     }
  38.    
  39.     public CadastrarPlanoSaude(PrintStream out, Scanner in, PlanoSaudeService service) {
  40.         this.out = out;
  41.         this.in = in;
  42.         this.planoSaudeService = service;
  43.     }
  44.  
  45.     public void doMain() {
  46.         final PlanoSaude planoSaude = new PlanoSaude();
  47.        
  48.         new ValueReader() { void set(String nome) {
  49.             planoSaude.setNome(nome);
  50.         }}.read("Nome do Plano :", true);
  51.        
  52.         new ValueReader() { void set(String contato) {
  53.             planoSaude.setContato(contato);
  54.         }}.read("Fone Contato (ex: \"(81)1234-5678\"): ", false);
  55.        
  56.         new ValueReader() { void set(String precoPadrao) {
  57.             planoSaude.setPrecoPadrao(new BigDecimal(precoPadrao));
  58.         }}.read("Preço Padrão da Consulta: ", false);
  59.        
  60.         planoSaudeService.add(planoSaude);
  61.     }
  62.    
  63.     public static void main(String args[]) {
  64.         Bootstrap app = new Bootstrap();
  65.        
  66.         try {
  67.             app.startCli();
  68.             app.pico.getComponent(CadastrarPlanoSaude.class).doMain();
  69.         } finally {
  70.             app.stop();
  71.         }
  72.     }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement