简体   繁体   English

如何从数组列表中取出一个元素并将其分配给另一个数组列表

[英]How to take an element from an array list and assign it into another array list

I have to create a program in java, which is based on an ordering system.我必须在 java 创建一个基于订购系统的程序。 I have the following classes: orders, orders list, supplier, supplier list, payment, payment list.我有以下类:订单、订单列表、供应商、供应商列表、付款、付款列表。 The program is developed in javafx, and the user can create new orders, create suppliers, make payments for the orders based on the id of the order.程序开发于javafx,用户可以根据订单id创建新订单,创建供应商,为订单付款。 But I can not figure out how to assign a supplier to an order.但我不知道如何为订单分配供应商。 The order has to specify what supplier it came from.订单必须指定它来自哪个供应商。

I will make screenshots of my code, I tried writing if else conditions and methods, but they do not work, so some help would be appreciated.我将制作我的代码的屏幕截图,我尝试编写 if else 条件和方法,但它们不起作用,因此将不胜感激。

I have thought about storing suppliers in the list, and then try and modify the method that creates the order to ask for a supplier input based on the stored values, but I can not figure out how to do that.我考虑过将供应商存储在列表中,然后尝试修改创建订单的方法以根据存储的值请求供应商输入,但我不知道该怎么做。

this is the code for the creating orders with my attempt这是我尝试创建订单的代码

static void addHandler(int noOfOrderNrIn, OrderList listIn,int noOfSupplierNrIn, SupplierList slistIn)
{
    
    
    System.out.println("Enter order number: ");
    int orderNrEntered = EasyScanner.nextInt();
    
    System.out.println("Enter name: ");
    String nameOrdEntered = EasyScanner.nextString();
    
    System.out.println("Enter description: ");
    String descEntered = EasyScanner.nextString();
    
    System.out.println("Enter quantity: ");
    String quantityEntered = EasyScanner.nextString();
    
    System.out.println("Enter supplier number: ");
    int  suppNrEntered = EasyScanner.nextInt();
    
    
    //Order o =  new Order(orderNrEntered, nameOrdEntered, descEntered, quantityEntered);
    //listIn.addOrder(o);
    //System.out.println("New order "   +  orderNrEntered +  " successfully added");
    System.out.println("Checking if the supplier exists: ");
    
    
    
    
    
    
 
    if(orderNrEntered < 1 || orderNrEntered > noOfOrderNrIn)
    {
        System.out.println ("There are only "  + noOfOrderNrIn  + " orders");
    } 
    else if(listIn.search(orderNrEntered) !=  null)
    {
        System.out.println("Order  "  + orderNrEntered  + " exists");
        System.out.println("Checking if the supplier exists: ");
    }
    else if(suppNrEntered< 1 || suppNrEntered > noOfSupplierNrIn)
    {
        System.out.println ("There are only "  + noOfSupplierNrIn  + " suppliers");
    }
    else if(slistIn.searchNr(suppNrEntered) !=  null) 
    {
        System.out.println("Supplier  "  + suppNrEntered  + " exists");
        Order o =  new Order(orderNrEntered, nameOrdEntered, descEntered, quantityEntered);
        listIn.addOrder(o);
        System.out.println("New order "     +  orderNrEntered +  " successfully added");
    }
    
    
}

this is the code to add suppliers, the code for adding orders was similar to this one before my attempt这是添加供应商的代码,添加订单的代码在我尝试之前与此类似

  static void addHandler2(int noOfSupplierNrIn, SupplierList listIn)
  {
      
      System.out.println("Enter supplier number: ");
      int supplierNrEntered = EasyScanner.nextInt();
      
      System.out.println("Enter name: ");
      String nameSuppEntered = EasyScanner.nextString();
      
      System.out.println("Enter Address: ");
      String addressEntered = EasyScanner.nextString();
      
      System.out.println("Enter phone number: ");
      String numberEntered = EasyScanner.nextString();
     
   
      if(supplierNrEntered < 1 || supplierNrEntered > noOfSupplierNrIn)
      {
          System.out.println ("There are only "  + noOfSupplierNrIn  + " suppliers");
      } 
      else if(listIn.searchNr(supplierNrEntered) !=  null)
      {
          System.out.println("Supplier  "  + supplierNrEntered  + " exists");
      }
      else  // ok to add an supplier
      {
          Supplier s =  new Supplier(supplierNrEntered, nameSuppEntered,addressEntered, numberEntered);
          listIn.addSupplier(s);
          System.out.println("New Supplier"     +  supplierNrEntered +  " successfully added");
      }
  }

Does a supplier have to be assigned to an order when the order is created?创建订单时是否必须将供应商分配给订单? Or can orders exist without suppliers and be added later?或者订单是否可以在没有供应商的情况下存在并在以后添加?

If orders don't need to have a supplier right away, you can just make a method in your order class that assigns a passed supplier:如果订单不需要立即有供应商,您可以在订单 class 中创建一个方法来分配通过的供应商:

public class Order {
    private Supplier orderSupplier;

    ...

    public void setSupplier(Supplier s) {
        this.orderSupplier = s;
    }
}

If suppliers are assigned at the point in time when orders are created then you can pass a supplier to an order through the order's constructor:如果供应商是在订单创建时分配的,那么您可以通过订单的构造函数将供应商传递给订单:

public class Order {
    private Supplier orderSupplier;

    public Order(Supplier s)
        this.orderSupplier = s;
    }
}

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

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