Divide classes with parameters with Java

0

I have this code that calls a file txt , stores it in a ArrayList , and finally unordered and sorts all the characters in it.

public static void main (String args []) throws IOException{
   String linea = "PALABRA2.TXT";
   ArrayList<String> archivoLista = new ArrayList<>();
   try (BufferedReader br = new BufferedReader(new  FileReader("Archivo.txt")))  {
        while ((linea = br.readLine()) != null) {
             System.out.println(linea);
            archivoLista.add(linea); /* Agrega la Línea leída a la lista */
        }
   }
   System.out.println("LISTA DESORDENADA: ");
   Collections.shuffle(archivoLista); /* Modifica Orden*/
   /* Imprimir en una sola línea */
   for(String temp: archivoLista){    
      System.out.println(temp); 
   }
   /* Ordenar nuevamente*/
   System.out.println("LISTA ORDENADA: ");
   Collections.sort(archivoLista);
   for(String temp: archivoLista){
    System.out.println(temp);
   }
 }
}

What I want is to improve the design of the code. Someone here advised me to divide each function into different classes.

I need the first one with the method main to show the file txt in a Arraylist , the second one receives that List<String> and messes it up, and finally the third one receives it and orders it again.

The issue is that I do not know how it is done so that the classes have as parameter the arrayList of main . And so perform ordering and messing up.

    
asked by Mario 16.06.2017 в 02:23
source

1 answer

0

In this case having a class to sort or sort a list does not seem necessary to me since you are not doing more than invoking Collections.shuffle and Collections.sort , and this does not seem enough responsibility / behavior to build a class .

If, in the future, other forms of disorder or ordering appear in your requirements, perhaps it makes sense to have a class with the responsibility of ordering collections and another class to disarrange.

In this case it would be better to simply have a single class, separate the 3 actions into 3 methods of that class to be able to invoke part of the logic of your program separately and from other points of a system.

Still, responding promptly to your query:

  

The issue is that I do not know how it is done so that the classes have as parameter the arrayList of the main. And so perform ordering and messing up.

One possible solution could be the following:

File reading and list creation:

 public class TextFileReader {

    private List<String> readFromFile(String fileName) throws FileNotFoundException, IOException{
        ArrayList<String> archivoLista = new ArrayList<>();
        String linea = "";
        try (BufferedReader br = new BufferedReader(new FileReader(fileName))){
            while ((linea = br.readLine()) != null) {
                archivoLista.add(linea); 
            }
        }
        System.out.println(archivoLista.toString());
        return archivoLista;
    }
 }

List ordering methods:

public class Shuffler {
    public void shuffle(List<String> list){
        Collections.shuffle(list);
        System.out.println(list.toString());
    }

    /*
     * More shuffle methods..
     */
}

List ordering methods:

public class Sorter {

    public void sort(List<String> list){
        Collections.sort(list);
        System.out.println(list.toString());

    }

    /*
     * More sort methods..
     */
}

Main class , instantiation and invocation of previous classes:

public class MainClass {

    public static void main (String args []) throws IOException{
            TextFileReader test = new TextFileReader();
            Sorter sorter = new Sorter();
            Shuffler shuffler = new Shuffler();
            List<String> archivoLista = test.readFromFile("src/Archivo.txt");
            shuffler.shuffle(archivoLista);
            sorter.sort(archivoLista);
    }
}
    
answered by 16.06.2017 / 02:47
source