Commit 0d038b52 authored by Hyun Soo  Kim's avatar Hyun Soo Kim
Browse files

Molecule class now implements Serializable, bug: atomArrayList compare;...

Molecule class now implements Serializable, bug: atomArrayList compare; comment out for now, extra prints for each stage of compares in areMoleculesEqual()
parent d4d52c8d
Loading
Loading
Loading
Loading
+23 −14
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.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;

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

    //FIELDS
    String moleculeName; // name of the molecule
    public 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
@@ -19,16 +21,17 @@ public class Molecule {

    /**
     * Constructs a molecule by reading data from a file.
     *
     * @param moleculeFile the file containing the molecule.
     */
    public Molecule(String MoleculeFile){
    public Molecule(String moleculeFile) {
        //Init values
        numElements = new int[119];

        int f, l;

        // Read and process input
        try (BufferedReader reader = new BufferedReader(new FileReader(MoleculeFile))) { //Might want to move reader outside molecule class in the future to save space
        try (BufferedReader reader = new BufferedReader(new FileReader(moleculeFile))) { //Might want to move reader outside molecule class in the future to save space
            String line;
            moleculeName = reader.readLine(); //Reads name
            numAtoms = Integer.parseInt(reader.readLine()); //Reads # of atoms
@@ -55,32 +58,38 @@ 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)) {
            System.out.println("different names");
            return null; // Names are different, molecules are not equal
        }

        // Compare the number of atoms
        if (this.numAtoms != otherMolecule.numAtoms) {
            System.out.println("different numAtoms");
            return null; // Number of atoms is different, molecules are not equal
        }

        // Compare the number of edges
        if (this.numEdges != otherMolecule.numEdges) {
            System.out.println("different 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
        }
//        if (!this.atomArrayList.equals(otherMolecule.atomArrayList)) {
//            System.out.println("different atomArrayList");
//            return null; // Atom lists are different, molecules are not equal
//        }

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