1
0
Fork 0

Add add cottage action on list

This commit is contained in:
Ethanell 2020-06-09 11:52:34 +02:00
parent b2563a3ed2
commit 766dc9294c
5 changed files with 343 additions and 4 deletions

View file

@ -39,7 +39,7 @@ public class Request {
List<Cottage> cottages = new ArrayList<>();
Connection c = Connexion.getConnexion();
try {
PreparedStatement ps = c.prepareStatement("SELECT IDGITE, NOMGITE, ADGITE, CPGITE, VILLEGITE, NBPLACES, NBCHAMBRES, SURFACE, PRIX FROM GITE WHERE IDPROPRIO = ?");
PreparedStatement ps = c.prepareStatement("SELECT IDGITE, NOMGITE, ADGITE, CPGITE, VILLEGITE, NBPLACES, NBCHAMBRES, SURFACE, PRIX FROM GITE WHERE IDPROPRIO = ? ORDER BY IDGITE");
ps.setInt(1, owner.getId());
if (ps.execute()) {
for (ResultSet rs = ps.getResultSet(); rs.next(); )
@ -78,4 +78,40 @@ public class Request {
}
return false;
}
public static Cottage addCottage(String name, String address, String zip, String city, int numberPlaces, int numberBedrooms, int area, int price, Owner owner) {
Connection c = Connexion.getConnexion();
try {
Statement s = c.createStatement();
int id;
if (s.execute("SELECT max(IDGITE) FROM GITE")) {
ResultSet rs = s.getResultSet();
rs.next();
id = rs.getInt(1) + 1;
s.close();
} else
id = 1;
PreparedStatement ps = c.prepareStatement("INSERT INTO GITE (IDGITE, NOMGITE, ADGITE, CPGITE, VILLEGITE, NBPLACES, NBCHAMBRES, SURFACE, PRIX, IDPROPRIO) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
ps.setInt(1, id);
ps.setString(2, name);
ps.setString(3, address);
ps.setString(4, zip);
ps.setString(5, city);
ps.setInt(6, numberPlaces);
ps.setInt(7, numberBedrooms);
ps.setInt(8, area);
ps.setInt(9, price);
ps.setInt(10, owner.getId());
int res = ps.executeUpdate();
ps.close();
c.close();
if (res == 1)
return new Cottage(id, name, address, zip, city, numberPlaces, numberBedrooms, area, price, owner);
} catch (SQLException e) {
System.err.println(e);
JOptionPane.showMessageDialog(null, "Error when insert cottage !");
}
return null;
}
}

195
src/GUI/AddCottageGUI.form Normal file
View file

@ -0,0 +1,195 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="GUI.AddCottageGUI">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="48" y="54" width="436" height="550"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="9538f" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="e7465" class="javax.swing.JButton" binding="buttonOK">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="OK"/>
</properties>
</component>
<component id="5723f" class="javax.swing.JButton" binding="buttonCancel">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Cancel"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<grid id="e3588" layout-manager="GridLayoutManager" row-count="16" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="6ddc1" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="5032e"/>
<text value="Name"/>
</properties>
</component>
<component id="5032e" class="javax.swing.JTextField" binding="nameField">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="52a57" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="a34ad"/>
<text value="Address"/>
</properties>
</component>
<component id="a34ad" class="javax.swing.JTextField" binding="addressField">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="19798" class="javax.swing.JLabel">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="6ef75"/>
<text value="ZIP"/>
</properties>
</component>
<component id="730a5" class="javax.swing.JLabel">
<constraints>
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="f8cc"/>
<text value="City"/>
</properties>
</component>
<component id="f8cc" class="javax.swing.JTextField" binding="cityField">
<constraints>
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="660d8" class="javax.swing.JLabel">
<constraints>
<grid row="8" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="6df21"/>
<text value="Number places"/>
</properties>
</component>
<component id="2f594" class="javax.swing.JLabel">
<constraints>
<grid row="10" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="62de6"/>
<text value="Number bedrooms"/>
</properties>
</component>
<component id="91ae9" class="javax.swing.JLabel">
<constraints>
<grid row="12" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="abc9e"/>
<text value="Area"/>
</properties>
</component>
<component id="621b5" class="javax.swing.JLabel">
<constraints>
<grid row="14" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="11a39"/>
<text value="Price"/>
</properties>
</component>
<component id="6df21" class="javax.swing.JFormattedTextField" binding="numberPlacesFormattedTextField" custom-create="true">
<constraints>
<grid row="9" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="62de6" class="javax.swing.JFormattedTextField" binding="numberBedroomsFormattedTextField" custom-create="true">
<constraints>
<grid row="11" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="abc9e" class="javax.swing.JFormattedTextField" binding="areaFormattedTextField" custom-create="true">
<constraints>
<grid row="13" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="11a39" class="javax.swing.JFormattedTextField" binding="priceFormattedTextField" custom-create="true">
<constraints>
<grid row="15" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="6ef75" class="javax.swing.JFormattedTextField" binding="zipFormattedTextField" custom-create="true">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
</children>
</grid>
</children>
</grid>
</form>

View file

@ -0,0 +1,91 @@
package GUI;
import DB.Request;
import Objects.Cottage;
import Objects.Owner;
import javax.swing.*;
import javax.swing.text.NumberFormatter;
import java.awt.event.*;
import java.text.NumberFormat;
public class AddCottageGUI extends JDialog {
private JPanel contentPane;
private JButton buttonOK;
private JButton buttonCancel;
private JTextField nameField;
private JTextField addressField;
private JTextField cityField;
private JFormattedTextField numberPlacesFormattedTextField;
private JFormattedTextField numberBedroomsFormattedTextField;
private JFormattedTextField areaFormattedTextField;
private JFormattedTextField priceFormattedTextField;
private JFormattedTextField zipFormattedTextField;
private Owner owner;
private DefaultListModel defaultListModel;
public AddCottageGUI(Owner owner, ListModel model) {
this.owner = owner;
this.defaultListModel = (DefaultListModel) model;
setTitle("Add cottage");
setLocationRelativeTo(null);
setContentPane(contentPane);
setModal(true);
getRootPane().setDefaultButton(buttonOK);
buttonOK.addActionListener(actionEvent -> onOK());
buttonCancel.addActionListener(actionEvent -> onCancel());
// call onCancel() when cross is clicked
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
onCancel();
}
});
// call onCancel() on ESCAPE
contentPane.registerKeyboardAction(actionEvent -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
pack();
setVisible(true);
}
private void onOK() {
// add your code here
Cottage cottage = Request.addCottage(nameField.getText(),
addressField.getText(),
zipFormattedTextField.getText(),
cityField.getText(),
new Integer(numberPlacesFormattedTextField.getText()),
new Integer(numberBedroomsFormattedTextField.getText()),
new Integer(areaFormattedTextField.getText()),
new Integer(priceFormattedTextField.getText()),
owner);
if (cottage != null) {
defaultListModel.add(defaultListModel.size(), cottage);
dispose();
}
}
private void onCancel() {
dispose();
}
private void createUIComponents() {
NumberFormat format = NumberFormat.getIntegerInstance();
format.setGroupingUsed(false);
NumberFormatter formatter = new NumberFormatter(format);
formatter.setValueClass(Integer.class);
formatter.setMinimum(0);
formatter.setMaximum(Integer.MAX_VALUE);
formatter.setAllowsInvalid(false);
zipFormattedTextField = new JFormattedTextField(formatter);
numberPlacesFormattedTextField = new JFormattedTextField(formatter);
numberBedroomsFormattedTextField = new JFormattedTextField(formatter);
areaFormattedTextField = new JFormattedTextField(formatter);
priceFormattedTextField = new JFormattedTextField(formatter);
}
}

