package ru.job4j.testTask;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

/**
 * @author Alexander Ivanov
 * @version 1.0
 * @since 15.04.2017
 */
public class Parser {
    public static Map<String, HashMap<Integer, Order>> scan(String path) {
        Map<String, HashMap<Integer, Order>> bookList = new TreeMap<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
            String s;
            while ((s = reader.readLine()) != null) {
                if (s.charAt(1) == 'A') {
                    String[] array = parseAdd(s);
                    int id = Integer.valueOf(array[4]);
                    HashMap<Integer, Order> map = bookList.get(array[0]);
                    if (map == null) {
                        map = new HashMap<>();
                        bookList.put(array[0], map);
                    }
                    map.put(id, new Order(array[0], array[1], Float.valueOf(array[2]), Integer.valueOf(array[3]), Integer.valueOf(array[4])));
                } else if (s.charAt(1) == 'D') {
                    String[] array = parseDelete(s);
                    int id = Integer.valueOf(array[1]);
                    bookList.get(array[0]).remove(id);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bookList;
    }

    public static String[] parseAdd(String s) {
        char[] arr = s.toCharArray();
        String[] array = new String[5];
        boolean flag = false;
        int start = 15;
        int index = 0;
        for (int end = start; end < arr.length - 2; end++) {
            if (arr[end] == '\"') {
                if (flag) {
                    array[index++] = s.substring(start + 1, end);
                    flag = false;
                } else {
                    flag = true;
                }
                start = end;
            }
        }
        return array;
    }

    public static String[] parseDelete(String s) {
        char[] arr = s.toCharArray();
        String[] array = new String[2];
        boolean flag = false;
        int start = 17;
        int index = 0;
        for (int end = start; end < arr.length - 2; end++) {
            if (arr[end] == '\"') {
                if (flag) {
                    array[index++] = s.substring(start + 1, end);
                    flag = false;
                } else {
                    flag = true;
                }
                start = end;
            }
        }
        return array;
    }

    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        Map<String, HashMap<Integer, Order>> books = Parser.scan("D:\\orders.xml");
        Map<String, OrderBook> map = new TreeMap<>();
        for (Map.Entry<String, HashMap<Integer, Order>> m : books.entrySet()) {
            String name = m.getKey();
            OrderBook book = new OrderBook(name, m.getValue());
            map.put(name, book);
            book.addOrdersToBooks();
        }
        for (Map.Entry<String, OrderBook> m : map.entrySet()) {
            System.out.println(String.format("%s\t\t\t\t\t\t\t\t%s", System.lineSeparator(), m.getKey().toUpperCase()));
            m.getValue().showAndFillLists();
        }
        for (Map.Entry<String, OrderBook> m : map.entrySet()) {
            m.getValue().matchBIDtoASK();
            m.getValue().showAfterDeal();
        }
        System.out.println("Work time - " + (System.currentTimeMillis() - start) + " ms");
    }
}
@@ -124,17 +124,71 @@
 va.util.
-*
+HashMap;%0Aimport java.util.Map;%0Aimport java.util.TreeMap
 ;%0A%0A/**%0A 
@@ -1078,16 +1078,44 @@
 ray%5B3%5D),
+%0A                           
  Integer

Add test