Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main
- package jfxas1;
- import java.net.URL;
- import javafx.application.Application;
- import javafx.fxml.FXMLLoader;
- import javafx.scene.Scene;
- import javafx.scene.layout.BorderPane;
- import javafx.stage.Stage;
- public class Main extends Application{
- public static void main(String[] args) {
- launch(args);
- }
- @Override
- public void start(Stage primaryStage) throws Exception {
- URL fxmlUrl = getClass().getClassLoader().getResource("view/ProdajaView.fxml");
- BorderPane root = FXMLLoader.<BorderPane>load(fxmlUrl);
- Scene scene = new Scene(root);
- primaryStage.setScene(scene);
- primaryStage.setTitle("Registrovanje prodaje internet-paketa");
- primaryStage.centerOnScreen();
- primaryStage.setResizable(false);
- primaryStage.show();
- }
- }
- Prodaja model
- package model;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- import javafx.beans.property.IntegerProperty;
- import javafx.beans.property.ObjectProperty;
- import javafx.beans.property.SimpleIntegerProperty;
- import javafx.beans.property.SimpleObjectProperty;
- import javafx.beans.property.SimpleStringProperty;
- import javafx.beans.property.StringProperty;
- public class ProdajaModel {
- List<ProdajaModel> listaProdaja;
- private final IntegerProperty brzina = new SimpleIntegerProperty(this,"brzina");
- private final ObjectProperty protok = new SimpleObjectProperty(this,"protok");
- private final IntegerProperty ugovor = new SimpleIntegerProperty(this,"ugovor");
- private final IntegerProperty id = new SimpleIntegerProperty(this,"id");
- private final StringProperty imePrezime = new SimpleStringProperty(this,"imePrezime");
- private final StringProperty adresa = new SimpleStringProperty(this,"adresa");
- private final ObjectProperty<ArrayList<String>> errorList = new SimpleObjectProperty<>(this, "errorList", new ArrayList<>());
- public ProdajaModel() {}
- public ProdajaModel(int brzina,Object protok,int ugovor,int id,String imePrezime,String adresa) {
- this.brzina.set(brzina);
- this.protok.set(protok);
- this.ugovor.set(ugovor);
- this.id.set(id);
- this.imePrezime.set(imePrezime);
- this.adresa.set(adresa);
- }
- public int getBrzina() {return brzina.get();}
- public void setBrzina(int brzina) {this.brzina.set(brzina);}
- public IntegerProperty brzinaProperty() {return brzina;}
- public Object getProtok() {return protok.get();}
- public void setProtok(Object protok) {this.protok.set(protok);}
- public ObjectProperty protokProperty() {return protok;}
- public int getUgovor() {return ugovor.get();}
- public void setUgovor(int ugovor) {this.ugovor.set(ugovor);}
- public IntegerProperty ugovorProperty() {return ugovor;}
- public int getId() {return id.get();}
- public void setId(int id) {this.id.set(id);}
- public IntegerProperty idProperty() {return id;}
- public String getImePrezime() {return imePrezime.get();}
- public void setImePrezime(String imePrezime) {this.imePrezime.set(imePrezime);}
- public StringProperty imePrezimeProperty() {return imePrezime;}
- public String getAdresa() {return adresa.get();}
- public void setAdresa(String adresa) {this.adresa.set(adresa);}
- public StringProperty adresaProperty() {return adresa;}
- public ObjectProperty<ArrayList<String>> errorsProperty() {return errorList;}
- public boolean isValid() {
- boolean isValid = true;
- if (imePrezime.get() == null) {
- errorList.getValue().add(" Ime i prezime moraju biti uneti!");
- isValid = false;
- }
- if (adresa.get() == null) {
- errorList.getValue().add(" Adresa mora biti uneta!");
- isValid = false;
- }
- if (protok.get() == null) {
- errorList.getValue().add(" Protok mora biti odabran!");
- isValid = false;
- }
- if (ugovor.get() == 0) {
- errorList.getValue().add(" Trajanje ugovora mora biti odredjeno!");
- isValid = false;
- }
- if (brzina.get() == 0) {
- errorList.getValue().add(" Brzina protoka mora biti odabrana!");
- isValid = false;
- }
- return isValid;
- }
- public String save() throws ClassNotFoundException {
- String poruka = "Unos uspesno obavljen";
- if (isValid()) {
- Class.forName("com.mysql.jdbc.Driver");
- try( Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/internet_paketi","root","");) {
- PreparedStatement st = conn.prepareStatement("insert into prodaja (prodajaId, imePrezime,adresa,ugovor,brzina,protok) values (?,?,?,?,?,?)");
- st.setString(1, Integer.toString(id.get()));
- st.setString(2, imePrezime.get());
- st.setString(3, adresa.get());
- st.setString(4, Integer.toString(ugovor.get()));
- st.setString(5, Integer.toString(brzina.get()));
- st.setString(6, protok.get().toString());
- st.execute();
- } catch (SQLException e) {
- poruka = "GRESKA! " + e.getMessage();
- }
- }
- return poruka;
- }
- @Override
- public String toString() {
- return
- "ID: " + id.get() + "\n" +
- "Ime i prezime: " + imePrezime.get() + "\n" +
- "Adresa: " + adresa.get() + "\n" +
- "Ugovor: " + ugovor.get() + " god." + "\n" +
- "Brzina: " + brzina.get() + " Mbit" +"\n" +
- "Protok: " + protok.get() + " GB";
- }
- }
- prodajacontroller
- package controller;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.fxml.FXML;
- import javafx.scene.control.Alert;
- import javafx.scene.control.Alert.AlertType;
- import javafx.scene.control.ChoiceBox;
- import javafx.scene.control.ListView;
- import javafx.scene.control.TextField;
- import model.ProdajaModel;
- public class ProdajaController {
- ProdajaModel prodaja;
- public ProdajaController() {
- }
- @FXML
- private ChoiceBox cbBrzina;
- @FXML
- private ChoiceBox cbProtok;
- @FXML
- private ChoiceBox cbUgovor;
- @FXML
- private TextField tfImePrezime;
- @FXML
- private TextField tfAdresa;
- @FXML
- private ListView listView;
- ObservableList<ProdajaModel> prodajaLista = FXCollections.observableArrayList();
- private ObservableList<ProdajaModel> prikaz() throws ClassNotFoundException {
- Class.forName("com.mysql.jdbc.Driver");
- ObservableList<ProdajaModel> prodajaLista1 = FXCollections.observableArrayList();
- try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/internet_paketi", "root", "");) {
- Statement st = conn.createStatement();
- st.executeQuery("select * from prodaja");
- ResultSet rs = st.getResultSet();
- while (rs.next()) {
- ProdajaModel prodaja = new ProdajaModel(
- Integer.parseInt(rs.getString("brzina")),
- rs.getString("protok"),
- Integer.parseInt(rs.getString("ugovor")),
- Integer.parseInt(rs.getString("prodajaId")),
- rs.getString("imePrezime"),
- rs.getString("adresa"));
- prodajaLista1.add(prodaja);
- prodajaLista = prodajaLista1;
- }
- } catch (SQLException e) {
- System.out.println("GRESKA: " + e.getMessage());
- }
- return prodajaLista1;
- }
- @FXML
- private void initialize() throws ClassNotFoundException {
- prodaja = new ProdajaModel();
- cbBrzina.getItems().addAll(2, 5, 10, 20, 50, 100);
- cbBrzina.valueProperty().bindBidirectional(prodaja.brzinaProperty());
- cbProtok.getItems().addAll(1, 5, 10, 100, "Flat");
- cbProtok.valueProperty().bindBidirectional(prodaja.protokProperty());
- cbUgovor.getItems().addAll(1, 2);
- cbUgovor.valueProperty().bindBidirectional(prodaja.ugovorProperty());
- tfImePrezime.textProperty().bindBidirectional(prodaja.imePrezimeProperty());
- tfAdresa.textProperty().bindBidirectional(prodaja.adresaProperty());
- prikaz();
- listView.setItems(prodajaLista);
- }
- @FXML
- private void obrisiProdaju() throws ClassNotFoundException {
- Class.forName("com.mysql.jdbc.Driver");
- int selIdx = listView.getSelectionModel().getSelectedIndex();
- String modId = null;
- for (ProdajaModel mod : prodajaLista) {
- if (selIdx == prodajaLista.indexOf(mod)) {
- modId = Integer.toString(mod.getId());
- }
- }
- try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/internet_paketi", "root", "");) {
- PreparedStatement st = conn.prepareStatement("delete from prodaja where prodajaId=?");
- st.setString(1, modId);
- st.execute();
- listView.setItems(prikaz());
- } catch (SQLException e) {
- System.out.println("GRESKA! " + e.getMessage());
- }
- }
- @FXML
- private void prodaj() throws ClassNotFoundException {
- if (prodaja.isValid()) {
- prodaja.setId(prodajaLista.size() + 1);
- prodaja.save();
- listView.setItems(prikaz());
- } else {
- StringBuilder errMsg = new StringBuilder();
- ArrayList<String> errList = prodaja.errorsProperty().get();
- for (String errList1 : errList) {
- errMsg.append(errList1);
- }
- Alert alert = new Alert(AlertType.ERROR);
- alert.setTitle("Prodaja se ne moze izvrsiti!");
- alert.setHeaderText(null);
- alert.setContentText(errMsg.toString());
- alert.showAndWait();
- errList.clear();
- }
- }
- }
- prodajaview
- <?xml version="1.0" encoding="UTF-8"?>
- <?import java.lang.*?>
- <?import java.net.*?>
- <?import java.util.*?>
- <?import javafx.scene.*?>
- <?import javafx.scene.control.*?>
- <?import javafx.scene.layout.*?>
- <?import javafx.geometry.Insets?>
- <BorderPane id="bp1" prefHeight="500.0" prefWidth="500.0" styleClass="mainFxmlClass"
- xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.ProdajaController">
- <stylesheets>
- <URL value="@ProdajaStil.css"/>
- </stylesheets>
- <top>
- <HBox fx:id="hbTit">
- <Label fx:id="lbl1" text="Registrovanje prodaje:" />
- </HBox>
- </top>
- <left>
- <VBox fx:id="vbox1" spacing="20">
- <BorderPane.margin>
- <Insets top="25.0" />
- </BorderPane.margin>
- <children>
- <HBox spacing="10" alignment="BASELINE_LEFT">
- <children>
- <Label fx:id="lbl2" text="Brzina protoka: " />
- <ChoiceBox fx:id="cbBrzina" value="2" />
- <Label text=" Mbit"/>
- </children>
- </HBox>
- <HBox spacing="10" alignment="BASELINE_LEFT">
- <children>
- <Label fx:id="lbl3" text="Protok: " />
- <ChoiceBox fx:id="cbProtok" />
- <Label text=" GB" />
- </children>
- </HBox>
- <HBox spacing="10" alignment="BASELINE_LEFT">
- <children>
- <Label fx:id="lbl4" text="Trajanje ugovora: " />
- <ChoiceBox fx:id="cbUgovor"/>
- <Label text=" god." />
- </children>
- </HBox>
- <HBox spacing="10" alignment="BASELINE_LEFT">
- <children>
- <Label fx:id="lbl5" text="Ime i prezime korisnika: " />
- <TextField fx:id="tfImePrezime"/>
- </children>
- </HBox>
- <HBox spacing="10" alignment="BASELINE_LEFT">
- <children>
- <Label fx:id="lbl6" text="Adresa korisnika: " />
- <TextField fx:id="tfAdresa"/>
- </children>
- </HBox>
- <Button fx:id="prodajaBtn" text="Prodaj" AnchorPane.leftAnchor="0.0" onAction="#prodaj"/>
- <Label /> <Label /> <Label />
- <AnchorPane fx:id="apDel">
- <HBox fx:id="vbDel" alignment="BASELINE_CENTER" spacing="10" AnchorPane.topAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="10.0">
- <Button fx:id="delBtn" text="Obrisi selektovanu prodaju" onAction="#obrisiProdaju"/>
- </HBox>
- </AnchorPane>
- </children>
- </VBox>
- </left>
- <center>
- <AnchorPane>
- <BorderPane.margin>
- <Insets bottom="0.0" left="25.0" top="25.0" right="0.0"/>
- </BorderPane.margin>
- <ListView fx:id="listView" AnchorPane.bottomAnchor="0.0" AnchorPane.topAnchor="0.0" />
- </AnchorPane>
- </center>
- </BorderPane>
- prodaja stil
- .root {
- -fx-padding: 50;
- -fx-background-color: #c1cdd9;
- }
- #apDel {
- -fx-Background-color: #006400;
- }
- #hbTit .label {
- -fx-text-fill: black;
- }
- sql
- CREATE DATABASE IF NOT EXISTS `internet_paketi` /*!40100 DEFAULT CHARACTER SET latin1 */;
- USE `internet_paketi`;
- -- MySQL dump 10.13 Distrib 5.5.57, for debian-linux-gnu (i686)
- --
- -- Host: 127.0.0.1 Database: internet_paketi
- -- ------------------------------------------------------
- -- Server version 5.5.57-0ubuntu0.14.04.1
- /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
- /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
- /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
- /*!40101 SET NAMES utf8 */;
- /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
- /*!40103 SET TIME_ZONE='+00:00' */;
- /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
- /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
- /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
- /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
- --
- -- Table structure for table `prodaja`
- --
- DROP TABLE IF EXISTS `prodaja`;
- /*!40101 SET @saved_cs_client = @@character_set_client */;
- /*!40101 SET character_set_client = utf8 */;
- CREATE TABLE `prodaja` (
- `prodajaId` int(11) NOT NULL,
- `imePrezime` varchar(45) NOT NULL,
- `adresa` varchar(45) NOT NULL,
- `ugovor` int(11) NOT NULL,
- `brzina` int(11) NOT NULL,
- `protok` varchar(45) NOT NULL,
- PRIMARY KEY (`prodajaId`)
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
- /*!40101 SET character_set_client = @saved_cs_client */;
- --
- -- Dumping data for table `prodaja`
- --
- LOCK TABLES `prodaja` WRITE;
- /*!40000 ALTER TABLE `prodaja` DISABLE KEYS */;
- INSERT INTO `prodaja` VALUES (1,'Dino Curic','Sarajevo',2,100,'Flat'),(2,'Darko Bojkovic','Beograd',2,50,'Flat');
- /*!40000 ALTER TABLE `prodaja` ENABLE KEYS */;
- UNLOCK TABLES;
- /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
- /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
- /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
- /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
- /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
- /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
- /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
- /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement