Skip to content

Commit 5a848fd

Browse files
author
lifxue
committed
add CSVHelper for exporting exchange data to CSV file.
1 parent 1a189d8 commit 5a848fd

File tree

3 files changed

+52
-33
lines changed

3 files changed

+52
-33
lines changed

src/main/java/org/mapleaf/cointda/util/CSVHelper.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.mapleaf.cointda.util;
1717

18+
import java.io.File;
1819
import java.io.FileNotFoundException;
1920
import java.io.FileReader;
2021
import java.io.FileWriter;
@@ -40,7 +41,7 @@ public class CSVHelper {
4041
private static final char DELIMITER = ',';
4142
private static final Charset CHARSET = Charset.forName("UTF-8");
4243

43-
public static void writeCsv(String[] header, List<String[]> data, String filePath) {
44+
public static void writeCsv(String[] header, List<String[]> data, File filePath) {
4445
//初始化csvformat
4546
CSVFormat formator = CSVFormat.DEFAULT
4647
.withHeader(header)
@@ -63,8 +64,6 @@ public static void writeCsv(String[] header, List<String[]> data, String filePat
6364
logger.error(ex);
6465
}
6566

66-
logger.info("CSV文件创建成功,文件路径:" + filePath);
67-
6867
}
6968

7069
public static List<String[]> readCsv(String filePath) {

src/main/java/org/mapleaf/cointda/view/RootLayoutController.java

+48-29
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
*/
1616
package org.mapleaf.cointda.view;
1717

18+
import java.io.File;
1819
import java.io.IOException;
1920
import java.net.URL;
21+
import java.util.ArrayList;
22+
import java.util.List;
2023
import java.util.ResourceBundle;
2124
import javafx.event.ActionEvent;
2225
import javafx.fxml.FXML;
@@ -29,11 +32,16 @@
2932
import javafx.scene.control.MenuItem;
3033
import javafx.scene.layout.AnchorPane;
3134
import javafx.scene.layout.BorderPane;
35+
import javafx.stage.FileChooser;
36+
import javafx.stage.FileChooser.ExtensionFilter;
3237
import javafx.stage.Stage;
3338
import org.apache.logging.log4j.LogManager;
3439
import org.apache.logging.log4j.Logger;
40+
import org.mapleaf.cointda.bean.TradeDataBean;
3541
import org.mapleaf.cointda.crypto.CoinListingCollector;
3642
import org.mapleaf.cointda.dao.CoinListingDao;
43+
import org.mapleaf.cointda.dao.TradeDataDao;
44+
import org.mapleaf.cointda.util.CSVHelper;
3745

3846
/**
3947
* FXML Controller class
@@ -49,8 +57,6 @@ public class RootLayoutController implements Initializable {
4957
@FXML
5058
private ContextMenu settingMenu;
5159
@FXML
52-
private MenuItem typeItem;
53-
@FXML
5460
private MenuItem priceItem;
5561
@FXML
5662
private Button settingButton;
@@ -86,18 +92,53 @@ private void handleSetting(ActionEvent event) {
8692
settingMenu.show(settingButton, Side.RIGHT, 0, 0);
8793
}
8894

95+
/**
96+
* 导出交易数据到csv文件
97+
*
98+
* @param event
99+
*/
100+
@FXML
101+
private void handleExportData(ActionEvent event) {
102+
List<TradeDataBean> list = TradeDataDao.queryAll();
103+
List<String[]> data = new ArrayList<>();
104+
String[] headers = {"id", "coid_id", "简称", "买卖", "单价", "数量", "总价", "交易时间"};
105+
if (list == null) {
106+
Alert alert = new Alert(Alert.AlertType.INFORMATION);
107+
alert.setTitle("消息");
108+
alert.setHeaderText("导出数据失败");
109+
alert.setContentText("没有交易数据!");
110+
alert.showAndWait();
111+
return;
112+
}
113+
list.stream().map((bean) -> {
114+
String[] str = new String[8];
115+
str[0] = bean.getId().toString();
116+
str[1] = bean.getCoin_id().toString();
117+
str[2] = bean.getCoin_symbol();
118+
str[3] = bean.getSale_or_buy();
119+
str[4] = bean.getPrice().toString();
120+
str[5] = bean.getNum().toString();
121+
str[6] = bean.getTotal_price().toString();
122+
str[7] = bean.getTrade_date();
123+
return str;
124+
}).forEachOrdered((str) -> {
125+
data.add(str);
126+
});
127+
FileChooser fileChooser = new FileChooser();
128+
//文档类型过滤器
129+
ExtensionFilter extFilter = new ExtensionFilter("txt files (*.csv)", "*.csv");
130+
fileChooser.getExtensionFilters().add(extFilter);
131+
File file = fileChooser.showSaveDialog(pane.getScene().getWindow());
132+
CSVHelper.writeCsv(headers, data, file);
133+
}
134+
89135
@FXML
90136
private void handleCloseApp(ActionEvent event) {
91137
//Platform.exit();
92138
Stage stage = (Stage) pane.getScene().getWindow();
93139
stage.close();
94140
}
95141

96-
@FXML
97-
private void handleSettingType(ActionEvent event) {
98-
showTypeSettingView();
99-
}
100-
101142
@FXML
102143
private void handleSettingPrice(ActionEvent event) {
103144
showSettingPriceView();
@@ -135,12 +176,6 @@ public void setPane(BorderPane pane) {
135176
*/
136177
private boolean update() {
137178
boolean ok = false;
138-
// CoinIDMapCollector coin = new CoinIDMapCollector();
139-
// if (CoinMarketCapDao.truncate()) {
140-
// if (CoinMarketCapDao.batchInsert(coin.getCoinMarketCapIds()).length > 0) {
141-
// ok = true;
142-
// }
143-
// }
144179
CoinListingCollector listing = new CoinListingCollector();
145180
if (CoinListingDao.truncate()) {
146181
if (CoinListingDao.batchInsert(listing.getCoinMarketListing()).length > 0) {
@@ -198,22 +233,6 @@ public void showPATableView() {
198233
}
199234
}
200235

201-
/**
202-
* 显示品种类型设置视图
203-
*/
204-
public void showTypeSettingView() {
205-
try {
206-
FXMLLoader loader = new FXMLLoader();
207-
loader.setLocation(RootLayoutController.class.getResource("TypeSettingView.fxml"));
208-
AnchorPane dataEditView = (AnchorPane) loader.load();
209-
pane.setCenter(dataEditView);
210-
//转移焦点到center
211-
dataEditView.requestFocus();
212-
} catch (IOException e) {
213-
LOGGER.error(e);
214-
}
215-
}
216-
217236
/**
218237
* 显示品种价格设置视图
219238
*/

src/main/resources/org/mapleaf/cointda/view/RootLayout.fxml

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<?import javafx.scene.layout.BorderPane?>
88
<?import javafx.scene.layout.VBox?>
99

10-
<BorderPane prefHeight="700.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/11.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.mapleaf.cointda.view.RootLayoutController">
10+
<BorderPane prefHeight="700.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.mapleaf.cointda.view.RootLayoutController">
1111
<left>
1212
<VBox alignment="TOP_CENTER" prefHeight="700.0" prefWidth="150.0" spacing="20.0" BorderPane.alignment="CENTER">
1313
<children>
@@ -26,6 +26,7 @@
2626
</items>
2727
</ContextMenu>
2828
</contextMenu></Button>
29+
<Button mnemonicParsing="false" onAction="#handleExportData" prefHeight="40.0" prefWidth="100.0" text="导出交易数据" />
2930
<Button mnemonicParsing="false" onAction="#handleUpdate" prefHeight="40.0" prefWidth="100.0" text="更新基础数据" />
3031
<Button mnemonicParsing="false" onAction="#handleCloseApp" prefHeight="40.0" prefWidth="100.0" text="退出" />
3132
</children>

0 commit comments

Comments
 (0)