Question

In: Computer Science

I need to create Create a new empty ArrayList, Ask the user for 5 items to...

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;
}
}

Solutions

Expert Solution

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 *********************************************************


Related Solutions

I need to create a linked list that contains a fixed arraylist. Each new entry is...
I need to create a linked list that contains a fixed arraylist. Each new entry is added to array list. If the arraylist is full, create a new arraylist and add to the linklist. In java please.
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will first enter a password, then enters the same password again; If the second input is the same as first one, the user successfully creates the password. Print “Well done.”; Otherwise, the user will be directed to repeat the whole process (go to step 1.)
Write a Python program to (a) create a new empty stack. Then, (b) add items onto...
Write a Python program to (a) create a new empty stack. Then, (b) add items onto the stack. (c) Print the stack contents. (d) Remove an item off the stack, and (e) print the removed item. At the end, (f) print the new stack contents:
I need to ask a user what numbers they want to enter. They can enter as...
I need to ask a user what numbers they want to enter. They can enter as many as they like. Then inside I need to use conditionals to determine if the numbers are <=20, <=323 && > 30, >200. I can't figure out how to let the user enter as many inputs as they want. I know I need to use a loop to check each number entered and determine if it is small middle or greater but I can't...
IN JAVA PLEASE ASAP !!! I just need the main and mergesort function Ask the user...
IN JAVA PLEASE ASAP !!! I just need the main and mergesort function Ask the user for the number of elements, not to exceed arraySize = 20 (put appropriate input validation) Ask the user for the type of data they will enter - EnglishGrade or MathGrade objects. Use your EnglishGrade and MathGrade classes Based on the input, create an appropriate array for the data to be entered. Write a helper function called recursionMergeSort such that: It is a standalone function...
Create a small program that contains the following. ask the user to input their name ask...
Create a small program that contains the following. ask the user to input their name ask the user to input three numbers check if their first number is between their second and third numbers
I am trying to create new empty dataset with a shape (40, 30) in phyton with...
I am trying to create new empty dataset with a shape (40, 30) in phyton with using pandas. How can I create it?
Language: C# Create a new Console Application. Your Application should ask the user to enter their...
Language: C# Create a new Console Application. Your Application should ask the user to enter their name and their salary. Your application should calculate how much they have to pay in taxes each year and output each amount as well as their net salary (the amount they bring home after taxes are paid!). The only taxes that we will consider for this Application are Federal and FICA. Your Application needs to validate all numeric input that is entered to make...
Using an ArrayList, create a program which does the following: If the user enters quit, the...
Using an ArrayList, create a program which does the following: If the user enters quit, the program ends. If the user enters add followed by a string, program adds the string only to your ArrayList. If the user enters print, the program prints out the ArrayList contents. If the user enters size, the program prints out size of ArrayList, If the user enters remove followed by string, it removes all strings. interface example: **** Welcome to my List Program ****...
I need to only cout "grocery list" if the user actually entered items. If they didnt...
I need to only cout "grocery list" if the user actually entered items. If they didnt enter any items it should just cout "No need for groceries!". Everything else in the program is fine. #include <iostream> #include <vector> using namespace std; // function prototypes char chooseMenu(); vector <string> addItem(vector <string>); void showGroceries(vector <string> list); // main program int main() { vector <string> list; char choice;    cout << "Welcome to Grocery List Manager\n"; cout << "===============================\n"; do{ choice = chooseMenu();...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT