Advertisement
Guest User

zad2

a guest
May 5th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 1.62 KB | None | 0 0
  1. /**
  2.  *
  3.  *  @author Kmiecik Mateusz S16412
  4.  *
  5.  */
  6.  
  7. package zad2;
  8.  
  9. import javax.swing.JFrame
  10. import javax.swing.JOptionPane
  11.  
  12. class Main {
  13.     static void main(String... args) {
  14.  
  15.         def frame = new JFrame("#2")
  16.  
  17.         def getData =  {...dataArgs ->
  18.             def type = String
  19.             def checker = {true}
  20.            
  21.             if(dataArgs.size() == 2) {
  22.                 type = dataArgs[0]
  23.                 checker = dataArgs[1]
  24.             }
  25.            
  26.             if(dataArgs.size() == 1) {
  27.                 if(dataArgs[0] instanceof Class) {
  28.                     type = dataArgs[0]
  29.                 } else {
  30.                     checker = dataArgs[0]
  31.                 }
  32.             }
  33.  
  34.             def splitted = []
  35.             def input = ''
  36.             def correct = false
  37.             def firstInput = true
  38.            
  39.             while(!correct) {
  40.                 def message = firstInput ? "Podaj napis" : "[POPRAW] Podaj napis"
  41.                
  42.                 input = JOptionPane.showInputDialog(frame, message, input)
  43.                 if(input == null) {
  44.                     break
  45.                 }
  46.                 if(firstInput == true) {
  47.                     firstInput = false
  48.                 }
  49.                 splitted = input.split(' ').grep()
  50.                
  51.                 def allWordsCorrect = true && splitted.size() > 0
  52.                 def inType = splitted.collect({
  53.                     try {
  54.                         return it.asType(type)
  55.                     } catch(err) {
  56.                         println 'wrong'
  57.                         println err
  58.                         allWordsCorrect = false
  59.                     }
  60.                 })
  61.                
  62.                 correct = allWordsCorrect && inType.every({
  63.                     checker(it)
  64.                 })
  65.             }
  66.            
  67.             splitted
  68.         }
  69.  
  70.         println getData(Integer) { it > 0 }            // liczby całkowite większe od 0
  71.         println getData() { it.size() > 3 }   // słowa o długości większej od 3 (domyślny typ: String)
  72.         println getData()                          // dowolne napisy (słowa)
  73.         println getData(BigDecimal)         // dowolne liczby
  74.  
  75.         System.exit(0)
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement