1
0
Fork 0

Add cottage list GUI

This commit is contained in:
Ethanell 2020-06-05 15:09:15 +02:00
parent e8067d46a8
commit abdfd1a3b2
6 changed files with 250 additions and 34 deletions

View file

@ -1,12 +1,16 @@
package DB; package DB;
import Objects.Cottage;
import Objects.Owner; import Objects.Owner;
import javax.swing.*; import javax.swing.*;
import java.sql.*; import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class Request { public class Request {
public static Owner getOwner(String lastName, String zip) { public static Owner getOwner(String lastName, String zip) {
Owner owner = null;
Connection c = Connexion.getConnexion(); Connection c = Connexion.getConnexion();
try { try {
PreparedStatement ps = c.prepareStatement("SELECT IDPROPRIO, PRENOMPROPRIO, NOMPROPRIO, ADPROPRIO, CPPROPRIO, VILLEPROPRIO FROM PROPRIETAIRE WHERE NOMPROPRIO = ? AND CPPROPRIO = ?"); PreparedStatement ps = c.prepareStatement("SELECT IDPROPRIO, PRENOMPROPRIO, NOMPROPRIO, ADPROPRIO, CPPROPRIO, VILLEPROPRIO FROM PROPRIETAIRE WHERE NOMPROPRIO = ? AND CPPROPRIO = ?");
@ -15,17 +19,47 @@ public class Request {
if (ps.execute()) { if (ps.execute()) {
ResultSet rs = ps.getResultSet(); ResultSet rs = ps.getResultSet();
if (rs.next()) if (rs.next())
return new Owner(rs.getInt("IDPROPRIO"), owner = new Owner(rs.getInt("IDPROPRIO"),
rs.getString("PRENOMPROPRIO"), rs.getString("PRENOMPROPRIO"),
rs.getString("NOMPROPRIO"), rs.getString("NOMPROPRIO"),
rs.getString("ADPROPRIO"), rs.getString("ADPROPRIO"),
rs.getString("CPPROPRIO"), rs.getString("CPPROPRIO"),
rs.getString("VILLEPROPRIO")); rs.getString("VILLEPROPRIO"));
} }
return null; ps.close();
c.close();
} catch (SQLException e) { } catch (SQLException e) {
System.err.println(e);
JOptionPane.showMessageDialog(null, "Error when query owner !"); JOptionPane.showMessageDialog(null, "Error when query owner !");
return null;
} }
return owner;
}
public static List<Cottage> getCottages(Owner owner) {
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 = ?");
ps.setInt(1, owner.getId());
if (ps.execute()) {
for (ResultSet rs = ps.getResultSet(); rs.next(); )
cottages.add(new Cottage(rs.getInt("IDGITE"),
rs.getString("NOMGITE"),
rs.getString("ADGITE"),
rs.getString("CPGITE"),
rs.getString("VILLEGITE"),
rs.getInt("NBPLACES"),
rs.getInt("NBCHAMBRES"),
rs.getInt("SURFACE"),
rs.getInt("PRIX"),
owner));
}
ps.close();
c.close();
} catch (SQLException e) {
System.err.println(e);
JOptionPane.showMessageDialog(null, "Error when query cottages !");
}
return cottages;
} }
} }

View file

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="GUI.ListCottageGUI">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="1" 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="297"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="e3588" 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>
<grid id="9538f" layout-manager="GridLayoutManager" row-count="2" 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"/>
</constraints>
<properties/>
<border type="none"/>
<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"/>
</constraints>
<properties>
<text value="Exit"/>
</properties>
</component>
<component id="51ebf" class="javax.swing.JButton" binding="actionButton" default-binding="true">
<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>
<enabled value="false"/>
<text value="Action"/>
</properties>
</component>
</children>
</grid>
<scrollpane id="c326d">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="39c0c" class="javax.swing.JList" binding="cottageList">
<constraints/>
<properties/>
</component>
</children>
</scrollpane>
</children>
</grid>
</children>
</grid>
</form>

View file

@ -0,0 +1,59 @@
package GUI;
import DB.Request;
import Objects.Cottage;
import Objects.Owner;
import javax.swing.*;
import java.awt.event.*;
public class ListCottageGUI extends JDialog {
private JPanel contentPane;
private JButton exitButton;
private JList cottageList;
private JButton actionButton;
public ListCottageGUI(Owner owner) {
setTitle("List cottage");
setLocationRelativeTo(null);
setContentPane(contentPane);
setModal(true);
getRootPane().setDefaultButton(actionButton);
fillCottageList(owner);
actionButton.addActionListener(actionEvent -> onAction());
exitButton.addActionListener(actionEvent -> onExit());
// call onCancel() when cross is clicked
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
onExit();
}
});
// call onCancel() on ESCAPE
contentPane.registerKeyboardAction(actionEvent -> onExit(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
pack();
setVisible(true);
}
private void onAction() {
// add your code here
dispose();
}
private void onExit() {
// add your code here if necessary
dispose();
}
private void fillCottageList(Owner owner) {
DefaultListModel listModel = new DefaultListModel();
for (Cottage c: Request.getCottages(owner))
listModel.addElement(c);
cottageList.setModel(listModel);
}
}

