Hey guys. I am creating an ordered list using an array. Not an ArrayList or anything else. I can not figure out how to get all of the data from the OrderedList constructor to carry through to my "insert" method. Therefore, when I am checking if "count == 0" it knows what count is. Currently, as the code sits, insert does not know any of the variables created in the constructor. I am a little rusty in Java right now and I am hoping it isn't something simple.
Lines 66-70 are commented out to see if lines 57-61 would allow the program to recognized the data it is testing for in the if statements below it. If anybody could help get a jump start to a solution I would greatly appreciate it.
/**
*
* @author Harry
*
*/
class Vehicle
{
public Vehicle(int x)
{
vin = x;
d = this.getDescription();
}
private int vin;
private static String d;
private int count = 0;
public int getVin()
{
return vin;
}
public String getDescription()
{
return d;
}
public int getCount()
{
return count;
}
public static void setDescription(String describe)
{
d = describe;
}
// main: driver for vehicle class\
// public static void main(String[] argv)
// {
// Vehicle v = new Vehicle(1234567890);
// System.out.println("VIN Number: " + v.getVin());
//
// setDescription("Silver four-door sports sedan");
// System.out.println("Description: " + v.getDescription());
// }
}
class OrderedList
{
public OrderedList(Vehicle v)
{
int vin = v.getVin();
int max = 5;
Vehicle[] array;
array = new Vehicle[max];
int count = v.getCount();
}
// insert vehicle to array
public void insert(Vehicle v)
{
// int vin = v.getVin();
// int max = 5;
// Vehicle[] array;
// array = new Vehicle[max];
// int count = v.getCount();
// Array at max capacity
if (count == max)
{
System.out.println("List of vehicles is full.");
return;
}
// Array is empty
if (count == 0)
{
// Add first vehicle to list
array[0] = v;
count++;
}
// Array has Vehicles in it
if (count > 0)
{
// Add next entry in order
for (int i=0; i<count; i++)
{
// Duplicate
if (vin == array[i].getVin())
{ System.out.println("Vehicle with same VIN is already in the list.");
return;
}
// New VIN is numerically lower than corresponding
// vehicle in the array.
if (vin < array[i].getVin())
{
// Shift elements right, add Vehicle
for (int j=0; j < count; j++)
{
array[j] = v;
Vehicle tmp = array[j];
array[j] = array[j+1];
array[j] = tmp;
count++;
}
}
// New VIN is numerically higher than corresponding
// vehicle in the array.
if (vin > array[i].getVin())
{
// Shift elements left, add Vehicle
for (int k = 0; k < count; k--)
{
array[k] = v;
Vehicle tmp = array[k];
array[k] = array[k-1];
array[k] = tmp;
count++;
}
}
}
}
}
// Remove Vehicle from list
public void remove(int id)
{
int count = v.getCount();
int vin = id;
for (int i=0; i<count; i++)
{
// ALGORITHM TO REMOVE
// VEHICLE GOES HERE...
}
}
// Find Vehicle in list
boolean find(int vin)
{
// ALGORITHM TO FIND
// VEHICLE GOES HERE...
}
}