In: Computer Science
I need to create Create a new empty ArrayList, Ask the user for 5 items to add to a shopping list and add them to the ArrayList (get from user via the keyboard). Prompt the user for an item to search for in the list. Output a message to the user letting them know whether the item exists in the shopping list. Use a method that is part of the ArrayList class to do the search. Prompt the user for an item to delete from the shopping list and remove it. (be sure to handle the case where they don’t want to delete anything). Use a method that is part of the ArrayList class to do the delete. Prompt the user for an item to insert into the list. Ask them what they want to insert and where they want to insert it (after which item). Use a method that is part of the ArrayList class to do the insertion. Output the final list to the user. It keeps giving me cannot define errors import java.util.ArrayList; import java.util.Scanner;
I keep getting cannot define errors
import java.util.ArrayList;
import java.util.Scanner;
public class ShoppingList {
private static Scanner scanner = new
Scanner(System.in);
private static ShoppingList shoppingList = new
ShoppingList();
public static void main (String[] args) {
boolean quit = false;
int choice = 0;
printInstructions();
while(!quit) {
System.out.println("Enter your choice: ");
choice =
scanner.nextInt();
scanner.hasNextLine();
switch(choice)
{
case 0:
printInstructions();
break;
case 1:
shoppingList.getShoppingList();
break;
case 2:
addItem();
break;
case 3:
modifyItem();
break;
case 4:
removeItem();
break;
case 5:
searchForItem();
break;
case 6:
processArrayList();
break;
case 7:
quit = true;
break;
}
}
}
public static void
printInstructions() {
System.out.println("\nPress");
System.out.println("\t - To Print The Instructions");
System.out.println("\t - To Add Item");
System.out.println("\t - To Modify Item");
System.out.println("\t - To Remove Item");
System.out.println("\t - To Search for an Item");
System.out.println("\t - To Process Array List");
System.out.println("\t - To Not Continue");
}
public static void addItem()
{
System.out.print("Add : Shopping Item: ");
shoppingList.addShoppingItem(scanner.nextLine());
}
public static void modifyItem()
{
System.out.print("Modify: Enter the Item: ");
String curItem =
scanner.nextLine();
System.out.print("Modify: Replace with: ");
String newItem =
scanner.nextLine();
shoppingList.modifyShoppingItem(curItem, newItem);
}
public static void searchForItem()
{
System.out.print("Item Search: ");
String
searchItem = scanner.nextLine();
if(shoppingList.onFile(searchItem)) {
System.out.println("Item " + searchItem + " is
on the list");
}
else {
System.out.println(searchItem + "Is not
found");
}
}
public static void
processArrayList() {
ArrayList<String> newArrayList = new
ArrayList<String>();
newArrayList.addAll(shoppingList.getShoppingList());
ArrayList<String> anotherArrayList = new
ArrayList<>(shoppingList.getShoppingList());
String[] myArray
= new String[shoppingList.getShoppingList().size()];
myArray =
shoppingList.getShoppingList().toArray(myArray);
}
public static void setShoppingList(ArrayList<String>
shoppingList) {
this.shoppingList = shoppingList;
}
public void getShoppingList() {
System.out.println("You have " + shoppingList.size() +
" items")
for(int i=0; i< shoppingListsize(); i++) {
System.out.println((i+1) + " . " +
shoppingList.get(i));
}
}
public void modifyShoppingItem(String currentItem, String newItem)
{
int position = findItem(currentItem);
if(position >= 0) {
modifyShoppingItem(position,
newItem);
}
}
private void modifyShoppingItem(int position, String item) {
shoppingList.set(position, item);
System.out.println("Shopping Item " + (item) + " has
been modified");
}
public void removeItem(String item) {
int position = findItem(item);
if(position >= 0) {
removeItem(position);
}
}
private void removeItem(int position) {
shoppingList.removeItem(position);
}
private int findItem(String searchItem) {
return shoppingList.indexOf(searchItem);
}
public boolean onFile(String item) {
int position = findItem(item);
if(position >= 0) {
return true;
}
return false;
}
}
Item.java
public class Item {
private int id;
private String name;
private double price;
public Item()
{
this.id = 0;
this.name = "";
this.price = 0;
}
public Item(int id, String name, double price)
{
this.id = id;
this.name = name;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString()
{
return("ID: " + this.id + ", Item Name: " + this.name + ", Price:
$" + String.format("%.2f", this.price));
}
}
ShoppingList.java (Driver class)
import java.util.ArrayList;
import java.util.Scanner;
public class ShoppingList {
private ArrayList<Item> items;
public ShoppingList()
{
this.items = new ArrayList<>();
}
public void addItem()
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter item ID: ");
int id = Integer.parseInt(sc.nextLine().trim());
System.out.print("Enter item name: ");
String name = sc.nextLine().trim();
System.out.print("Enter item price: ");
double price = Double.parseDouble(sc.nextLine().trim());
items.add(new Item(id, name, price));
System.out.println("\n" + name + " successfully added to the
list.\n");
}
public void modifyItem()
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter item ID: ");
int id = Integer.parseInt(sc.nextLine().trim());
boolean found = false;
int index = -1;
for(int i = 0; i < items.size(); i++)
{
if(items.get(i).getId() == id)
{
found = true;
index = i;
break;
}
}
if(!found)
System.out.println("\nNo such item found with ID: " + id +
".\n");
else
{
System.out.print("Item confirmed...\nEnter new name for the item:
");
String name = sc.nextLine().trim();
System.out.print("Enter the new price for the item: ");
double price = Double.parseDouble(sc.nextLine().trim());
items.get(index).setName(name);
items.get(index).setPrice(price);
System.out.println("\nModified Item Details:\n" +
items.get(index).toString() + "\n");
}
}
public void removeItem()
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter item ID: ");
int id = Integer.parseInt(sc.nextLine().trim());
boolean found = false;
int index = -1;
for(int i = 0; i < items.size(); i++)
{
if(items.get(i).getId() == id)
{
found = true;
index = i;
break;
}
}
if(!found)
System.out.println("\nNo such item found with ID: " + id +
".\n");
else
{
String name = items.get(index).getName();
items.remove(items.get(index));
System.out.println(name + " has been successfully removed from the
list.\n");
}
}
public void searchItem()
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter item ID: ");
int id = Integer.parseInt(sc.nextLine().trim());
boolean found = false;
int index = -1;
for(int i = 0; i < items.size(); i++)
{
if(items.get(i).getId() == id)
{
found = true;
index = i;
break;
}
}
if(!found)
System.out.println("\nNo such item found with ID: " + id +
".\n");
else
System.out.println("\nMatch found!\n" + items.get(index).toString()
+ "\n");
}
public void displayAllItems()
{
if(items.isEmpty())
System.out.println("\nNo items in the list to display!\n");
else
{
System.out.println("\n*** ALL ITEMS IN THE LIST
***\n-----------------------------");
for(Item item : items)
{
System.out.println(item.toString());
}
System.out.println();
}
}
// MAIN FUNCTION
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
ShoppingList list = new ShoppingList();
int choice;
do
{
printMenu();
choice = Integer.parseInt(sc.nextLine());
switch(choice)
{
case 1:
{
list.addItem();
break;
}
case 2:
{
list.modifyItem();;
break;
}
case 3:
{
list.removeItem();
break;
}
case 4:
{
list.searchItem();
break;
}
case 5:
{
list.displayAllItems();
break;
}
case 6:
{
System.out.println();
break;
}
case 7:
{
System.out.println("\nThank you!\nGood Bye.\n");
System.exit(0);
}
default:
System.out.println("\nInvalid choice!\n");
}
}while(choice != 7);
}
private static void printMenu()
{
System.out.print("1. Add an item\n"
+ "2. Modify an item\n"
+ "3. Remove an item\n"
+ "4. Search for an item\n"
+ "5. Display all items\n"
+ "6. Print menu\n"
+ "7. Exit\nEnter your choice: ");
}
}
****************************************************************** SCREENSHOT *********************************************************