Source : SaleItemList.java

CS22120 Group Project - project source listings

Questions and comments on this file can be directed to this classes author: Paul Smith


package cs221.groupk.common;

import java.io.Serializable;
import java.util.Vector;

import cs221.groupk.exceptions.database.*;
import cs221.groupk.common.*;

/**
 * SaleItemList provides access methods to the list of sale items. This class
 * is the price lookup, but the information here is only accessable via the
 * Disk interface methods.
 * Its methods are only accessable through those made public in the Disk class.
 *
 * <P><I>Code spell-checked - Chris Milner - 5th December 2000.</I><P>
 *
 * @author <A HREF="mailto:[email protected]">Paul Smith</A>
 */
public class SaleItemList implements Constants, Serializable
{
  protected Vector saleItems;
  protected int saleListID; //used for database normalisation and synchronisation.
  protected static UserSet userSet;
  protected boolean accessable;
  protected boolean admin;

  public SaleItemList() throws InsufficientAccessPrivilegeException
  {
    throw new InsufficientAccessPrivilegeException("You cannot access a sale item list without a valid UserSet.");
  }

  public SaleItemList(UserSet userSet) throws InsufficientAccessPrivilegeException, UserSetInvalidException
  {
    try
    {
      if (userSet != null)
      {
        this.userSet = userSet;
        if (!((this.userSet.getCurrentAccessLevel() == VALIDATION_SUCCESFULL_ALL) ||
            (this.userSet.getCurrentAccessLevel() == VALIDATION_SUCCESFULL_STATIONS) ||
            (this.userSet.getCurrentAccessLevel() == VALIDATION_SUCCESFULL_BOTTOM_STATION) ||
            (this.userSet.getCurrentAccessLevel() == VALIDATION_SUCCESFULL_TOP_STATION)))
        {
          throw new InsufficientAccessPrivilegeException("Specified UserSet has not been validated.");
        }
        if(this.userSet.getCurrentAccessLevel() == VALIDATION_SUCCESFULL_ALL)
        {
          admin = true;
        }
        else
        {
          admin = false;
        }
      }
      else
      {
        throw new UserSetInvalidException("The supplied UserSet was not valid.");
      }
    }
    catch(Exception e)
    {
      throw new UserSetInvalidException("The supplied UserSet was not valid.");
    }
  }

  /**
   * getItem returns a specifed item, or throws an exception if it isnt found.
   */
  public SaleItem getItem(int index) throws SaleItemNotFoundException, SaleItemIndexOutOfBounds
  {
    if (index > saleItems.size())
    {
      throw new SaleItemIndexOutOfBounds("The specifed Index is out of range.");
    }
    SaleItem si;
    try
    {
      si = (SaleItem) saleItems.elementAt(index);
    }
    catch(Exception e)
    {
      throw new SaleItemNotFoundException("Sale Item not Found.");
    }
    return si;
  }

  /**
   * getSaleListID returns the ID of this list.
   */
  public int getSaleListID()
  {
    return saleListID;
  }

  /**
   * equals compares this SaleItemList with another SaleItemList - test -
   * returning true if all the attributes in this class, and all the SaleItems
   * within it matches those being tested.
   */
  public boolean equals(SaleItemList test)
  {
    if (test.saleListID == this.saleListID)
    {//same ID.
      if(test.count() == this.count())
      {//Same # of items.
        return true;//IMPLEMENT CHECK EACH ITEM ONE BY ONE!
      }
    }
    return false;
  }

  /**
   * addItem adds theSaleItem, and returns its position, or a negative error
   * code.
   * @throws InsufficientAccessPrivilegeException is thrown if a user without
   * Admin status attempts to access them, or the current user has not been
   * validated.
   */
  public int addItem(SaleItem saleItem) throws InsufficientAccessPrivilegeException
  {
    if(!admin)
    {
      throw new InsufficientAccessPrivilegeException("You do do not have the access rights to make this change.");
    }
    saleItems.add(saleItem);
    return OPERATION_SUCESS;
  }

  /**
   * setSaleListID Sets this Lists sale id with theSaleListID.
   * @param int theSaleListID - specifies the SaleListID to set.
   * @throws InsufficientAccessPrivilegeException is thrown if a user without
   * Admin status attempts to access them, or the current user has not been
   * validated.
   */
  public void setSaleListID(int theSaleListID) throws InsufficientAccessPrivilegeException
  {
    if(!admin)
    {
      throw new InsufficientAccessPrivilegeException("You do do not have the access rights to make this change.");
    }
  }

  /**
   * replaceSaleItem looks for theOldSaleItem, and if found replaces it with
   * theNewSaleItem. The return value specifes if the operation failed, and
   * why.
   * @throws InsufficientAccessPrivilegeException is thrown if a user without
   * Admin status attempts to access them, or the current user has not been
   * validated.
   */
  public int replaceSaleItem(SaleItem theOldSaleItem, SaleItem theNewSaleItem) throws InsufficientAccessPrivilegeException, SaleItemNotFoundException
  {
    if(!admin)
    {
      throw new InsufficientAccessPrivilegeException("You do do not have the access rights to make this change.");
    }
    return -1;
  }

  /**
   * count returns the number of <CODE>SaleItems</CODE> in this <CODE>
   * <CODE>SaleItemList</CODE>. This method has been declared as final to
   * prevent it from being overridden in subsequent sub-classes, as this class
   * simply returns the result of a call to <CODE>saleItems.count()</CODE>
   * @return the number of SaleItems within this <CODE>SaleItemList</CODE>
   */
  public final int count()
  {
    return saleItems.size();
  }
}


Up one level.

Page automatically generated on: 28/12/00 at: 7:58:13 PM.