【问题标题】:Java arraylist add the qtyJava arraylist 添加数量
【发布时间】:2023-03-05 09:32:02
【问题描述】:

我在比较从另一个类导入的 myProduct.setRefno(product.getRefno()) 的属性以及描述、价格和数量时遇到问题。我需要能够输入refno,如果篮子容器中存在参考编号,那么我只添加数量而不是所有项目详细信息:

目前程序工作如下:

ref1 description1 price1 1(qty)
ref2 description2 price2 1(qty)
ref1 description1 price1 1(qty)

但我希望它是:

ref1 description1 price1 2(qty)
ref2 description2 price2 1(qty)

如果它是相同的参考编号,那么它只添加数量。

public class Zapper {

    public static void main(String[] args) throws ItemException {
        System.out.println("\n\nThis is Purchases\n\n");

        Products stock=new Products();// Define Variable 
        Products basket=new Products();// Define Variable 
        Purchase product;
        String refno;
        int offer;
        int count;
        int grandtotal;         
        char option;//char variable option

        boolean finished=false;//variable "boolean" set 

        while (!finished) { 
            try {
                option=Console.askOption("\n A)dd P)rint R)emove Q)uit");
                stock.open("stock.lin");
                switch (option) {   
                    case 'A': 
                        product= new Purchase();
                        refno= Console.askString("Enter Ref No:..");
                        product=(Purchase)stock.find(refno);//cast operator 
                        if ( product == null) {
                            System.out.println("Cannot find Ref No");
                        } else {
                            product.print("");
                            Purchase myProduct = new Purchase();
                            myProduct.setRefno(product.getRefno());
                            myProduct.setDescription(product.getDescription());
                            myProduct.setPrice(product.getPrice());
                            myProduct.setQty(1);
                            myProduct.setOffer(product.getOffer());
                            basket.add(myProduct);//add the value of item into Container stock  
                        }
                        break;//end of case statement Add

                    case 'R': 
                        refno= Console.askString("Enter Ref No:..");
                        Product myProduct = new Product();
                        myProduct=(Purchase)basket.find(refno);//cast operator
                        myProduct.setQty(1);
                        if ( myProduct == null)
                            System.out.println("Cannot find Ref No");

                        else {
                            basket.remove(myProduct);
                            basket.print("");   
                        }
                        break;//end of case statement Find

                    case 'P': 
                        basket.print("\nYou have purchased...");
                        break;//end of case statement Print             
                    case 'Q': 
                        finished=true;
                        break;//end of case statement "Q" 
                    case '\0':/*Do Nothing*/
                        break;//end of case statement "Do Nothing"
                    default:    
                        System.out.println("Error: Invalid Option ");
                        break;//end of case statement default
                }
            } catch (ItemException e) {
                System.out.println(e.getMessage());
            } catch (IOException e) {
                System.out.println(e.getMessage());
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }   
        }
        System.out.println("\n\nPurchases Finished \n\n");
    }

}

