Add search by name
parent
95ad6006fe
commit
fb64d82bb9
|
|
@ -45,16 +45,12 @@ public class Main {
|
|||
}
|
||||
|
||||
public static ArrayList<String> readFile(String path) throws FileNotFoundException {
|
||||
|
||||
ArrayList<String> lines = new ArrayList<>();
|
||||
Scanner sc = new Scanner(new File(path));
|
||||
|
||||
while (sc.hasNextLine()) {
|
||||
lines.add(sc.nextLine());
|
||||
}
|
||||
|
||||
sc.close();
|
||||
|
||||
return lines;
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,6 @@ package org.example;
|
|||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class Shop {
|
||||
|
||||
|
|
@ -41,6 +40,19 @@ public class Shop {
|
|||
return null;
|
||||
}
|
||||
|
||||
public Product getProductByNameSearch(String name) {
|
||||
Product foundProduct = null;
|
||||
for (Product product : products.keySet()) {
|
||||
if (product.name.toLowerCase().contains(name.toLowerCase())) {
|
||||
if (foundProduct != null) {
|
||||
return null;
|
||||
}
|
||||
foundProduct = product;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Product> getProductList() {
|
||||
return products.keySet().stream().toList();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,7 +29,14 @@ public class TUI {
|
|||
case "checkout" -> checkout(null);
|
||||
case "list" -> listProducts(shop.products);
|
||||
case "cart" -> listCart(shop.getCart());
|
||||
case "add" -> shop.addProductToCart(shop.getProductById(Integer.parseInt(tokens[1])));
|
||||
case "add" -> {
|
||||
try {
|
||||
shop.addProductToCart(shop.getProductById(Integer.parseInt(tokens[1])));
|
||||
} catch (NumberFormatException e) {
|
||||
Product productByName = shop.getProductByNameSearch(tokens[1]);
|
||||
shop.addProductToCart(productByName);
|
||||
}
|
||||
}
|
||||
case "remove" -> shop.removeProductFromCart(shop.getProductById(Integer.parseInt(tokens[1])));
|
||||
case "set" ->
|
||||
shop.setProductQuantityInCart(shop.getProductById(Integer.parseInt(tokens[1])), Integer.parseInt(tokens[2]));
|
||||
|
|
|
|||
Loading…
Reference in New Issue