public ArrayList<Vehicle> getVehiclesSoldRecently()
{
ArrayList<Vehicle> a=new ArrayList<>();
ListIterator<Vehicle> it= list.listIterator();
while(it.hasNext())
{
Vehicle b=it.next();
if(b.getAgeSolding()<=14)
{
a.add(b);
}
}
return a;
}
J'ai créé cette méthode afin de créer une ArrayList des véhicules vendus au cours des 14 derniers jours, et j'ai un problème. La méthode getAgeSolding
fonctionne parfaitement, et la condition dans le if
est vérifiée aussi, dans le cas où j'applique cette méthode.Ajouter des objets à un arraylist
Mais pourquoi le b
n'est-il pas ajouté à ArrayList si la condition est vérifiée? J'obtiens une ArrayList vide dans tous les cas. Pourquoi?
la classe de véhicule
package model;
import java.time.*;
import java.time.temporal.ChronoUnit;
public class Vehicle
{
private String manufacturer, model, VIN;
private LocalDate datemanuf, datesold;
private Customer cust;
private boolean sold;
private final char taxband;
private final int price;
public Vehicle(String manufacturer, String model, String VIN, LocalDate datemanuf, char taxband, int price)
{
this.manufacturer = manufacturer;
this.model = model;
this.VIN = VIN;
this.datemanuf = datemanuf;
this.taxband = taxband;
this.price = price;
this.cust=null;
this.datesold=null;
this.sold=false;
}
public String getManufacturer()
{
return manufacturer;
}
public String getModel()
{
return model;
}
public Customer getCust()
{
return cust;
}
public String getVIN()
{
return VIN;
}
public LocalDate getDatemanuf()
{
return datemanuf;
}
public LocalDate getDatesold()
{
return datesold;
}
public boolean isSold()
{
return sold;
}
public char getTaxband()
{
return taxband;
}
public int getPrice()
{
return price;
}
public void buy(Customer cust, LocalDate datesold)
{
this.cust=cust;
this.datesold=datesold;
this.sold =true;
}
public long getAgeOfTheVehicle()
{
LocalDate Now=LocalDate.now();
long a=datemanuf.until(Now,ChronoUnit.WEEKS);
return a;
}
public long getAgeSolding()
{
LocalDate Now=LocalDate.now();
long a=datesold.until(Now,ChronoUnit.DAYS);
return a;
}
@Override
public String toString()
{
String str1="";
String str2;
if(sold==true)// TODO code application logic here
{
str1="Vehicle owned by "+cust.getName()+" since "+datesold;
}
switch(taxband)
{
case 'A':
str2="0-100";
break;
case 'B':
str2="101-110";
break;
case 'C':
str2="111-120";
break;
case 'D':
str2="121-130";
break;
case 'E':
str2="131-140";
break;
case 'F':
str2="141-150";
break;
case 'G':
str2="151-160";
break;
default:
str2="";
}
return "Manufacturer: "+manufacturer+"\n"+"Model: "+model+"\n"+"VIN: "+VIN+"\n"+"Date of manufacture: "+datemanuf+"\n"+"Price :"+price+" £\n"+"Tax Band: "+str2+"\n"+"Age of Vehicle: "+this.getAgeOfTheVehicle()+" weeks.\n"+str1+"\n";
}
}
la classe Salle d'exposition
public class Showroom
{
private ArrayList<Vehicle> list;
private int position;
public Showroom()
{
this.list =new ArrayList<>();
this.position=1;
}
public int getPosition()
{
return position;
}
public ArrayList<Vehicle> getList()
{
return list;
}
public boolean add(Vehicle v)
{
list.add(v);
return true;
}
public Vehicle find(String VIN)
{
ListIterator<Vehicle> it= list.listIterator();
int n=1;
while(it.hasNext())
{
Vehicle a=it.next();
if(a.getVIN().equalsIgnoreCase(VIN))
{
this.position=n;
return a;
}
n++;
}
return null;
}
public Vehicle next()
{
int n=this.position;
ListIterator<Vehicle> it= list.listIterator(n);
Vehicle a=it.next();
position++;
return a;
}
public Vehicle previous()
{
int n=this.position;
ListIterator<Vehicle> it= list.listIterator(n-1);
Vehicle a=it.previous();
position--;
return a;
}
public Vehicle current()
{
int n=this.position;
ListIterator<Vehicle> it= list.listIterator(n);
Vehicle a=it.previous();
return a;
}
public ArrayList<Vehicle> getVehiclesSoldRecently()
{
ArrayList<Vehicle> a=new ArrayList<>();
ListIterator<Vehicle> it= list.listIterator();
while(it.hasNext())
{
Vehicle b=it.next();
if(b.getAgeSolding()<=14)
{
a.add(b);
}
return a;
}
}
Où définissez-vous 'list'? – apnorton
Avez-vous vérifié ce que 'it.next();' renvoie? – DigitalNinja
Il s'agit d'une donnée privée (type ArrayList) dans ma salle d'exposition, où cette méthode est définie – TotorAndMimine