Reputation: 33
I stuck with the logic and need help for my project.
I have a text file with 5 vehicle names in it.
Then I have to read them into an ArrayList
, which I did 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 every item (vehicle name) in the ArrayList
, which I did using:
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 have attributes: power
, angle
, speed
;
and behaviours: powerOn()
, powerOff()
, SpeedUp()
, SlowDown()
, etc.. which I defined in Vehicle
class.
Now, the user has to enter Vehicle name command. Then I split them and took [0]
, which is the vehicle name, and have to first check if it is one of those vehicle names in the ArrayList
(derived from txt file), and if it is then have to refer it to that particular object I created above. And then have to take in the [1]
, which is command that corresponds to the particular behaviour of object, which can 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, let's consider there are 5 vehicles in the txt (thereby in the ArrayList
) named: Audi, Bmw, Mercedez, Volkswagen, Porsche.
The problem is that whatever I enter in the vehicle name command, the program by default is taking the last element, in this case Porsche in the ArrayList
.
I.e., when I say "Audi, powerON", it still prints out "Porsche is powered ON."
I think I messed up in linking the user entered name to check with the ArrayList
and then refering it to that particular object.
Upvotes: 3
Views: 134
Reputation: 13232
You might want to try storing the vehicles in a 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 key,value
pair in your case key=carname
value=Vehicle object
. This will make it easy to go back and select the car you want based on the user input. Also currently your not storing your Vehicle
objects you are only storing the last one...
Upvotes: 0
Reputation: 1110
Keep 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 your second for loop
retrieve 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;
...
...
Upvotes: 1