Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.TextField;
- import javafx.scene.control.TextFormatter;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.VBox;
- import javafx.stage.Modality;
- import javafx.stage.Stage;
- import java.sql.SQLException;
- import static sample.Main.*;
- public class RegisterUser {
- public static Scene sceneRegister, sceneConfirm;
- public static Button registerBtn;
- public static Button cancelBtn;
- public static Button confirmBtn;
- public static Label label1;
- public static BorderPane layoutConfirm;
- public static HBox buttonBar, labelBar;
- public static VBox labelAndButtons;
- public static TextField userText, assetText, enterpriseTf;
- public static void registerUser(){
- Main.startWindow = new Stage();
- Main.totalAsset = new Label();
- Main.startWindow.initModality(Modality.APPLICATION_MODAL);
- Main.startWindow.setResizable(false);
- Main.startWindow.setMaxWidth(350);
- Main.startWindow.setMaxHeight(200);
- Main.startWindow.setMinWidth(350);
- Main.startWindow.setMinHeight(200);
- Main.startWindow.setTitle("Set up user");
- Label label = new Label("User name : ");
- Label label1 = new Label("Asset : ");
- Label label2 = new Label("Company name : ");
- //label.setMaxWidth(150);
- //label1.setMaxWidth(150);
- //label2.setMaxWidth(150);
- userText = new TextField();
- userText.setPromptText("User name");
- assetText = new TextField();
- assetText.setPromptText("Asset");
- enterpriseTf = new TextField();
- enterpriseTf.setPromptText("My company");
- VBox verticalBox = new VBox();
- HBox horizontalBox = new HBox();
- HBox horizontalBoxBehind = new HBox();
- HBox enterpriseBox = new HBox();
- BorderPane layoutRegisterUser = new BorderPane();
- registerBtn = new Button("Register");
- registerBtn.resize(150,50);
- sceneRegister = new Scene(layoutRegisterUser, 250, 250);
- userText.setMinWidth(125);
- assetText.setMinWidth(150);
- enterpriseTf.setMinWidth(100);
- horizontalBox.getChildren().addAll(label, userText);
- horizontalBox.setAlignment(Pos.CENTER);
- horizontalBox.setSpacing(37);
- horizontalBoxBehind.getChildren().addAll(label1, assetText);
- horizontalBoxBehind.setAlignment(Pos.CENTER);
- horizontalBoxBehind.setSpacing(65);
- enterpriseBox.getChildren().addAll(label2, enterpriseTf);
- enterpriseBox.setAlignment(Pos.CENTER);
- enterpriseBox.setSpacing(10);
- verticalBox.getChildren().addAll(horizontalBox, horizontalBoxBehind, enterpriseBox, registerBtn);
- verticalBox.setAlignment(Pos.CENTER);
- verticalBox.setSpacing(10);
- layoutRegisterUser.setCenter(verticalBox);
- Main.startWindow.setScene(sceneRegister);
- registerBtn.setOnAction(Event -> registerBtnHandler());
- startWindow.setOnCloseRequest(event -> {
- event.consume();
- ExitWindow.display();
- });
- Main.startWindow.showAndWait();
- }
- private static void registerBtnHandler() {
- subWindow = new Stage();
- subWindow.initModality(Modality.APPLICATION_MODAL);
- subWindow.setResizable(false);
- subWindow.setTitle("Confirm input");
- confirmBtn = new Button("Yes");
- cancelBtn = new Button("No");
- label1 = new Label("Are you sure about your input?");
- layoutConfirm = new BorderPane();
- buttonBar = new HBox();
- labelBar = new HBox();
- confirmBtn.setMinWidth(100);
- cancelBtn.setMinWidth(100);
- buttonBar.getChildren().addAll(confirmBtn, cancelBtn);
- labelBar.getChildren().addAll(label1);
- labelBar.setAlignment(Pos.CENTER);
- buttonBar.setAlignment(Pos.CENTER);
- labelAndButtons = new VBox();
- labelAndButtons.getChildren().addAll(labelBar,buttonBar);
- labelAndButtons.setAlignment(Pos.CENTER);
- labelAndButtons.setSpacing(25);
- layoutConfirm.setCenter(labelAndButtons);
- layoutConfirm.setMinWidth(100.0);
- sceneConfirm = new Scene(layoutConfirm, 400, 150);
- subWindow.setScene(sceneConfirm);
- subWindow.show();
- confirmBtn.setOnAction(Event -> {
- try {
- confirmBtnAction();
- } catch (SQLException e) {
- e.printStackTrace();
- cancelBtnAction();
- }
- });
- cancelBtn.setOnAction(Event -> cancelBtnAction());
- }
- private static void confirmBtnAction()throws SQLException{
- sql = "UPDATE user SET userName = '" + userText.getText() + "', asset = " + Double.parseDouble(assetText.getText().toString())
- + ", companyName = '"+ enterpriseTf.getText()+"', firstDataBase = 1 WHERE id = 1;";
- Main.totalAsset.setText("asset = ");
- Main.assetDouble = Double.parseDouble(assetText.getText().toString());
- Main.assetDisplayTF.setText("" + Main.assetDouble);
- DBHandler.stmt.execute(sql);
- Main.subWindow.close();
- Main.startWindow.close();
- }
- private static void cancelBtnAction(){
- Main.subWindow.close();
- }
- }
- ################################################################# DB HANDLER#####################################################
- package sample;
- import javafx.scene.control.ComboBox;
- import javafx.scene.control.TextField;
- import java.sql.*;
- import static sample.Main.*;
- public class DBHandler {
- public static Statement stmt;
- public static Connection con;
- public static String url = "jdbc:sqlite:" + System.getProperty("user.dir" ) +"\\erp.db";
- public static void createNewDatabase(){
- try {
- connect();
- stmt = con.createStatement();
- if (con != null) {
- DatabaseMetaData meta = con.getMetaData();
- createTable();
- }
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- }
- public static void createTable () {
- // SQLite connection string
- connect();
- try {
- stmt = con.createStatement();
- // SQL statement for creating a new table
- String sql2 = "CREATE TABLE IF NOT EXISTS transactions (\n"
- + " id integer PRIMARY KEY AUTOINCREMENT,\n"
- + " number integer,\n"
- + " productName text NOT NULL,\n"
- + " unitPrice real,\n"
- + " total real);";
- String sql1 = "CREATE TABLE IF NOT EXISTS stocks (\n"
- + " id integer PRIMARY KEY AUTOINCREMENT, \n"
- + " name text UNIQUE NOT NULL,\n"
- + " amount integer,\n"
- + " purchasingPrice real,\n"
- + " purchasingPriceSum real,\n"
- + " retailPriceSum real,\n"
- + " diffOfSum real,\n"
- + " winPerSell real,\n"
- + " retailPrice real);";
- String userRegistration = "CREATE TABLE IF NOT EXISTS user (\n"
- + " userName text PRIMARY KEY,\n"
- + " asset real DEFAULT null,\n"
- + " companyName text,\n"
- + " firstDataBase integer DEFAULT 0,\n"
- + " id integer);";
- stmt.execute(sql1);
- stmt.execute(sql2);
- stmt.execute(userRegistration);
- int counter = 0;
- try {
- sql = "SELECT * FROM user WHERE id = 1;";
- stmt.execute(sql);
- // Default user has entries null!
- if(stmt.getResultSet().getInt("firstDataBase") == 0)
- {
- RegisterUser.registerUser();
- }
- }
- catch (Exception e){
- sql = "INSERT INTO user (userName, asset,companyName, id) VALUES (NULL, NULL,NULL, 1);";
- stmt.execute(sql);
- }
- // If user enters the program for the first time
- sql = "SELECT * FROM user WHERE id = 1;";
- stmt.execute(sql);
- // Default user has entries null!
- if(stmt.getResultSet().getInt("firstDataBase") == 0)
- {
- RegisterUser.registerUser();
- }
- }
- catch (Exception e){
- }
- }
- public static Connection connect() {
- // SQLite connection string
- try {
- con = DriverManager.getConnection(url);
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- return con;
- }
- // Displays / updates stocks table and fills combo boxes (Buy and sell).
- public static void displayStocksTable() throws SQLException{
- stmt = con.createStatement();
- stocksData.clear();
- articelList.clear();
- resStocks = stmt.executeQuery("SELECT * FROM stocks;");
- while(resStocks.next())
- {
- stocksData.addAll(new ProductInfo( resStocks.getInt("amount"), resStocks.getString("name"),
- resStocks.getDouble("purchasingPrice"), resStocks.getDouble("retailPrice") ));
- articelList.addAll(resStocks.getString("name"));
- }
- stocks.setItems(stocksData);
- }
- // Displays and updates transactionsTable
- public static void displayTransactionsTable()throws SQLException{
- stmt = con.createStatement();
- tableData.clear();
- resTransations = stmt.executeQuery("SELECT * FROM transactions;");
- while(resTransations.next())
- {
- tableData.addAll(new Transactions(resTransations.getString("productName"), resTransations.getInt("number"),
- resTransations.getDouble("unitPrice"), resTransations.getDouble("total") ));
- }
- transactions.setItems(tableData);
- }
- public static void comboBoxHandler(ComboBox comboBox, TextField textField, TextField textField2, TextField numberInput, boolean isSale) throws SQLException{
- try {
- String s = comboBox.getValue().toString();
- int amountField = Integer.parseInt(numberInput.getText());
- if(isSale){
- amountField*=-1;
- }
- if(!s.equals("Articles")){
- if (s != null)
- {
- sql = "SELECT * FROM stocks WHERE name = '" + s + "';";
- stmt = con.createStatement();
- stmt.execute(sql);
- double resultDoubelevalue = stmt.getResultSet().getDouble("purchasingPrice");
- textField.setText("" + resultDoubelevalue);
- textField2.setText("" + (resultDoubelevalue*amountField) );
- }
- }
- }
- catch (Exception e){
- }
- }
- public static void comboBoxHandler2(ComboBox comboBox, TextField textField, TextField textField2, TextField numberInput, boolean isSale) throws SQLException{
- try {
- String s = comboBox.getValue().toString();
- int amountField = Integer.parseInt(numberInput.getText());
- if(isSale){
- amountField*=-1;
- }
- if(!s.equals("Articles")){
- if (s != null)
- {
- sql = "SELECT * FROM stocks WHERE name = '" + s + "';";
- stmt = con.createStatement();
- stmt.execute(sql);
- double resultDoubelevalue = stmt.getResultSet().getDouble("retailPrice");
- textField.setText("" + resultDoubelevalue);
- textField2.setText("" + (resultDoubelevalue*amountField) );
- }
- }
- }
- catch (Exception e){
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement