I have here such a code I was given the task of breaking the method into classes so that polymorphism would be used. Help a good idea what can be done here?

private void start() { System.out.println("ΠŸΠΎΠΌΠΎΡ‰ΡŒ: \"h\""); ArrayList<String> target = null; String command; try (Scanner scanner = new Scanner(System.in)) { command = scanner.nextLine(); while (!"q".equals(command)) { if ("-f".equals(command)) { // ΠΌΠΎΠΆΠ½ΠΎ ΡΠ΄Π΅Π»Π°Ρ‚ΡŒ System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΏΠΎΠ»Π½ΠΎΠ΅ имя Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΈ для поиска Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ \"absolute path.../dir\":"); Dir dir = new Dir(scanner.nextLine()); System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ имя Ρ„Π°ΠΉΠ»Π° для поиска:"); target = dir.searchByName(scanner.nextLine()); target.forEach(System.out::println); } else if ("-r".equals(command)) { System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΏΠΎΠ»Π½ΠΎΠ΅ имя Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΈ для поиска Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ \"absolute path.../dir\":"); Dir dir = new Dir(scanner.nextLine()); System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Ρ‡Π°ΡΡ‚ΡŒ ΠΈΠΌΠ΅Π½ΠΈ Ρ„Π°ΠΉΠ»Π° для поиска:"); target = dir.searchRegExp(scanner.nextLine()); System.out.println("НайдСно:"); target.forEach(System.out::println); } else if ("-o".equals(command)) { if (target != null) { System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΏΠΎΠ»Π½ΠΎΠ΅ имя Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΈ ΠΊΡƒΠ΄Π° ΡΠΎΡ…Ρ€Π°Π½ΠΈΡ‚ΡŒ Ρ„Π°ΠΉΠ» Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ \"absolute path.../dir/\":"); String path = scanner.nextLine(); System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ имя Ρ„Π°ΠΉΠ»Π° Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ \"file.extension\":"); String name = scanner.nextLine(); // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³ ΠΊΡƒΠ΄Π° ΡΠΎΡ…Ρ€Π°Π½ΡΡ‚ΡŒ if (this.exist(path, name)) { this.write(target, path + name); } else { System.out.println("ДСйствиС Π½Π΅ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ."); } } } else if ("h".equals(command)) { printManual(); } else { System.err.println("НСизвСстная ΠΊΠΎΠΌΠ°Π½Π΄Π°."); printManual(); } command = scanner.nextLine(); } } } 
  • one
    Or maybe it would be easier to look at the design pattern of the Команда and make it? - Alexey Shimansky
  • Are you sure that this needs to be replaced by polymorphism? - etki

1 answer 1

You need to declare several classes and one interface. Further, in a while go over the collection of objects implementing this class, compare names and, if they match, call the invoke method

 public interface Command{ String getName(); void invoke(Scanner scanner); } public class FirstCommand implements Command { public String getName(){ return "-f"; } public void invoke(Scanner scanner){ // ΠΌΠΎΠΆΠ½ΠΎ ΡΠ΄Π΅Π»Π°Ρ‚ΡŒ System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΏΠΎΠ»Π½ΠΎΠ΅ имя Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΈ для поиска Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ \"absolute path.../dir\":"); Dir dir = new Dir(scanner.nextLine()); System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ имя Ρ„Π°ΠΉΠ»Π° для поиска:"); ArrayList<String> target = dir.searchByName(scanner.nextLine()); target.forEach(System.out::println); } } 
  • Interesting ... And how will the while loop look like? How do i associate an instanceof with a string flag. The flag will come to me from the console, for example, "-f" as in the example, and then it will still get if ("- f". Equals (and it went off ... Or did I misunderstand something? - Pavel
  • create a while before loop while Collection <Command> commands, and run over it when you receive a new command, pulling the getName method, if there is a match, then invoke invoke, otherwise go to the next element - Artem Konovalov
  • Understood thanks! This is what you need! - Pavel