Reputation: 5286
The problem I'm having is that a Vehicle object is not being deleted from an array (I'm not sure if it's even storing correctly.)
The user must first add a Vehicle object to the array. And then it brings them back to the menu to ask if they want to add another, or delete an Vehicle.
The method in this class accepts a choice (either 1 or 2) to add a Vehicle to an array, or delete a vehicle from an array:
private void validate(int choice, File file) throws FileNotFoundException
{
Vehicle[] vehicle = new Vehicle[4];
switch(choice)
{
case 1:
store = new Store(file);
store.addVehicle(vehicle);
run();
break;
case 2:
store = new Store();
if(store.deleteVehicle(vehicle) == false)
{
System.out.println("Vehicle not deleted");
}
run();
break;
}
Let's say the user wants to add a vehicle. It gets passed to the Store class: My declarations in the Store class:
Vehicle[] vehicles = new Vehicle[4];
The addVehicle method in the Store class:
public void addVehicle(Vehicle vehicle[]) throws FileNotFoundException
{
boolean found = false;
if(canAddVehicle())
{
for(int i = 0; i < vehicles.length || !found; i++)
{
if(vehicles[i] == null)
{
Scanner reader = new Scanner(file);
vehicle[i] = new Vehicle();
vehicle[i].readRecord(reader);
found = true;
reader.close();
}
}
System.out.println("Vehicle Added!");
}
else
{
System.out.println("You can not add more than 4 vehicles.");
}
System.out.println(vehicle[0].getVIN());
}
I added that last line at the bottom to test if it was storing the variables in the Vehicles class from when I called readRecord(). (It shows up correctly.)
Now it will go back to the first method posted and let's say the user wants to delete a Vehicle. It gets passed to the Store class method (deleteVehicle(vehicle)):
public boolean deleteVehicle(Vehicle vehicle[])
{
System.out.println(vehicle[0].getVIN());
Scanner input = new Scanner(System.in);
String VIN = null;
System.out.println("Please enter the VIN of the vehicle you want to delete:");
VIN = input.next();
for(int i = 0; i < vehicles.length; i++)
{
if(vehicle[i] != null)
{
if(vehicle[i].getVIN().equals(VIN))
{
vehicle[i] = null;
return true;
}
}
}
return false;
}
I added that line at the top to see if there was still a value for the VIN in slot 0 of the vehicles array... it turns out I get a NullPointerException error there... so it seems the first Vehicle object added is not being stored in the array correctly? I'm new to arrays and kind of lost as to what I am doing wrong. Any help would be appreciated. Thanks!
Upvotes: 0
Views: 255
Reputation: 9320
case 2:
store = new Store();
if(store.deleteVehicle(vehicle) == false)
{
System.out.println("Vehicle not deleted");
}
run();
break;
Since you set store to new Store() every time, this is a fresh new instance of the Store class, so there is nothing to be deleted.
Upvotes: 4
Reputation: 11162
The design here is rather frightening. A lot of the functionality in store should be in vehicle, and the store should contain a the array of veichles, it shouldn't be passed to the store. A basic design would be something like this:
class Vehicle {
public Vehicle(/*parameters*/)
static Vehicle readVehicle();
public boolean equals(Vehicle other);
// a ton of things to deal with vehicle data.
}
class Store {
//an array of vehicles, or an arrayList of vehicles.
public void addVehicle(Vehicle v);
public boolean removeVehicle(Vehicle v);
}
Now that your data structures make more sense, we can move on to what the actual algorithm should be. I assume that you want only one store, which might be initialized elsewhere, but which will be modified as you read and delete more vehicles.
validate()
is a confusing choice for a name, because you're not validating the input, you're actually changing your data. In psudeocode, I think you want something like this:
Store s = new Store(file);
int choice;
while((choice=getChoice())!=CHOICE_QUIT)
runChoice(choice, s);
with the new runChoice function looking something like
void runChoice(int choice, Store store){
//perhaps this is not appropriate for all choices?
//Where would you move it if not?
Vehicle v = Vehicle.readVehicle();
switch(choice){
//i think you know what to do ....
}
}
Upvotes: 0
Reputation: 3095
Tyr using ArrayList and
ArrayList<Vehicle> vehicleList=new ArrayList<Vehicle>();
//Add vehicle
vehicleList.add(new Vehicle());
vehicleList.remove(index);
Using this you can have any nos of elements in your list.
Upvotes: 0
Reputation: 764
It seems to me that the problem might not be your array logic at all, but the fact that you are using different "store" objects in each call to your validate method.
Upvotes: 1
Reputation: 28697
Arrays store references to objects. You can modify these objects however you'd like (maybe even "marking" them as deleted, but nothing that you do to the objects contained within the array will affect the array itself. Furthermore, you can't just add or remove items from an Array - all you can do is replace them. (So you could set a given array position to null
- but then you'd have to remember to always check for this.)
If you really need to add or remove elements from an array, you need to re-create the array to the appropriate size, then copy over the remaining elements. There are built-in methods with the Java API to help with many of these operations. Alternatively, use a Collection - e.g. an ArrayList
.
Upvotes: 0
Reputation: 94645
To remove an element from an array is bit difficult. You can use System.arraycopy method to move/copy some of elements from source array to the destination array. However, I suggest to use ArrayList<T>
instead of arrays.
Upvotes: 0