View file

@ -16,7 +16,7 @@
<properties/>
<border type="none"/>
<children>
<grid id="9538f" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="9538f" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@ -26,7 +26,7 @@
<children>
<component id="5723f" class="javax.swing.JButton" binding="exitButton">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Exit"/>
@ -34,13 +34,21 @@
</component>
<component id="51ebf" class="javax.swing.JButton" binding="deleteButton">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="false"/>
<text value="Delete"/>
</properties>
</component>
<component id="b6d8" class="javax.swing.JButton" binding="addButton">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Add"/>
</properties>
</component>
</children>
</grid>
<scrollpane id="c326d">

View file

@ -14,8 +14,11 @@ public class ListCottageGUI extends JDialog {
private JButton exitButton;
private JList cottageList;
private JButton deleteButton;
private JButton addButton;
private Owner owner;
public ListCottageGUI(Owner owner) {
this.owner = owner;
setTitle("List cottage");
setLocationRelativeTo(null);
setContentPane(contentPane);
@ -32,6 +35,8 @@ public class ListCottageGUI extends JDialog {
deleteButton.setEnabled(true);
});
addButton.addActionListener(actionEvent -> onAdd());
// call onCancel() when cross is clicked
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@ -54,6 +59,10 @@ public class ListCottageGUI extends JDialog {
((DefaultListModel) cottageList.getModel()).remove(cottageList.getSelectedIndex());
}
private void onAdd() {
new AddCottageGUI(owner, cottageList.getModel());
}
private void onExit() {
// add your code here if necessary
dispose();