diff --git a/src/DB/Request.java b/src/DB/Request.java index 3868027..82e4cf2 100644 --- a/src/DB/Request.java +++ b/src/DB/Request.java @@ -39,7 +39,7 @@ public class Request { 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 = ?"); + 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; + } } diff --git a/src/GUI/AddCottageGUI.form b/src/GUI/AddCottageGUI.form new file mode 100644 index 0000000..805881d --- /dev/null +++ b/src/GUI/AddCottageGUI.form @@ -0,0 +1,195 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/GUI/AddCottageGUI.java b/src/GUI/AddCottageGUI.java new file mode 100644 index 0000000..88d9a9b --- /dev/null +++ b/src/GUI/AddCottageGUI.java @@ -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); + } +} diff --git a/src/GUI/ListCottageGUI.form b/src/GUI/ListCottageGUI.form index 311515c..c396571 100644 --- a/src/GUI/ListCottageGUI.form +++ b/src/GUI/ListCottageGUI.form @@ -16,7 +16,7 @@ - + @@ -26,7 +26,7 @@ - + @@ -34,13 +34,21 @@ - + + + + + + + + + diff --git a/src/GUI/ListCottageGUI.java b/src/GUI/ListCottageGUI.java index 99ade70..6662969 100644 --- a/src/GUI/ListCottageGUI.java +++ b/src/GUI/ListCottageGUI.java @@ -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();