Hello, I got an car inventory program that stores the names of cars etc. I thin a class arraylist would work better. Like an array that can "grow" and "shrink". But I do not know how to convert the code from array to arraylist. It should still keep the member methods I'm having there now. The get(), add(), remove(). etc.
Can somebody tell me how to do this? This is just a part of the code. The other parts are fine and don't need to be converted.
Thank you
package assignment1solution;
import java.util.ArrayList;
public class Inventory
{
private ArrayList<Car> data;
public Inventory()
{
data = new ArrayList<Car>();
}
public boolean isExist(String vin)
{
if (data.size()>0)
{
for(int i=0; i<data.size(); i++)
{
if(data.get(i).getVIN().equals(vin))
{
return true;
}
}
}
else
{
return false;
}
return false;
}
public void addRecord(Car record)
{
if ( data.size()<20)
{
boolean found = false;
for(int i=0; i<data.size(); i++)
{
if( data.get(i).equals(record) )
{
found = true;
}
}
if( !found && record.getYear()>1980)
{
// VIN Check
data.add(record);
}
else if (found)
{
System.out.println("Existing Record: " + record + " not added!");
}
}
else
{
System.out.println("Inventory Full!");
}
}
public void removeRecord(String vin)
{
if (data.size()>0)
{
if( isExist(vin))
{
for(int i=0; i<data.size(); i++)
{
if( data.get(i).getVIN().equals(vin) )
{
data.remove(i);
System.out.println("Car removed successfully!");
break;
}
}
}
}
else
{
System.out.println("No record in the inventory!");
}
}
public void showRecords()
{
if (data.size()>0 )
{
for(int i=0; i<data.size(); i++)
{
System.out.println( data.get(i) );
}
}
else
{
System.out.println("No record in the inventory!");
}
}
public void showRecords(int year, String model)
{
if (data.size()>0 )
{
for(int i=0; i<data.size(); i++)
{
if( (data.get(i).getYear()==year) && (data.get(i).getModel().equals(model) ) )
{
System.out.println( data.get(i) );
}
}
}
else
{
System.out.println("No record in the inventory!");
}
}
}