Hi there!
So I'm working on my next assignment and it's all about implementing a few exceptions in order to get us familiar with the topic.
I am tasked with implementing 3 very simple exceptions to my previous program. So far I have 2 out of 3 working fine with zero issues.
It's the third one that's causing me some headaches. Now, I will be honest, I posted a similar topic to this on another board, but I left out a few details that weren't extremely relevant to the main problem here. I will post those to see if you guys would be able to help me out!
Well first thing's first, here's my code. It's very long and I will make sure to bold the areas of interest. Also, I'm only posting the relevant classes:
MAIN:
package assignment.pkg3;
import java.util.Scanner;
public class Assignment3 {
//Function for user menu display and input
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 remove a Subscriber \n"
+ "Enter 4 to remove an Application \n"
+ "Enter 5 to display all Subscribers \n"
+ "Enter 6 to display all Applications \n"
+ "Enter 7 to Quit "
+ "\n-----------------------------\n");
return keyboard.nextInt();
}
public static void main(String[] args)
{
//Field Declarations
Set<Element> anElementSet;
Element anElement;
Subscriber aSub;
Application anApp;
Scanner keyboard = new Scanner(System.in);
int menuChoice;
String search = "";
boolean isDone = false;
boolean correctInt = false;
menuChoice = getMenuChoice();
anElementSet = new Set<Element>();
//Initiate loop to display menu
while(!isDone)
{
if(menuChoice == 7)
{
System.out.println("Are you sure you want to quit? (Y/N)");
String askQuit;
askQuit = keyboard.nextLine().toUpperCase();
if(askQuit.equals("Y"))
{
isDone = true;
break;
}
}
switch(menuChoice)
{
case 1:
//add subscriber to Element Set array
anElement = new Subscriber();
do
{
try
{
anElement.readIn();
correctInt = true;
}
catch(NegativeIntException e)
{
System.out.println("\nTry again.");
correctInt = false;
}
catch(NumberFormatException e)
{
System.out.println("\nTry again.");
correctInt = false;
}
}
while(!correctInt);
//Try to add the member
try
{
anElementSet.add(anElement);
}
catch(DuplicateObjectException e)
{
System.out.println("\nPlease enter a different Subscriber");
}
break;
case 2:
//add application to Element Set array
anElement = new Application();
do
{
try
{
anElement.readIn();
correctInt = true;
}
catch(NegativeIntException e)
{
System.out.println("\nTry again.");
correctInt = false;
}
catch(NumberFormatException e)
{
System.out.println("\nTry again.");
correctInt = false;
}
}
while(!correctInt);
//Try to add the member
try
{
anElementSet.add(anElement);
}
catch(DuplicateObjectException e)
{
System.out.println("\nPlease enter a different Application.");
}
break;
case 3:
//remove subscriber from Element Set array
[B]System.out.println("Enter Subscriber's name: ");
search = keyboard.nextLine().toUpperCase();
for(int i = 0; i < anElementSet.size(); i++)
{
anElement = anElementSet.getCurrent();
if(anElement instanceof Subscriber)
{
aSub =(Subscriber)anElement;
try
{
if((aSub.getName()).equals(search))
{
anElementSet.removeObject(aSub);
}
}
catch(CannotRemoveException e)
{
System.out.println("Member not found, "
+ "removal unsuccessful.");
}
}
}[/B]
break;
case 4:
//remove application from Element Set array
[B]System.out.println("Enter Application's name: ");
search = keyboard.nextLine().toUpperCase();
for(int i = 0; i < anElementSet.size(); i++)
{
anElement = anElementSet.getCurrent();
if(anElement instanceof Application)
{
anApp =(Application)anElement;
try
{
if((anApp.getName()).equals(search))
{
anElementSet.removeObject(anApp);
}
}
catch(CannotRemoveException e)
{
System.out.println("Member not found, "
+ "removal unsuccessful.");
}
}
}[/B]
break;
case 5:
//display all Subscribers in the Element Set array
anElementSet.displayAllInClass(Subscriber.class.getName());
break;
case 6:
//display all Applications in the Element Set array
anElementSet.displayAllInClass(Application.class.getName());
break;
}
menuChoice = getMenuChoice();
}
}
}
ARRAYLIST (SET) CLASS:
package assignment.pkg3;
import java.util.ArrayList;
/**
This revised version of the Set<T> generic class uses
the revised version of the Element class, the version
that uses the Object parameter for the abstract method,
equals..
This allows us to reintroduce the isMemberOf method to
the Set data structure.
*/
public class Set <T>
{
// Fields ...
ArrayList<T> theList;
// Will reference an ArrayList of objects
// from the class T.
int currentIndex;
// Index of current element in the set
final int START_CAP = 100;
// Initial capacity of the ArrayList.
// This over-rides the default initial
// capacity of 10.
// Constructor ...
/**
The Set constructor sets up an ArrayList of T references
with STARTSIZE-many cells. It also initializes currentIndex
to -1.
*/
public Set()
{
theList = new ArrayList<T>(START_CAP);
currentIndex = -1;
}
// Test methods
/**
The isEmpty method returns true if the calling object
is empty and false otherwise.
@return true if the calling object is empty and false
otherwise.
*/
public boolean isEmpty()
{
return theList.isEmpty();
}
/**
The isMemberOf method tests to see if the parameter, aT,
is already a member of the Set. This assumes the type
T has an appropriate equals method, redefining the Object
class equals method, so that we are not just comparing
references.
@param anElement the object being checked for membership in
the set
@return true if anElement is already in the set and false
*/
public boolean isMemberOf(T aT)
{
// Local data ...
String paramClass = aT.getClass().getName();
String currClass;
// Logic ...
for (T currT: theList)
{
currClass = currT.getClass().getName();
// Only compare aT against those objects
// that belong to aT's class
if (currClass.equals(paramClass))
{
if (currT.equals(aT))
{
return true;
}
}
}
// No matching object found
return false;
}
// Access methods
/**
The size method returns the number of objects
currently in the set.
@return the value of currentSize
*/
public int size()
{
return theList.size();
}
/**
The getCurrent() method returns a reference to the
current object in the set. Note the pre-condition.
This method should only be called if the set is
not empty. The method advances currentIndex to
the next object to set up for the next call to
getCurrent. If getCurrent returns a copy of
the last object, currentIndex is reset to 0. Note
that this method assumes the type T has a
copy constructor.
Pre: currentIndex is not -1 (which can only
occur if currentSize is not 0).
@return reference to the current object in the set
*/
public T getCurrent()
{
// Local data ...
int saveIndex = currentIndex;
// Logic ...
if (currentIndex == theList.size() - 1)
{
// Recycle to beginning of list
currentIndex = 0;
}
else
{
// Advance currentIndex to next object
currentIndex++;
}
// Return a reference to the current object
return theList.get(saveIndex);
// NOTE: WE ARE RETURNING A REFERENCE
// TO AN OBJECT IN THE SET, NOT A COPY
// OF THE OBJECT. WHY? BECAUSE IF WE
// TRIED TO USE THE clone() METHOD HERE
// THE COMPILER GETS GRUMPY! ALL METHODS
// WE APPLY TO OBJECTS IN THE SET MUST BE
// IN THE Object CLASS.
}
// Mutator methods ...
/**
The add method attempts to add the aT parameter to the set.
It will fail if aT is already in the calling object set.
@param aT the T-thing we attempt to add
@return false if the object is a duplicate and
it couldn't be added and true otherwise
*/
public void add(T aT)
throws DuplicateObjectException
{
// Check if aT is already in the calling object
if (this.isMemberOf(aT))
{
throw new DuplicateObjectException(aT);
}
theList.add(aT);
if (theList.size() == 1)
{
currentIndex = 0;
// Successful add
System.out.println("\nMember successfully added to set.");
}
}
/**
The clear method resets the set to the empty set.
*/
public void clear()
{
currentIndex = -1;
theList = new ArrayList<T>();
}
[B]public void removeObject(T anObject)
throws CannotRemoveException
{
String paramClass = anObject.getClass().getName();
String currClass;
int removalCount = 0;
// Logic ...
for (T currT: theList)
{
currClass = currT.getClass().getName();
if (currClass.equals(paramClass))
{
if (currT.equals(anObject))
{
theList.remove(anObject);
System.out.println("Member found and has been removed from "
+ "the set.");
removalCount++;
}
}
}
if (removalCount == 0)
{
throw new CannotRemoveException();
}
}
[/B]
//Declare universal class display method
public void displayAllInClass(String theClassName)
{
if (theList.isEmpty())
{
System.out.println("There are no members in the set. ");
}
else
{
for (int i = 0; i < theList.size(); i++)
{
if(theClassName.equals(theList.get(i).getClass().getName()))
{
System.out.println(theList.get(i).toString());
System.out.println("\n");
}
}
}
}
// The display method
/**
The display method displays all of the objects in the
set. This method assumes that the class T implements
the toString method in an appropriate manner, so that
the contents of the relevant object can be displayed.
*/
public void display()
{
// Local variables
// we will display
if (theList.isEmpty())
{
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 < theList.size(); i++)
{
System.out.println(theList.get(i).toString());
System.out.println("\n");
}
}
}
}
Okay! So I bolded the areas that are giving me issues. I can't quite figure out the logic for the exceptions in main that will coincide with how I have the function call set up, if that makes sense.
Here's a test run of me attempting to remove a non-existent object:
-----------------------------
Here are your Choices:
Enter 1 to add a Subscriber
Enter 2 to add an Application
Enter 3 to remove a Subscriber
Enter 4 to remove an Application
Enter 5 to display all Subscribers
Enter 6 to display all Applications
Enter 7 to Quit
-----------------------------1
Subscriber's name:
b
Subscriber's url:
b
Subscriber's ad clicks:
123Member successfully added to set.
-----------------------------
Here are your Choices:
Enter 1 to add a Subscriber
Enter 2 to add an Application
Enter 3 to remove a Subscriber
Enter 4 to remove an Application
Enter 5 to display all Subscribers
Enter 6 to display all Applications
Enter 7 to Quit
-----------------------------3
Enter Subscriber's name:
g-----------------------------
Here are your Choices:
Enter 1 to add a Subscriber
Enter 2 to add an Application
Enter 3 to remove a Subscriber
Enter 4 to remove an Application
Enter 5 to display all Subscribers
Enter 6 to display all Applications
Enter 7 to Quit
-----------------------------
It just does nothing.
Here's what happens if I attempt to remove an object that exists in the set:
-----------------------------
Here are your Choices:
Enter 1 to add a Subscriber
Enter 2 to add an Application
Enter 3 to remove a Subscriber
Enter 4 to remove an Application
Enter 5 to display all Subscribers
Enter 6 to display all Applications
Enter 7 to Quit
-----------------------------1
Subscriber's name:
b
Subscriber's url:
b
Subscriber's ad clicks:
123Member successfully added to set.
-----------------------------
Here are your Choices:
Enter 1 to add a Subscriber
Enter 2 to add an Application
Enter 3 to remove a Subscriber
Enter 4 to remove an Application
Enter 5 to display all Subscribers
Enter 6 to display all Applications
Enter 7 to Quit
-----------------------------3
Enter Subscriber's name:
b
Member found and has been removed from the set.
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:819)
at java.util.ArrayList$Itr.next(ArrayList.java:791)
at assignment.pkg3.Set.removeObject(Set.java:199)
at assignment.pkg3.Assignment3.main(Assignment3.java:150)
Java Result: 1
BUILD SUCCESSFUL (total time: 9 seconds)
Whoa! What the heck is that!!??
I am greatly confused here and any guidance would be extremely helpful! If you need any more info I can post more of my code, just let me know! Thank you!