Skip to content

Instantly share code, notes, and snippets.

@un4ckn0wl3z
Created August 23, 2017 17:21
Show Gist options
  • Save un4ckn0wl3z/8e72675d77f1cc5599d19fd2cc4c7472 to your computer and use it in GitHub Desktop.
Save un4ckn0wl3z/8e72675d77f1cc5599d19fd2cc4c7472 to your computer and use it in GitHub Desktop.
package com.un4;
import java.util.ArrayList;
/**
* Created by UN4CKN0WL3Z on 23/08/2017.
*/
public class GroceryList {
private ArrayList<String> groceryList = new ArrayList<>();
public void addGroceryItem(String item){
groceryList.add(item);
}
public void printGroceryList(){
System.out.println("You have " + groceryList.size() +
" items in your grocery list");
for (int i=0;i<groceryList.size();i++){
System.out.println((i+1) + ". " + groceryList.get(i));
}
}
public void modifyGroceryItem(int position,String newItem){
groceryList.set(position,newItem);
System.out.println("Grocery item " + (position+1) + " has been modified.");
}
public void removeGroceryItem(int position){
String theItem = groceryList.get(position);
groceryList.remove(position);
System.out.println("Grocery item " + theItem + " in position "+(position+1) + " has been deleted.");
}
public String findItem(String searchItem){
// boolean exists = groceryList.contains(searchItem);
int position = groceryList.indexOf(searchItem);
if(position>= 0){
return groceryList.get(position);
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment