Commit a06edd43 authored by Caelan Reese Wong's avatar Caelan Reese Wong
Browse files

implement --delete molecule feature

parent f2e906d4
Loading
Loading
Loading
Loading
+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
                    printVerbose("Molecule not deleted or already 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) {
                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
     */