Hi again guys. I currently have a class named ElementSet which houses an array of objects as well as an array of boolean values which pertain to two function tasked with flagging or unflagging an individual object. I also have an abstract class, Element, and two inheriting classes named Subscriber and Application.

I am trying to store an array of objects from either the Subscriber and/or Application class into the array of ElementSet objects and, from there execute the functions within the ElementSet class such as flagIt, unFlagIt, and displayAllInClass.

I am having trouble trying to figure out exactly how I would prompt the user to enter the name of the object they're looking for in the ElementSet class and then search through the ElementSet array for that object and execute the function (be it flagIt, unFlagIt, etc).

For this assignment, our instructor had us use some of his code for parts of the program which is why I am so confused. I am not sure how to correctly implement these functions.

ELEMENTSET CLASS:

package assignment.pkg2;


public class ElementSet {
    private Element[] elementList;
    
    private boolean[] flagged;
    
    private int currentIndex;
    private int currentSize;
    
    private final int MAXSETSIZE = 100;
    
    public ElementSet()
    {
        elementList = new Element[MAXSETSIZE];
        flagged = new boolean[MAXSETSIZE];
        
        for(int i = 0; i < MAXSETSIZE; i++)
        {
            flagged[i] = false;
        }
  
        currentIndex = -1;
        currentSize = 0;
    }
    
    public boolean isMemberOf(Element anElement)
    {
        String paramClass = anElement.getClassName();
        String currClass;
        
        for (int i = 0; i < currentSize; i++)
        {
            currClass = elementList[i].getClassName();
            
            if (currClass.equals(paramClass))
            {
                if (elementList[i].equals(anElement))
                {
                    return true;
                }
            }
        }
        return false;
    }
    
    public boolean isFull()
    {
         return currentSize == MAXSETSIZE;
    }
    
    public boolean isEmpty()
    {
         return currentSize == 0;
    }
    
    public int size()
    {
         return currentSize;
    }
    
    public Element getCurrent()
    {
         // Local data ...
         int saveIndex = currentIndex;
       	
      	// Logic ...
         if (currentIndex == currentSize - 1)
         {
            // Recycle to beginning of list
            currentIndex = 0;
         }
         else
         {
            // Advance currentIndex to next object
            currentIndex++;
         }
         
      	// Return a reference to a clone of the current object
         return elementList[saveIndex].clone(); 
    }
    
    public int add(Element anElement)
    {
          // Logic ...
         if (currentSize == MAXSETSIZE)
         {
            return 0;  // set is full
         }
         else if (this.isMemberOf(anElement))
         {
            return -1; // it's already in there
         }
       
         // We will add a clone of anElement to
      	// the set.
         elementList[currentSize] = anElement.clone();
       
         // Increment currentSize.
         currentSize++;
      
         // Set currentIndex to object we just added if it was the
         // first object in the set. 
         if (currentSize == 1) currentIndex = 0;
      
         // We succeeded.
         return 1;
    }
     
    public void clear()
      {
       	// Clean up the memory associated with this object 
      	// while it is still in use.
         for (int i = 0; i < currentSize; i++)
         {
            elementList[i] = null;
         }
         
      	// Reset currentSize and currentIndex to empty set 
      	// values.
         currentIndex = -1;
         currentSize = 0;
      }
   
     public void display()
      {
         if (currentSize == 0)
         {
            System.out.println("There are no objects in the set. ");
         }
         else
         {
            System.out.println("Here are the objects in the set: \n");
            for (int i = 0; i < currentSize; i++)
            {
               elementList[i].display();
               System.out.println("\n");
            }
         }
      
      }
   

    public int flagIt(Element anElement)
    {
        String paramClass = anElement.getClassName();
        String currClass;
        
        for (int i = 0; i < currentSize; i++)
        {
            currClass = elementList[i].getClassName();
            
            if (currClass.equals(paramClass))
            {
                if (elementList[i].equals(anElement) && flagged[i] == true)
                {
                    System.out.println("Object found, but has already been"
                            + " flagged.");
                    return 1;
                }
                
                else if (elementList[i].equals(anElement) && flagged[i] != true)
                {
                    System.out.println("Object found and is now"
                            + " flagged.");
                    flagged[i] = true;
                    return 0;
                }
            }
        }
        System.out.println("Object not found.");
        return -1;
    }
    
