|
| 1 | +package com.codedifferently.lesson28.cli; |
| 2 | + |
| 3 | +import com.codedifferently.lesson28.factory.LibraryDataLoader; |
| 4 | +import com.codedifferently.lesson28.factory.LibraryDbDataLoader; |
| 5 | +import com.codedifferently.lesson28.factory.LibraryFactory; |
| 6 | +import com.codedifferently.lesson28.library.Book; |
| 7 | +import com.codedifferently.lesson28.library.Library; |
| 8 | +import com.codedifferently.lesson28.library.LibraryInfo; |
| 9 | +import com.codedifferently.lesson28.library.MediaItem; |
| 10 | +import com.codedifferently.lesson28.library.search.SearchCriteria; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Scanner; |
| 13 | +import java.util.Set; |
| 14 | +import org.apache.commons.cli.CommandLine; |
| 15 | +import org.apache.commons.cli.CommandLineParser; |
| 16 | +import org.apache.commons.cli.DefaultParser; |
| 17 | +import org.apache.commons.cli.HelpFormatter; |
| 18 | +import org.apache.commons.cli.Option; |
| 19 | +import org.apache.commons.cli.Options; |
| 20 | +import org.apache.commons.cli.ParseException; |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.stereotype.Service; |
| 23 | + |
| 24 | +@Service |
| 25 | +public final class LibraryApp { |
| 26 | + @Autowired private LibraryDbDataLoader defaultLibraryDataLoader; |
| 27 | + |
| 28 | + public void run(String[] args) throws Exception { |
| 29 | + // Load the library using the specified loader from the command line or the default. |
| 30 | + LibraryDataLoader loader = getLoaderOrDefault(args, defaultLibraryDataLoader); |
| 31 | + Library library = LibraryFactory.createWithLoader(loader); |
| 32 | + |
| 33 | + // Show stats about the loaded library to the user. |
| 34 | + printLibraryInfo(library); |
| 35 | + |
| 36 | + try (var scanner = new Scanner(System.in)) { |
| 37 | + LibraryCommand command; |
| 38 | + // Main application loop. |
| 39 | + while ((command = promptForCommand(scanner)) != LibraryCommand.EXIT) { |
| 40 | + switch (command) { |
| 41 | + case SEARCH -> doSearch(scanner, library); |
| 42 | + default -> System.out.println("\nNot ready yet, coming soon!"); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private void printLibraryInfo(Library library) { |
| 49 | + LibraryInfo info = library.getInfo(); |
| 50 | + Map<String, Set<MediaItem>> checkedOutItemsByGuest = info.getCheckedOutItemsByGuest(); |
| 51 | + int numCheckedOutItems = checkedOutItemsByGuest.values().stream().mapToInt(Set::size).sum(); |
| 52 | + System.out.println(); |
| 53 | + System.out.println("========================================"); |
| 54 | + System.out.println("Library id: " + library.getId()); |
| 55 | + System.out.println("Number of items: " + info.getItems().size()); |
| 56 | + System.out.println("Number of guests: " + info.getGuests().size()); |
| 57 | + System.out.println("Number of checked out items: " + numCheckedOutItems); |
| 58 | + System.out.println("========================================"); |
| 59 | + System.out.println(); |
| 60 | + } |
| 61 | + |
| 62 | + private static LibraryDataLoader getLoaderOrDefault( |
| 63 | + String[] args, LibraryDataLoader defaultLoader) throws Exception { |
| 64 | + String loaderType = getLoaderFromCommandLine(args); |
| 65 | + return loaderType == null |
| 66 | + ? defaultLoader |
| 67 | + : Class.forName(loaderType) |
| 68 | + .asSubclass(LibraryDataLoader.class) |
| 69 | + .getDeclaredConstructor() |
| 70 | + .newInstance(); |
| 71 | + } |
| 72 | + |
| 73 | + private static String getLoaderFromCommandLine(String[] args) throws IllegalArgumentException { |
| 74 | + Options options = new Options(); |
| 75 | + Option input = new Option("l", "loader", true, "data loader type"); |
| 76 | + input.setRequired(false); |
| 77 | + options.addOption(input); |
| 78 | + CommandLineParser parser = new DefaultParser(); |
| 79 | + HelpFormatter formatter = new HelpFormatter(); |
| 80 | + try { |
| 81 | + CommandLine cmd = parser.parse(options, args); |
| 82 | + return cmd.getOptionValue("loader"); |
| 83 | + } catch (ParseException e) { |
| 84 | + System.out.println(); |
| 85 | + System.out.println(e.getMessage()); |
| 86 | + formatter.printHelp("utility-name", options); |
| 87 | + |
| 88 | + System.exit(1); |
| 89 | + } |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + private static LibraryCommand promptForCommand(Scanner scanner) { |
| 94 | + var command = LibraryCommand.UNKNOWN; |
| 95 | + while (command == LibraryCommand.UNKNOWN) { |
| 96 | + printMenu(); |
| 97 | + var input = scanner.nextLine(); |
| 98 | + try { |
| 99 | + command = LibraryCommand.fromValue(Integer.parseInt(input.trim())); |
| 100 | + } catch (IllegalArgumentException e) { |
| 101 | + System.out.println("Invalid command: " + input); |
| 102 | + } |
| 103 | + } |
| 104 | + return command; |
| 105 | + } |
| 106 | + |
| 107 | + private static void printMenu() { |
| 108 | + System.out.println("\nEnter the number of the desired command:"); |
| 109 | + System.out.println("1) << EXIT"); |
| 110 | + System.out.println("2) SEARCH"); |
| 111 | + System.out.println("3) CHECKOUT"); |
| 112 | + System.out.println("4) RETURN"); |
| 113 | + System.out.print("command> "); |
| 114 | + } |
| 115 | + |
| 116 | + private void doSearch(Scanner scanner, Library library) { |
| 117 | + LibrarySearchCommand command = promptForSearchCommand(scanner); |
| 118 | + if (command == LibrarySearchCommand.RETURN) { |
| 119 | + return; |
| 120 | + } |
| 121 | + SearchCriteria criteria = getSearchCriteria(scanner, command); |
| 122 | + Set<MediaItem> results = library.search(criteria); |
| 123 | + printSearchResults(results); |
| 124 | + } |
| 125 | + |
| 126 | + private LibrarySearchCommand promptForSearchCommand(Scanner scanner) { |
| 127 | + var command = LibrarySearchCommand.UNKNOWN; |
| 128 | + while (command == LibrarySearchCommand.UNKNOWN) { |
| 129 | + printSearchMenu(); |
| 130 | + var input = scanner.nextLine(); |
| 131 | + try { |
| 132 | + command = LibrarySearchCommand.fromValue(Integer.parseInt(input.trim())); |
| 133 | + } catch (IllegalArgumentException e) { |
| 134 | + System.out.println("Invalid command: " + input); |
| 135 | + } |
| 136 | + } |
| 137 | + return command; |
| 138 | + } |
| 139 | + |
| 140 | + private void printSearchMenu() { |
| 141 | + System.out.println("\nEnter the number of the desired search criteria:"); |
| 142 | + System.out.println("1) << RETURN"); |
| 143 | + System.out.println("2) TITLE"); |
| 144 | + System.out.println("3) AUTHOR"); |
| 145 | + System.out.println("4) TYPE"); |
| 146 | + System.out.print("search> "); |
| 147 | + } |
| 148 | + |
| 149 | + private SearchCriteria getSearchCriteria(Scanner scanner, LibrarySearchCommand command) { |
| 150 | + System.out.println(); |
| 151 | + switch (command) { |
| 152 | + case TITLE -> { |
| 153 | + System.out.println("Enter the title to search for: "); |
| 154 | + System.out.print("title> "); |
| 155 | + var title = scanner.nextLine(); |
| 156 | + return SearchCriteria.builder().title(title).build(); |
| 157 | + } |
| 158 | + case AUTHOR -> { |
| 159 | + System.out.println("Enter the author to search for: "); |
| 160 | + System.out.print("author> "); |
| 161 | + var author = scanner.nextLine(); |
| 162 | + return SearchCriteria.builder().author(author).build(); |
| 163 | + } |
| 164 | + case TYPE -> { |
| 165 | + System.out.println("Enter the type to search for: "); |
| 166 | + System.out.print("type> "); |
| 167 | + var type = scanner.nextLine(); |
| 168 | + return SearchCriteria.builder().type(type).build(); |
| 169 | + } |
| 170 | + default -> System.out.println("Invalid search command: " + command); |
| 171 | + } |
| 172 | + return null; |
| 173 | + } |
| 174 | + |
| 175 | + private void printSearchResults(Set<MediaItem> results) { |
| 176 | + System.out.println(); |
| 177 | + |
| 178 | + if (results.isEmpty()) { |
| 179 | + System.out.println("No results found."); |
| 180 | + return; |
| 181 | + } |
| 182 | + |
| 183 | + System.out.println("Search results:\n"); |
| 184 | + for (MediaItem item : results) { |
| 185 | + System.out.println("ID: " + item.getId()); |
| 186 | + System.out.println("TITLE: " + item.getTitle()); |
| 187 | + if (item instanceof Book book) { |
| 188 | + System.out.println("AUTHOR(S): " + String.join(", ", book.getAuthors())); |
| 189 | + } |
| 190 | + System.out.println("TYPE: " + item.getType().toUpperCase()); |
| 191 | + System.out.println(); |
| 192 | + } |
| 193 | + System.out.println("Found " + results.size() + " result(s).\n"); |
| 194 | + } |
| 195 | +} |
0 commit comments