【问题讨论】:

    标签: java arrays if-statement add


    【解决方案1】:

    您只需更改您的Products 类中的add-方法。假设您将 Purchase-objects 存储在 Product 类的 List 中,它可能看起来像这样:

    private List<Purchase> products; 
    
    public void add(Purchase product) {
        String refNo = product.getRefno();
        for (Purchase p : this.products) { //for every product
            if (p.getRefno().equals(refNo)) { //if two refNumbers equals
                p.setQty(p.getQty() + product.getQty()); //add the desired quantity
                return; //force method to abort
            }
        }
        this.products.add(product); //otherwise, add the new product
    }
    

    尽管如此,我不得不说我发现你的一些类名称有点不寻常。请记住,它们应该始终对它们实际代表的内容给出一个很好的提示,例如,您的Purchase 类看起来更像Product。 :)

    【讨论】:

      【解决方案2】:

      如果您使用一些适当的 OOP 编码技术,您的很多问题都会迎刃而解。

      让我们从类的结构开始。

      public class Zapper {
          public static void main(String[] args) throws ItemException {
              System.out.println("\n\nThis is Purchases\n\n");
      
              Products stock=new Products();// Define Variable 
              Products basket=new Products();// Define Variable 
              Purchase product;
              String refno;
              int offer;
              int count;
              int grandtotal;         
              char option;//char variable option
      
              //Do the work...
          }
      }
      

      首先,在任何情况下,main 都不应该抛出异常,更不用说像 ItemException 这样的特定异常。所有这些事情都应该由程序优雅地处理。其次,您实例化了一组真正应该作为成员字段保存在类中的对象,Zapper

      这可能更符合您的要求:

      public class Zapper {
          //These things will stay throughout the program
          private Products stock = new Products();
          private Products basket = new Products();
          private Purchase product;
          private boolean quitSignalReceived = false;
          private Set<Option> options;//A list of keyboard inputs
      
          public static void main(String[] args) {
              System.out.println("\n\nInitiating Purchase of items\n\n"); //use a proper entrance message to your program
      
              Zapper zapper = new Zapper();//Create an object! Sets up all class variables on construction.
              zapper.run();//Let a method handle running the program.
      
              System.out.println("\n\nPurchases Finished \n\n");//Say goodbye!
          }
      
          public void run() {
              //actually does the work!
          }
      }
      

      现在您只需要关注run() 所做的事情。具体来说,它处理您的“主循环”:

      public void run() {
          boolean finished = false;
          stock.open("stock.lin"); //Should happen every time we enter the run() loop
          while (!finished) {
            option=Console.askOption("\n A)dd P)rint R)emove Q)uit");
            processOption(option);
            if (quitSignalReceived) {
                //Do anything that MUST happen before the program quits.
                finished = true;
            }
          }
      }
      

      您已经敏锐地注意到此时我们需要在其中添加选项并对其进行处理。

      public Zapper() {
         this.options.add(new AddOption());
         this.options.add(new QuitOption());
         //etc.
      }
      
      public class Option {
        //Constructor, etc.
      
        process(String option) {
          for (Option o : options) {
            if (o.getKey.equals(option)) {
              o.process(this);
            }
          }
        }
      }
      

      这自然需要一个抽象类 Option,您将其子类化:

      public abstract class Option {
        public String getKey();//returns the keyboard key associated with this option
        public void process(Zapper z); //actually does what you need the option to do. 
      }
      

      让我们考虑一下您的“A”案例:

      case 'A': 
                          product= new Purchase();
                          refno= Console.askString("Enter Ref No:..");
                          product=(Purchase)stock.find(refno);//cast operator 
                          if ( product == null) {
                              System.out.println("Cannot find Ref No");
                          } else {
                              product.print("");
                              Purchase myProduct = new Purchase();
                              myProduct.setRefno(product.getRefno());
                              myProduct.setDescription(product.getDescription());
                              myProduct.setPrice(product.getPrice());
                              myProduct.setQty(1);
                              myProduct.setOffer(product.getOffer());
                              basket.add(myProduct);//add the value of item into Container stock  
                          }
                          break;//end of case statement Add
      

      首先,你new() 你的产品,它实例化了一个对象。两行之后,您将变量完全设置为另一个对象。其次,您从不返回 Purchase 对象的方法中获取该对象,您应该不惜一切代价避免这种情况,并且至少封装以防将来发生更改。然后你有一个if 切换到null - 这是你应该始终避免的另一种做法。

      在添加产品的情况下,您希望 Option 子类的 process 方法如下所示。

      public void process (Zapper zap) {
          refno= Console.askString("Enter Ref No:..");
          Purchase stockItem;
          bool success = zap.getPurchase(refno, item);
          if ( !success ) {
              System.out.println("Item not in stock.");//Errors should make sense!
          } else {
              zap.addToBasket(stockItem);
          }
      }
      

      这需要将以下方法添加到Zapper

      public bool findPurchase(String refno, Purchase item) {
          item = this.stock.find(refno);
          if (item == null) { return false; }
      }
      
      public void addToBasket(Purchase item) {
          //actually do the work to add it to your purchases.
      }
      

      对于产品:

      //This method copies the object into a new object and returns it.
      public Purchase getPurchaseItem() {
          Purchase myProduct = new Purchase();
          myProduct.setRefno(product.getRefno());
          myProduct.setDescription(product.getDescription());
          myProduct.setPrice(product.getPrice());
          myProduct.setQty(1);
          myProduct.setOffer(product.getOffer());
      }
      

      现在,请注意,如果您更改此设置,则会为您完成大量工作: 私有产品库存 = 新产品(); 私有产品篮 = new Products();

      对此: 私有地图库存 = new HashMap(); 私有地图篮 = new HashMap();

      在这种情况下,您对 increment-or-add 的调用如下所示:

      if (basket.containsKey(item)){
          int quantity = basket.get(item) + 1;
          basket.set(item, quantity);
      } else {
          basket.set(item, 1);
      }
      

      虽然这很长,但它涉及到许多可以清理此代码的方法,将责任放在它所属的地方和最简单的地方,并解决你的问题。如果Map 类不足以满足您的需求,您可能会考虑这样一种情况:您有一个包含库存商品的Item 类和一个扩展该商品的PurchasedItem 类,这就是您购买的东西。那完全是另一回事了。

      【讨论】:

        猜你喜欢
        • 2016-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多