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.