From abdfd1a3b2fde03980f0a4e96835a7f9be29c310 Mon Sep 17 00:00:00 2001 From: flifloo Date: Fri, 5 Jun 2020 15:09:15 +0200 Subject: [PATCH] Add cottage list GUI --- src/DB/Request.java | 40 +++++++++++++++++++-- src/GUI/ListCottageGUI.form | 63 ++++++++++++++++++++++++++++++++ src/GUI/ListCottageGUI.java | 59 ++++++++++++++++++++++++++++++ src/GUI/LoginGUI.form | 10 +++--- src/GUI/LoginGUI.java | 40 ++++++++------------- src/Objects/Cottage.java | 72 +++++++++++++++++++++++++++++++++++++ 6 files changed, 250 insertions(+), 34 deletions(-) create mode 100644 src/GUI/ListCottageGUI.form create mode 100644 src/GUI/ListCottageGUI.java create mode 100644 src/Objects/Cottage.java diff --git a/src/DB/Request.java b/src/DB/Request.java index 1e9cae6..ca4a2f8 100644 --- a/src/DB/Request.java +++ b/src/DB/Request.java @@ -1,12 +1,16 @@ package DB; +import Objects.Cottage; import Objects.Owner; import javax.swing.*; import java.sql.*; +import java.util.ArrayList; +import java.util.List; public class Request { public static Owner getOwner(String lastName, String zip) { + Owner owner = null; Connection c = Connexion.getConnexion(); try { 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()) { ResultSet rs = ps.getResultSet(); if (rs.next()) - return new Owner(rs.getInt("IDPROPRIO"), + owner = new Owner(rs.getInt("IDPROPRIO"), rs.getString("PRENOMPROPRIO"), rs.getString("NOMPROPRIO"), rs.getString("ADPROPRIO"), rs.getString("CPPROPRIO"), rs.getString("VILLEPROPRIO")); } - return null; + ps.close(); + c.close(); } catch (SQLException e) { + System.err.println(e); JOptionPane.showMessageDialog(null, "Error when query owner !"); - return null; } + return owner; + } + + public static List getCottages(Owner owner) { + List 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; } } diff --git a/src/GUI/ListCottageGUI.form b/src/GUI/ListCottageGUI.form new file mode 100644 index 0000000..551ec54 --- /dev/null +++ b/src/GUI/ListCottageGUI.form @@ -0,0 +1,63 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/GUI/ListCottageGUI.java b/src/GUI/ListCottageGUI.java new file mode 100644 index 0000000..eb23f2f --- /dev/null +++ b/src/GUI/ListCottageGUI.java @@ -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); + } +} diff --git a/src/GUI/LoginGUI.form b/src/GUI/LoginGUI.form index 01d441d..e13399e 100644 --- a/src/GUI/LoginGUI.form +++ b/src/GUI/LoginGUI.form @@ -21,7 +21,7 @@ - + @@ -29,20 +29,20 @@ - + - + - + - + diff --git a/src/GUI/LoginGUI.java b/src/GUI/LoginGUI.java index e1eee97..a7b27e5 100644 --- a/src/GUI/LoginGUI.java +++ b/src/GUI/LoginGUI.java @@ -8,27 +8,21 @@ import java.awt.event.*; public class LoginGUI extends JDialog { private JPanel contentPane; - private JButton buttonOK; - private JButton buttonCancel; + private JButton logginButton; + private JButton cancelButton; private JTextField lastNameField; private JTextField zipField; public LoginGUI() { + setTitle("Login"); + setLocationRelativeTo(null); setContentPane(contentPane); setModal(true); - getRootPane().setDefaultButton(buttonOK); + getRootPane().setDefaultButton(logginButton); - buttonOK.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - onOK(); - } - }); + logginButton.addActionListener(actionEvent -> onLogin()); - buttonCancel.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - onCancel(); - } - }); + cancelButton.addActionListener(actionEvent -> onCancel()); // call onCancel() when cross is clicked setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); @@ -39,20 +33,17 @@ public class LoginGUI extends JDialog { }); // call onCancel() on ESCAPE - contentPane.registerKeyboardAction(new ActionListener() { - public void actionPerformed(ActionEvent e) { - onCancel(); - } - }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); + contentPane.registerKeyboardAction(actionEvent -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); + pack(); + setVisible(true); } - private void onOK() { + private void onLogin() { Owner owner = Request.getOwner(lastNameField.getText(), zipField.getText()); if (owner != null) { - System.out.println(owner); dispose(); - } - else + new ListCottageGUI(owner); + } else JOptionPane.showMessageDialog(null, "Invalid login !"); } @@ -62,9 +53,6 @@ public class LoginGUI extends JDialog { } public static void main(String[] args) { - LoginGUI dialog = new LoginGUI(); - dialog.pack(); - dialog.setVisible(true); - System.exit(0); + new LoginGUI(); } } diff --git a/src/Objects/Cottage.java b/src/Objects/Cottage.java new file mode 100644 index 0000000..dffb624 --- /dev/null +++ b/src/Objects/Cottage.java @@ -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 "N°" + id + " " + name + " " + address + ", " + zip + ", " + city + ". places: " + numberPlaces + ", bedrooms: " + numberBedrooms + ", area: " + area + ", price:" + price + ", id owner: " + owner.getId(); + } +}