daily pastebin goal
45%
SHARE
TWEET

zadatakdino

a guest Mar 17th, 2018 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. main
  2. package jfxas1;  
  3.    
  4.  import java.net.URL;  
  5.  import javafx.application.Application;  
  6.  import javafx.fxml.FXMLLoader;  
  7.  import javafx.scene.Scene;  
  8.   import javafx.scene.layout.BorderPane;  
  9.   import javafx.stage.Stage;  
  10.    
  11.  
  12.   public class Main extends Application{  
  13.          
  14.     public static void main(String[] args) {  
  15.       launch(args);  
  16.     }  
  17.    
  18.     @Override  
  19.     public void start(Stage primaryStage) throws Exception {  
  20.        
  21.       URL fxmlUrl = getClass().getClassLoader().getResource("view/ProdajaView.fxml");  
  22.       BorderPane root = FXMLLoader.<BorderPane>load(fxmlUrl);  
  23.      Scene scene = new Scene(root);  
  24.       primaryStage.setScene(scene);  
  25.       primaryStage.setTitle("Registrovanje prodaje internet-paketa");  
  26.       primaryStage.centerOnScreen();  
  27.       primaryStage.setResizable(false);  
  28.       primaryStage.show();  
  29.        
  30.     }  
  31.      
  32.   }  
  33. Prodaja model
  34.   package model;  
  35.   import java.sql.Connection;  
  36.   import java.sql.DriverManager;  
  37.   import java.sql.PreparedStatement;  
  38.   import java.sql.SQLException;  
  39.   import java.util.ArrayList;  
  40.   import java.util.List;  
  41.   import javafx.beans.property.IntegerProperty;  
  42.   import javafx.beans.property.ObjectProperty;  
  43.   import javafx.beans.property.SimpleIntegerProperty;  
  44.  import javafx.beans.property.SimpleObjectProperty;  
  45.   import javafx.beans.property.SimpleStringProperty;  
  46.   import javafx.beans.property.StringProperty;  
  47.    
  48.    
  49.   public class ProdajaModel {  
  50.      
  51.     List<ProdajaModel> listaProdaja;  
  52.      
  53.     private final IntegerProperty brzina = new SimpleIntegerProperty(this,"brzina");  
  54.     private final ObjectProperty protok = new SimpleObjectProperty(this,"protok");  
  55.     private final IntegerProperty ugovor = new SimpleIntegerProperty(this,"ugovor");  
  56.     private final IntegerProperty id = new SimpleIntegerProperty(this,"id");  
  57.     private final StringProperty imePrezime = new SimpleStringProperty(this,"imePrezime");  
  58.     private final StringProperty adresa = new SimpleStringProperty(this,"adresa");  
  59.     private final ObjectProperty<ArrayList<String>> errorList = new SimpleObjectProperty<>(this, "errorList", new ArrayList<>());  
  60.      
  61.     public ProdajaModel() {}  
  62.      
  63.     public ProdajaModel(int brzina,Object protok,int ugovor,int id,String imePrezime,String adresa) {  
  64.       this.brzina.set(brzina);  
  65.       this.protok.set(protok);  
  66.     this.ugovor.set(ugovor);  
  67.      this.id.set(id);  
  68.      this.imePrezime.set(imePrezime);  
  69.      this.adresa.set(adresa);  
  70.    }  
  71.      
  72.     public int getBrzina() {return brzina.get();}  
  73.     public void setBrzina(int brzina) {this.brzina.set(brzina);}  
  74.     public IntegerProperty brzinaProperty() {return brzina;}  
  75.      
  76.     public Object getProtok() {return protok.get();}  
  77.     public void setProtok(Object protok) {this.protok.set(protok);}  
  78.     public ObjectProperty protokProperty() {return protok;}  
  79.      
  80.     public int getUgovor() {return ugovor.get();}  
  81.    public void setUgovor(int ugovor) {this.ugovor.set(ugovor);}  
  82.     public IntegerProperty ugovorProperty() {return ugovor;}  
  83.      
  84.     public int getId() {return id.get();}  
  85.     public void setId(int id) {this.id.set(id);}  
  86.     public IntegerProperty idProperty() {return id;}  
  87.      
  88.    public String getImePrezime() {return imePrezime.get();}  
  89.    public void setImePrezime(String imePrezime) {this.imePrezime.set(imePrezime);}  
  90.    public StringProperty imePrezimeProperty() {return imePrezime;}  
  91.      
  92.    public String getAdresa() {return adresa.get();}  
  93.    public void setAdresa(String adresa) {this.adresa.set(adresa);}  
  94.    public StringProperty adresaProperty() {return adresa;}  
  95.      
  96.     public ObjectProperty<ArrayList<String>> errorsProperty() {return errorList;}  
  97.      
  98.     public boolean isValid() {  
  99.        boolean isValid = true;  
  100.      
  101.       if (imePrezime.get() == null) {  
  102.         errorList.getValue().add(" Ime i prezime moraju biti uneti!");  
  103.         isValid = false;  
  104.       }  
  105.       if (adresa.get() == null) {  
  106.         errorList.getValue().add(" Adresa mora biti uneta!");  
  107.         isValid = false;  
  108.       }  
  109.       if (protok.get() == null) {  
  110.        errorList.getValue().add(" Protok mora biti odabran!");  
  111.         isValid = false;  
  112.       }  
  113.       if (ugovor.get() == 0) {  
  114.         errorList.getValue().add(" Trajanje ugovora mora biti odredjeno!");  
  115.         isValid = false;  
  116.      }  
  117.      if (brzina.get() == 0) {  
  118.        errorList.getValue().add(" Brzina protoka mora biti odabrana!");  
  119.        isValid = false;  
  120.       }  
  121.       return isValid;  
  122.     }  
  123.      
  124.     public String save() throws ClassNotFoundException {  
  125.        
  126.       String poruka = "Unos uspesno obavljen";  
  127.        
  128.       if (isValid()) {  
  129.         Class.forName("com.mysql.jdbc.Driver");  
  130.        try( Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/internet_paketi","root","");) {  
  131.        PreparedStatement st = conn.prepareStatement("insert into prodaja (prodajaId, imePrezime,adresa,ugovor,brzina,protok) values (?,?,?,?,?,?)");  
  132.         st.setString(1, Integer.toString(id.get()));  
  133.        st.setString(2, imePrezime.get());  
  134.         st.setString(3, adresa.get());  
  135.         st.setString(4, Integer.toString(ugovor.get()));  
  136.         st.setString(5, Integer.toString(brzina.get()));  
  137.         st.setString(6, protok.get().toString());  
  138.         st.execute();  
  139.              
  140.       } catch (SQLException e) {  
  141.         poruka = "GRESKA! " + e.getMessage();  
  142.       }  
  143.       }  
  144.       return poruka;  
  145.     }  
  146.      
  147.     @Override  
  148.     public String toString() {  
  149.      return  
  150.           "ID: " + id.get() + "\n" +  
  151.           "Ime i prezime: " + imePrezime.get() + "\n" +  
  152.           "Adresa: " + adresa.get() + "\n" +  
  153.           "Ugovor: " + ugovor.get() + " god." + "\n" +  
  154.           "Brzina: " + brzina.get() + " Mbit" +"\n" +  
  155.           "Protok: " + protok.get() + " GB";  
  156.     }  
  157.   }  
  158.  
  159. prodajacontroller
  160. package controller;  
  161.  
  162. import java.sql.Connection;  
  163. import java.sql.DriverManager;  
  164. import java.sql.PreparedStatement;  
  165. import java.sql.ResultSet;  
  166. import java.sql.SQLException;  
  167. import java.sql.Statement;  
  168. import java.util.ArrayList;  
  169. import javafx.collections.FXCollections;  
  170. import javafx.collections.ObservableList;  
  171. import javafx.fxml.FXML;  
  172. import javafx.scene.control.Alert;  
  173. import javafx.scene.control.Alert.AlertType;  
  174. import javafx.scene.control.ChoiceBox;  
  175. import javafx.scene.control.ListView;  
  176. import javafx.scene.control.TextField;  
  177. import model.ProdajaModel;  
  178.    
  179. public class ProdajaController {  
  180.  
  181. ProdajaModel prodaja;  
  182.  
  183. public ProdajaController() {  
  184. }  
  185.  
  186. @FXML  
  187. private ChoiceBox cbBrzina;  
  188. @FXML  
  189. private ChoiceBox cbProtok;  
  190. @FXML  
  191. private ChoiceBox cbUgovor;  
  192. @FXML  
  193.    private TextField tfImePrezime;  
  194. @FXML  
  195. private TextField tfAdresa;  
  196. @FXML  
  197. private ListView listView;  
  198.  
  199. ObservableList<ProdajaModel> prodajaLista = FXCollections.observableArrayList();  
  200.  
  201. private ObservableList<ProdajaModel> prikaz() throws ClassNotFoundException {  
  202. Class.forName("com.mysql.jdbc.Driver");  
  203. ObservableList<ProdajaModel> prodajaLista1 = FXCollections.observableArrayList();  
  204. try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/internet_paketi", "root", "");) {  
  205. Statement st = conn.createStatement();  
  206.         st.executeQuery("select * from prodaja");  
  207. ResultSet rs = st.getResultSet();  
  208.  
  209. while (rs.next()) {  
  210. ProdajaModel prodaja = new ProdajaModel(  
  211. Integer.parseInt(rs.getString("brzina")),  
  212. rs.getString("protok"),  
  213. Integer.parseInt(rs.getString("ugovor")),  
  214. Integer.parseInt(rs.getString("prodajaId")),  
  215.               rs.getString("imePrezime"),  
  216. rs.getString("adresa"));  
  217. prodajaLista1.add(prodaja);  
  218.          prodajaLista = prodajaLista1;  
  219. }  
  220.  
  221. } catch (SQLException e) {  
  222. System.out.println("GRESKA: " + e.getMessage());  
  223. }  
  224.  
  225. return prodajaLista1;  
  226. }  
  227.  
  228. @FXML  
  229. private void initialize() throws ClassNotFoundException {  
  230. prodaja = new ProdajaModel();  
  231.  
  232. cbBrzina.getItems().addAll(2, 5, 10, 20, 50, 100);  
  233. cbBrzina.valueProperty().bindBidirectional(prodaja.brzinaProperty());  
  234. cbProtok.getItems().addAll(1, 5, 10, 100, "Flat");  
  235. cbProtok.valueProperty().bindBidirectional(prodaja.protokProperty());  
  236. cbUgovor.getItems().addAll(1, 2);  
  237. cbUgovor.valueProperty().bindBidirectional(prodaja.ugovorProperty());  
  238. tfImePrezime.textProperty().bindBidirectional(prodaja.imePrezimeProperty());  
  239. tfAdresa.textProperty().bindBidirectional(prodaja.adresaProperty());  
  240. prikaz();  
  241. listView.setItems(prodajaLista);  
  242.  
  243. }  
  244.  
  245. @FXML  
  246. private void obrisiProdaju() throws ClassNotFoundException {  
  247. Class.forName("com.mysql.jdbc.Driver");  
  248.  
  249. int selIdx = listView.getSelectionModel().getSelectedIndex();  
  250. String modId = null;  
  251.  
  252. for (ProdajaModel mod : prodajaLista) {  
  253. if (selIdx == prodajaLista.indexOf(mod)) {  
  254. modId = Integer.toString(mod.getId());  
  255. }  
  256. }  
  257.  
  258. try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/internet_paketi", "root", "");) {  
  259. PreparedStatement st = conn.prepareStatement("delete from prodaja where prodajaId=?");  
  260. st.setString(1, modId);  
  261. st.execute();  
  262. listView.setItems(prikaz());  
  263.  
  264. } catch (SQLException e) {  
  265. System.out.println("GRESKA! " + e.getMessage());  
  266. }  
  267.  
  268. }  
  269.  
  270. @FXML  
  271. private void prodaj() throws ClassNotFoundException {  
  272.  
  273. if (prodaja.isValid()) {  
  274.  
  275. prodaja.setId(prodajaLista.size() + 1);  
  276. prodaja.save();  
  277. listView.setItems(prikaz());  
  278.  
  279. } else {  
  280. StringBuilder errMsg = new StringBuilder();  
  281.  
  282. ArrayList<String> errList = prodaja.errorsProperty().get();  
  283.  
  284. for (String errList1 : errList) {  
  285. errMsg.append(errList1);  
  286. }  
  287.  
  288. Alert alert = new Alert(AlertType.ERROR);  
  289. alert.setTitle("Prodaja se ne moze izvrsiti!");  
  290. alert.setHeaderText(null);  
  291. alert.setContentText(errMsg.toString());  
  292. alert.showAndWait();  
  293. errList.clear();  
  294. }
  295. }
  296. }
  297. prodajaview
  298. <?xml version="1.0" encoding="UTF-8"?>  
  299.    
  300.   <?import java.lang.*?>  
  301.   <?import java.net.*?>  
  302.   <?import java.util.*?>  
  303.   <?import javafx.scene.*?>  
  304.   <?import javafx.scene.control.*?>  
  305.   <?import javafx.scene.layout.*?>  
  306.   <?import javafx.geometry.Insets?>  
  307.    
  308.   <BorderPane id="bp1" prefHeight="500.0" prefWidth="500.0" styleClass="mainFxmlClass"  
  309.         xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.ProdajaController">  
  310.     <stylesheets>  
  311.       <URL value="@ProdajaStil.css"/>  
  312.     </stylesheets>  
  313.     <top>  
  314.       <HBox fx:id="hbTit">  
  315.         <Label fx:id="lbl1" text="Registrovanje prodaje:" />  
  316.       </HBox>  
  317.     </top>  
  318.     <left>  
  319.       <VBox fx:id="vbox1" spacing="20">  
  320.         <BorderPane.margin>  
  321.           <Insets top="25.0" />  
  322. </BorderPane.margin>  
  323.         <children>  
  324.           <HBox spacing="10" alignment="BASELINE_LEFT">  
  325.             <children>  
  326.               <Label fx:id="lbl2" text="Brzina protoka: " />  
  327.               <ChoiceBox fx:id="cbBrzina" value="2" />  
  328.               <Label text=" Mbit"/>  
  329.             </children>  
  330.           </HBox>  
  331. <HBox spacing="10" alignment="BASELINE_LEFT">  
  332.             <children>  
  333.               <Label fx:id="lbl3" text="Protok: " />  
  334.               <ChoiceBox fx:id="cbProtok" />  
  335.               <Label text=" GB" />  
  336.             </children>  
  337. </HBox>  
  338.           <HBox spacing="10" alignment="BASELINE_LEFT">  
  339. <children>  
  340.               <Label fx:id="lbl4" text="Trajanje ugovora: " />  
  341.               <ChoiceBox fx:id="cbUgovor"/>  
  342.               <Label text=" god." />  
  343.             </children>  
  344.           </HBox>  
  345.           <HBox spacing="10" alignment="BASELINE_LEFT">  
  346.             <children>  
  347.               <Label fx:id="lbl5" text="Ime i prezime korisnika: " />  
  348.               <TextField fx:id="tfImePrezime"/>  
  349. </children>  
  350.           </HBox>  
  351.           <HBox spacing="10" alignment="BASELINE_LEFT">  
  352.             <children>  
  353.               <Label fx:id="lbl6" text="Adresa korisnika: " />  
  354.               <TextField fx:id="tfAdresa"/>  
  355.             </children>  
  356.           </HBox>  
  357.           <Button fx:id="prodajaBtn" text="Prodaj" AnchorPane.leftAnchor="0.0" onAction="#prodaj"/>  
  358.           <Label /> <Label /> <Label />  
  359.           <AnchorPane fx:id="apDel">  
  360.             <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">  
  361.               <Button fx:id="delBtn" text="Obrisi selektovanu prodaju" onAction="#obrisiProdaju"/>  
  362.             </HBox>  
  363. </AnchorPane>      
  364.         </children>  
  365.       </VBox>  
  366.     </left>    
  367.     <center>  
  368.      <AnchorPane>  
  369.        <BorderPane.margin>  
  370.           <Insets bottom="0.0" left="25.0" top="25.0" right="0.0"/>  
  371.         </BorderPane.margin>  
  372.           <ListView fx:id="listView" AnchorPane.bottomAnchor="0.0" AnchorPane.topAnchor="0.0" />  
  373.       </AnchorPane>  
  374.     </center>  
  375.   </BorderPane>  
  376.  
  377. prodaja stil
  378.  
  379. .root {  
  380.     -fx-padding: 50;  
  381.     -fx-background-color: #c1cdd9;  
  382.   }  
  383.    
  384. #apDel {  
  385.     -fx-Background-color: #006400;  
  386.   }  
  387.  
  388. #hbTit .label {  
  389.     -fx-text-fill: black;  
  390.   }  
  391.  
  392. sql
  393.  
  394. CREATE DATABASE IF NOT EXISTS `internet_paketi` /*!40100 DEFAULT CHARACTER SET latin1 */;  
  395.  USE `internet_paketi`;  
  396.  -- MySQL dump 10.13 Distrib 5.5.57, for debian-linux-gnu (i686)  
  397.  --  
  398.  -- Host: 127.0.0.1  Database: internet_paketi  
  399.  -- ------------------------------------------------------  
  400.  -- Server version     5.5.57-0ubuntu0.14.04.1  
  401.    
  402.  /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;  
  403.  /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;  
  404.  /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;  
  405.  /*!40101 SET NAMES utf8 */;  
  406.  /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;  
  407.  /*!40103 SET TIME_ZONE='+00:00' */;  
  408.  /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;  
  409.  /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;  
  410.  /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;  
  411.  /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;  
  412.    
  413.  --  
  414.  -- Table structure for table `prodaja`  
  415.  --  
  416.    
  417.  DROP TABLE IF EXISTS `prodaja`;  
  418.  /*!40101 SET @saved_cs_client   = @@character_set_client */;  
  419.  /*!40101 SET character_set_client = utf8 */;  
  420.  CREATE TABLE `prodaja` (  
  421.   `prodajaId` int(11) NOT NULL,  
  422.   `imePrezime` varchar(45) NOT NULL,  
  423.   `adresa` varchar(45) NOT NULL,  
  424.   `ugovor` int(11) NOT NULL,  
  425.   `brzina` int(11) NOT NULL,  
  426.   `protok` varchar(45) NOT NULL,  
  427.   PRIMARY KEY (`prodajaId`)  
  428.  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;  
  429.  /*!40101 SET character_set_client = @saved_cs_client */;  
  430.    
  431.  --  
  432.  -- Dumping data for table `prodaja`  
  433.  --  
  434.    
  435.  LOCK TABLES `prodaja` WRITE;  
  436.  /*!40000 ALTER TABLE `prodaja` DISABLE KEYS */;  
  437.  INSERT INTO `prodaja` VALUES (1,'Dino Curic','Sarajevo',2,100,'Flat'),(2,'Darko Bojkovic','Beograd',2,50,'Flat');  
  438.  /*!40000 ALTER TABLE `prodaja` ENABLE KEYS */;  
  439.  UNLOCK TABLES;  
  440.  /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;  
  441.    
  442.  /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;  
  443.  /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;  
  444.  /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;  
  445.  /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;  
  446.  /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;  
  447.  /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;  
  448.  /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top