ArrayList objects (java)
I was sticking with logic and needed help for my project.
I have a text file with 5 car names. Then I have to read them in ArrayList
which I was using using:
public static void main(String[] args) throws IOException
{
BufferedReader bTextFileVehicleNames = new BufferedReader(new FileReader("vehicles.txt"));
ArrayList<String> vechicleNamesArray = new ArrayList<String>();
while((textFileLine = bTextFileVehicleNames.readLine()) != null)
{
vehicleNamesArray.add(textFileLine);
}
Now I need to create an object for each item (vehicle name) in ArrayList
which I have used with:
for(String s : vehicleNamesArray)
{
newVehicle = new Vehicle(s);
//I defined this Vehicle newVehicle = null; in the main class. My main is also in the same Vehicle.java file.
}
Each of these objects has attributes: power
, angle
, speed
; and behavior: powerOn()
, powerOff()
, SpeedUp()
, SlowDown()
, etc., which I have identified in the classroom Vehicle
.
The user must now enter the Vehicle name command. Then I split them and took [0]
which is the name of the vehicle and must first check if it is one of these vehicle names in ArrayList
(obtained from the txt file) and if this then needs to be attributed to that particular entity that I created higher. And then you need to accept [1]
, which is the command that corresponds to the specific behavior of the object, which may be powerOn
, speedUp
etc. Etc.
for (int i=0; i< vehicleNamesArray.size(); i++)
{
if (vehicleNamesArray.get(i).matches(userVehicleName))
//userVehicleName is the [0] vehicle name entered by user
//userVehicleCommand is the [1] command entered by user
{
switch (userVehicleCommand.toLowerCase())
{
case "power on":
newVehicle.powerOn(); //calling that particular bahaviour of the object defined in Vehicle class.
System.out.println(newVehicle.getName()+" is powered ON.");
break;
...
...
For example, consider that in txt there are 5 vehicles (thus in ArrayList
) named: Audi, Bmw, Mercedez, Volkswagen, Porsche. The problem is that no matter what I enter into the vehicle name command, the program defaults to the last item, in this case Porsche's ArrayList
. Ie when I say "Audi, powerON" it still prints "Porsche on".
I think I messed up the link to the username entered to validate it with ArrayList
and then referencing that particular object.
source to share
Store objects created in List
also:
List<Vehicle> vehicleList = new ArrayList<>;
for(String s : vehicleNamesArray)
{
newVehicle = new Vehicle(s);
vehicleList.add(newVehicle);
//I defined this Vehicle newVehicle = null; in the main class. My main is also in the same Vehicle.java file.
}
Then, in the second, for loop
extract that vehicle object and update it:
for (int i=0; i< vehicleNamesArray.size(); i++)
{
if (vehicleNamesArray.get(i).matches(userVehicleName))
//userVehicleName is the [0] vehicle name entered by user
//userVehicleCommand is the [1] command entered by user
{
switch (userVehicleCommand.toLowerCase())
{
case "power on":
vehicleList.get(i).powerOn(); //calling that particular bahaviour of the object defined in Vehicle class.
System.out.println(vehicleList.get(i).getName()+" is powered ON.");
break;
...
...
source to share
You might want to store vehicles in HashMap
or something:
HashMap<String, Vehicle> vehicles = new HashMap<String, Vehicles>();
for(String s : vehicleNamesArray)
{
vehicles.put(s, new Vehicle(s));
}
//Access the vehicle from user input
Vehicle currentVehicle = vehicles.get(userVehicleName);
switch (userVehicleCommand.toLowerCase())
{
case "power on":
currentVehicle.powerOn(); //calling that particular bahaviour of the object defined in Vehicle class.
System.out.println(currentVehicle.getName()+" is powered ON.");
break;
//etc...
}
A HashMap
can be used to store a pair key,value
in your case key=carname
value=Vehicle object
. This will make it easier to return and select the vehicle you want based on user input. Also currently you are not saving your objects Vehicle
, you are only saving the last one ...
source to share