Commit c7bcaa1b authored by Phuong Khanh Tran's avatar Phuong Khanh Tran
Browse files

Update Molecule.java

parent c153e668
Loading
Loading
Loading
Loading
+48 −7
Original line number Diff line number Diff line
package edu.bu.ec504.project;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * Represents a molecule containing atoms and edges.
 */
public class Molecule {

    //FIELDS
    String moleculeName; // name of the molecule
    int numAtoms; // number of atoms in the molecule
    int numEdges; // number of edges in the molecule
    ArrayList<Atom> atomArrayList;  // list of atoms in the molecule
    int[] numElements; // array to store quantity of each element

    /**
     * Constructs a molecule by reading data from a file.
     * @param moleculeFile the file containing the molecule.
     */
    public Molecule(String MoleculeFile){
        //Init values
        numElements= new int [119];
@@ -39,13 +53,40 @@ public class Molecule {
        }
    }

    /**
     * Compare two molecules and check if they are isomorphic.
     * @param otherMolecule The molecule to compare with.
     * @return The first molecule if they are equal, otherwise null.
     */
    public Molecule areMoleculesEqual(Molecule otherMolecule) {
        // Compare the name of the molecule
        if (!this.moleculeName.equals(otherMolecule.moleculeName)) {
            return null; // Names are different, molecules are not equal
        }

    //FIELDS
    String moleculeName;
    int numAtoms;
    int numEdges;
    ArrayList<Atom> atomArrayList;
    int[] numElements; //stores quantity of each element
        // Compare the number of atoms
        if (this.numAtoms != otherMolecule.numAtoms) {
            return null; // Number of atoms is different, molecules are not equal
        }

        // Compare the number of edges
        if (this.numEdges != otherMolecule.numEdges) {
            return null; // Number of edges is different, molecules are not equal
        }

        // Compare the atom lists
        if (!this.atomArrayList.equals(otherMolecule.atomArrayList)) {
            return null; // Atom lists are different, molecules are not equal
        }

        // Compare the number of elements
        if (!Arrays.equals(this.numElements, otherMolecule.numElements)) {
            return null; // Number of elements is different, molecules are not equal
        }

        // If all comparisons passed, the molecules are equal
        return this;
    }

}