View file

@ -21,7 +21,7 @@
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/> <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
</hspacer> </hspacer>
<grid id="9538f" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1"> <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"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <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"/> <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"/>
@ -29,20 +29,20 @@
<properties/> <properties/>
<border type="none"/> <border type="none"/>
<children> <children>
<component id="e7465" class="javax.swing.JButton" binding="buttonOK"> <component id="5723f" class="javax.swing.JButton" binding="cancelButton">
<constraints> <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="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> </constraints>
<properties> <properties>
<text value="OK"/> <text value="Cancel"/>
</properties> </properties>
</component> </component>
<component id="5723f" class="javax.swing.JButton" binding="buttonCancel"> <component id="e7465" class="javax.swing.JButton" binding="logginButton">
<constraints> <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"/> <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> </constraints>
<properties> <properties>
<text value="Cancel"/> <text value="Login"/>
</properties> </properties>
</component> </component>
</children> </children>

View file

@ -8,27 +8,21 @@ import java.awt.event.*;
public class LoginGUI extends JDialog { public class LoginGUI extends JDialog {
private JPanel contentPane; private JPanel contentPane;
private JButton buttonOK; private JButton logginButton;
private JButton buttonCancel; private JButton cancelButton;
private JTextField lastNameField; private JTextField lastNameField;
private JTextField zipField; private JTextField zipField;
public LoginGUI() { public LoginGUI() {
setTitle("Login");
setLocationRelativeTo(null);
setContentPane(contentPane); setContentPane(contentPane);
setModal(true); setModal(true);
getRootPane().setDefaultButton(buttonOK); getRootPane().setDefaultButton(logginButton);
buttonOK.addActionListener(new ActionListener() { logginButton.addActionListener(actionEvent -> onLogin());
public void actionPerformed(ActionEvent e) {
onOK();
}
});
buttonCancel.addActionListener(new ActionListener() { cancelButton.addActionListener(actionEvent -> onCancel());
public void actionPerformed(ActionEvent e) {
onCancel();
}
});
// call onCancel() when cross is clicked // call onCancel() when cross is clicked
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
@ -39,20 +33,17 @@ public class LoginGUI extends JDialog {
}); });
// call onCancel() on ESCAPE // call onCancel() on ESCAPE
contentPane.registerKeyboardAction(new ActionListener() { contentPane.registerKeyboardAction(actionEvent -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
public void actionPerformed(ActionEvent e) { pack();
onCancel(); setVisible(true);
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
} }
private void onOK() { private void onLogin() {
Owner owner = Request.getOwner(lastNameField.getText(), zipField.getText()); Owner owner = Request.getOwner(lastNameField.getText(), zipField.getText());
if (owner != null) { if (owner != null) {
System.out.println(owner);
dispose(); dispose();
} new ListCottageGUI(owner);
else } else
JOptionPane.showMessageDialog(null, "Invalid login !"); JOptionPane.showMessageDialog(null, "Invalid login !");
} }
@ -62,9 +53,6 @@ public class LoginGUI extends JDialog {
} }
public static void main(String[] args) { public static void main(String[] args) {
LoginGUI dialog = new LoginGUI(); new LoginGUI();
dialog.pack();
dialog.setVisible(true);
System.exit(0);
} }
} }

72
src/Objects/Cottage.java Normal file
View file

@ -0,0 +1,72 @@
package Objects;
public class Cottage {
private int id;
private String name;
private String address;
private String zip;
private String city;
private int numberPlaces;
private int numberBedrooms;
private int area;
private int price;
private Owner owner;
public Cottage(int id, String name, String address, String zip, String city, int numberPlaces, int numberBedrooms, int area, int price, Owner owner) {
this.id = id;
this.name = name;
this.address = address;
this.zip = zip;
this.city = city;
this.numberPlaces = numberPlaces;
this.numberBedrooms = numberBedrooms;
this.area = area;
this.price = price;
this.owner = owner;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getZip() {
return zip;
}
public String getCity() {
return city;
}
public int getNumberPlaces() {
return numberPlaces;
}
public int getNumberBedrooms() {
return numberBedrooms;
}
public int getArea() {
return area;
}
public int getPrice() {
return price;
}
public Owner getOwner() {
return owner;
}
@Override
public String toString() {
return "" + id + " " + name + " " + address + ", " + zip + ", " + city + ". places: " + numberPlaces + ", bedrooms: " + numberBedrooms + ", area: " + area + ", price:" + price + ", id owner: " + owner.getId();
}
}