Advertisement
Guest User

Untitled

a guest
May 21st, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.33 KB | None | 0 0
  1. package sample;
  2.  
  3. import javafx.geometry.Pos;
  4. import javafx.scene.Scene;
  5. import javafx.scene.control.Button;
  6. import javafx.scene.control.Label;
  7. import javafx.scene.control.TextField;
  8. import javafx.scene.control.TextFormatter;
  9. import javafx.scene.layout.BorderPane;
  10. import javafx.scene.layout.HBox;
  11. import javafx.scene.layout.VBox;
  12. import javafx.stage.Modality;
  13. import javafx.stage.Stage;
  14.  
  15. import java.sql.SQLException;
  16.  
  17. import static sample.Main.*;
  18.  
  19. public class RegisterUser {
  20. public static Scene sceneRegister, sceneConfirm;
  21. public static Button registerBtn;
  22. public static Button cancelBtn;
  23. public static Button confirmBtn;
  24. public static Label label1;
  25. public static BorderPane layoutConfirm;
  26. public static HBox buttonBar, labelBar;
  27. public static VBox labelAndButtons;
  28. public static TextField userText, assetText, enterpriseTf;
  29.  
  30. public static void registerUser(){
  31. Main.startWindow = new Stage();
  32. Main.totalAsset = new Label();
  33. Main.startWindow.initModality(Modality.APPLICATION_MODAL);
  34. Main.startWindow.setResizable(false);
  35. Main.startWindow.setMaxWidth(350);
  36. Main.startWindow.setMaxHeight(200);
  37. Main.startWindow.setMinWidth(350);
  38. Main.startWindow.setMinHeight(200);
  39. Main.startWindow.setTitle("Set up user");
  40.  
  41. Label label = new Label("User name : ");
  42. Label label1 = new Label("Asset : ");
  43. Label label2 = new Label("Company name : ");
  44. //label.setMaxWidth(150);
  45. //label1.setMaxWidth(150);
  46. //label2.setMaxWidth(150);
  47. userText = new TextField();
  48. userText.setPromptText("User name");
  49. assetText = new TextField();
  50. assetText.setPromptText("Asset");
  51. enterpriseTf = new TextField();
  52. enterpriseTf.setPromptText("My company");
  53.  
  54. VBox verticalBox = new VBox();
  55. HBox horizontalBox = new HBox();
  56. HBox horizontalBoxBehind = new HBox();
  57. HBox enterpriseBox = new HBox();
  58. BorderPane layoutRegisterUser = new BorderPane();
  59. registerBtn = new Button("Register");
  60. registerBtn.resize(150,50);
  61. sceneRegister = new Scene(layoutRegisterUser, 250, 250);
  62.  
  63. userText.setMinWidth(125);
  64. assetText.setMinWidth(150);
  65. enterpriseTf.setMinWidth(100);
  66.  
  67. horizontalBox.getChildren().addAll(label, userText);
  68. horizontalBox.setAlignment(Pos.CENTER);
  69. horizontalBox.setSpacing(37);
  70.  
  71. horizontalBoxBehind.getChildren().addAll(label1, assetText);
  72. horizontalBoxBehind.setAlignment(Pos.CENTER);
  73. horizontalBoxBehind.setSpacing(65);
  74.  
  75. enterpriseBox.getChildren().addAll(label2, enterpriseTf);
  76. enterpriseBox.setAlignment(Pos.CENTER);
  77. enterpriseBox.setSpacing(10);
  78.  
  79. verticalBox.getChildren().addAll(horizontalBox, horizontalBoxBehind, enterpriseBox, registerBtn);
  80. verticalBox.setAlignment(Pos.CENTER);
  81. verticalBox.setSpacing(10);
  82.  
  83. layoutRegisterUser.setCenter(verticalBox);
  84.  
  85. Main.startWindow.setScene(sceneRegister);
  86. registerBtn.setOnAction(Event -> registerBtnHandler());
  87.  
  88. startWindow.setOnCloseRequest(event -> {
  89. event.consume();
  90. ExitWindow.display();
  91. });
  92.  
  93. Main.startWindow.showAndWait();
  94. }
  95.  
  96.  
  97. private static void registerBtnHandler() {
  98. subWindow = new Stage();
  99. subWindow.initModality(Modality.APPLICATION_MODAL);
  100. subWindow.setResizable(false);
  101. subWindow.setTitle("Confirm input");
  102. confirmBtn = new Button("Yes");
  103. cancelBtn = new Button("No");
  104. label1 = new Label("Are you sure about your input?");
  105. layoutConfirm = new BorderPane();
  106. buttonBar = new HBox();
  107. labelBar = new HBox();
  108.  
  109. confirmBtn.setMinWidth(100);
  110. cancelBtn.setMinWidth(100);
  111. buttonBar.getChildren().addAll(confirmBtn, cancelBtn);
  112. labelBar.getChildren().addAll(label1);
  113. labelBar.setAlignment(Pos.CENTER);
  114. buttonBar.setAlignment(Pos.CENTER);
  115.  
  116.  
  117. labelAndButtons = new VBox();
  118. labelAndButtons.getChildren().addAll(labelBar,buttonBar);
  119. labelAndButtons.setAlignment(Pos.CENTER);
  120. labelAndButtons.setSpacing(25);
  121.  
  122. layoutConfirm.setCenter(labelAndButtons);
  123. layoutConfirm.setMinWidth(100.0);
  124.  
  125. sceneConfirm = new Scene(layoutConfirm, 400, 150);
  126. subWindow.setScene(sceneConfirm);
  127. subWindow.show();
  128.  
  129. confirmBtn.setOnAction(Event -> {
  130. try {
  131. confirmBtnAction();
  132.  
  133. } catch (SQLException e) {
  134. e.printStackTrace();
  135. cancelBtnAction();
  136. }
  137. });
  138. cancelBtn.setOnAction(Event -> cancelBtnAction());
  139.  
  140. }
  141.  
  142.  
  143. private static void confirmBtnAction()throws SQLException{
  144.  
  145. sql = "UPDATE user SET userName = '" + userText.getText() + "', asset = " + Double.parseDouble(assetText.getText().toString())
  146. + ", companyName = '"+ enterpriseTf.getText()+"', firstDataBase = 1 WHERE id = 1;";
  147.  
  148.  
  149. Main.totalAsset.setText("asset = ");
  150. Main.assetDouble = Double.parseDouble(assetText.getText().toString());
  151. Main.assetDisplayTF.setText("" + Main.assetDouble);
  152. DBHandler.stmt.execute(sql);
  153. Main.subWindow.close();
  154. Main.startWindow.close();
  155.  
  156. }
  157.  
  158. private static void cancelBtnAction(){
  159. Main.subWindow.close();
  160.  
  161. }
  162.  
  163. }
  164.  
  165.  
  166. ################################################################# DB HANDLER#####################################################
  167.  
  168. package sample;
  169.  
  170. import javafx.scene.control.ComboBox;
  171. import javafx.scene.control.TextField;
  172.  
  173. import java.sql.*;
  174.  
  175. import static sample.Main.*;
  176.  
  177. public class DBHandler {
  178. public static Statement stmt;
  179. public static Connection con;
  180. public static String url = "jdbc:sqlite:" + System.getProperty("user.dir" ) +"\\erp.db";
  181.  
  182. public static void createNewDatabase(){
  183. try {
  184. connect();
  185. stmt = con.createStatement();
  186.  
  187. if (con != null) {
  188. DatabaseMetaData meta = con.getMetaData();
  189. createTable();
  190. }
  191.  
  192. } catch (SQLException e) {
  193. System.out.println(e.getMessage());
  194. }
  195. }
  196.  
  197.  
  198. public static void createTable () {
  199. // SQLite connection string
  200.  
  201. connect();
  202. try {
  203. stmt = con.createStatement();
  204.  
  205. // SQL statement for creating a new table
  206. String sql2 = "CREATE TABLE IF NOT EXISTS transactions (\n"
  207. + " id integer PRIMARY KEY AUTOINCREMENT,\n"
  208. + " number integer,\n"
  209. + " productName text NOT NULL,\n"
  210. + " unitPrice real,\n"
  211. + " total real);";
  212.  
  213. String sql1 = "CREATE TABLE IF NOT EXISTS stocks (\n"
  214. + " id integer PRIMARY KEY AUTOINCREMENT, \n"
  215. + " name text UNIQUE NOT NULL,\n"
  216. + " amount integer,\n"
  217. + " purchasingPrice real,\n"
  218. + " purchasingPriceSum real,\n"
  219. + " retailPriceSum real,\n"
  220. + " diffOfSum real,\n"
  221. + " winPerSell real,\n"
  222. + " retailPrice real);";
  223.  
  224. String userRegistration = "CREATE TABLE IF NOT EXISTS user (\n"
  225. + " userName text PRIMARY KEY,\n"
  226. + " asset real DEFAULT null,\n"
  227. + " companyName text,\n"
  228. + " firstDataBase integer DEFAULT 0,\n"
  229. + " id integer);";
  230.  
  231.  
  232. stmt.execute(sql1);
  233. stmt.execute(sql2);
  234. stmt.execute(userRegistration);
  235. int counter = 0;
  236.  
  237. try {
  238. sql = "SELECT * FROM user WHERE id = 1;";
  239. stmt.execute(sql);
  240.  
  241. // Default user has entries null!
  242. if(stmt.getResultSet().getInt("firstDataBase") == 0)
  243. {
  244. RegisterUser.registerUser();
  245.  
  246. }
  247. }
  248. catch (Exception e){
  249. sql = "INSERT INTO user (userName, asset,companyName, id) VALUES (NULL, NULL,NULL, 1);";
  250. stmt.execute(sql);
  251. }
  252.  
  253. // If user enters the program for the first time
  254.  
  255. sql = "SELECT * FROM user WHERE id = 1;";
  256. stmt.execute(sql);
  257.  
  258. // Default user has entries null!
  259. if(stmt.getResultSet().getInt("firstDataBase") == 0)
  260. {
  261. RegisterUser.registerUser();
  262.  
  263. }
  264.  
  265. }
  266. catch (Exception e){
  267.  
  268. }
  269.  
  270.  
  271.  
  272. }
  273. public static Connection connect() {
  274. // SQLite connection string
  275. try {
  276. con = DriverManager.getConnection(url);
  277. } catch (SQLException e) {
  278. System.out.println(e.getMessage());
  279. }
  280. return con;
  281. }
  282.  
  283. // Displays / updates stocks table and fills combo boxes (Buy and sell).
  284. public static void displayStocksTable() throws SQLException{
  285. stmt = con.createStatement();
  286. stocksData.clear();
  287. articelList.clear();
  288. resStocks = stmt.executeQuery("SELECT * FROM stocks;");
  289.  
  290. while(resStocks.next())
  291. {
  292. stocksData.addAll(new ProductInfo( resStocks.getInt("amount"), resStocks.getString("name"),
  293. resStocks.getDouble("purchasingPrice"), resStocks.getDouble("retailPrice") ));
  294. articelList.addAll(resStocks.getString("name"));
  295.  
  296. }
  297. stocks.setItems(stocksData);
  298. }
  299.  
  300. // Displays and updates transactionsTable
  301. public static void displayTransactionsTable()throws SQLException{
  302. stmt = con.createStatement();
  303. tableData.clear();
  304. resTransations = stmt.executeQuery("SELECT * FROM transactions;");
  305. while(resTransations.next())
  306. {
  307. tableData.addAll(new Transactions(resTransations.getString("productName"), resTransations.getInt("number"),
  308. resTransations.getDouble("unitPrice"), resTransations.getDouble("total") ));
  309. }
  310. transactions.setItems(tableData);
  311.  
  312. }
  313.  
  314. public static void comboBoxHandler(ComboBox comboBox, TextField textField, TextField textField2, TextField numberInput, boolean isSale) throws SQLException{
  315. try {
  316. String s = comboBox.getValue().toString();
  317. int amountField = Integer.parseInt(numberInput.getText());
  318. if(isSale){
  319. amountField*=-1;
  320. }
  321. if(!s.equals("Articles")){
  322.  
  323. if (s != null)
  324. {
  325. sql = "SELECT * FROM stocks WHERE name = '" + s + "';";
  326. stmt = con.createStatement();
  327. stmt.execute(sql);
  328. double resultDoubelevalue = stmt.getResultSet().getDouble("purchasingPrice");
  329. textField.setText("" + resultDoubelevalue);
  330. textField2.setText("" + (resultDoubelevalue*amountField) );
  331. }
  332. }
  333. }
  334. catch (Exception e){
  335.  
  336. }
  337. }
  338.  
  339. public static void comboBoxHandler2(ComboBox comboBox, TextField textField, TextField textField2, TextField numberInput, boolean isSale) throws SQLException{
  340. try {
  341. String s = comboBox.getValue().toString();
  342. int amountField = Integer.parseInt(numberInput.getText());
  343. if(isSale){
  344. amountField*=-1;
  345. }
  346. if(!s.equals("Articles")){
  347.  
  348. if (s != null)
  349. {
  350. sql = "SELECT * FROM stocks WHERE name = '" + s + "';";
  351. stmt = con.createStatement();
  352. stmt.execute(sql);
  353. double resultDoubelevalue = stmt.getResultSet().getDouble("retailPrice");
  354. textField.setText("" + resultDoubelevalue);
  355. textField2.setText("" + (resultDoubelevalue*amountField) );
  356. }
  357. }
  358. }
  359. catch (Exception e){
  360.  
  361. }
  362. }
  363.  
  364.  
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement