0

The idea is I'm making a little restaurant app in Java so in this class is given the option to make an order. It works this way: the user writes which products want and they are stored in the ArrayList. I'd like to know how to compare the products stored in the ArrayList with the data I have in my MySQL table. Moreover, what's stored in the ArrayList is a row of the table and I'd like to get another row from the table which coincides with the product stored in the ArrayList.

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

public class Order {
        Scanner in = new Scanner(System.in);
        String product;
        String deleteProduct;
        String answer;
        String answer2;
        ArrayList<String> order;
        boolean keepOrdering = true;
        Welcome welcome;
        
        public void Run() {
            order = new ArrayList<String>();
            while(keepOrdering) {
                AddProducts();
                System.out.println("Do you want to add or delete a product?");
                answer = in.nextLine();
                if(answer.equalsIgnoreCase("Yes") {
                    System.out.println("Specify if you want to add or delete a product: ");
                    answer2 = in.nextLine();
                    if(answer2.equalsIgnoreCase("Add")) {
                        AddProducts();
                        Check();
                    }
                    else {
                        DeleteProducts();
                        Check();
                    }
                }
                else {
                    Check();

                    
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    welcome = new Welcome();
                    welcome.Run();
                }
                
            }
            
        }

        
        public void AddProducts() {
            System.out.println("Write down the products you want: ");
            System.out.println("Write STOP to stop");
            do {
                product = in.nextLine();
                order.add(product);
            }while(!(product).equalsIgnoreCase("STOP"));
        }
        
        public void DeleteProducts() {
            System.out.println("Write down the products you want to delete: ");
            System.out.println("Write STOP to stop");
            do {
                deleteProduct = in.nextLine();
                if(order.contains(deleteProduct)) {
                    order.remove(deleteProduct);
                }
                else {
                    System.out.println("The product " + deleteProduct + " it's not in the order");
                }
            }while(!(deleteProduct).equalsIgnoreCase("STOP"));
        }

        public void Check() {
            keepOrdering = false;
            System.out.println("Your order has: ");
            for(int i=0; i<order.size(); i++) {
                order.remove("STOP");
                System.out.println(order.get(i));
            }
            
            System.out.println("¡Thank you for ordering!");
        }
}

MySQL table is:

    create table product
(
    product_id int NOT NULL AUTO_INCREMENT,
    name varchar(50) DEFAULT NULL,
    description varchar(500),
    price double DEFAULT NULL,
    category_id int DEFAULT NULL,
    constraint PJ_product PRIMARY KEY (product_id),
    constraint FK_product_category FOREIGN KEY (category_id) REFERENCES category(category_id)
);

In the ArrayList I store product which I'd like to compare with the data stored in the name column, I'd also like to display the data stored in price which corresponds to name column.

  • 1
    Do you want to send your list to MySQL and retrieve the list which contains only the values which are present in the table? or all values with additional column with presence flag? Or maybe you want to retrieve all existing values from MySQL and compare on Java side? – Akina May 13 '22 at 05:49
  • I want to compare the data stored in nombre column with the content of the ArrayList – alexquilis1 May 14 '22 at 10:56
  • Not using English field names doesn't make it any easier to read. – Martin Zeitler May 14 '22 at 11:19
  • @MartinZeitler changed it! – alexquilis1 May 14 '22 at 12:48
  • you could use `IN` clause in your query condition to filter `product` that have matching name in the `ArrayList`. see [previous QA](https://stackoverflow.com/a/3107059) for reference. hope it helps. – Bagus Tesa May 14 '22 at 12:57

0 Answers0