Registration of internet package sales
It's a simple, MVC, JavaFX, single scene app.
Actually it's a form for registering the sale of an internet package with a ListView of previous purchases.
The internet package consists of few parameters : speed, flow, duration of the contract, id, name and surname of the user and user address.
The functionalities are: review all sales, adding new sales and deletion of existing sales.
jfxas1.Main.java
model.ProdajaModel.java
controller.ProdajaController.java
view.ProdajaView.fxml
view.ProdajaStil.css
internet_paketiDB.sql
Actually it's a form for registering the sale of an internet package with a ListView of previous purchases.
The internet package consists of few parameters : speed, flow, duration of the contract, id, name and surname of the user and user address.
The functionalities are: review all sales, adding new sales and deletion of existing sales.
jfxas1.Main.java
1: /*
2: * JavaFX - Registrovanje prodaje internet-paketa
3: */
4: package jfxas1;
5:
6: import java.net.URL;
7: import javafx.application.Application;
8: import javafx.fxml.FXMLLoader;
9: import javafx.scene.Scene;
10: import javafx.scene.layout.BorderPane;
11: import javafx.stage.Stage;
12:
13: /**
14: *
15: * @author djordje gavrilovic
16: */
17: public class Main extends Application{
18:
19: public static void main(String[] args) {
20: launch(args);
21: }
22:
23: @Override
24: public void start(Stage primaryStage) throws Exception {
25:
26: URL fxmlUrl = getClass().getClassLoader().getResource("view/ProdajaView.fxml");
27: BorderPane root = FXMLLoader.<BorderPane>load(fxmlUrl);
28: Scene scene = new Scene(root);
29: primaryStage.setScene(scene);
30: primaryStage.setTitle("Registrovanje prodaje internet-paketa");
31: primaryStage.centerOnScreen();
32: primaryStage.setResizable(false);
33: primaryStage.show();
34:
35: }
36:
37: }
38:
model.ProdajaModel.java
1: package model;
2:
3: import java.sql.Connection;
4: import java.sql.DriverManager;
5: import java.sql.PreparedStatement;
6: import java.sql.SQLException;
7: import java.util.ArrayList;
8: import java.util.List;
9: import javafx.beans.property.IntegerProperty;
10: import javafx.beans.property.ObjectProperty;
11: import javafx.beans.property.SimpleIntegerProperty;
12: import javafx.beans.property.SimpleObjectProperty;
13: import javafx.beans.property.SimpleStringProperty;
14: import javafx.beans.property.StringProperty;
15:
16:
17: public class ProdajaModel {
18:
19: List<ProdajaModel> listaProdaja;
20:
21: private final IntegerProperty brzina = new SimpleIntegerProperty(this,"brzina");
22: private final ObjectProperty protok = new SimpleObjectProperty(this,"protok");
23: private final IntegerProperty ugovor = new SimpleIntegerProperty(this,"ugovor");
24: private final IntegerProperty id = new SimpleIntegerProperty(this,"id");
25: private final StringProperty imePrezime = new SimpleStringProperty(this,"imePrezime");
26: private final StringProperty adresa = new SimpleStringProperty(this,"adresa");
27: private final ObjectProperty<ArrayList<String>> errorList = new SimpleObjectProperty<>(this, "errorList", new ArrayList<>());
28:
29: public ProdajaModel() {}
30:
31: public ProdajaModel(int brzina,Object protok,int ugovor,int id,String imePrezime,String adresa) {
32: this.brzina.set(brzina);
33: this.protok.set(protok);
34: this.ugovor.set(ugovor);
35: this.id.set(id);
36: this.imePrezime.set(imePrezime);
37: this.adresa.set(adresa);
38: }
39:
40: public int getBrzina() {return brzina.get();}
41: public void setBrzina(int brzina) {this.brzina.set(brzina);}
42: public IntegerProperty brzinaProperty() {return brzina;}
43:
44: public Object getProtok() {return protok.get();}
45: public void setProtok(Object protok) {this.protok.set(protok);}
46: public ObjectProperty protokProperty() {return protok;}
47:
48: public int getUgovor() {return ugovor.get();}
49: public void setUgovor(int ugovor) {this.ugovor.set(ugovor);}
50: public IntegerProperty ugovorProperty() {return ugovor;}
51:
52: public int getId() {return id.get();}
53: public void setId(int id) {this.id.set(id);}
54: public IntegerProperty idProperty() {return id;}
55:
56: public String getImePrezime() {return imePrezime.get();}
57: public void setImePrezime(String imePrezime) {this.imePrezime.set(imePrezime);}
58: public StringProperty imePrezimeProperty() {return imePrezime;}
59:
60: public String getAdresa() {return adresa.get();}
61: public void setAdresa(String adresa) {this.adresa.set(adresa);}
62: public StringProperty adresaProperty() {return adresa;}
63:
64: public ObjectProperty<ArrayList<String>> errorsProperty() {return errorList;}
65:
66: public boolean isValid() {
67: boolean isValid = true;
68:
69: if (imePrezime.get() == null) {
70: errorList.getValue().add(" Ime i prezime moraju biti uneti!");
71: isValid = false;
72: }
73: if (adresa.get() == null) {
74: errorList.getValue().add(" Adresa mora biti uneta!");
75: isValid = false;
76: }
77: if (protok.get() == null) {
78: errorList.getValue().add(" Protok mora biti odabran!");
79: isValid = false;
80: }
81: if (ugovor.get() == 0) {
82: errorList.getValue().add(" Trajanje ugovora mora biti odredjeno!");
83: isValid = false;
84: }
85: if (brzina.get() == 0) {
86: errorList.getValue().add(" Brzina protoka mora biti odabrana!");
87: isValid = false;
88: }
89: return isValid;
90: }
91:
92: public String save() throws ClassNotFoundException {
93:
94: String poruka = "Unos uspesno obavljen";
95:
96: if (isValid()) {
97: Class.forName("com.mysql.jdbc.Driver");
98: try( Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/internet_paketi","root","");) {
99: PreparedStatement st = conn.prepareStatement("insert into prodaja (prodajaId, imePrezime,adresa,ugovor,brzina,protok) values (?,?,?,?,?,?)");
100: st.setString(1, Integer.toString(id.get()));
101: st.setString(2, imePrezime.get());
102: st.setString(3, adresa.get());
103: st.setString(4, Integer.toString(ugovor.get()));
104: st.setString(5, Integer.toString(brzina.get()));
105: st.setString(6, protok.get().toString());
106: st.execute();
107:
108: } catch (SQLException e) {
109: poruka = "GRESKA! " + e.getMessage();
110: }
111: }
112: return poruka;
113: }
114:
115: @Override
116: public String toString() {
117: return
118: "ID: " + id.get() + "\n" +
119: "Ime i prezime: " + imePrezime.get() + "\n" +
120: "Adresa: " + adresa.get() + "\n" +
121: "Ugovor: " + ugovor.get() + " god." + "\n" +
122: "Brzina: " + brzina.get() + " Mbit" +"\n" +
123: "Protok: " + protok.get() + " GB";
124: }
125:
126: }
127:
controller.ProdajaController.java
1: package controller;
2:
3: import java.sql.Connection;
4: import java.sql.DriverManager;
5: import java.sql.PreparedStatement;
6: import java.sql.ResultSet;
7: import java.sql.SQLException;
8: import java.sql.Statement;
9: import java.util.ArrayList;
10: import javafx.collections.FXCollections;
11: import javafx.collections.ObservableList;
12: import javafx.fxml.FXML;
13: import javafx.scene.control.Alert;
14: import javafx.scene.control.Alert.AlertType;
15: import javafx.scene.control.ChoiceBox;
16: import javafx.scene.control.ListView;
17: import javafx.scene.control.TextField;
18: import model.ProdajaModel;
19:
20: public class ProdajaController {
21:
22: ProdajaModel prodaja;
23:
24: public ProdajaController() {
25: }
26:
27: @FXML
28: private ChoiceBox cbBrzina;
29: @FXML
30: private ChoiceBox cbProtok;
31: @FXML
32: private ChoiceBox cbUgovor;
33: @FXML
34: private TextField tfImePrezime;
35: @FXML
36: private TextField tfAdresa;
37: @FXML
38: private ListView listView;
39:
40: ObservableList<ProdajaModel> prodajaLista = FXCollections.observableArrayList();
41:
42: private ObservableList<ProdajaModel> prikaz() throws ClassNotFoundException {
43: Class.forName("com.mysql.jdbc.Driver");
44: ObservableList<ProdajaModel> prodajaLista1 = FXCollections.observableArrayList();
45: try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/internet_paketi", "root", "");) {
46: Statement st = conn.createStatement();
47: st.executeQuery("select * from prodaja");
48: ResultSet rs = st.getResultSet();
49:
50: while (rs.next()) {
51: ProdajaModel prodaja = new ProdajaModel(
52: Integer.parseInt(rs.getString("brzina")),
53: rs.getString("protok"),
54: Integer.parseInt(rs.getString("ugovor")),
55: Integer.parseInt(rs.getString("prodajaId")),
56: rs.getString("imePrezime"),
57: rs.getString("adresa"));
58: prodajaLista1.add(prodaja);
59: prodajaLista = prodajaLista1;
60: }
61:
62: } catch (SQLException e) {
63: System.out.println("GRESKA: " + e.getMessage());
64: }
65:
66: return prodajaLista1;
67: }
68:
69: @FXML
70: private void initialize() throws ClassNotFoundException {
71: prodaja = new ProdajaModel();
72:
73: cbBrzina.getItems().addAll(2, 5, 10, 20, 50, 100);
74: cbBrzina.valueProperty().bindBidirectional(prodaja.brzinaProperty());
75: cbProtok.getItems().addAll(1, 5, 10, 100, "Flat");
76: cbProtok.valueProperty().bindBidirectional(prodaja.protokProperty());
77: cbUgovor.getItems().addAll(1, 2);
78: cbUgovor.valueProperty().bindBidirectional(prodaja.ugovorProperty());
79: tfImePrezime.textProperty().bindBidirectional(prodaja.imePrezimeProperty());
80: tfAdresa.textProperty().bindBidirectional(prodaja.adresaProperty());
81: prikaz();
82: listView.setItems(prodajaLista);
83:
84: }
85:
86: @FXML
87: private void obrisiProdaju() throws ClassNotFoundException {
88: Class.forName("com.mysql.jdbc.Driver");
89:
90: int selIdx = listView.getSelectionModel().getSelectedIndex();
91: String modId = null;
92:
93: for (ProdajaModel mod : prodajaLista) {
94: if (selIdx == prodajaLista.indexOf(mod)) {
95: modId = Integer.toString(mod.getId());
96: }
97: }
98:
99: try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/internet_paketi", "root", "");) {
100: PreparedStatement st = conn.prepareStatement("delete from prodaja where prodajaId=?");
101: st.setString(1, modId);
102: st.execute();
103: listView.setItems(prikaz());
104:
105: } catch (SQLException e) {
106: System.out.println("GRESKA! " + e.getMessage());
107: }
108:
109: }
110:
111: @FXML
112: private void prodaj() throws ClassNotFoundException {
113:
114: if (prodaja.isValid()) {
115:
116: prodaja.setId(prodajaLista.size() + 1);
117: prodaja.save();
118: listView.setItems(prikaz());
119:
120: } else {
121: StringBuilder errMsg = new StringBuilder();
122:
123: ArrayList<String> errList = prodaja.errorsProperty().get();
124:
125: for (String errList1 : errList) {
126: errMsg.append(errList1);
127: }
128:
129: Alert alert = new Alert(AlertType.ERROR);
130: alert.setTitle("Prodaja se ne moze izvrsiti!");
131: alert.setHeaderText(null);
132: alert.setContentText(errMsg.toString());
133: alert.showAndWait();
134: errList.clear();
135:
136: }
137: }
138:
139: }
140:
view.ProdajaView.fxml
1: <?xml version="1.0" encoding="UTF-8"?>
2:
3: <?import java.lang.*?>
4: <?import java.net.*?>
5: <?import java.util.*?>
6: <?import javafx.scene.*?>
7: <?import javafx.scene.control.*?>
8: <?import javafx.scene.layout.*?>
9: <?import javafx.geometry.Insets?>
10:
11: <BorderPane id="bp1" prefHeight="500.0" prefWidth="500.0" styleClass="mainFxmlClass"
12: xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.ProdajaController">
13: <stylesheets>
14: <URL value="@ProdajaStil.css"/>
15: </stylesheets>
16: <top>
17: <HBox fx:id="hbTit">
18: <Label fx:id="lbl1" text="Registrovanje prodaje:" />
19: </HBox>
20: </top>
21: <left>
22: <VBox fx:id="vbox1" spacing="20">
23: <BorderPane.margin>
24: <Insets top="25.0" />
25: </BorderPane.margin>
26: <children>
27: <HBox spacing="10" alignment="BASELINE_LEFT">
28: <children>
29: <Label fx:id="lbl2" text="Brzina protoka: " />
30: <ChoiceBox fx:id="cbBrzina" value="2" />
31: <Label text=" Mbit"/>
32: </children>
33: </HBox>
34: <HBox spacing="10" alignment="BASELINE_LEFT">
35: <children>
36: <Label fx:id="lbl3" text="Protok: " />
37: <ChoiceBox fx:id="cbProtok" />
38: <Label text=" GB" />
39: </children>
40: </HBox>
41: <HBox spacing="10" alignment="BASELINE_LEFT">
42: <children>
43: <Label fx:id="lbl4" text="Trajanje ugovora: " />
44: <ChoiceBox fx:id="cbUgovor"/>
45: <Label text=" god." />
46: </children>
47: </HBox>
48: <HBox spacing="10" alignment="BASELINE_LEFT">
49: <children>
50: <Label fx:id="lbl5" text="Ime i prezime korisnika: " />
51: <TextField fx:id="tfImePrezime"/>
52: </children>
53: </HBox>
54: <HBox spacing="10" alignment="BASELINE_LEFT">
55: <children>
56: <Label fx:id="lbl6" text="Adresa korisnika: " />
57: <TextField fx:id="tfAdresa"/>
58: </children>
59: </HBox>
60: <Button fx:id="prodajaBtn" text="Prodaj" AnchorPane.leftAnchor="0.0" onAction="#prodaj"/>
61: <Label /> <Label /> <Label />
62: <AnchorPane fx:id="apDel">
63: <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">
64: <Button fx:id="delBtn" text="Obrisi selektovanu prodaju" onAction="#obrisiProdaju"/>
65: </HBox>
66: </AnchorPane>
67: </children>
68: </VBox>
69: </left>
70: <center>
71: <AnchorPane>
72: <BorderPane.margin>
73: <Insets bottom="0.0" left="25.0" top="25.0" right="0.0"/>
74: </BorderPane.margin>
75: <ListView fx:id="listView" AnchorPane.bottomAnchor="0.0" AnchorPane.topAnchor="0.0" />
76: </AnchorPane>
77: </center>
78: </BorderPane>
79:
view.ProdajaStil.css
1: .root {
2: -fx-padding: 20;
3: -fx-background-color: #c5d5cb;
4: }
5:
6: #apDel {
7: -fx-Background-color: #9fa8a3;
8: }
9:
10: #hbTit .label {
11: -fx-text-fill: white;
12: }
13:
14:
internet_paketiDB.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,'Suzana Pavlovic','Zrenjanin',2,100,'Flat'),(2,'Djordje Gavrilovic','Bajina Basta',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 */;
-- Dump completed on 2017-08-16 15:35:53
Hello. This code is just what i needed for my school project. There is only one issue. when i compile the code i get no errors, but when i run it i get the following error:
ReplyDeleteException in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException:
/C:/Users/Sorin/Desktop/Online%20Shop/InternetServiceRegistration/build/classes/view/ProdajaView.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2543)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at jfxas1.Main.start(Main.java:20)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,8]
Message: The processing instruction target matching "[xX][mM][lL]" is not allowed.
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:604)
at javax.xml.stream.util.StreamReaderDelegate.next(StreamReaderDelegate.java:88)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2513)
... 17 more
Exception running application jfxas1.Main
Java Result: 1
Do you know what could be the issue?