0

Basically I am working on a project where certain class attributes and names of classes are stored in a text file. The goal here is to create a list of the objects of a certain data type (in this case Car) listed in the text file (of which I have already done), and then assign these to the data types within the text file. Below is an example of a text file that I would work with:


Car: 2

4 1 1 Red 3 80.5 20 60 2 aadawd

1 3 2 Blue 3 80 30 20 1 aaxzd

Bike: 3

2 1

2 2

2 3


Basically, 'Car: 2' indicates that we have 2 cars. The two lines below it indicate the attributes of the respective cars. To not waste time on unnecessary information I'll just list the data types of the lines below Car: <int, int, int, String, int, double, double, double, int, char[]>

Getters/setters are already established for the Car classes.

So far, I converted the file into an ArrayList called list, where each line of the file represents is an element of the array. This allowed me to create an ArrayList of type Car quite easily. All I need now is to set the attributes of the Car. Any thoughts?

            for (int i = 0; i < list.size(); i++) {


                if (list.get(i).contains("Red")) {

                    

                    carList.add(new RedCar());
                    
                    

                } else if (list.get(i).contains("Blue")) {

                    

                    carList.add(new BlueCar());
                    

                }  else if (list.get(i).contains("Yellow")) {


                    carList.add(new YellowCar());
                    

                }

            }





1
  • you need to get 2 lists 1 for car 1 for bike and then split them using space then you can add those in setters of your cars Commented Sep 9, 2020 at 7:13

1 Answer 1

0

As far as I can see, you have everything you need. To fill your carlist with properties you have to create an object and put it on a variable before pushing it to a list.

...
if (list.get(i).contains("Red")) {
  //hopefully all cars inherts from Car
  Car c = new RedCar();
  final String[] attributes = list.get(i).split(" ");
  c.setFirstAttr(attributes[0]);
  c.setSecondAttr(attributes[1]);
  //and so on
  carList.add(c);
}

It could be a better way to use the color of the car as an attribute. Because from this perspective it looks like that the cars are all the same, except their color.

//you dont have to decide which type if car you create
for (int i = 0; i < list.size(); i++) {
  //hopefully all cars inherts from Car
  Car c = new Car();
  final String[] attributes = list.get(i).split(" ");
  c.setFirstAttr(attributes[0]);
  //and so on
  c.setColor(attributes[3]);
  carList.add(c);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect, thank you. Just a quick question though, why use 'final' on the attributes array? What would the difference be if this access modifier was removed?
Final is used when the variable will not be changed in the program. It is recommended to use always final when you declare variables, because it is clearer for the reader if variables are changing or not. I guess it is also better for the compiler. Always try to make final variables and private fields. IntelliJ helps a lot in this direction ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.