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.