Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @author Kmiecik Mateusz S16412
- *
- */
- package zad2;
- import javax.swing.JFrame
- import javax.swing.JOptionPane
- class Main {
- static void main(String... args) {
- def frame = new JFrame("#2")
- def getData = {...dataArgs ->
- def type = String
- def checker = {true}
- if(dataArgs.size() == 2) {
- type = dataArgs[0]
- checker = dataArgs[1]
- }
- if(dataArgs.size() == 1) {
- if(dataArgs[0] instanceof Class) {
- type = dataArgs[0]
- } else {
- checker = dataArgs[0]
- }
- }
- def splitted = []
- def input = ''
- def correct = false
- def firstInput = true
- while(!correct) {
- def message = firstInput ? "Podaj napis" : "[POPRAW] Podaj napis"
- input = JOptionPane.showInputDialog(frame, message, input)
- if(input == null) {
- break
- }
- if(firstInput == true) {
- firstInput = false
- }
- splitted = input.split(' ').grep()
- def allWordsCorrect = true && splitted.size() > 0
- def inType = splitted.collect({
- try {
- return it.asType(type)
- } catch(err) {
- println 'wrong'
- println err
- allWordsCorrect = false
- }
- })
- correct = allWordsCorrect && inType.every({
- checker(it)
- })
- }
- splitted
- }
- println getData(Integer) { it > 0 } // liczby całkowite większe od 0
- println getData() { it.size() > 3 } // słowa o długości większej od 3 (domyślny typ: String)
- println getData() // dowolne napisy (słowa)
- println getData(BigDecimal) // dowolne liczby
- System.exit(0)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement