diff --git a/ModelER_store.pdf b/ModelER_store.pdf new file mode 100644 index 0000000..74bffd0 Binary files /dev/null and b/ModelER_store.pdf differ diff --git a/StoreOlist/.classpath b/StoreOlist/.classpath new file mode 100644 index 0000000..ca0b262 --- /dev/null +++ b/StoreOlist/.classpath @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/StoreOlist/.project b/StoreOlist/.project new file mode 100644 index 0000000..382963d --- /dev/null +++ b/StoreOlist/.project @@ -0,0 +1,17 @@ + + + StoreOlist + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/StoreOlist/.settings/org.eclipse.jdt.core.prefs b/StoreOlist/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..f2525a8 --- /dev/null +++ b/StoreOlist/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=11 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=11 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=11 diff --git a/StoreOlist/ConnectBD.java b/StoreOlist/ConnectBD.java new file mode 100644 index 0000000..9dbc5d6 --- /dev/null +++ b/StoreOlist/ConnectBD.java @@ -0,0 +1,46 @@ +package br.com.olist.store; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + + + +public class ConnectBD { + + public Connection getConnect() { + + //System.out.println("Teste ao acesso a uma BD MySQL\n"); + Connection con = null; + try { + //Indica que será utilizado o driver Connector/J + Class.forName("com.mysql.cj.jdbc.Driver"); + + /* Liga ao servidor de BD local, com o utilizador "root" + e sem password, acedendo à BD "bd1" */ + con = DriverManager.getConnection("jdbc:mysql://localhost:3306/store?useSSL=false", "ana", "123"); + //System.out.println("Ligação efectuada com sucesso\n"); + + } + //Trata a excepção lançada pelo método forName da classe Class + catch(ClassNotFoundException cnfe) { + System.out.println("ClassNotFoundException"); + } + //Trata a excepção lançada pelo método getConnection da classe DriverManager + catch(SQLException sqle) { + System.out.println("SQLException"); + } + return con; + } + + public void closeConnection (Connection con) { + + try { + con.close(); + //System.out.println("\nLigação fechada com sucesso\n"); + } + catch(SQLException sqle) { + System.out.println("SQLException"); + } + } +} \ No newline at end of file diff --git a/StoreOlist/Libs/mysql-connector-java-8.0.21.jar b/StoreOlist/Libs/mysql-connector-java-8.0.21.jar new file mode 100644 index 0000000..51e270c Binary files /dev/null and b/StoreOlist/Libs/mysql-connector-java-8.0.21.jar differ diff --git a/StoreOlist/MainScreen.java b/StoreOlist/MainScreen.java new file mode 100644 index 0000000..dd168bb --- /dev/null +++ b/StoreOlist/MainScreen.java @@ -0,0 +1,102 @@ +package br.com.olist.store; + +import javax.swing.JFrame; +import javax.swing.JMenuBar; +import javax.swing.JMenu; +import javax.swing.JMenuItem; +import java.awt.FlowLayout; +import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseEvent; + +public class MainScreen extends JFrame { + + /** + * + */ + private static final long serialVersionUID = 1L; + JMenuBar barMenu = new JMenuBar(); + JMenu product = new JMenu("Inserir/Alterar"); + JMenu search = new JMenu ("Pesquisar"); + JMenuItem prod = new JMenuItem("Criar novo produto"); + JMenuItem resarch= new JMenuItem("Ler dados do produto"); + JMenuItem upgread = new JMenuItem("Atualizar dados do produto"); + JMenuItem del = new JMenuItem("Excluir os dados do produto"); + JMenu exit = new JMenu("Sair"); + + NewProduct newProduct; +// + + public MainScreen() { + setTitle("Store Products & Categories"); + setSize(550,590); + setLocation(50,50); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + setLayout(new FlowLayout()); + setJMenuBar(barMenu); + product.add(prod); + + product.add(upgread); + product.add(del); + search.add(resarch); + barMenu.add(product); + barMenu.add(search); + + barMenu.add(exit); + + + + prod.addActionListener(new EventoJMenuItem()); + resarch.addActionListener(new EventoJMenuItem()); + upgread.addActionListener(new EventoJMenuItem()); + del.addActionListener(new EventoJMenuItem()); + + exit.addMouseListener(new EventoJMenuSair()); + } + + public static void main(String[] args) { + new MainScreen(); + } + + private class EventoJMenuItem implements ActionListener { + + public void actionPerformed(ActionEvent ev) { + if (ev.getSource() == prod) { + newProduct = new NewProduct(); + newProduct.setVisible(true); + } + else if (ev.getSource() == resarch) { +// novoCliente = new NovoCliente(); +// novoCliente.setVisible(true); + } + else if (ev.getSource() == upgread) { +// novoAluguer = new NovoAluguer(); +// novoAluguer.setVisible(true); + } + else if (ev.getSource() == del) { +// novoTipoCarro = new NovoTipoCarro(); +// novoTipoCarro.setVisible(true); + } + + } + } + + private class EventoJMenuSair implements MouseListener { + + public void mouseClicked(MouseEvent ev) { + System.exit(0); + } + + public void mouseEntered (MouseEvent ev) {} + + public void mouseExited(MouseEvent ev) {} + + public void mouseReleased(MouseEvent ev) {} + + public void mousePressed(MouseEvent ev) {} + } + } + + diff --git a/StoreOlist/NewProduct.java b/StoreOlist/NewProduct.java new file mode 100644 index 0000000..826c7ce --- /dev/null +++ b/StoreOlist/NewProduct.java @@ -0,0 +1,236 @@ +package br.com.olist.store; +import java.awt.Dimension; +import java.awt.FlowLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.math.BigDecimal; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTable; +import javax.swing.JTextField; +import javax.swing.table.DefaultTableModel; + + + + + +public class NewProduct extends JFrame { + + private static final long serialVersionUID = 1L; + //Declara e cria os componentes + JPanel jpPanel1 = new JPanel(); + JLabel jlName = new JLabel ("Nome"); + JTextField jtfName = new JTextField(40); + JLabel jlDescription = new JLabel ("Descrição"); + JTextField jtfDescription = new JTextField(40); + JLabel jlValue = new JLabel ("Valor"); + JTextField jtfValue = new JTextField(40); + JLabel jlCategory = new JLabel ("Categorias"); + JComboBox jcbCategory = new JComboBox(); + JButton jbAdd = new JButton("Adicionar"); + DefaultTableModel tmCategory = new DefaultTableModel (null, new String[]{"Código", "Categoria"}); + JTable jtCategory = new JTable(tmCategory); + JScrollPane jspCategory = new JScrollPane(jtCategory); + JButton jbSave = new JButton("Salvar"); + JButton jbClean= new JButton("Limpar"); + JLabel jlEmpty1 = new JLabel(""); + JLabel jlEmpty2 = new JLabel(""); + + + + //Construtor + + public NewProduct() { + + //Define as porpriedades da janela + setTitle("Novo Produto"); + setSize(610,350); + setLocation(100,100); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + setLayout(new FlowLayout()); + setVisible(true); + + //Define o tamanho dos rótulos + jlName.setPreferredSize(new Dimension(75,20)); + jlDescription.setPreferredSize(new Dimension(75,20)); + jlValue.setPreferredSize(new Dimension(75,20)); + jlCategory.setPreferredSize(new Dimension(80,20)); + jlEmpty1.setPreferredSize(new Dimension(600,20)); + jlEmpty2.setPreferredSize(new Dimension(600,10)); + + jcbCategory.setPreferredSize(new Dimension(325,20)); + jbAdd.setPreferredSize(new Dimension(110,20)); + jspCategory.setPreferredSize(new Dimension(500,100)); + + + + jpPanel1.setPreferredSize(new Dimension(600,340)); + + + + jpPanel1.add(jlEmpty1); + jpPanel1.add(jlName); + jpPanel1.add(jtfName); + jpPanel1.add(jlDescription); + jpPanel1.add(jtfDescription); + jpPanel1.add(jlValue); + jpPanel1.add(jtfValue); + jpPanel1.add(jlCategory); + jpPanel1.add(jcbCategory); + jpPanel1.add(jlCategory); + jpPanel1.add(jbAdd); + jpPanel1.add(jspCategory); + jpPanel1.add(jlEmpty2); + jpPanel1.add(jbSave); + jpPanel1.add(jbClean); + + + add(jpPanel1); + + + String sqlTypeProduct = "SELECT * FROM category"; + try { + ConnectBD connectBD = new ConnectBD(); + Connection con = connectBD.getConnect(); + Statement stmt = con.createStatement(); + ResultSet rs = stmt.executeQuery(sqlTypeProduct); + while (rs.next()) + jcbCategory.addItem(rs.getString("name_category")); + connectBD.closeConnection(con); + } + catch(SQLException sqle) { + System.out.println("Não foi possível efetuar a operação sobre a BD!"); + sqle.printStackTrace(); + } + + + + jbSave.addActionListener(new EventJBSave()); + jbAdd.addActionListener(new EventJBAdd()); + jbClean.addActionListener(new EventJBClear()); + + } + + + public static void main(String[] args) { + new NewProduct(); + } + + //Classe interna que contém o código que é executado quando se pressiona o botão jbAdicionar + private class EventJBAdd implements ActionListener { + + public void actionPerformed(ActionEvent ev) { + + String sqlPesquisaCategory = "SELECT id_category FROM category WHERE name_category LIKE ?"; + try { + PreparedStatement pstmt; + ConnectBD connectBD = new ConnectBD(); + Connection con = connectBD.getConnect(); + + pstmt = con.prepareStatement(sqlPesquisaCategory); + pstmt.setString(1, String.valueOf(jcbCategory.getSelectedItem())); + ResultSet rs = pstmt.executeQuery(); + String[] campos = new String[] {null, null}; + if (rs.next()) { + tmCategory.addRow(campos); + tmCategory.setValueAt(jcbCategory.getSelectedItem(),tmCategory.getRowCount()-1,1); + tmCategory.setValueAt(rs.getInt("id_category"),tmCategory.getRowCount()-1,0); + + } + connectBD.closeConnection(con); + } + catch(SQLException sqle) { + System.out.println("Não foi possível efetuar a operação sobre a BD!"); + sqle.printStackTrace(); + } + } + } + + + //Classe interna que contém o código que é executado quando se pressiona o botão jbSave + private class EventJBSave implements ActionListener { + + public void actionPerformed(ActionEvent ev) { + + if (jtfName.getText().equals("") || jtfDescription.getText().equals("") || jtfValue.getText().equals("")) + JOptionPane.showMessageDialog(null,"Todos os campos são de preenchimento obrigatório!"); + else { + try { + PreparedStatement pstmt1, pstmt2; + ConnectBD connectBD = new ConnectBD(); + Connection con = connectBD.getConnect(); + String sqlNewProduct = "INSERT INTO product VALUES(null,?,?,?)"; + pstmt1 = con.prepareStatement(sqlNewProduct); + pstmt1.setString(1, jtfName.getText()); + pstmt1.setString(2, jtfDescription.getText()); + pstmt1.setBigDecimal(3, new BigDecimal(jtfValue.getText())); + pstmt1.executeUpdate(); + + String sqlID_product= "SELECT id_product AS codP FROM product WHERE id_product = (last_insert_id())"; + Statement stmt = con.createStatement(); + ResultSet rs = stmt.executeQuery(sqlID_product); + int cod_prod = 0; + while (rs.next()) + cod_prod = rs.getInt("codP"); + + + String sqlSelctCategory = "INSERT INTO category_product VALUES(null,?,?)"; + pstmt2 = con.prepareStatement(sqlSelctCategory); + + for (int i = 0; i < tmCategory.getRowCount(); i++) { + pstmt2.setInt(1,Integer.parseInt(String.valueOf(tmCategory.getValueAt(i,0)))); + pstmt2.setInt(2, cod_prod); + pstmt2.executeUpdate(); + } + + + connectBD.closeConnection(con); + JOptionPane.showMessageDialog(null,"Os dados foram salvos com sucesso!"); + + cleanField(); + clearTableRows(); + } + catch(SQLException sqle) { + System.out.println("Não foi possível efetuar a operação sobre a BD!"); + sqle.printStackTrace(); + } + } + } + } + + //Classe interna que contém o código que é executado quando se pressiona o botão jbLimpar + private class EventJBClear implements ActionListener { + + public void actionPerformed(ActionEvent ev) { + cleanField(); + clearTableRows() ; + } + } + + + private void cleanField() { + jtfName.setText(""); + jtfDescription.setText(""); + jtfValue.setText(""); + } + + private void clearTableRows() { + while (tmCategory.getRowCount() > 0) + tmCategory.removeRow(0); + } +} + + + + + diff --git a/StoreOlist/NewSearch.java b/StoreOlist/NewSearch.java new file mode 100644 index 0000000..dbd0115 --- /dev/null +++ b/StoreOlist/NewSearch.java @@ -0,0 +1,196 @@ +package br.com.olist.store; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JTextField; +import javax.swing.JButton; +import javax.swing.JComboBox; + +import java.awt.FlowLayout; +import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; +import java.awt.event.MouseEvent; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.sql.Statement; +import java.sql.ResultSet; +import java.awt.Dimension; +import javax.swing.JOptionPane; +import javax.swing.table.DefaultTableModel; +import javax.swing.JTable; +import javax.swing.JScrollPane; + +public class NewSearch extends JFrame { + + + private static final long serialVersionUID = 1L; + + JLabel jlName = new JLabel ("Nome"); + JTextField jtfName = new JTextField(35); + JLabel jlDescription = new JLabel ("Descrição"); + JTextField jtfDescription = new JTextField(35); + JLabel jlValue = new JLabel ("Valor"); + JTextField jtfValue = new JTextField(35); + JButton jbSearch = new JButton("Pesquisar"); + DefaultTableModel tmAnswer = new DefaultTableModel (null, new String[]{"Produto", "Descrição", "Valor", "Categoria"}); + JTable jtAnswer = new JTable(tmAnswer); + JScrollPane jspAnswer= new JScrollPane(jtAnswer); + JLabel jlCategory = new JLabel ("Categorias"); + JComboBox jcbCategory = new JComboBox(); + + + + + + //Construtor + public NewSearch() { + + //Define as porpriedades da janela + setTitle("Pesquisa de Produtos"); + setSize(500,410); + setLocation(100,100); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + setLayout(new FlowLayout()); + setVisible(true); + + //Define o tamanho dos componentes + jlName.setPreferredSize(new Dimension(75,20)); + jlDescription.setPreferredSize(new Dimension(75,20)); + jlValue.setPreferredSize(new Dimension(75,20)); + jspAnswer.setPreferredSize(new Dimension(460,100)); + jlCategory.setPreferredSize(new Dimension(80,20)); + jcbCategory.setPreferredSize(new Dimension(325,20)); + + + + + + //Adiciona os componentes à janela + + add(jlName); + add(jtfName); + add(jlDescription); + add(jtfDescription); + add(jlValue); + add(jtfValue); + add(jlCategory); + add(jcbCategory); + add(jbSearch); + add(jtAnswer); + add(jspAnswer); + + String sqlTypeProduct = "SELECT * FROM category"; + try { + ConnectBD connectBD = new ConnectBD(); + Connection con = connectBD.getConnect(); + Statement stmt = con.createStatement(); + ResultSet rs = stmt.executeQuery(sqlTypeProduct); + while (rs.next()) + jcbCategory.addItem(rs.getString("name_category")); + connectBD.closeConnection(con); + } + catch(SQLException sqle) { + System.out.println("Não foi possível efetuar a operação sobre a BD!"); + sqle.printStackTrace(); + } + + + + + /*Registo do listener ActionListener e do listener MouseListener + junto dos botões. Quando for gerado um evento por estes componentes, é + criada uma instância da classe EventoJBGPesquisar ou EventoJTPesquisar, + onde está o código que deve ser executado quando tal acontece*/ + jbSearch.addActionListener(new EventoJBSearch()); + + + //Limpa os dados que possam ter ficado desde a última utilização da janela + //Os métodos invocados estão implementados em baixo + + clearTableRows(); + } + + + + public static void main(String[] args) { + new NewSearch(); + } + + //Classe interna que contém o código que é executado quando se pressiona o botão jbPesquisar + private class EventoJBSearch implements ActionListener { + + public void actionPerformed(ActionEvent ev) { + + + clearTableRows(); + + boolean findProduct = false; + + if (jtfName.getText().equals("") && jtfDescription.getText().equals("") && jtfValue.getText().equals("")) + JOptionPane.showMessageDialog(null, "Preencha um ou mais campos para realiza a pesquisa!"); + else { + try { + PreparedStatement pstmt; + ResultSet rs; + String sqlSearch1 = "SELECT * FROM product WHERE name_product LIKE ? " + + "OR SELECT * FOR product WHERE description_product LIKE ? " + + "OR SELECT * FOR product WHERE value_product LIKE ?"; + ConnectBD connectBD = new ConnectBD(); + Connection con = connectBD.getConnect(); + pstmt = con.prepareStatement(sqlSearch1); + pstmt.setString(1, '%' + jtfName.getText() + '%'); + rs = pstmt.executeQuery(); + int i=0; + String[] campos = new String[] {null, null, null, null}; + while (rs.next()) { + findProduct = true; + tmAnswer.addRow(campos); + tmAnswer.setValueAt(rs.getString("name_product"), i, 0); + tmAnswer.setValueAt(rs.getString("description_product"), i, 1); + tmAnswer.setValueAt(rs.getInt("value_product"), i, 2); + + + i++; + } + if (findProduct == false) { + JOptionPane.showMessageDialog(null, "Não foi encontrado nenhum cliente com esse nome!"); + cleanField(); + } + connectBD.closeConnection(con); + + } + catch(SQLException sqle) { + System.out.println("Não foi possível efetuar a operação sobre a BD!"); + sqle.printStackTrace(); + } + } + } + } + + + + + + public void mousePressed(MouseEvent ev) {} + + public void mouseReleased(MouseEvent ev) {} + + public void mouseEntered(MouseEvent ev) {} + + public void mouseExited(MouseEvent ev) {} + + + private void cleanField() { + jtfName.setText(""); + jtfDescription.setText(""); + jtfValue.setText(""); + } + + private void clearTableRows() { + while (tmAnswer.getRowCount() > 0) + tmAnswer.removeRow(0); + } +} + + + diff --git a/StoreOlist/bin/br/com/olist/store/ConnectBD.class b/StoreOlist/bin/br/com/olist/store/ConnectBD.class new file mode 100644 index 0000000..1dfd2d4 Binary files /dev/null and b/StoreOlist/bin/br/com/olist/store/ConnectBD.class differ diff --git a/StoreOlist/bin/br/com/olist/store/MainScreen$EventoJMenuItem.class b/StoreOlist/bin/br/com/olist/store/MainScreen$EventoJMenuItem.class new file mode 100644 index 0000000..d640e0a Binary files /dev/null and b/StoreOlist/bin/br/com/olist/store/MainScreen$EventoJMenuItem.class differ diff --git a/StoreOlist/bin/br/com/olist/store/MainScreen$EventoJMenuSair.class b/StoreOlist/bin/br/com/olist/store/MainScreen$EventoJMenuSair.class new file mode 100644 index 0000000..b17fe97 Binary files /dev/null and b/StoreOlist/bin/br/com/olist/store/MainScreen$EventoJMenuSair.class differ diff --git a/StoreOlist/bin/br/com/olist/store/MainScreen.class b/StoreOlist/bin/br/com/olist/store/MainScreen.class new file mode 100644 index 0000000..6ed7f08 Binary files /dev/null and b/StoreOlist/bin/br/com/olist/store/MainScreen.class differ diff --git a/StoreOlist/bin/br/com/olist/store/NewProduct$EventJBAdd.class b/StoreOlist/bin/br/com/olist/store/NewProduct$EventJBAdd.class new file mode 100644 index 0000000..aef0636 Binary files /dev/null and b/StoreOlist/bin/br/com/olist/store/NewProduct$EventJBAdd.class differ diff --git a/StoreOlist/bin/br/com/olist/store/NewProduct$EventJBClear.class b/StoreOlist/bin/br/com/olist/store/NewProduct$EventJBClear.class new file mode 100644 index 0000000..3933b2c Binary files /dev/null and b/StoreOlist/bin/br/com/olist/store/NewProduct$EventJBClear.class differ diff --git a/StoreOlist/bin/br/com/olist/store/NewProduct$EventJBSave.class b/StoreOlist/bin/br/com/olist/store/NewProduct$EventJBSave.class new file mode 100644 index 0000000..b7a1ab2 Binary files /dev/null and b/StoreOlist/bin/br/com/olist/store/NewProduct$EventJBSave.class differ diff --git a/StoreOlist/bin/br/com/olist/store/NewProduct.class b/StoreOlist/bin/br/com/olist/store/NewProduct.class new file mode 100644 index 0000000..9d9f491 Binary files /dev/null and b/StoreOlist/bin/br/com/olist/store/NewProduct.class differ diff --git a/StoreOlist/bin/br/com/olist/store/NewSearch$EventoJBSearch.class b/StoreOlist/bin/br/com/olist/store/NewSearch$EventoJBSearch.class new file mode 100644 index 0000000..4b44ee2 Binary files /dev/null and b/StoreOlist/bin/br/com/olist/store/NewSearch$EventoJBSearch.class differ diff --git a/StoreOlist/bin/br/com/olist/store/NewSearch.class b/StoreOlist/bin/br/com/olist/store/NewSearch.class new file mode 100644 index 0000000..5f1a0d9 Binary files /dev/null and b/StoreOlist/bin/br/com/olist/store/NewSearch.class differ diff --git a/model.mwb.bak b/model.mwb.bak new file mode 100644 index 0000000..d2c6ee9 Binary files /dev/null and b/model.mwb.bak differ diff --git a/storeg.mwb b/storeg.mwb new file mode 100644 index 0000000..143e556 Binary files /dev/null and b/storeg.mwb differ diff --git a/storeg.mwb.bak b/storeg.mwb.bak new file mode 100644 index 0000000..143e556 Binary files /dev/null and b/storeg.mwb.bak differ