Skip to content

Commit

Permalink
Initial Accounting report. Products.
Browse files Browse the repository at this point in the history
  • Loading branch information
Labusyuk committed Feb 9, 2020
0 parents commit a4be957
Show file tree
Hide file tree
Showing 11 changed files with 795 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea/*
.class
out/
target/
logs/
18 changes: 18 additions & 0 deletions Accounting report.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:8.0.18" level="project" />
<orderEntry type="library" name="Maven: com.google.protobuf:protobuf-java:3.6.1" level="project" />
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.10" level="project" />
</component>
</module>
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>groupId</groupId>
<artifactId>Accounting report</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>


</project>
118 changes: 118 additions & 0 deletions src/main/java/sample/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package sample;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import sample.dao.ProductDao;
import sample.model.Product;
import javafx.scene.control.TableColumn;
public class Controller {

@FXML
public TextField fieldId;
@FXML
public TextField fieldName;
@FXML
public TextField fieldType;
@FXML
public TextField fieldWholesalePrice;
@FXML
public TextField fieldRetailPrice;
@FXML
public TextField fieldProvider;
@FXML
public TextField fieldCount;
ProductDao productDao = new ProductDao();
private ObservableList<Product> productList = FXCollections.observableArrayList();
@FXML
private TableView<Product> tableProduct;
@FXML
private TableColumn<Product, Integer> idColumn;
@FXML
private TableColumn<Product, String> nameColumn;
@FXML
private TableColumn<Product, String> typeColumn;
@FXML
private TableColumn<Product, Integer> wholesalePriceColumn;
@FXML
private TableColumn<Product, Integer> retailPriceColumn;
@FXML
private TableColumn<Product, String> providerColumn;
@FXML
private TableColumn<Product, Integer> countColumn;


// инициализируем форму данными
@FXML
private void initialize() {
initData();

// устанавливаем тип и значение которое должно хранится в колонке
idColumn.setCellValueFactory(new PropertyValueFactory<Product, Integer>("id"));
nameColumn.setCellValueFactory(new PropertyValueFactory<Product, String>("name"));
typeColumn.setCellValueFactory(new PropertyValueFactory<Product, String>("type"));
wholesalePriceColumn.setCellValueFactory(new PropertyValueFactory<Product, Integer>("wholesale_price"));
retailPriceColumn.setCellValueFactory(new PropertyValueFactory<Product, Integer>("retail_price"));
providerColumn.setCellValueFactory(new PropertyValueFactory<Product, String>("provider_id"));
countColumn.setCellValueFactory(new PropertyValueFactory<Product, Integer>("count"));



// заполняем таблицу данными
tableProduct.setItems(productList);

}

// подготавливаем данные для таблицы
// вы можете получать их с базы данных
private void initData() {

productList.addAll(productDao.getAll());

/*
productList.add(new Product(1, "Xiaomi A1","Телефон",500,600,0,1));
productList.add(new Product(2, "Samsung Galaxy S5","Телефон",800,100,0,1));
productList.add(new Product(3, "Iphone XS", "Телефон", 1000,1200,0,1));
productList.add(new Product(4, "Nokia", "Телефон", 20,21,0,1));
productList.add(new Product(5, "Samsung HotBench", "Мікрохвильова піч", 1000,1200,0,1));
for(Product product:productList)
productDao.create(product);
*/
}

public void onClickTableView(MouseEvent mouseEvent) {
int index = tableProduct.getSelectionModel().getSelectedIndex();
fieldId.setText(Integer.toString(tableProduct.getSelectionModel().getTableView().getItems().get(index).getId()));
fieldName.setText(tableProduct.getSelectionModel().getTableView().getItems().get(index).getName());
fieldType.setText(tableProduct.getSelectionModel().getTableView().getItems().get(index).getType());
fieldRetailPrice.setText(Integer.toString(tableProduct.getSelectionModel().getTableView().getItems().get(index).getRetail_price()));
fieldWholesalePrice.setText(Integer.toString(tableProduct.getSelectionModel().getTableView().getItems().get(index).getWholesale_price()));
fieldProvider.setText(Integer.toString(tableProduct.getSelectionModel().getTableView().getItems().get(index).getProvider_id()));
fieldCount.setText(Integer.toString(tableProduct.getSelectionModel().getTableView().getItems().get(index).getCount()));
}

public void onClickApply(MouseEvent mouseEvent) {
Product product = new Product(Integer.valueOf(fieldId.getText()),fieldName.getText(),fieldType.getText(), Integer.valueOf(fieldWholesalePrice.getText()),Integer.valueOf(fieldRetailPrice.getText()),Integer.valueOf(fieldProvider.getText()),Integer.valueOf(fieldCount.getText()));
if(!fieldId.getText().isEmpty() && productDao.findByID(Integer.valueOf(fieldId.getText()))!=null){
productDao.updateProduct(product);
}else{
productDao.create(product);
}
productList.clear();
productList.addAll(productDao.getAll());

}

