【问题标题】:Java ArrayList / RMIJava 数组列表/RMI
【发布时间】:2014-11-07 17:19:49
【问题描述】:

我已经建立了一个简单的物品类;

class itemInfo{
        int auctionID; 
        int startPrice;
        int buyoutPrice;        
}

我已经创建了一个 ArrayList;

ArrayList<itemInfo> itemSet = new ArrayList<itemInfo>();

我这里还有一个方法可以让用户创建一个项目(这个方法不完整,目前我只尝试过实现choice == 1!);

public void auctionChoice(){    

    System.out.println("---- What would you like to do? ----\n");
    System.out.println("1: List an item for auction\n");
    System.out.println("2: Bid on an existing item\n");
    System.out.println("3: Remove an item from the auction\n");

    if(scanner.next().equals("1")){

        itemInfo createdItem = new itemInfo();

        System.out.println("----Enter the auctionID----");
        createdItem.auctionID = scanner.nextInt();

        System.out.println("----Enter the item startPrice----");
        createdItem.startPrice = scanner.nextInt();

        System.out.println("----Enter the buyoutPrice----");
        createdItem.buyoutPrice = scanner.nextInt();

        System.out.println("Auction ID:" +createdItem.auctionID+ "\nstartPrice:" +createdItem.startPrice+ "\nbuyoutPrice:" +createdItem.buyoutPrice);

        itemSet.add(createdItem);
    }
}

我坚持的是构建一个允许用户查看当前物品拍卖列表的方法,基本上是一种打印出 itemSet ArrayList 的方法。

我已经研究过使用 toString(),但我不确定如何让它返回多个值,即auctionID、startPrice、buyoutPrice。

理想情况下,我希望用户选择“查看当前拍卖”等选项,然后选择以“拍卖 ID:**** 起始价格:**** 买断”等格式打印整个 ArrayList 的方法价格:****",其中 **** 显然是用户输入的数字。

【问题讨论】:

    标签: java arraylist rmi tostring


    【解决方案1】:

    作为 ItemSet,是一个 itemInfo 对象的 ArrayList,你可以像这样循环遍历它们:

    for(itemInfo info : itemSet){
    
        System.out.println(info.actionID);
        System.out.println(info.auctionPrice);
        System.out.println(info.buyoutPrice);
    
    }
    

    这会将它们全部打印出来。 也许,当您包含 ID 时,您可以要求用户接下来输入 ID,然后您可以从 arraylist 中检索该 ID。您可以通过遍历它们并将它们的 ID 与用户输入的 ID 进行比较来做到这一点。 例如:

    // get the ID
    int auctionId = scanner.nextInt();
    itemInfo selectedInfo;
    
    // find that item
     for(itemInfo info : itemSet){
        if(info.auctionId = auctionId){
            selectedInfo = info;
            break;
        }
    }
    
    if(selectedInfo == null){
        // the ID was not valid!
       // do something to handle this case.
    } else {
        System.out.println(selectedInfo.auctionID);
        System.out.println(selectedInfo.auctionPrice);
        System.out.println(selectedInfo.buyoutPrice);
    }
    

    在你学习的过程中,这里有一些东西可以让你的代码更好一点:

    1- 类名应以大写开头,您应该将 itemInfo 更改为 ItemInfo。

    2- 您通常应该使用 getter 和 setter,因此您应该使用 selectedInfo.getAuctionId()selectedInfo.setAuctionId(x); 而不是 selectedInfo.auctionID

    3- 您可能应该考虑使用开关而不是 if(scanner.next().equals("1"))。此外,如果你最终写 else if(scanner.next().equals("2")) 那么你会遇到一个问题,因为每次调用scanner.next() 时,它都需要输入,因此它会期望每个 if 的输入。相反,您应该在开关之外使用scanner.next(),然后使用读入的值。例如:

    int menuSelection = scanner.nextInt();
    switch(menuSelection){
        case 1: 
            // do your stuff
            break;
        case 2:
            // do something else
            break;
        default:
            // handle any input which isn't a menu option
     }
    

    4- 最后,您可能应该将处理每个菜单选项的功能拆分为单独的方法。如果你把所有东西都放在这个方法中,它会很快变得又大又丑(难以维护)。

    【讨论】:

    • 很高兴这有帮助,请阅读我添加的最后一点,因为它将帮助您改进代码,使您的代码在未来更易于维护和使用。
    • 帮了大忙,谢谢!还要感谢我的代码的一般提示,尽量优雅。只是问一下 case/switch 方法,有没有一种方法可以在用户选择“1”后,输入所有变量(auctionID 等),代码会自动返回给他们再次菜单选择?希望这是有道理的。
    • 这确实有道理——您可能想使用循环来做到这一点。考虑在对“methodChoice()”的调用周围放置一个循环。根据您想要停止的时间,您将需要从 do、for 或 while 中选择适当的循环。例如,我认为您可以在菜单中添加退出选项。
    • 非常感谢。很抱歉打扰,但你能给我一些建议,让我知道如何从拍卖清单中删除一件物品吗?我需要从arraylist 中删除整个对象(auctionID、startPrice 和buyoutPrice),但只删除与输入的auctionID 相关的对象。例如,用户出价,出价 >= 买断价格,因此需要删除特定对象。非常感谢任何帮助。
    • 是的。您想使用 arraylists 删除方法。所以这将是 itemSet.remove(selectedInfo) (这将删除他们选择的那个)。您可以在此处查看更多信息:docs.oracle.com/javase/7/docs/api/java/util/…
    【解决方案2】:

    基于ThePerson's 答案:

    for(ItemInfo info : itemSet){
        System.out.println(info.actionID);
        System.out.println(info.auctionPrice);
        System.out.println(info.buyoutPrice);
    }
    

    您可以在 itemInfo 类上使用 toString()。

    class ItemInfo{
        int auctionID; 
        int startPrice;
        int buyoutPrice;
    
        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append("Auction ID: ");
            sb.append(auctionID);
            sb.append("\nStart price: ");
            sb.append(startPrice);
            sb.append("\nBuyout price: ");
            sb.append(buyoutPrice);
            return sb.toString();
    }
    

    那么for循环就变成了

    for(ItemInfo info : itemSet){
        System.out.println(info);
    }
    

    【讨论】:

      【解决方案3】:

      您可以覆盖 toString() 方法以返回带有 3 个 getter 的 itemInfo 以返回 3 个存储的值。每个值都必须转换为字符串,因为 getter 将返回整数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-17
        • 1970-01-01
        • 2011-09-21
        • 1970-01-01
        • 1970-01-01
        • 2012-03-07
        • 2011-10-31
        • 1970-01-01
        相关资源
        最近更新 更多