Età media per colore
This commit is contained in:
@@ -5,10 +5,11 @@ import java.util.Calendar;
|
||||
public class Automobile {
|
||||
private String numeroTelaio;
|
||||
private Calendar dataDiVendita;
|
||||
private String colore;
|
||||
private String colore;
|
||||
|
||||
private double prezzoDiVendita;
|
||||
private int etaAcquirente;
|
||||
|
||||
|
||||
public Automobile(String numeroTelaio, Calendar dataDiVendita, String colore, double prezzoDiVendita,
|
||||
int etaAcquirente) {
|
||||
this.numeroTelaio = numeroTelaio;
|
||||
@@ -18,4 +19,11 @@ public class Automobile {
|
||||
this.etaAcquirente = etaAcquirente;
|
||||
}
|
||||
|
||||
public String getColore() {
|
||||
return colore;
|
||||
}
|
||||
|
||||
public int getEtaAcquirente() {
|
||||
return etaAcquirente;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.InputMismatchException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.ListIterator;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
@@ -51,7 +52,7 @@ public class ConcessionariaLista {
|
||||
pausa();
|
||||
break;
|
||||
case 2:
|
||||
|
||||
System.out.println(etaMediaPerColore(concessionaria));
|
||||
pausa();
|
||||
break;
|
||||
case 3:
|
||||
@@ -454,4 +455,29 @@ public class ConcessionariaLista {
|
||||
|
||||
return etaAcquirente;
|
||||
}
|
||||
|
||||
private static String etaMediaPerColore(LinkedList<Automobile> concessionaria) {
|
||||
String etaMediaPerColore;
|
||||
String colore = colore();
|
||||
ListIterator<Automobile> iterator = concessionaria.listIterator();
|
||||
int etaTotale = 0;
|
||||
int quantitaEta = 0;
|
||||
Automobile corrente;
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
corrente = iterator.next();
|
||||
if (corrente.getColore().equals(colore)) {
|
||||
etaTotale += corrente.getEtaAcquirente();
|
||||
quantitaEta++;
|
||||
}
|
||||
}
|
||||
|
||||
if (quantitaEta == 0) {
|
||||
etaMediaPerColore = "Nessuno acquista un'auto di colore " + colore.toLowerCase();
|
||||
} else {
|
||||
etaMediaPerColore = "L'età media di chi acquista un'auto di colore " + colore.toLowerCase() + " è " + (etaTotale/quantitaEta);
|
||||
}
|
||||
|
||||
return etaMediaPerColore;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user