当只有1个手册存储在库中时提示用户输入

时间:2015-01-04 15:44:01

标签: java input

我正在开发一个库应用程序,它允许存储,借用,返回和删除技术手册。

目前我正在处理我的应用程序的借阅部分,我已经让它通过输入指定的手动索引号,询问用户他们希望借用哪个手册(从库中存储的手册列表中)。如图所示:

enter image description here

但是,如果库中只存储了1个手册,则应用程序会自动借用它而不向用户询问索引号。如图所示:

enter image description here

虽然自动借用唯一可用的书是有意义的,但我仍然希望用户被要求索引编号,因为这可以最大限度地减少混淆。

以下是我正在使用的相关代码:

public static void borrowManual(){
    displayManualList();

    //register user's Manual choice.
    ManualChoice = (Console.readInteger(Messages.enterManualIndexMessage, Messages.ManualIndexNotInListMessage, 0, Library.ManualList.size() - 1));

    borrowLoop:
    while(Menu.menuChoice == 3){
        //Check if the Manual to be borrowed is available.
        //ManualChoice = (Console.readInteger(Messages.enterManualIndexMessage, Messages.ManualIndexNotInListMessage, 1, Library.ManualList.size()));

        if ((ManualList.get(ManualChoice).status.equalsIgnoreCase(status1)) && (ManualList.size() >= ManualChoice)){
            //Print the borrowed Manual information and change the Manual status to borrowed.
            ManualList.get(ManualChoice).status = "Borrowed";
            ManualList.get(ManualChoice).borrower = User.userName;
            ManualList.get(ManualChoice).borrowDate = "Today.";
            ManualList.get(ManualChoice).returnDate = "In two weeks.";
            //Add the borrowed Manual to the borrowedManuals arraylist:
            borrowedManuals.add(ManualList.get(ManualChoice));

            System.out.printf("\n==========================================================================\n");
            System.out.printf("\n\nYou have chosen the following Manual:\n\n %s\n\n", ManualList.get(ManualChoice).displayManual());
            System.out.println("Please return the Manual within two weeks!\n");
            System.out.println("\n--------------------------------------------------------------------------");
            System.out.println("\n                             Manual borrowed!\n");
            System.out.println("--------------------------------------------------------------------------\n");
            break borrowLoop;

        }else if(ManualList.get(ManualChoice).status.equalsIgnoreCase(status2) && ManualList.size() >= ManualChoice){
            System.out.println("\n\n--------------------------------------------------------------------------");
            System.out.println("\n            "
                    + " The Manual you wish to borrow is already on loan.");
            System.out.println("\n--------------------------------------------------------------------------\n");
            break borrowLoop;

        }else if(ManualChoice > ManualList.size()-1){
            System.out.println(Messages.noSuchManualMessage);
            break borrowLoop;
        }
    }
    Menu.displayMenu();
}

如果有人知道如何让我的应用程序始终要求用户输入索引编号,请告诉我:D

1 个答案:

答案 0 :(得分:0)

这是你的问题,如果只有1个手册,它会自动借用它。我想你可以删除这个&&检查,它应该工作正常。

(ManualList.size() >= ManualChoice)){