import java.util.Scanner;
public class TestGroceryList
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the maximum number of items " +
"grocery list can hold: " );
int n = keyboard.nextInt();
keyboard.nextLine(); // clears buffer
GroceryList shoppingList = new GroceryList(n);
System.out.print("Enter grocery items, one item per line. ");
System.out.println("Enter 'quit' to quit");
String item = keyboard.nextLine();
while ( item.compareTo("quit") != 0)
{
try
{
shoppingList.add(item);
}
catch( ListFullException e)
{
System.out.println( e);
}
item = keyboard.nextLine();
}
System.out.println("My Shopping List \n" + shoppingList);
}
}
/*---------Sample Run --------------jGRASP exec: java TestGroceryList
Enter the maximum number of items grocery list can hold: 4
Enter grocery items, one item per line. Enter 'quit' to quit
apples
oranges
cereal
bread
milk
ListFullException: List is full. Cannot add milk
tea
ListFullException: List is full. Cannot add tea
quit
My Shopping List
apples
oranges
cereal
bread
*/
Test Grocery List
of 1
Report
Tell us what’s wrong with it:
Thanks, got it!
We will moderate it soon!
Free up your schedule!
Our EduBirdie Experts Are Here for You 24/7! Just fill out a form and let us know how we can assist you.
Take 5 seconds to unlock
Enter your email below and get instant access to your document