49 lines
1.3 KiB
Java
49 lines
1.3 KiB
Java
/*
|
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
|
*/
|
|
package eserciziogarage;
|
|
|
|
import java.util.Scanner;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
/**
|
|
*
|
|
* @author Verde
|
|
*/
|
|
public class Auto {
|
|
private final String regexTarga = "[A-Z]{2}[0-9]{3}[A-Z]{2}";
|
|
String targa;
|
|
Scanner sc = new Scanner(System.in);
|
|
|
|
public Auto() throws Exception{
|
|
System.out.println("Inserire la targa dell'auto: ");
|
|
this.targa = VerificaTarga(sc.nextLine().toUpperCase());
|
|
}
|
|
|
|
private String VerificaTarga(String p_targa) throws Exception{
|
|
String ritorno = "";
|
|
|
|
if (!this.ControlloRegEx(p_targa, regexTarga)) {
|
|
throw new Exception("Errore: targa non valida. Riprovare.");
|
|
} else {
|
|
ritorno = p_targa;
|
|
}
|
|
return ritorno;
|
|
}
|
|
private boolean ControlloRegEx(String p_stringa, String p_regex) {
|
|
Pattern pattern = Pattern.compile(p_regex);
|
|
Matcher matcher = pattern.matcher(p_stringa);
|
|
return matcher.find();
|
|
}
|
|
|
|
public String GetTarga(){
|
|
return this.targa;
|
|
}
|
|
|
|
public void SetTarga(String p_targa){
|
|
this.targa = p_targa;
|
|
}
|
|
}
|