diff --git a/client/src/main/java/ru/trader/controllers/MainController.java b/client/src/main/java/ru/trader/controllers/MainController.java index a450e25..6780b68 100644 --- a/client/src/main/java/ru/trader/controllers/MainController.java +++ b/client/src/main/java/ru/trader/controllers/MainController.java @@ -15,6 +15,7 @@ import org.slf4j.LoggerFactory; import org.xml.sax.SAXException; import ru.trader.Main; import ru.trader.World; +import ru.trader.maddavo.Parser; import ru.trader.model.*; import ru.trader.view.support.Localization; @@ -27,6 +28,7 @@ import java.io.UnsupportedEncodingException; import java.util.Locale; import java.util.Optional; import java.util.ResourceBundle; +import java.util.function.Consumer; public class MainController { private final static Logger LOG = LoggerFactory.getLogger(MainController.class); @@ -255,6 +257,51 @@ public class MainController { Screeners.showFilter(market.getAnalyzer().getFilter()); } + public void impMadSystems(ActionEvent actionEvent) { + chooseFile(new FileChooser.ExtensionFilter("CSV files (*.csv)", "*.csv"), file -> { + try { + Parser.parseSystems(file, World.getMarket()); + reload(); + } catch (IOException e) { + LOG.error("Error on import file", e); + } + }); + } + + public void impMadStations(ActionEvent actionEvent) { + chooseFile(new FileChooser.ExtensionFilter("CSV files (*.csv)", "*.csv"), file -> { + try { + Parser.parseStations(file, World.getMarket()); + reload(); + } catch (IOException e) { + LOG.error("Error on import file", e); + } + }); + } + + public void impMadOffers(ActionEvent actionEvent) { + chooseFile(new FileChooser.ExtensionFilter("Prices files (*.prices)", "*.prices"), file -> { + try { + Parser.parsePrices(file, World.getMarket()); + reload(); + } catch (IOException e) { + LOG.error("Error on import file", e); + } + }); + } + + private void chooseFile(FileChooser.ExtensionFilter filter, Consumer action) { + FileChooser fileChooser = new FileChooser(); + fileChooser.getExtensionFilters().add(filter); + fileChooser.setInitialDirectory(new File(".")); + File file = fileChooser.showOpenDialog(null); + if (file !=null) { + action.accept(file); + } + } + + + private void reload(){ if (world != null) world.getModeler().clear(); world = new MarketModel(World.getMarket()); diff --git a/client/src/main/resources/view/main.fxml b/client/src/main/resources/view/main.fxml index 6df1c88..d31a602 100644 --- a/client/src/main/resources/view/main.fxml +++ b/client/src/main/resources/view/main.fxml @@ -11,7 +11,14 @@ - + + + + + + + + diff --git a/utils/src/main/java/ru/trader/maddavo/Parser.java b/utils/src/main/java/ru/trader/maddavo/Parser.java new file mode 100644 index 0000000..6d0c0d9 --- /dev/null +++ b/utils/src/main/java/ru/trader/maddavo/Parser.java @@ -0,0 +1,38 @@ +package ru.trader.maddavo; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import ru.trader.core.Market; + +import java.io.*; + +public class Parser { + private final static Logger LOG = LoggerFactory.getLogger(Parser.class); + + public static void parseSystems(File file, Market market) throws IOException { + parseFile(file, new SystemHandler(market), 1); + + } + + public static void parseStations(File file, Market market) throws IOException { + parseFile(file, new StationHandler(market), 1); + + } + + public static void parsePrices(File file, Market market) throws IOException { + parseFile(file, new OffersHandler(market, true), 0); + + } + + private static void parseFile(File file, ParseHandler handler, int skip) throws IOException { + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { + int row = 0; + String line; + while ((line = reader.readLine()) != null) { + row++; + if (row <= skip) continue; + handler.parse(line); + } + } + } +}