    public int unFlagIt(Element anElement)
    {
        String paramClass = anElement.getClassName();
        String currClass;
        
        for (int i = 0; i < currentSize; i++)
        {
            currClass = elementList[i].getClassName();
            
            if (currClass.equals(paramClass))
            {
                if (elementList[i].equals(anElement) && flagged[i] == true)
                {
                    flagged[i] = false;
                    System.out.println("Object found, this user is no longer"
                            + " flagged");
                    return 1;
                }
                
                else if (elementList[i].equals(anElement) && flagged[i] != true)
                {
                    System.out.println("Object found, but is already "
                            + "unflagged");
                    return 0;
                }
            }
        }
        System.out.println("Object not found.");
        return -1;    
    }
    
    public void displayAllInClass(String theClassName)
    {
        
    }

}

MAIN:

package assignment.pkg2;

import java.util.Scanner;

public class Assignment2 {

    public static final int MAX_NUM = 10;
    
    public static int getMenuChoice()
        {
            Scanner keyboard = new Scanner(System.in);
            
            System.out.println("\nHere are your Choices: \n"
            + "Enter 1 to add a Subscriber \n"
            + "Enter 2 to add an Application \n"
            + "Enter 3 to flag a Subscriber \n"
            + "Enter 4 to flag an Application \n"
            + "Enter 5 to unflag a Subscriber \n"
            + "Enter 6 to unflag and Application \n"
            + "Enter 7 to display all Subscribers \n"
            + "Enter 8 to display all Applications \n"
            + "Enter 9 to Quit \n");
            
            return keyboard.nextInt();
        }
    
    public static void main(String[] args) {
        
        ElementSet set = new ElementSet();
        Subscriber subList[] = new Subscriber [MAX_NUM];
        
        Scanner keyboard = new Scanner(System.in);
        
        int menuChoice;
        int subIndex = 0;
        
        String anyMoreSub;
        
        
        for(int i = 0; i < MAX_NUM ;i++)
        {
            subList[i] = new Subscriber("", "", 0);
        }
        
       
        
        menuChoice = getMenuChoice();

        while(menuChoice != 9)
        {
            switch(menuChoice)
            {
        
            case 1:
                //add subscriber to Element Set array
                for(int i = 0; i < MAX_NUM; i++)
                {
                    subList[i].readIn();
                    
                    System.out.println("\nDo you want to enter another subscriber? "
                    + "(Y/N): ");
                    anyMoreSub = keyboard.nextLine().toUpperCase();
                    subIndex++;
                
                        if (anyMoreSub.equals("N"))
                        {
                            set.add(subList[i]);
                            System.out.println("Testing display");
                            set.display();
                            
                            break;
                        }

                }
            break;
            
            case 2:
                //add application to Element Set array
            break;
            
            case 3:
                //flag a specified subscriber
            break;
            
            case 4:
                //flag a specified application
            break;
            
            case 5:
                //unflag a specified subscriber
            break;
            
            case 6:
                //unflag a specified application
            break;
            
            case 7:
                //display all subscribers in the Element Set array
            break;
            
            case 8:
                //display all applications in the Element Set array
            break;
                
            }
            
            menuChoice = getMenuChoice();
        }

    }  

}

Also, it's probably worth noting that both the Subscriber and Application class have an equals method:

public boolean equals(Element otherSubscriber)
    {
        return name.equals(((Subscriber) otherSubscriber).name);
    }

I am unsure whether this is of importance, though.

As you can see, I attempted this somewhat in 'case 1' and it does seem to work. When I call the display function from ElementSet, it displays it all correctly. But how would I go about searching for an individual object to apply the flagIt/unFlagIt functions?

Thank you.

EDIT:

Sorry! Not trying to bump here, but there was no Edit button available for my initial post (no clue why).

