简体   繁体   English

如何在arrayList中搜索然后更改?

[英]How to search in arrayList then change?

So i'm making a supermarket program that allows to add products and sell them, so far i have this:所以我正在制作一个允许添加产品并销售它们的超市程序,到目前为止我有这个:

class Product
{
    String name;
    double price;
    int day;
    int month;
    int year;
    int stock;
    public Product(String name,double price,int day,int month,int year,int stock)
    {
        this.name=name;
        this.price=price;
        this.day=day;
        this.month=month;
        this.year=year;
        this.stock=stock;
    }
}

class SuperMarket{
    protected String name;
    protected int numMax;
    private List<Product> pr;
    
    public SuperMarket(String name,int numMax)
    {
        this.name=name;
        this.numMax=numMax;
        this.pr = new ArrayList<Product>();
        
    }
    public void addProduct() {
       //deleted to post here
    }
    
    public void sellProduct()//its here i need help
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("What product do you want to sell?");
        String name=sc.nextLine();
                
    }
    
}

I´d like to know how to search in Product list by name and then change the stock(subtract-n) to sell n of that product.我想知道如何按名称在产品列表中搜索,然后更改库存(减去 n)以销售该产品的 n。

You can use Stream API to find out the product by name.您可以使用 Stream API 按名称查找产品。 Filter the list by checking the name of product and get first match.通过检查产品名称过滤列表并获得第一个匹配项。

Optional<Product> product = productList.stream()
                                       .filter(e -> e.name.equals(inputedName))
                                       .findFirst();

Then can check is product found then update the stock然后可以检查是否找到产品然后更新库存

if(product.isPresent()){
   Product p = product.get();
   p.stock = p.stock - numberOfSoldProduct;
}

Suggestion to use getter/setter for fields.建议对字段使用 getter/setter。

In case if you are looking to get search using O(1), you can use map.如果您希望使用 O(1) 进行搜索,则可以使用 map。 however i don't see necessary of list though.但是我认为没有必要列出清单。

public SuperMarket(String storeName,int storeId){
    this.storeName = storeName;
    this.storeId = storeId;
    this.products = new ArrayList<Product>();
    this.productByName = new HashMap<String, Product>();
}

public void addProduct(Product product) {
    String productName = product.name;
    if(productByName.containsKey(productName)){
        Product existingProduct = productByName.get(productName);
        existingProduct.stock += product.stock;
        existingProduct.price = product.price;
    }else{
        productByName.put(productName, product);
        products.add(product);
    }
}

public void sellProduct(String productName, int count){
    if(!productByName.containsKey(productName)){
        System.out.println(productName + " is unavailable !!!");
        return;
    }
    Product existingProduct = productByName.get(productName);
    existingProduct.stock -= count;
    if(existingProduct.stock <= 0){
        productByName.remove(productName);
        products.remove(existingProduct);
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM