Home > Enterprise >  How to create different objects from a List of Strings?
How to create different objects from a List of Strings?

Time:10-13

I have a file with a various figures like this:

SQUARE;10

SQUARE;20

RECTANGLE;10;30

CIRCLE;10

After adding the lines to the String List, I would like it to create all the objects after one run through the list. how to do it when, for example, a rectangle has two parameters and the rest only one?

So far this is my code which works but it is buggy, because for each type of figure I have to go through the list again, I also can't use switch for this task.

    BufferedReader br = new BufferedReader(new FileReader("figury.txt"));
    List<List<String>> list1 = new ArrayList<>();
    String s;
    while ((s = br.readLine()) != null) {
        List<String> list = Arrays.asList(s.split(";"));
        list1.add(list);

    }
    list1.stream().filter(k -> k.contains("SQUARE")).forEach(i -> new Square(Integer.parseInt(i.get(1))));
    list1.stream().filter(k -> k.contains("CIRCLE")).forEach(i -> new Circle(Integer.parseInt(i.get(1))));
    list1.stream().filter(k -> k.contains("RECTANGLE")).forEach(i -> new Rectangle(Integer.parseInt(i.get(1)), Integer.parseInt(i.get(2))));

All figures are subClasses of abstract class Figure

CodePudding user response:

Try this

        Map<String, Function<List<String>, Figure>> converters = new HashMap<>();
        converters.put("SQUARE", l -> new Square(Integer.parseInt(l.get(1))));
        // Repeat for each shape

        List<Figure> list = list1.stream().map(l -> converters.get(l.get(0)).apply(l))
                .collect(Collectors.toList());

CodePudding user response:

check this out :

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public static void main(String[] args) throws IOException {

        

        BufferedReader br = new BufferedReader(new FileReader("figury.txt"));
        List<Object> list = new ArrayList<>();
        String s;
        while ((s = br.readLine()) != null) {
            String[] splitItems = s.split(";");
            String objectType = splitItems[0].toUpperCase();
            switch (objectType) {
                case "SQUARE" : {
                    list.add(new Square(splitItems[1]));
                    break;
                }
                case "RECTANGLE" : {
                    list.add(new Rectangle(splitItems[1], splitItems[2]));
                    break;
                }
                case "CIRCLE" : {
                    list.add(new Circle(splitItems[1]));
                    break;
                }
                default: {
                    //do nothing
                }
            }
        }

    }

CodePudding user response:

I would use a FigureFactory

class FigureFactory {
    static Figure fromStringList(List<String> list) {
        if (list.size() > 0) {
            switch(list.get(0)) {
                case "SQUARE" :
                    return new Square(Integer.parseInt(list.get(1)))
                case "CIRCLE" :
                    ....
            }
        } 
    }
}

Then use it like this :

    list1.stream().map(FigureFactory::fromStringList);

CodePudding user response:

You can do in this way by using the switch case approach:

list1.forEach(list -> {
            String shape = list.get(0);
            switch (shape){
                case "SQUARE":
                    System.out.println("It is square");
                    new Square(Integer.parseInt(list.get(1)));
                    break;
                case "CIRCLE":
                    System.out.println("It is circle");
                    new Circle(Integer.parseInt(list.get(1)));
                    break;
                case "RECTANGLE":
                    System.out.println("It is rectangle");
                    new Rectangle(Integer.parseInt(list.get(1)), Integer.parseInt(list.get(2)));
                    break;
                default:
                    break;
            }
        });

Output:

It is square
It is square
It is circle
It is rectangle
  • Related