代码之家  ›  专栏  ›  技术社区  ›  matteo panaro

向列表中添加项目

  •  0
  • matteo panaro  · 技术社区  · 1 年前

    我需要向列表中添加一个类项,必须通过键盘输入添加。我无法添加其属性

    产品类别

    import java.util.*;
    
    public class Product {
        public double price;
        public int quantity;
        private String id_product;
        
        public Prodotto (double price, int quantity, String id_product) {
            this.price = price;
            this.quantity = quantity;
            this.id_product = id_product;
        }
        public double getPrice() {
            return price;
        }
        public int getQuantity() {
            return quantity;
        }
        
        public String getId_product() {
            return id_product;
        }
        
    
    
    }
    
    

    产品列表类

    import java.util.*;
    
    public class ProductList {
        List <Product> productList;
        
        public ProductList() {
            productList = new LinkedList<Product>();
        }
        
        public void addProduct(Product p) {
            productList.add(p);
        }
        
        public int getSize() {
            return productList.size();
        }
        
        public Product getProduct(int i) {
            return productList.get(i);
        }
    
        public void add(String next) {
            
        }
        
        
    }
    
    

    主类仍在开发中,我删除了一些额外的操作,以便让你更清楚,但第一个操作给我带来了麻烦

    import java.util.*;
    
    public class WarehouseManagement {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
        
        ProductList el_product = new ProductList();
        
        Scanner sc = new Scanner(System.in);
        
        boolean stop = false;
        while(!stop) {
            System.out.println("Available Operations:");
            System.out.println("1 - Add Product");
            
            System.out.print("Insert your choice: ");
            int choice = sc.nextInt();
            
            Product [] products
            
            
            switch (choice) {
                case 1:
                    System.out.print("Insert product: ");
                    el_product.add(sc.next());
                    
                    break;
            }
        
            
            }
        }
    
        }
    
    
    

    有了这个,我可以添加一个产品,但不能添加他的价格、数量和Id,我该怎么做?

    2 回复  |  直到 1 年前
        1
  •  2
  •   ControlAltDel    1 年前

    我建议您在Product中添加一个静态方法,通过扫描仪获取输入并返回Product

    public static Product createProduct (Scanner s) {
        double price, 
        int quantity;
        String id_product;
        System.out.println("Enter the price");
        price = s.nextDouble();
        System.out.println("Enter the quantity");
        quantity = s.nextInt();
        System.out.println("Enter the product ID");    
        id_product = s.next();
        return new Product(price, quantity, id_product);
    }
    

    您可能希望进一步细分,以便最初只创建产品id,然后可以创建添加数量或设置价格的方法。

        2
  •  0
  •   Marce Puente    1 年前

    也许这样的东西会帮助你:

    public static void main( String[] args ) {
       ProductList el_product = new ProductList();
       Scanner sc = new Scanner( System.in );
       double price = 0d;
       int quantity = 0;
       String id_product = "";
    
       // we create the string with all messages
       String msgs = "Enter the number of products//product//Enter the price//Enter the quantity//Enter the product ID";
    
       // we create the array of messages
       String messages[] = msgs.split( "//" );
       
       // we ask the user how many products he/she wants to create
       // and instantiate "numberOfProduct" with the obtained value
       System.out.println( messages[ 0 ] );
       int numberOfProduct = Integer.parseInt( sc.nextLine() );
    
       for( int k = 0; k < numberOfProduct; k ++ ) {
    
          // show the second message
          System.out.println( "create the " + ( k + 1 ) + "° " + messages[ 1 ] );
    
          // i = 2 to start displaying from the third message
          for( int i = 2; i < messages.length; i ++ ) {
             while( true ) {
    
                // show the message for each item
                System.out.println( messages[ i ] );
                try {
                   String data = sc.nextLine();
                   if( i == 0 ) {
                      price = Double.parseDouble( data );
                      break;
                   }
                   else if( i == 1 ) {
                      quantity = Integer.parseInt( data );
                      break;
                   }
                   else {
                      id_product = data;
                      break;
                   }
                }
                catch( Exception e ) { }
             }
          }
          // add to the list the new "Product"
          el_product.add( new Product( price, quantity, id_product ));
       }
    }