Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.cieslin;
- import java.util.Scanner;
- public class Main {
- private static Scanner scanner = new Scanner(System.in);
- private static GroceryList groceryList = new GroceryList();
- public static void main(String[] args) {
- groceryList.addGroceryItem("Bread");
- groceryList.addGroceryItem("Cheess");
- groceryList.addGroceryItem("Milk");
- groceryList.printGroceryList();
- groceryList.addGroceryItem("Egg");
- groceryList.printGroceryList();
- groceryList.modifyGroceryItem(1, "Water");
- System.out.println(groceryList.findItem("Egg"));
- groceryList.printGroceryList();
- groceryList.removeGroceryItem(1);
- groceryList.removeGroceryItem(2);
- groceryList.printGroceryList();
- groceryList.removeGroceryItem(1);
- groceryList.printGroceryList();
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////
- package com.cieslin;
- import java.util.ArrayList;
- public class GroceryList {
- private ArrayList<String> groceryList = new ArrayList<String>();
- 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);
- }
- public void removeGroceryItem(int position){
- groceryList.remove(position-1);
- }
- public String findItem(String searchItem){
- //boolean exists = groceryList.contains(searchItem);
- int position = groceryList.indexOf(searchItem);
- if(position >= 0)
- return groceryList.get(position);
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement