Opzione 1
This commit is contained in:
21
src/concessionarialista/Automobile.java
Normal file
21
src/concessionarialista/Automobile.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package concessionarialista;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
public class Automobile {
|
||||
private String numeroTelaio;
|
||||
private Calendar dataDiVendita;
|
||||
private String colore;
|
||||
private double prezzoDiVendita;
|
||||
private int etaAcquirente;
|
||||
|
||||
public Automobile(String numeroTelaio, Calendar dataDiVendita, String colore, double prezzoDiVendita,
|
||||
int etaAcquirente) {
|
||||
this.numeroTelaio = numeroTelaio;
|
||||
this.dataDiVendita = dataDiVendita;
|
||||
this.colore = colore;
|
||||
this.prezzoDiVendita = prezzoDiVendita;
|
||||
this.etaAcquirente = etaAcquirente;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,7 +4,12 @@
|
||||
*/
|
||||
package concessionarialista;
|
||||
|
||||
import java.util.Date;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.InputMismatchException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
@@ -19,15 +24,17 @@ public class ConcessionariaLista {
|
||||
|
||||
static Scanner sc = new Scanner(System.in);
|
||||
static final String ERRORE_GENERICO = "Errore: opzione non valida.";
|
||||
static final String FORMATO_DATA = "dd/MM/yyyy";
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main() {
|
||||
int scelta = -1;
|
||||
LinkedList<Automobile> concessionaria = new LinkedList<>();
|
||||
|
||||
do {
|
||||
System.out.println("Scegliere un'opzione:");
|
||||
System.out.println("1. ");
|
||||
System.out.println("2. ");
|
||||
System.out.println("3. ");
|
||||
System.out.println("1. Inserisci auto");
|
||||
System.out.println("2. Calcola età media in base al colore");
|
||||
System.out.println("3. Media prezzo auto blu in periodo di tempo");
|
||||
System.out.println("0. Esci");
|
||||
System.out.print("Opzione: ");
|
||||
|
||||
@@ -39,7 +46,8 @@ public class ConcessionariaLista {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
|
||||
inserisciAuto(concessionaria);
|
||||
System.out.println("Automobile inserita correttamente.");
|
||||
pausa();
|
||||
break;
|
||||
case 2:
|
||||
@@ -55,8 +63,7 @@ public class ConcessionariaLista {
|
||||
pausa();
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (InputMismatchException _) {
|
||||
} catch (InputMismatchException _) {
|
||||
System.out.println(ERRORE_GENERICO);
|
||||
pausa();
|
||||
}
|
||||
@@ -67,4 +74,103 @@ public class ConcessionariaLista {
|
||||
System.out.println("Premere un tasto per continuare. . .");
|
||||
sc.nextLine();
|
||||
}
|
||||
|
||||
private static void inserisciAuto(LinkedList<Automobile> concessionaria) {
|
||||
String numeroTelaio;
|
||||
Calendar dataDiVendita = Calendar.getInstance();
|
||||
String colore;
|
||||
double prezzoDiVendita;
|
||||
int etaAcquirente;
|
||||
|
||||
System.out.print("Inserire il numero di telaio: ");
|
||||
numeroTelaio = sc.nextLine();
|
||||
|
||||
dataDiVendita.setTime(dataDiVendita());
|
||||
|
||||
System.out.print("Inserire il colore: ");
|
||||
colore = sc.nextLine();
|
||||
|
||||
prezzoDiVendita = prezzoDiVendita();
|
||||
|
||||
etaAcquirente = etaAcquirente();
|
||||
|
||||
concessionaria.add(new Automobile(numeroTelaio, dataDiVendita, colore, prezzoDiVendita, etaAcquirente));
|
||||
}
|
||||
|
||||
private static Date dataDiVendita() {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(FORMATO_DATA);
|
||||
boolean error;
|
||||
Date data = null;
|
||||
sdf.setLenient(false);
|
||||
|
||||
do {
|
||||
error = false;
|
||||
System.out.print("Inserire la data di vendita nel formato " + FORMATO_DATA + ": ");
|
||||
try {
|
||||
data = sdf.parse(sc.nextLine());
|
||||
} catch (ParseException _) {
|
||||
System.out.println("Errore: data non valida.");
|
||||
pausa();
|
||||
error = true;
|
||||
}
|
||||
} while (error);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
private static double prezzoDiVendita() {
|
||||
double prezzoDiVendita = 0;
|
||||
boolean error;
|
||||
|
||||
do {
|
||||
error = false;
|
||||
try {
|
||||
System.out.print("Inserisci il prezzo di vendita: ");
|
||||
prezzoDiVendita = sc.nextDouble();
|
||||
sc.nextLine();
|
||||
|
||||
if (prezzoDiVendita <= 0) {
|
||||
System.out.println("Errore: il prezzo di vendita non può essere negativo.");
|
||||
pausa();
|
||||
error = true;
|
||||
}
|
||||
} catch (InputMismatchException _) {
|
||||
System.out.println(ERRORE_GENERICO);
|
||||
pausa();
|
||||
error = true;
|
||||
}
|
||||
} while (error);
|
||||
|
||||
return prezzoDiVendita;
|
||||
}
|
||||
|
||||
static int etaAcquirente() {
|
||||
int etaAcquirente = 0;
|
||||
boolean error;
|
||||
final int ETA_MINIMA = 18;
|
||||
final int ETA_MASSIMA = 110;
|
||||
|
||||
do {
|
||||
error = false;
|
||||
try {
|
||||
System.out.print("Inserisci l'età dell'acquirente: ");
|
||||
etaAcquirente = sc.nextInt();
|
||||
sc.nextLine();
|
||||
|
||||
if (etaAcquirente < ETA_MINIMA || etaAcquirente > ETA_MASSIMA) {
|
||||
System.out.println(
|
||||
"Errore: l'età dell'acquirente deve essere compresa tra " + ETA_MINIMA + " e " + ETA_MASSIMA
|
||||
+ ".");
|
||||
pausa();
|
||||
error = true;
|
||||
}
|
||||
} catch (InputMismatchException _) {
|
||||
System.out.println(ERRORE_GENERICO);
|
||||
pausa();
|
||||
error = true;
|
||||
}
|
||||
} while (error);
|
||||
|
||||
return etaAcquirente;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user