Commit 0d829fb8 authored by Caelan Reese Wong's avatar Caelan Reese Wong
Browse files

Merge branch 'deleteMolecule' into 'master'

Delete molecule

See merge request ec504/ec504_projects/group4!14
parents f2e906d4 2f68541c
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ public class GUI extends JFrame {
    private JButton displayMoleculeButton;
    private JButton findSubgraphButton;
    private JButton downloadPubChemButton;
    private JButton deleteMoleculeButton;
    private JTextField filePathField;
    private static MDB moleculeDb;
    private Socket clientSocket;
@@ -42,6 +43,7 @@ public class GUI extends JFrame {
        JScrollPane scrollPane = new JScrollPane(outputTextArea);
        chooseFileButton = new JButton("Choose File");
        downloadPubChemButton = new JButton("Download PubChem");
        deleteMoleculeButton = new JButton("Delete Molecule");
        addMoleculeButton = new JButton("Add Molecule");
        findMoleculeButton = new JButton("Find Molecule");
        findSubgraphButton = new JButton("Find Subgraph");
@@ -58,6 +60,7 @@ public class GUI extends JFrame {
        controlPanel.setBackground(Color.BLACK);
        controlPanel.add(chooseFileButton);
        controlPanel.add(downloadPubChemButton);
        controlPanel.add(deleteMoleculeButton);
        controlPanel.add(addMoleculeButton);
        controlPanel.add(findMoleculeButton);
        controlPanel.add(findSubgraphButton);
@@ -109,6 +112,23 @@ public class GUI extends JFrame {
            }
        });


        //Action Listener for Delete Molecule button
        deleteMoleculeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // Get the molecule path from the text field
                String moleculePath = filePathField.getText();
                // Execute the findMolecule command
                boolean delete= moleculeDb.deleteMolecule(new Molecule(moleculePath));
                if(delete)
                    outputTextArea.append("Successfully Deleted" + "\n\n");
                else
                    outputTextArea.append("Molecule not in the database" + "\n\n");

            }
        });

        // Action listener for Add Molecule button
        addMoleculeButton.addActionListener(new ActionListener() {
            @Override
+23 −0
Original line number Diff line number Diff line
@@ -145,6 +145,29 @@ public class MDB {
        return similar;
    }


    /**
     * Delete a given molecule if it is in the database
     */
    public boolean deleteMolecule(Molecule molecule){
        // Retrieve the partitioned array list based on the number of atoms
        int numAtoms = molecule.getNumAtoms();
        if (!db.containsKey(numAtoms)) {
            return false;
        }
        ArrayList<Molecule> moleculesWithSameNumAtoms = db.get(numAtoms);

        // Iterate through the array list of molecules with the same number of atoms
        for (Molecule dbMolecule : moleculesWithSameNumAtoms) {
            Molecule result = dbMolecule.areMoleculesEqual(molecule);
            if (result != null) {
                this.db.get(numAtoms).remove(dbMolecule);
                return true; // successfully delete the molecule
            }
        }
        return false; // Return false if molecule not in database
    }

    /**
     * Find subgraph
     */
+7 −0
Original line number Diff line number Diff line
@@ -122,6 +122,13 @@ public class Main {
                    printVerbose("invalid Input");
                }
                break;
            case "--delete":
                boolean delete= moleculeDb.deleteMolecule(new Molecule(moleculePath));
                if(delete)
                    printVerbose("Successfully Deleted");
                else
                    System.out.println("Molecule not in the database");
                break;
            default:
                printVerbose("unrecognized command: " + cmd);
                break;
+24 −0
Original line number Diff line number Diff line
@@ -176,6 +176,30 @@ public class MoleculeDatabase {

        return returnList;
    }

    /**
     * Delete a given molecule if it is in the database
     */
    public boolean deleteMolecule(Molecule molecule){
        // Retrieve the partitioned array list based on the number of atoms
        int numAtoms = molecule.getNumAtoms();
        if (!db.containsKey(numAtoms)) {
            return false;
        }
        ArrayList<Molecule> moleculesWithSameNumAtoms = db.get(numAtoms);

        // Iterate through the array list of molecules with the same number of atoms
        for (Molecule dbMolecule : moleculesWithSameNumAtoms) {
            Molecule result = dbMolecule.areMoleculesEqual(molecule);
            if (result != null) {
                this.db.get(numAtoms).remove(dbMolecule);
                return true; // successfully delete the molecule
            }
        }
        return false; // Return false if molecule not in database
    }


    /**
     * Save database to file system
     */