Miglioria IngressoAuto e aggiunta UscitaAuto
This commit is contained in:
parent
52fe4e6334
commit
13922530b7
@ -4,16 +4,40 @@
|
|||||||
*/
|
*/
|
||||||
package eserciziogarage;
|
package eserciziogarage;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Verde
|
* @author Verde
|
||||||
*/
|
*/
|
||||||
public class Auto {
|
public class Auto {
|
||||||
|
private final String regexTarga = "[A-Z]{2}[0-9]{3}[A-Z]{2}";
|
||||||
String targa;
|
String targa;
|
||||||
public Auto(String p_targa){
|
Scanner sc = new Scanner(System.in);
|
||||||
this.targa = p_targa;
|
|
||||||
|
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(){
|
public String GetTarga(){
|
||||||
return this.targa;
|
return this.targa;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -51,7 +51,7 @@ public class EsercizioGarage {
|
|||||||
Pausa();
|
Pausa();
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
garage.UscitaAuto();
|
System.out.println(garage.UscitaAuto());
|
||||||
Pausa();
|
Pausa();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|||||||
@ -5,8 +5,6 @@
|
|||||||
package eserciziogarage;
|
package eserciziogarage;
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -34,30 +32,27 @@ public class Garage {
|
|||||||
return p_i;
|
return p_i;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean ControlloRegEx(String p_stringa, String p_regex) {
|
private int TrovaTarga(String p_targa) {
|
||||||
Pattern pattern = Pattern.compile(p_regex);
|
boolean exit = false;
|
||||||
Matcher matcher = pattern.matcher(p_stringa);
|
int i = 0;
|
||||||
return matcher.find();
|
for (; i < this.veicoli.length && !exit; i++) {
|
||||||
|
if (veicoli[i].GetTarga().equals(p_targa)) {
|
||||||
|
exit = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return exit ? --i : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String IngressoAuto() {
|
public String IngressoAuto() {
|
||||||
Scanner sc = new Scanner(System.in);
|
String ritorno;
|
||||||
String targa, ritorno;
|
|
||||||
final String regexTarga = "[A-Z]{2}[0-9]{3}[A-Z]{2}";
|
|
||||||
|
|
||||||
do {
|
|
||||||
System.out.println("Inserire la targa dell'auto: ");
|
|
||||||
targa = sc.nextLine().toUpperCase();
|
|
||||||
if (!ControlloRegEx(targa, regexTarga)) {
|
|
||||||
System.out.println("Errore: targa non valida. Riprovare.");
|
|
||||||
}
|
|
||||||
} while (!ControlloRegEx(targa, regexTarga));
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
veicoli[this.NextPosizioneLibera()] = new Auto(targa);
|
veicoli[this.NextPosizioneLibera()] = new Auto();
|
||||||
ritorno = "L'auto è posteggiata nella posizione %d".formatted(posizioneLibera++);
|
ritorno = "L'auto è posteggiata nella posizione %d".formatted(posizioneLibera++);
|
||||||
} catch (ArrayIndexOutOfBoundsException e) {
|
} catch (ArrayIndexOutOfBoundsException e) {
|
||||||
ritorno = "Errore: il garage è pieno.";
|
ritorno = "Errore: il garage è pieno.";
|
||||||
|
} catch (Exception e) {
|
||||||
|
ritorno = e.getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
return ritorno;
|
return ritorno;
|
||||||
@ -71,6 +66,24 @@ public class Garage {
|
|||||||
return "I posti liberi sono da %d a %d".formatted(--posizioneLibera, veicoli.length);
|
return "I posti liberi sono da %d a %d".formatted(--posizioneLibera, veicoli.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UscitaAuto() {
|
public String UscitaAuto() {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
String targa, ritorno;
|
||||||
|
int posizioneAuto;
|
||||||
|
|
||||||
|
System.out.println("Inserire la targa dell'auto: ");
|
||||||
|
targa = sc.nextLine().toUpperCase();
|
||||||
|
|
||||||
|
posizioneAuto = this.TrovaTarga(targa);
|
||||||
|
if (posizioneAuto != -1) {
|
||||||
|
this.veicoli[posizioneAuto] = null;
|
||||||
|
ritorno = "L'auto è uscita con successo";
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
ritorno = "Errore: targa non trovata. Riprovare.";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return ritorno;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user