uva230 java

·  阅读 68

TreeMap 的使用,正则表达式的使用

public class Main {

    public static void main(String[] args) {
        solution(System.in, new MyPrinter());
    }

    public static void solution(InputStream in, Printer p) {
        Scanner scanner = new Scanner(in);

        // read in all books
        HashMap<String, Book> titleBookMap = readInBooks(scanner);
        TreeSet<Book> shelf = new TreeSet<>(titleBookMap.values());
        while (scanner.hasNextLine()) {

            // execute commands
            HashSet<String> bookToReturn = readInCommands(scanner, shelf, titleBookMap);
            if (bookToReturn == null) {
                break;
            }

            // return books
            List<Book> returnList = bookToReturn.stream().map(titleBookMap::get).sorted().collect(Collectors.toList());
            for (Book book : returnList) {
                Book lower = shelf.lower(book);
                if (lower == null) {
                    p.pn("Put %s first", book.title);
                } else {
                    p.pn("Put %s after %s", book.title, lower.title);
                }
                shelf.add(book);
            }
            p.pn("END");
        }

    }

    private static HashMap<String, Book> readInBooks(Scanner scanner) {
        HashMap<String, Book> titleBookMap = new HashMap<>();
        Pattern pattern = Pattern.compile("(?<title>.*) by (?<author>.*)$");
        while (true){
            String line = scanner.nextLine();
            if ("END".equals(line)) {
                break;
            }
            Matcher matcher = pattern.matcher(line);
            boolean find = matcher.find();
            assert find;
            String title = matcher.group("title");
            String author = matcher.group("author");
            Book book = new Book(title, author);
            titleBookMap.put(title, book);
        }
        return titleBookMap;
    }

    private static HashSet<String> readInCommands(Scanner scanner, TreeSet<Book> shelf, HashMap<String, Book> titleBookMap) {
        HashSet<String> bookToReturn = new HashSet<>();
        while (true) {
            String command = scanner.nextLine();
            if (command.startsWith("BORROW")) {
                String title = command.replaceFirst("BORROW", "").trim();
                if (!bookToReturn.remove(title)) {
                    shelf.remove(titleBookMap.get(title));
                }
            } else if (command.startsWith("RETURN")) {
                bookToReturn.add(command.replaceFirst("RETURN", "").trim());
            } else if (command.startsWith("END")){
                return null;
            } else {
                break;
            }
        }
        return bookToReturn;
    }
}

class Book implements Comparable<Book> {

    String title;
    String author;

    public Book(String name, String author) {
        this.title = name;
        this.author = author;
    }

    @Override
    public int compareTo(Book o) {
        int authorCompare = author.compareTo(o.author);
        if (authorCompare != 0) {
            return authorCompare;
        }
        return title.compareTo(o.title);
    }
}
分类:
后端
标签:
分类:
后端
标签:
收藏成功!
已添加到「」, 点击更改