Achei interessante representar o estoque como uma classe :
Stock :
public class Stock {
private static List<Product> STORAGE= new ArrayList<>();
public static void addProduct(Product product){
STORAGE.add(product);
}
public static void removeProduct(Product product){
Optional<Product> productToRemove = STORAGE.stream()
.filter(p -> p.getName().equals(product.getName()))
.findFirst();
productToRemove.ifPresent(p -> STORAGE.remove(p));
}
}
Product :
public class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
if(price < 0 ){
System.out.println("Negative values are not allowed. Price set to 0");
this.price = 0;
}else{
this.price = price;
}
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
Main :
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(true){
System.out.println("Enter the product name : ");
String productName = scanner.nextLine();
System.out.println("Enter the prduct price : ");
double productPrice = scanner.nextDouble();
scanner.nextLine();
Stock.addProduct(new Product(productName,productPrice));
System.out.println("Do you want to store something else ?");
String answer = scanner.nextLine();
if(answer.equals("no")){
break;
}
}
}