ArrayList中的Java / RMI [英] Java ArrayList / RMI

查看:141
本文介绍了ArrayList中的Java / RMI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个简单的项目类;

I've built a simple item class;

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

我已经创建了一个ArrayList

I've created an ArrayList;

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

我这儿也有一个方法,它允许用户创建一个项目(该方法是不完整的,我只是试图实现的选择== 1为止!);

I also have a method here that allows a user to create an item (the method is incomplete, I've only tried implementing choice == 1 so far!);

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);
    }
}

我所停留在正在建设,将允许用户查看当前项目的拍卖清单,基本的方式来打印出套装的ArrayList的方法。

What I am stuck on is building a method that will allow the user to view a list of current item auctions, basically a way to print out the itemSet ArrayList.

我看着使用的toString(),但我不确定如何得到它返回多个值,即auctionID,startPrice,buyoutPrice。

I have looked into using toString() but I am unsure of how to get it to return more than one value, i.e auctionID, startPrice, buyoutPrice.

我非常希望用户选择一个选项,如查看当前的拍卖,然后该方法来打印的格式整个ArrayList中,如拍卖ID:*​​**起始价格:****收购价格:****具有明显的****是用户输入的号码

Ideally I would like the user to select a choice such as "view current auctions" and then the method to print the entire ArrayList in a format such as "Auction ID: **** Start Price: **** Buyout Price: ****" with obviously the **** being the number the user inputted.

推荐答案

由于套装,是itemInfo对象的ArrayList,你可以通过他们循环是这样的:

As ItemSet, is an ArrayList of itemInfo objects, you can loop through them like this:

for(itemInfo info : itemSet){

    System.out.println(info.actionID);
    System.out.println(info.auctionPrice);
    System.out.println(info.buyoutPrice);

}

这将全部打印出来。
也许,当你包括ID,您可以要求用户在ID输入下一个,然后你可以从ArrayList中的那一个。可以通过在它们的所有循环和其ID比较用户输入的ID做到这一点。
例如:

This will print them all out. Perhaps, as you include the ID, you can ask the user to type in the ID next, and then you can retrieve that one from the arraylist. You can do this by looping through them all and comparing their ID to the ID the user entered. For example:

// 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);
}

当你正在学习,这里有一些东西,使你的code位更好:

1类名称应该以一个大写的开始,你应该改变itemInfo是ItemInfo。

1- Class names should start with an uppercase, you should change itemInfo to be ItemInfo.

2 - 通常应该使用getter和setter方法​​,所以不是用 selectedInfo.auctionID ,你应该使用 selectedInfo.getAuctionId() selectedInfo.setAuctionId(X);

2- You should generally use getters and setters, so instead of using selectedInfo.auctionID, you should use selectedInfo.getAuctionId() and selectedInfo.setAuctionId(x);

3,您或许应该考虑使用交换机而不是如果(scanner.next()。等于(1))。另外,如果你最终是否(scanner.next()。等于(2))writng别人,那么你会遇到一个问题,因为每次scanner.next()被调用,它需要投入,因此期望输入每一个如果。相反,你应该有你的开关外的scanner.next(),然后用它在读取的值,例如:

3- You should probably consider using a switch rather than the if(scanner.next().equals("1")). Also, if you end up writng else if(scanner.next().equals("2")) then you will run into a problem, as each time scanner.next() is called, it expects input, therefore it would expect input for every if. Instead, you should have the scanner.next() outside of your switch, and then use the value which is read in. For example:

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-最后,你应该拆分功能为处理每这些菜单选项中单独的方法。如果你把它全部变成这种方法,会得到非常大的和丑陋的(难以维持)非常快的。

4- Finally, you should probably split the functionality for handling each of these menu options in to separate methods. If you put it all into this method it's going to get very big and ugly (hard to maintain) very fast.

这篇关于ArrayList中的Java / RMI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