如果用户输入不满足条件,则再次提示用户输入(数组列表)

时间:2018-07-12 13:53:59

标签: java

我是Java新手。这是一个createOrder类,该类允许用户对用户从列表中选择的项目数组进行排序。主要问题是当条件为false时,我无法提示用户重新输入itemID。问题详细信息在下面列出。

标识符和初始化

public static void createOrder(ArrayList <item> list, ArrayList <Stock> stock)
    {
                String addNextItem, itemID = "", invoiceNo = "", idLetter = "";
                final String INVLETTER = "INV";
                double itemPrice = 0.0, totalPrice = 0.0;
                int orderQuantity = 0, idDigit = 0, invDigit = 0, len = stock.size(), itemlen = list.size(), quantityOH = 0, j,k;
                InvoiceNo invoice = null;
                invoice = new InvoiceNo(INVLETTER, invDigit);

我的do while循环,用于确定用户是否要再次下订单。

        do
        {
                //enter invoice no
                checkStockItem(stock); 
                System.out.print("Order No: ");
                System.out.println(invoice.toString());

                System.out.print("Please enter item code: ");
                Scanner input = new Scanner(System.in);
                itemID = input.nextLine();
                idLetter = itemID.replaceAll("[\\d]", "");
                idDigit = Integer.parseInt(itemID.replaceAll("[\\D]", ""));
                ItemNo no = new ItemNo(idLetter, idDigit);

这就是我被困的地方。目前,我正在创建此for循环的目的是检查是否:
1.用户输入与stockItemID(来自另一个类)相匹配。
2.用户已超过其订单配额(一个订单中最多10个商品)。
3.在相同的顺序中有重复项(如果找到,则合并重复项)

                    //checks if item number does not match with user input
                    //if yes list out item details for every user input
                    //if no, user has to input itemID again to for validation.

                for(k = 0; k < itemlen; k++)
                {
            if(!list.get(k).getItemID().getItemNo().equals(no.getItemNo()))
                    {
                        for(j = 0; j < len; j++)
                        {
                            if(stock.get(j).getItemNo().getItemNo().equals(no.getItemNo()))
                            {
                                System.out.println("Item Description: [" + stock.get(j).getItemDescription() + "]");
                                System.out.println("Quantity on Hand: [" + stock.get(j).getQuantityOnHand()+ "]");
                                System.out.println("Unit Price: [" + stock.get(j).getUnitPrice()+ "]");
                                System.out.println("Item Description: [" + stock.get(j).getItemDescription() + "]");
                            }
                        }
                    }
                }

                    else
                    {
                        //i want to prompt user input for itemID again if failed
                    }



                //set quantity
                System.out.print("Enter item quantity: ");
                orderQuantity = input.nextInt();
                if(orderQuantity < 1)
                {
                    System.out.println("Please enter valid quantity");
                    orderQuantity = input.nextInt();
                }
                item i = new item(no, invoice, itemPrice, orderQuantity, totalPrice, quantityOH);
                list.add(i);
                //asking if the user need to input another item
                System.out.println("Do you want to add next Item Y/N: ");
                Scanner input2 = new Scanner(System.in);
                addNextItem = input2.next();
                if(!"Y".equals(addNextItem.toUpperCase())&& !"N".equals(addNextItem.toUpperCase()))
                {   
                    System.out.println("Invalid character! Only Y/N");
                }
            }while ("Y".equals(addNextItem.toUpperCase())); //user will continue to add item until addNextItem=='Y'
        }

1 个答案:

答案 0 :(得分:0)

我不确定我是否完全理解您的问题。这样的事情会成功吗?

do
{
    //enter invoice no
    checkStockItem(stock); 
    System.out.print("Order No: ");
    System.out.println(invoice.toString());
    Scanner input = new Scanner(System.in);

    // Get a valid itemID
    Boolean validId = true;
    do
    {
        System.out.print("Please enter item code: ");
        itemID = input.nextLine();
        idLetter = itemID.replaceAll("[\\d]", "");
        idDigit = Integer.parseInt(itemID.replaceAll("[\\D]", ""));
        ItemNo no = new ItemNo(idLetter, idDigit);
        //checks if item number does not match with user input
        //if yes list out item details for every user input
        //if no, user has to input itemID again to for validation.

        for(k = 0; k < itemlen; k++)
        {
            if (list.get(k).getItemID().getItemNo().equals(no.getItemNo()))
            {
                validId = false;
            }
        }
    } while (!validId);

    // We now have a valid itemID
    for(j = 0; j < len; j++)
    {
        if(stock.get(j).getItemNo().getItemNo().equals(no.getItemNo()))
        {
            System.out.println("Item Description: [" + stock.get(j).getItemDescription() + "]");
            System.out.println("Quantity on Hand: [" + stock.get(j).getQuantityOnHand()+ "]");
            System.out.println("Unit Price: [" + stock.get(j).getUnitPrice()+ "]");
            System.out.println("Item Description: [" + stock.get(j).getItemDescription() + "]");
        }
    }

    //set quantity
    System.out.print("Enter item quantity: ");
    orderQuantity = input.nextInt();
    if(orderQuantity < 1)
    {
        System.out.println("Please enter valid quantity");
        orderQuantity = input.nextInt();
     }
     item i = new item(no, invoice, itemPrice, orderQuantity, totalPrice, quantityOH);
     list.add(i);
     //asking if the user need to input another item
     System.out.println("Do you want to add next Item Y/N: ");
     Scanner input2 = new Scanner(System.in);
     addNextItem = input2.next();
     if(!"Y".equals(addNextItem.toUpperCase())&& !"N".equals(addNextItem.toUpperCase()))
     {   
         System.out.println("Invalid character! Only Y/N");
     }
 }while ("Y".equals(addNextItem.toUpperCase())); //user will continue to add item until addNextItem=='Y'

如果不是立即可见,我添加了另一个do while循环,该循环将获取用户输入并进行验证(使用与if else语句中类似的方法,并将循环如果输入无效,则再次输入。