I just wanted to add: Looking at my code, I am questioning whether or not it is even necessary to have two arrays. Couldn't I just add individual objects straight into the ElementSet array?

Thanks again and sorry for the bump!

Yes, just add them one at a time and forget the second array.
You already have code to find an element and flag/unflag it - so what's the problem you are asking in bold above?

Yes, just add them one at a time and forget the second array.
You already have code to find an element and flag/unflag it - so what's the problem you are asking in bold above?

I'm sorry, that is a bit confusing.

What I am trying to find is the function that asks the user for a name for either the Subscriber or Application class. The function then searches the ElementSet array for that name and, if found, executes the flagIt or unFlagIt function. I have made some progress and attempted that function, but I have an issue:

package assignment.pkg2;

import java.util.Scanner;

public class Assignment2 {

    public static final int MAX_NUM = 10;
    
    public static int getMenuChoice()
        {
            Scanner keyboard = new Scanner(System.in);
            
            System.out.println("\n-----------------------------"
            + "\nHere are your Choices: \n"
            + "Enter 1 to add a Subscriber \n"
            + "Enter 2 to add an Application \n"
            + "Enter 3 to flag a Subscriber \n"
            + "Enter 4 to flag an Application \n"
            + "Enter 5 to unflag a Subscriber \n"
            + "Enter 6 to unflag and Application \n"
            + "Enter 7 to display all Subscribers \n"
            + "Enter 8 to display all Applications \n"
            + "Enter 9 to Quit "
                    + "\n-----------------------------\n");
            
            return keyboard.nextInt();
        }
    
    public static void main(String[] args) {
        
        ElementSet set = new ElementSet();
        Element anElement;
        Subscriber aSub;
        Application anApp;

        Scanner keyboard = new Scanner(System.in);
        
        int menuChoice;
        
        String anyMoreSub;
        String anyMoreApps;
        String subSearch;
        String appSearch;
        String classId;
        String quit;
        
        menuChoice = getMenuChoice();

        while(menuChoice != 9)
        {
            switch(menuChoice)
            {
        
            case 1:
                //add subscriber to Element Set array
                anElement = new Subscriber();
                anElement.readIn();
            break;
            
            case 2:
                //add application to Element Set array
                anElement = new Application();
                anElement.readIn();
            break;
            
            case 3:
                //flag a specified subscriber
                System.out.println("Enter Subscriber's name: ");
                subSearch = keyboard.nextLine().toUpperCase();
                
                for(int i = 0; i < set.size(); i++)
                {
                    anElement = set.getCurrent();
                    classId = anElement.getClassName();
                    
                    if(classId.equals("Subscriber"))
                    {
                        aSub = (Subscriber) anElement;
                        if((aSub.getName()).equals(subSearch))
                        {
                            set.flagIt(aSub);
                        }
                    }
                }
            break;
            
            case 4:
                //flag a specified application
            break;
            
            case 5:
                //unflag a specified subscriber
            break;
            
            case 6:
                //unflag a specified application
            break;
            
            case 7:
                //display all subscribers in the Element Set array
                set.displayAllInClass("Subscriber");
            break;
            
            case 8:
                //display all applications in the Element Set array
                set.displayAllInClass("Application");
            break;
            
                
            }
            
            menuChoice = getMenuChoice();
        } 

    }  

}

LINE 71: It never initiates the for loop, it just sits at line 70. Any clue as to why it does not progress?

Also, does it look like I created that function correctly? Please let me know where I can improve it. Thanks again.

EDIT:

Here are the relevant getClassName and getCurrent methods:

getClassName

public String getClassName()
    { 
        String resultStr;
        
        int location;
        
        resultStr = this.toString();
        location = resultStr.indexOf('@');
        return resultStr.substring(0, location);
    }

getCurrent

public Element getCurrent()
    {
         // Local data ...
         int saveIndex = currentIndex;
       	
      	// Logic ...
         if (currentIndex == currentSize - 1)
         {
            // Recycle to beginning of list
            currentIndex = 0;
         }
         else
         {
            // Advance currentIndex to next object
            currentIndex++;
         }
         
      	// Return a reference to a clone of the current object
         return elementList[saveIndex].clone(); 
    }

