forked from raimonizard/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilities.java
More file actions
84 lines (72 loc) · 2.47 KB
/
Utilities.java
File metadata and controls
84 lines (72 loc) · 2.47 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
package utils;
import java.util.Scanner;
/**
* Classe Utilitats per a ser usada amb els mètodes statics.
* @author Raimon Izard
*/
public final class Utilities {
// L'atribut Scanner no ha de poder ser accedit des de fora
private static final Scanner llegir = new Scanner(System.in);
/**
* El constructor private no permet crear objectes de la classe.
* Tots els seus mètodes hauràn de ser a nivell de classe (static)
*/
private Utilities(){};
/**
* Mètode per a llegir enters amb control de tipatge de dades
* @param missatge Missatge a mostrar a l'usuari
* @return int capturat per teclat
*/
public static int llegirInt(String missatge) {
int x = 0;
boolean valorCorrecte = false;
do{
System.out.println(missatge);
valorCorrecte = llegir.hasNextInt();
if (!valorCorrecte){
System.out.println("ERROR: Valor no enter.");
llegir.nextLine();
}else{ // Tinc un enter
x = llegir.nextInt();
llegir.nextLine();
}
}while(!valorCorrecte);
return x;
}
/**
* Mètode per a llegir enters amb control de tipatge de dades i domini de valors
* @param missatge Missatge a mostrar a l'usuari
* @param min Valor mínim acceptat
* @param max Valor màxim acceptat
* @return int capturat per teclat
*/
public static int llegirInt(String missatge, int min, int max) {
int x = 0;
boolean valorCorrecte = false;
do{
System.out.println(missatge);
valorCorrecte = llegir.hasNextInt();
if (!valorCorrecte){
System.out.println("ERROR: Valor no enter.");
llegir.nextLine();
}else{ // Tinc un enter
x = llegir.nextInt();
llegir.nextLine();
if (x < min || x > max){
System.out.println("Opció no vàlida");
valorCorrecte = false;
}
}
}while(!valorCorrecte);
return x;
}
/**
* Mètode per a llegir frases
* @param missatge Missatge a mostrar a l'usuari
* @return retorna una frase String
*/
public static String llegirString(String missatge){
System.out.println(missatge);
return llegir.nextLine();
}
}