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;
}
I have created this method in order to make an ArrayList of the vehicles sold in the last 14 days, and I have a problem. The method getAgeSolding works perfectly, and the condition in the if is verified too, in the case I apply this method.
But why isn't b added to the ArrayList a if the condition is verified? I obtain an empty ArrayList in every case. Why?
the Vehicle Class
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";
}
}
the Showroom class
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;
}
}
list?it.next();returns?for (Type t : collection)in your casefor (Vehicle v : list) {...}