public void onClickDelete(MouseEvent mouseEvent) {
if (!fieldId.getText().isEmpty() && productDao.findByID(Integer.valueOf(fieldId.getText())) != null) {
productDao.deleteById(Integer.valueOf(fieldId.getText()));
}
productList.clear();
productList.addAll(productDao.getAll());
}
}
26 changes: 26 additions & 0 deletions src/main/java/sample/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
String fxmlFile = "/fxml/sample.fxml";
FXMLLoader loader = new FXMLLoader();
Parent root = (Parent) loader.load(getClass().getResourceAsStream(fxmlFile));

primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 750, 300));
primaryStage.show();
}


public static void main(String[] args) {
launch();
}
}
117 changes: 117 additions & 0 deletions src/main/java/sample/dao/ProductDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package sample.dao;

import sample.db.ConnectionPool;
import sample.model.Product;
import sample.model.Provider;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ProductDao {
private ConnectionPool pool = ConnectionPool.getInstance();
private final String INSERT_PRODUCT= "INSERT INTO trade_report.product (id, name, type, wholesale_price, retail_price, provider_id, count) VALUES (?, ?, ?, ?, ?, ?, ?)";
private final String SELECT_ALL_PRODUCT= "SELECT * FROM trade_report.product";
private final String DELETE_PRODUCT= "DELETE FROM trade_report.product WHERE id=?";
private final String UPDATE_PRODUCT= "UPDATE trade_report.product SET name=?, type=?, wholesale_price=?, retail_price=?, provider_id=?, count=? WHERE ID=?";
private final String SELECT_PRODUCT_BY_ID = "SELECT * FROM trade_report.product WHERE id=?";

public Integer create(Product product) {
int idUser = 0;
try (Connection connection = pool.takeConnection();
PreparedStatement statement = connection.prepareStatement(INSERT_PRODUCT, Statement.RETURN_GENERATED_KEYS)) {
statement.setInt(1, product.getId());
statement.setString(2, product.getName());
statement.setString(3, String.valueOf(product.getType()));
statement.setInt(4, product.getWholesale_price());
statement.setInt(5, product.getRetail_price());
statement.setInt(6, product.getProvider_id());
statement.setInt(7, product.getCount());
statement.executeUpdate();
ResultSet resultSet = statement.getGeneratedKeys();
if(resultSet.next())
idUser = resultSet.getInt(1);
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
return idUser;
}

public List<Product> getAll() {
List<Product> products = new ArrayList<>();
try (Connection connection = pool.takeConnection();
PreparedStatement statement = connection.prepareStatement(SELECT_ALL_PRODUCT);
ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
Product product = retrieveEntity(resultSet);
products.add(product);
}
} catch (SQLException e) {
e.printStackTrace();
}
return products;
}

public Product findByID(int id){
Product product = null;
try (Connection connection = pool.takeConnection();
PreparedStatement statement = connection.prepareStatement(SELECT_PRODUCT_BY_ID)) {
statement.setInt(1, id);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
product = retrieveEntity(resultSet);
}
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
return product;
}

public boolean updateProduct(Product product){
int idUser = 0;
try (Connection connection = pool.takeConnection();
PreparedStatement statement = connection.prepareStatement(UPDATE_PRODUCT)) {
statement.setString(1, product.getName());
statement.setString(2, String.valueOf(product.getType()));
statement.setInt(3, product.getWholesale_price());
statement.setInt(4, product.getRetail_price());
statement.setInt(5, product.getProvider_id());
statement.setInt(6, product.getCount());
statement.setInt(7, product.getId());
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}

return true; ///fix this shit;
}

public void deleteById(Integer id) {
try (Connection connection = pool.takeConnection();
PreparedStatement statement = connection.prepareStatement(DELETE_PRODUCT)) {
statement.setInt(1, id);
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}

public Product retrieveEntity(ResultSet resultSet) {
Product product = new Product();
try{
product.setId(resultSet.getInt("id"));
product.setName(resultSet.getString("name"));
product.setType(resultSet.getString("type"));
product.setWholesale_price(resultSet.getInt("wholesale_price"));
product.setRetail_price(resultSet.getInt("retail_price"));
product.setProvider_id(resultSet.getInt("provider_id"));
product.setCount(resultSet.getInt("count"));
} catch (SQLException e) {
e.printStackTrace();
}
return product;
}

}
Loading

0 comments on commit a4be957

Please sign in to comment.