-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResponsi1_PPBO_L0122136_Verify.java
75 lines (67 loc) · 2.54 KB
/
Responsi1_PPBO_L0122136_Verify.java
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
import java.util.Scanner;
import java.util.regex.Pattern;
public class Responsi1_PPBO_L0122136_Verify {
public static int checkInt(Scanner scanner) {
boolean isValidInput = false;
int result = -1;
while (!isValidInput) {
try {
String input = scanner.nextLine();
result = Integer.parseInt(input);
if (result < 0) {
System.out.println("Error: Input tidak boleh negatif!");
continue;
}
isValidInput = true;
} catch (Exception e) {
System.out.println("Error: Input yang Anda berikan tidak valid!");
}
}
return result;
// String inputTemp = scanner.nextLine();
// int inputInt = -1;
// try {
// inputInt = Integer.parseInt(inputTemp);
// if (inputInt < 0) {
// throw new IllegalArgumentException("Input harus merupakan bilangan bulat
// positif.");
// }
// } catch (NumberFormatException e) {
// throw new IllegalArgumentException("Input tidak valid. Harus berupa bilangan
// bulat.");
// }
// return inputInt;
}
// class ini digunakan untuk mengecek inputan user agar sesuai yang diiinginkan
// nantinya kelas ini akan melempar exception
public static String checkNameTask(Scanner scanner) {
String agenda = null;
boolean validInput = false;
while (!validInput) {
System.out.print("Nama Agenda => ");
agenda = scanner.nextLine().trim();
if (!agenda.isEmpty() && agenda.length() <= 15) {
validInput = true;
} else {
System.out.println("Nama Agenda diantara 0-15 kata.");
Responsi1_PPBO_L0122136_Utility.enterToContinue(scanner);
}
}
return agenda;
}
public static String checkDate(Scanner scanner) {
boolean inputValid = false;
String inputValidDate = null;
while (!inputValid) {
System.out.print("format(yyyy-mm-dd) => ");
inputValidDate = scanner.nextLine().trim();
if (Pattern.compile("^\\d{4}-(0\\d|1[0-2])-(0\\d|[12]\\d|3[01])$").matcher(inputValidDate).matches())
inputValid = true;
else {
System.out.println("Tanggal tidak valid, harap masukkan sesuai format");
Responsi1_PPBO_L0122136_Utility.enterToContinue(scanner);
}
}
return inputValidDate;
}
}