Lisa Davis

Lisa Davis

  • NA
  • 1
  • 1.1k

Deleting current record from array

Dec 7 2014 2:31 AM
Hello, I am in need of some assistance with this program.
I am needing to fill in the spaces marked as //TODO and am unsure how I go about it. Any help would be appreciated. 
Also could someone please let me know if I've done the INSERTING RECORDS part correctly?
-----------------------------------


public class EntityModel implements EntityModelInterface{


  /**
   *  The model can only store up to MAX_RECORDS
   */
  public static final int MAX_RECORDS = 10000;
  /**
   * The array storing the records
   */
  protected Entity[] allRecords;
  /**
   * The number of records being stored
   */
  protected int totalRecords;
  /**
   * The current record index. The main purpose of the class is to
   * maintain this.
   */
  protected int current;
  /**
   * A count of the number of records that have been marked for deletion
   */
  protected int delRecCount;


  // CONSTRUCTORS
  /**
   * Creates a EntityModel capable of storing <var>MAX_RECORDS</var> using
   * an array
   * <p>
   * The current position is set to -1 (undefined).
   * </p>
   * <ul>
   * <li>Creates the array</li>
   * <li>Initialises the totalRecords to zero</li>
   * <li>Initialises the delRecCount to zero</li>
   * <li>Sets current to be -1</li>
   * </ul>
   *
   */
  public EntityModel() {
 allRecords = new Entity[MAX_RECORDS]; 
 totalRecords = 0;
 delRecCount = 0;
 current = -1;
  }


  // STANDARD METHODS
  public String toString() {
    String result;
    result = super.toString() + "["
            + " current=" + getCurrentRecordPosition()
            + ",delRecCount=" + getDelRecCount()
            + ",totalRecords=" + getTotalRecords()
            + ",entities= ";
    for (int i = 0; i < getTotalRecords(); i++) {
      result = result + this.allRecords[i] + ",";
    }
    result = result + "]";
    return result;
  }


  public boolean equals(Object o) {


    if (!(o instanceof EntityModel)) {
      return false;
    }


    EntityModel sm = (EntityModel) o;


    boolean same = false;
    int i = 0;
    while (i < this.getTotalRecords() && this.allRecords[i].equals(sm.allRecords[i])) {
      i++;
    }
    same = (i == this.getTotalRecords());
    return same;
  }


  // INSERTING RECORDS
  public Entity add(Entity entity) {
 current = current +1;
 totalRecords = totalRecords+1;
 allRecords[current] = entity;
 entity.setId("current");  
 return entity;
  }


  // DELETE THE CURRENT RECORD
  public Entity deleteCurrentRecord() {
    //Current is usually moved to be the "next" record except if deleting the
    // last record - see below.


    // If it is a marked for deletion record then decrease the marked for 
    // deletion record count
    //TODO


    //Get rid of the record from the array
    //TODO
    //Adjust the total and current
    // Case when deleting the last record. Set current to be the new last record.
    //TODO


    // Return current but check if there are any records left. If not return null
    //TODO
    return null;  //remove or modify this line as needed


  }