· 8 years ago · May 21, 2018, 11:02 AM
1package sample;
2
3import javafx.application.Application;
4import javafx.beans.property.SimpleStringProperty;
5import javafx.collections.FXCollections;
6import javafx.collections.ObservableList;
7import javafx.event.Event;
8import javafx.geometry.Insets;
9import javafx.geometry.Pos;
10import javafx.scene.Scene;
11import javafx.scene.control.*;
12import javafx.scene.control.cell.PropertyValueFactory;
13import javafx.scene.layout.BorderPane;
14import javafx.scene.layout.HBox;
15import javafx.stage.Stage;
16
17import java.sql.Connection;
18import java.sql.ResultSet;
19import java.sql.SQLException;
20import java.sql.Statement;
21
22import static sample.Statistics.statsData;
23
24public class Main extends Application {
25 // Display variables for main window
26 public static Stage window;
27 public static Stage subWindow, startWindow, confirmWindow;
28 public static Alert alert = new Alert(Alert.AlertType.INFORMATION);
29 private static Scene mainScene;
30 private final int startMainWindowWidth = 1000;
31 private final int startMainWindowHeight = 600;
32
33
34 // Asset double safes the users asset value.
35 public static double assetDouble;
36 private static BorderPane layoutMainScene;
37 private static Button sellBtn,buyBtn, statisticsBtn, addDeleteBtn;
38 private static HBox mainButtonBar;
39
40 // Label for total amount
41 public static Label totalAsset;
42 // Tableview initialization
43 public static ResultSet resTransations;
44 public static ResultSet resStocks;
45 public static ResultSet resStats;
46 // TableViews
47 public static TableView stocks;
48 public static TableView transactions;
49
50 //Table coloumns
51 public static TableColumn<ProductInfo, Integer> amountColStocks = new TableColumn<ProductInfo, Integer>("Amount");
52 public static TableColumn<ProductInfo, String> nameColStocks = new TableColumn<ProductInfo, String>("Name");
53 public static TableColumn<ProductInfo, Double> purchColStocks = new TableColumn<ProductInfo, Double>("Purchasing price");
54 public static TableColumn<ProductInfo, Double> retailColStocks = new TableColumn<ProductInfo, Double>("Retail price");
55
56 public static TableColumn<Transactions, Integer> numberColTable = new TableColumn<Transactions, Integer>("Number");
57 public static TableColumn<Transactions, String> proNameCol = new TableColumn<Transactions, String>("Product name");
58 public static TableColumn<Transactions, Double> unitCol = new TableColumn<Transactions, Double>("Unit price");
59 public static TableColumn<Transactions, Double> totalCol = new TableColumn<Transactions, Double>("Total");
60
61 // Data for table views
62 public static ObservableList<ProductInfo> stocksData = FXCollections.observableArrayList();
63 public static ObservableList<Transactions> tableData = FXCollections.observableArrayList();
64 public static ObservableList<String> articelList= FXCollections.observableArrayList();
65
66 // Data base
67 public static DBHandler dataBase;
68 public static String sql;
69
70 // Initialize objects from other classes.
71 public static Statistics stats;
72 public static EditProduct edit;
73 public static Buy buy;
74 public static Sales sell;
75
76 public static TextField assetDisplayTF;
77
78
79 @Override
80 public void start(Stage primaryStage) throws Exception, SQLException{
81
82
83 subWindow = new Stage();
84 startWindow = new Stage();
85 window = primaryStage;
86 window.setTitle("Enterprise Resource Planing");
87 primaryStage.setResizable(false);
88
89 // Initializing borderpane layout!
90 layoutMainScene = new BorderPane();
91 mainScene = new Scene(layoutMainScene, startMainWindowWidth, startMainWindowHeight);
92 sellBtn = new Button("Sell");
93 buyBtn = new Button("Buy");
94 addDeleteBtn = new Button("Add / Delete products");
95 statisticsBtn = new Button("Statistics");
96 // Initializing other class objects, which will be used here
97 edit = new EditProduct();
98 sell = new Sales();
99 buy = new Buy();
100 mainButtonBar = new HBox();
101 alert = new Alert(Alert.AlertType.INFORMATION);
102 assetDisplayTF = new TextField();
103 dataBase = new DBHandler();
104
105 numberColTable.setMinWidth(250);
106 numberColTable.setCellValueFactory(new PropertyValueFactory<Transactions, Integer>("number"));
107 numberColTable.setId("0");
108 proNameCol.setMinWidth(250);
109 proNameCol.setCellValueFactory(new PropertyValueFactory<Transactions, String>("name"));
110 unitCol.setMinWidth(250);
111 unitCol.setCellValueFactory(new PropertyValueFactory<Transactions, Double>("unitPrice"));
112 totalCol.setMinWidth(250);
113 totalCol.setCellValueFactory(new PropertyValueFactory<Transactions, Double>("total"));
114
115
116 amountColStocks.setMinWidth(250);
117 amountColStocks.setCellValueFactory(new PropertyValueFactory<ProductInfo, Integer> ("amount"));
118 nameColStocks.setMinWidth(250);
119 nameColStocks.setCellValueFactory(new PropertyValueFactory<ProductInfo, String>("productName"));
120 purchColStocks.setMinWidth(250);
121 purchColStocks.setCellValueFactory(new PropertyValueFactory<ProductInfo, Double>("purchasingPrice"));
122 retailColStocks.setMinWidth(250);
123 retailColStocks.setCellValueFactory(new PropertyValueFactory<ProductInfo, Double>("retailPrice"));
124 dataBase.createNewDatabase();
125 stocks = new TableView();
126 transactions = new TableView();
127 totalAsset = new Label();
128 sql = "SELECT companyName FROM user WHERE ID = 1;";
129
130 DBHandler.stmt = DBHandler.con.createStatement();
131 DBHandler.stmt.execute(sql);
132 String EnterpriseName = DBHandler.stmt.getResultSet().getString("companyName");
133 window.setTitle("Enterprise Resource Planing - "+EnterpriseName);
134
135
136
137 assetDisplayTF = new TextField();
138 assetDisplayTF.setEditable(false);
139
140
141 totalAsset.setText("Asset = ");
142
143 // Initializes and refresh both tables and asset
144 refreshAsset();
145 DBHandler.displayStocksTable();
146 DBHandler.displayTransactionsTable();
147
148 // change productInformation to settings.
149 mainButtonBar.getChildren().addAll(sellBtn, buyBtn, statisticsBtn, addDeleteBtn, totalAsset, assetDisplayTF);
150 mainButtonBar.setSpacing(10);
151 mainButtonBar.setPadding(new Insets(10, 10, 10, 10));
152 mainButtonBar.setAlignment(Pos.CENTER);
153 layoutMainScene.setTop(transactions);
154 transactions.setMaxHeight(300);
155 layoutMainScene.setBottom(stocks);
156 layoutMainScene.setCenter(mainButtonBar);
157
158 window.setScene(mainScene);
159
160
161 articelList.add("Article");
162
163 stocks.getColumns().addAll(amountColStocks, nameColStocks, purchColStocks, retailColStocks);
164 transactions.getColumns().addAll(numberColTable, proNameCol, unitCol, totalCol);
165 stocks.setItems(stocksData);
166
167 window.setOnCloseRequest(event -> {
168 event.consume();
169 ExitWindow.display();
170 });
171
172 subWindow.setResizable(false);
173 window.show();
174 // Switching between scenes
175 addDeleteBtn.setOnAction(Event -> edit.displayEditWindow());
176 sellBtn.setOnAction(Event -> sell.displaySales());
177 buyBtn.setOnAction(Event -> {
178 try {
179 buy.displayBought();
180 } catch (SQLException e) {
181 e.printStackTrace();
182 }
183 });
184 statisticsBtn.setOnAction(Event -> {
185 try {
186 Statistics.displayStatisticGui();
187 } catch (SQLException e) {
188 e.printStackTrace();
189 }
190 });
191
192 }
193
194 public static void refreshAsset() throws SQLException{
195
196 sql = "SELECT asset FROM user WHERE ID = 1;";
197
198 DBHandler.stmt = DBHandler.con.createStatement();
199 DBHandler.stmt.execute(sql);
200 assetDouble = DBHandler.stmt.getResultSet().getDouble("asset");
201 assetDisplayTF.setText("" + assetDouble);
202
203 }
204
205
206
207 public static void main(String[] args) {
208 launch(args);
209 }
210}
211
212################################DB HANDLER######################################
213
214package sample;
215
216import javafx.scene.control.ComboBox;
217import javafx.scene.control.TextField;
218
219import java.sql.*;
220
221import static sample.Main.*;
222
223public class DBHandler {
224 public static Statement stmt;
225 public static Connection con;
226 public static String url = "jdbc:sqlite:" + System.getProperty("user.dir" ) +"\\erp.db";
227
228 public static void createNewDatabase(){
229 try {
230 connect();
231 stmt = con.createStatement();
232
233 if (con != null) {
234 DatabaseMetaData meta = con.getMetaData();
235 createTable();
236 }
237
238 } catch (SQLException e) {
239 System.out.println(e.getMessage());
240 }
241 }
242
243
244 public static void createTable () {
245 // SQLite connection string
246
247 connect();
248 try {
249 stmt = con.createStatement();
250
251 // SQL statement for creating a new table
252 String sql2 = "CREATE TABLE IF NOT EXISTS transactions (\n"
253 + " id integer PRIMARY KEY AUTOINCREMENT,\n"
254 + " number integer,\n"
255 + " productName text NOT NULL,\n"
256 + " unitPrice real,\n"
257 + " total real);";
258
259 String sql1 = "CREATE TABLE IF NOT EXISTS stocks (\n"
260 + " id integer PRIMARY KEY AUTOINCREMENT, \n"
261 + " name text UNIQUE NOT NULL,\n"
262 + " amount integer,\n"
263 + " purchasingPrice real,\n"
264 + " purchasingPriceSum real,\n"
265 + " retailPriceSum real,\n"
266 + " diffOfSum real,\n"
267 + " winPerSell real,\n"
268 + " retailPrice real);";
269
270 String userRegistration = "CREATE TABLE IF NOT EXISTS user (\n"
271 + " userName text PRIMARY KEY,\n"
272 + " asset real DEFAULT null,\n"
273 + " companyName text,\n"
274 + " firstDataBase integer DEFAULT 0,\n"
275 + " id integer);";
276
277
278 stmt.execute(sql1);
279 stmt.execute(sql2);
280 stmt.execute(userRegistration);
281 int counter = 0;
282
283 try {
284 sql = "SELECT * FROM user WHERE id = 1;";
285 stmt.execute(sql);
286
287 // Default user has entries null!
288 if(stmt.getResultSet().getInt("firstDataBase") == 0)
289 {
290 RegisterUser.registerUser();
291
292 }
293 }
294 catch (Exception e){
295 sql = "INSERT INTO user (userName, asset,companyName, id) VALUES (NULL, NULL,NULL, 1);";
296 stmt.execute(sql);
297 }
298
299 // If user enters the program for the first time
300
301 sql = "SELECT * FROM user WHERE id = 1;";
302 stmt.execute(sql);
303
304 // Default user has entries null!
305 if(stmt.getResultSet().getInt("firstDataBase") == 0)
306 {
307 RegisterUser.registerUser();
308
309 }
310
311 }
312 catch (Exception e){
313
314 }
315
316
317
318 }
319 public static Connection connect() {
320 // SQLite connection string
321 try {
322 con = DriverManager.getConnection(url);
323 } catch (SQLException e) {
324 System.out.println(e.getMessage());
325 }
326 return con;
327 }
328
329 // Displays / updates stocks table and fills combo boxes (Buy and sell).
330 public static void displayStocksTable() throws SQLException{
331 stmt = con.createStatement();
332 stocksData.clear();
333 articelList.clear();
334 resStocks = stmt.executeQuery("SELECT * FROM stocks;");
335
336 while(resStocks.next())
337 {
338 stocksData.addAll(new ProductInfo( resStocks.getInt("amount"), resStocks.getString("name"),
339 resStocks.getDouble("purchasingPrice"), resStocks.getDouble("retailPrice") ));
340 articelList.addAll(resStocks.getString("name"));
341
342 }
343 stocks.setItems(stocksData);
344 }
345
346 // Displays and updates transactionsTable
347 public static void displayTransactionsTable()throws SQLException{
348 stmt = con.createStatement();
349 tableData.clear();
350 resTransations = stmt.executeQuery("SELECT * FROM transactions;");
351 while(resTransations.next())
352 {
353 tableData.addAll(new Transactions(resTransations.getString("productName"), resTransations.getInt("number"),
354 resTransations.getDouble("unitPrice"), resTransations.getDouble("total") ));
355 }
356 transactions.setItems(tableData);
357
358 }
359
360 public static void comboBoxHandler(ComboBox comboBox, TextField textField, TextField textField2, TextField numberInput, boolean isSale) throws SQLException{
361 try {
362 String s = comboBox.getValue().toString();
363 int amountField = Integer.parseInt(numberInput.getText());
364 if(isSale){
365 amountField*=-1;
366 }
367 if(!s.equals("Articles")){
368
369 if (s != null)
370 {
371 sql = "SELECT * FROM stocks WHERE name = '" + s + "';";
372 stmt = con.createStatement();
373 stmt.execute(sql);
374 double resultDoubelevalue = stmt.getResultSet().getDouble("purchasingPrice");
375 textField.setText("" + resultDoubelevalue);
376 textField2.setText("" + (resultDoubelevalue*amountField) );
377 }
378 }
379 }
380 catch (Exception e){
381
382 }
383 }
384
385 public static void comboBoxHandler2(ComboBox comboBox, TextField textField, TextField textField2, TextField numberInput, boolean isSale) throws SQLException{
386 try {
387 String s = comboBox.getValue().toString();
388 int amountField = Integer.parseInt(numberInput.getText());
389 if(isSale){
390 amountField*=-1;
391 }
392 if(!s.equals("Articles")){
393
394 if (s != null)
395 {
396 sql = "SELECT * FROM stocks WHERE name = '" + s + "';";
397 stmt = con.createStatement();
398 stmt.execute(sql);
399 double resultDoubelevalue = stmt.getResultSet().getDouble("retailPrice");
400 textField.setText("" + resultDoubelevalue);
401 textField2.setText("" + (resultDoubelevalue*amountField) );
402 }
403 }
404 }
405 catch (Exception e){
406
407 }
408 }
409
410
411}