· 8 years ago · Mar 17, 2018, 12:10 PM
1main
2package 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 }
33Prodaja 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
159prodajacontroller
160package controller;
161
162import java.sql.Connection;
163import java.sql.DriverManager;
164import java.sql.PreparedStatement;
165import java.sql.ResultSet;
166import java.sql.SQLException;
167import java.sql.Statement;
168import java.util.ArrayList;
169import javafx.collections.FXCollections;
170import javafx.collections.ObservableList;
171import javafx.fxml.FXML;
172import javafx.scene.control.Alert;
173import javafx.scene.control.Alert.AlertType;
174import javafx.scene.control.ChoiceBox;
175import javafx.scene.control.ListView;
176import javafx.scene.control.TextField;
177import model.ProdajaModel;
178
179public class ProdajaController {
180
181ProdajaModel prodaja;
182
183public ProdajaController() {
184}
185
186@FXML
187private ChoiceBox cbBrzina;
188@FXML
189private ChoiceBox cbProtok;
190@FXML
191private ChoiceBox cbUgovor;
192@FXML
193 private TextField tfImePrezime;
194@FXML
195private TextField tfAdresa;
196@FXML
197private ListView listView;
198
199ObservableList<ProdajaModel> prodajaLista = FXCollections.observableArrayList();
200
201private ObservableList<ProdajaModel> prikaz() throws ClassNotFoundException {
202Class.forName("com.mysql.jdbc.Driver");
203ObservableList<ProdajaModel> prodajaLista1 = FXCollections.observableArrayList();
204try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/internet_paketi", "root", "");) {
205Statement st = conn.createStatement();
206 st.executeQuery("select * from prodaja");
207ResultSet rs = st.getResultSet();
208
209while (rs.next()) {
210ProdajaModel prodaja = new ProdajaModel(
211Integer.parseInt(rs.getString("brzina")),
212rs.getString("protok"),
213Integer.parseInt(rs.getString("ugovor")),
214Integer.parseInt(rs.getString("prodajaId")),
215 rs.getString("imePrezime"),
216rs.getString("adresa"));
217prodajaLista1.add(prodaja);
218 prodajaLista = prodajaLista1;
219}
220
221} catch (SQLException e) {
222System.out.println("GRESKA: " + e.getMessage());
223}
224
225return prodajaLista1;
226}
227
228@FXML
229private void initialize() throws ClassNotFoundException {
230prodaja = new ProdajaModel();
231
232cbBrzina.getItems().addAll(2, 5, 10, 20, 50, 100);
233cbBrzina.valueProperty().bindBidirectional(prodaja.brzinaProperty());
234cbProtok.getItems().addAll(1, 5, 10, 100, "Flat");
235cbProtok.valueProperty().bindBidirectional(prodaja.protokProperty());
236cbUgovor.getItems().addAll(1, 2);
237cbUgovor.valueProperty().bindBidirectional(prodaja.ugovorProperty());
238tfImePrezime.textProperty().bindBidirectional(prodaja.imePrezimeProperty());
239tfAdresa.textProperty().bindBidirectional(prodaja.adresaProperty());
240prikaz();
241listView.setItems(prodajaLista);
242
243}
244
245@FXML
246private void obrisiProdaju() throws ClassNotFoundException {
247Class.forName("com.mysql.jdbc.Driver");
248
249int selIdx = listView.getSelectionModel().getSelectedIndex();
250String modId = null;
251
252for (ProdajaModel mod : prodajaLista) {
253if (selIdx == prodajaLista.indexOf(mod)) {
254modId = Integer.toString(mod.getId());
255}
256}
257
258try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/internet_paketi", "root", "");) {
259PreparedStatement st = conn.prepareStatement("delete from prodaja where prodajaId=?");
260st.setString(1, modId);
261st.execute();
262listView.setItems(prikaz());
263
264} catch (SQLException e) {
265System.out.println("GRESKA! " + e.getMessage());
266}
267
268}
269
270@FXML
271private void prodaj() throws ClassNotFoundException {
272
273if (prodaja.isValid()) {
274
275prodaja.setId(prodajaLista.size() + 1);
276prodaja.save();
277listView.setItems(prikaz());
278
279} else {
280StringBuilder errMsg = new StringBuilder();
281
282ArrayList<String> errList = prodaja.errorsProperty().get();
283
284for (String errList1 : errList) {
285errMsg.append(errList1);
286}
287
288Alert alert = new Alert(AlertType.ERROR);
289alert.setTitle("Prodaja se ne moze izvrsiti!");
290alert.setHeaderText(null);
291alert.setContentText(errMsg.toString());
292alert.showAndWait();
293errList.clear();
294}
295}
296}
297prodajaview
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
377prodaja 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
392sql
393
394CREATE 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 */;