I was able to figure it out. I was going about it all wrong.

Here's the code after updating if anyone was wondering:

package assignment.pkg2;

import java.util.Scanner;

public class Assignment2 {
    
    public static int getMenuChoice()
        {
            Scanner keyboard = new Scanner(System.in);
            
            System.out.println("\n-----------------------------"
            + "\nHere are your Choices: \n"
            + "Enter 1 to add a Subscriber \n"
            + "Enter 2 to add an Application \n"
            + "Enter 3 to flag a Subscriber \n"
            + "Enter 4 to flag an Application \n"
            + "Enter 5 to unflag a Subscriber \n"
            + "Enter 6 to unflag and Application \n"
            + "Enter 7 to display all Subscribers \n"
            + "Enter 8 to display all Applications \n"
            + "Enter 9 to Quit "
                    + "\n-----------------------------\n");
            
            return keyboard.nextInt();
        }
    
    public static void main(String[] args) {
        
        ElementSet set = new ElementSet();
        Element anElement;
        Subscriber aSub;
        Application anApp;

        Scanner keyboard = new Scanner(System.in);
        
        int menuChoice;
        
        String anyMoreSub;
        String anyMoreApps;
        String subSearch;
        String appSearch;

        menuChoice = getMenuChoice();

        while(menuChoice != 9)
        {
            switch(menuChoice)
            {
        
            case 1:
                //add subscriber to Element Set array
                anElement = new Subscriber();
                anElement.readIn();
                set.add(anElement);
            break;
            
            case 2:
                //add application to Element Set array
                anElement = new Application();
                anElement.readIn();
                set.add(anElement);         
            break;
            
            case 3:
                //flag a specified subscriber
                System.out.println("Enter Subscriber's name: ");
                subSearch = keyboard.nextLine().toUpperCase();

                for(int i = 0; i < set.size(); i++)
                {
                    anElement = set.getCurrent();

                    aSub = (Subscriber) anElement;
                        if((aSub.getName()).equals(subSearch))
                        {
                            set.flagIt(aSub);
                        }
                        else
                        {
                            System.out.println("Member not found.");
                        }
                }
                
            break;
            
            case 4:
                //flag a specified application
                System.out.println("Enter Application's name: ");
                subSearch = keyboard.nextLine().toUpperCase();
                for(int i = 0; i < set.size(); i++)
                {
                    anElement = set.getCurrent();

                        anApp = (Application) anElement;
                        if((anApp.getName()).equals(subSearch))
                        {
                            
                            set.flagIt(anApp);
                        }
                        else
                        {
                            System.out.println("Member not found.");
                        }
                }
                
            break;
            
            case 5:
                //unflag a specified subscriber
                System.out.println("Enter Subscriber's name: ");
                subSearch = keyboard.nextLine().toUpperCase();

                for(int i = 0; i < set.size(); i++)
                {
                    anElement = set.getCurrent();

                    aSub = (Subscriber) anElement;
                        if((aSub.getName()).equals(subSearch))
                        {
                            set.unFlagIt(aSub);
                        }
                        else
                        {
                            System.out.println("Member not found.");
                        }
                }
            break;
            
            case 6:
                //unflag a specified application
                System.out.println("Enter Application's name: ");
                subSearch = keyboard.nextLine().toUpperCase();

                for(int i = 0; i < set.size(); i++)
                {
                    anElement = set.getCurrent();

                        anApp = (Application) anElement;
                        if((anApp.getName()).equals(subSearch))
                        {
                            set.unFlagIt(anApp);
                        }
                        else
                        {
                            System.out.println("Member not found.");
                        }
                }
            break;
            
            case 7:
                //display all subscribers in the Element Set array
                set.displayAllInClass("Subscriber");
            break;
            
            case 8:
                //display all applications in the Element Set array
                set.displayAllInClass("Application");
            break;
            
                
            }
            
            menuChoice = getMenuChoice();
        } 

    }  

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.