Skip to content

Mini Project – Exercise 3

Making a grocery store list

  • Project
  • 1 hint
  • 15 of 15 in this chapter

Getting Started

Make a java file with the name GroceryStoreExample. Import ArrayList and Scanner to the java file with these two lines at the top...

Java
import java.util.ArrayList;
import java.util.Scanner;

What we want to do

We want to create a grocery list that has 4 simple commands. We want to be able to add items to the grocery list by simply typing the item name. We want to be able to remove items from the list by typing ‘remove’. We want to be able to clear every item from the list by typing ‘clear’. We want to be able to stop the program by typing ‘stop.’ Lastly we do not want any command to be case sensitive. Meaning that we should be able to type ‘sToP’ and the program should stop.

Here is what an example output should look like...

Java
Enter an item, or type 'remove','clear','display, or 'stop' to stop adding to the list:
milk
Enter an item, or type 'remove','clear','display, or 'stop' to stop adding to the list:
eggs
Enter an item, or type 'remove','clear','display, or 'stop' to stop adding to the list:
cheese
Enter an item, or type 'remove','clear','display, or 'stop' to stop adding to the list:
corn
Enter an item, or type 'remove','clear','display, or 'stop' to stop adding to the list:
stop
These are the Items in your grocery list:
milk
eggs
cheese
corn

We should be able to display and remove an element from the list. As well as clear the list...

Java
Enter an item, or type 'remove','clear','display, or 'stop' to stop adding to the list:
display
These are the items in your Grocery list:
milk
bread
cheese
Enter an item, or type 'remove','clear','display, or 'stop' to stop adding to the list:
remove
These are your items what would you like to remove:
milk
bread
cheese
I want to remove bread
Enter an item, or type 'remove','clear','display, or 'stop' to stop adding to the list:
display
These are the items in your Grocery list:
milk
cheese
Enter an item, or type 'remove','clear','display, or 'stop' to stop adding to the list:
clear
Enter an item, or type 'remove','clear','display, or 'stop' to stop adding to the list:
display
These are the items in your Grocery list:
Enter an item, or type 'remove','clear','display, or 'stop' to stop adding to the list:
Stop
These are the Items in your grocery list:

Hints

Hint 1Sections to review

You should review: ArrayList, Scanner, if/else statements, for loops, and strings. They will prove to be very helpful.

Solution

The Solution

This is the solution...

Java
import java.util.ArrayList;
import java.util.Scanner;
public class GroceryStoreExample {
 public static void main(String[] args) {
 ArrayList groceryList = new ArrayList();
 Scanner input = new Scanner(System.in);
 String groceryItem = "";
 while(!groceryItem.equalsIgnoreCase("Stop")){
 System.out.println("Enter an item, or type 'remove','clear','display, or 'stop' to stop adding to the list:");
 groceryItem = input.nextLine();
 if(groceryItem.equalsIgnoreCase("Stop")){
 System.out.println("These are the Items in your grocery list:");
 for (String i : groceryList) {
 System.out.println(i);
 }
 }
 else if(groceryItem.equalsIgnoreCase("Remove")){
 System.out.println("These are your items what would you like to remove:");
 for (String i : groceryList) {
 System.out.println(i);
 }
 groceryItem = input.nextLine();
 if(!groceryItem.equalsIgnoreCase("Stop")){
 boolean foundItem = false;
 for (int i = 0; i < groceryList.size(); i++) {
 String compare = groceryList.get(i);
 if(compare.equalsIgnoreCase(groceryItem)){
 groceryList.remove(i);
 foundItem = true;
 }
 }
 if(foundItem == false){
 System.out.println("Sorry that Item does not exist");
 }
 }
 else{
 System.out.println("These are the items in your grocery list");
 for (String i : groceryList) {
 System.out.println(i);
 }
 }
 }
 else if(groceryItem.equalsIgnoreCase("Display")){
 System.out.println("These are the items in your Grocery list:");
 for (String i : groceryList) {
 System.out.println(i);
 }
 }
 else if(groceryItem.equalsIgnoreCase("Clear")){
 groceryList.clear();
 }
 else{
 groceryList.add(groceryItem);
 }
 }

 }
} 

That was pretty long. It can be simplified with object oriented programming, which we are doing next.

Give the project a try first.