-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidator.java
More file actions
116 lines (89 loc) · 2.46 KB
/
Validator.java
File metadata and controls
116 lines (89 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package gruppeinnlevering;
import java.time.DateTimeException;
import java.time.LocalDate;
import java.util.Scanner;
/**
*
* @author norby
*/
public class Validator {
public Validator() {
}
// }
/**
* creates a new Scanner reades next input as String
*
* @return input String typed by user
*/
public String stringScanner() {
Scanner reader = new Scanner(System.in);
String input = reader.nextLine();
return input;
}
/**
* creates a new scanner Reades next input as int
*
* @return int input returns the number written by user.
*/
public int intScanner() {
Scanner reader = new Scanner(System.in);
int input = reader.nextInt();
return input;
}
/**
* Check if String is not null or empty
*
* @return booelan true/false
*/
public String checkString() {
String stringToCheck = "error";
boolean stringValid = false;
while (!stringValid) {
stringToCheck = stringScanner();
if (stringToCheck == null || stringToCheck.isEmpty()) {
System.out.println("String is empty, PLease try again");
} else {
stringValid = true;
}
}
return stringToCheck;
}
/**
*
* Check if value is inside av given range
*
* @param value
*
* @param min
* @param max
* @return boolean true/false
*/
public int checkInt(int min, int max) {
int valueToCheck = 0;
boolean check = false;
while (!check) {
valueToCheck = intScanner();
if (valueToCheck >= min && valueToCheck <= max) {
check = true;
} else {
System.out.println("Invalid Input" + "\nMin: " + " " + min + "\nMax: " + " " + max);
}
}
return valueToCheck;
}
public boolean testDate(int year, int month, int dayOfMonth) {
boolean valid = true;
try {
LocalDate date = LocalDate.of(year, month, dayOfMonth);
} catch (DateTimeException e) {
valid = false;
System.out.println("Exception thrown :" + e);
}
return valid;
}
}