diff --git a/src/DB/Request.java b/src/DB/Request.java index 3868027..21b5606 100644 --- a/src/DB/Request.java +++ b/src/DB/Request.java @@ -78,4 +78,37 @@ 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(); + s.execute("SELECT IDGITE FROM GITE ORDER BY IDGITE DESC"); + ResultSet rs = s.getResultSet(); + rs.next(); + int id = rs.getInt(1)+1; + s.close(); + + 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 @@