0
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;

 }

}

5
  • Where do you define list? Commented Apr 17, 2015 at 23:33
  • Have you checked what it.next(); returns? Commented Apr 17, 2015 at 23:36
  • It is a private data (type ArrayList) in my class showroom, where this method is defined Commented Apr 17, 2015 at 23:37
  • So what you are trying to do is to add every element of b where getAgeSolding() returns less than or equal to 14? For this I recommend you to use a foreach loop instead of an iterator. for (Type t : collection) in your case for (Vehicle v : list) {...} Commented Apr 17, 2015 at 23:40
  • I test to put "System.out.println(b.getAgeSolding())" in the loop if, and the result was 6. Commented Apr 17, 2015 at 23:42

2 Answers 2

3

You should use a foreach loop instead of a while loop. Foreach loops are perfect for looking at every item in a list.

for(Vehicle vehicle : list){
    if(vehicle.getAgeSolding()<=14){
        a.add(vehicle);
    }
}

The way you've done it should theoretically work as well. But try converting it to a foreach loop and see if you can tell why it's not working, since this is a more natural way to do it.

If you are sure that list has vehicles inside of it, then the only answer is that none of the vehicles return an int less than or equal to 14.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes i am sure that list has vehicles inside.
Try logging out the result of vehicle.getAgeSolding() for each vehicle inside the loop to see what the result is. If none return a number less than or equal to 14 than it is working correctly.
The only thing I see is that you are returning a long but comparing it to an int? But that shouldn't be a problem. For kicks you can try if(vehicle.getAgeSolding()<=14L), but it probably won't work any differently.
Ok, change your getAgeSolding() method to just return 5; That should work for sure for every vehicle. If it does, then you know there is something strange going on with your time stuff to calculate the number of days.
I tested something like that before, the result is the same!
1

Your code works fine:

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class Main {

    static List<Vehicle> list;

    public static void main(String[] args) {
        list=new ArrayList<>();
        list.add(new Vehicle(10));
        list.add(new Vehicle(20));
        System.out.println(getVehiclesSoldRecently().get(0).getAgeSolding());
    }

    public static 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;

    }

}

The problem has to be elsewhere.

But you could do it elegantly in Java8:

public static List<Vehicle> getVehiclesSoldRecently2(){
    return list.stream()
            .filter(x->x.getAgeSolding()<=14)
            .collect(Collectors.toList());
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.