Java ArrayList/RMI
我构建了一个简单的item类Java ArrayList/RMI,java,arraylist,rmi,tostring,Java,Arraylist,Rmi,Tostring,我构建了一个简单的item类 class itemInfo{ int auctionID; int startPrice; int buyoutPrice; } 我创建了一个ArrayList ArrayList<itemInfo> itemSet = new ArrayList<itemInfo>(); 我坚持的是建立一种方法,允许用户查看当前物品拍卖的列表,基本上是一种打印物品集ArrayList的
class itemInfo{
int auctionID;
int startPrice;
int buyoutPrice;
}
我创建了一个ArrayList
ArrayList<itemInfo> itemSet = new ArrayList<itemInfo>();
我坚持的是建立一种方法,允许用户查看当前物品拍卖的列表,基本上是一种打印物品集ArrayList的方法
我已经研究过使用toString(),但我不确定如何让它返回多个值,即auctionID、startPrice、buyoutPrice
理想情况下,我希望用户选择一个选项,如“查看当前拍卖”,然后选择以“拍卖ID:***起始价格:***买断价格:***”等格式打印整个ArrayList的方法,其中****显然是用户输入的数字。您可以覆盖toString()方法返回itemInfo,使用3个getter返回3个存储的值。每个变量必须转换为字符串,因为getter将返回int。作为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);
}
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
}
在您学习的过程中,有几件事可以让您的代码变得更好:
1-类名应以大写字母开头,您应将itemInfo更改为itemInfo
2-您通常应该使用getter和setter,因此不应该使用selectedInfo.auctionID
,而应该使用selectedInfo.getAuctionId()
和selectedInfo.setAuctionId(x)代码>
3——您应该考虑使用开关,而不是IF(扫描器.NEXT)(相等)(“1”)。另外,如果您最后写入else if(scanner.next().equals(“2”)),那么您将遇到问题,因为每次调用scanner.next()时,它都需要输入,因此每个if都需要输入。相反,您应该将scanner.next()置于开关外部,然后使用读入的值。例如:
// 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);
}
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-最后,您可能应该将处理这些菜单选项的功能拆分为单独的方法。如果你把所有这些都投入到这个方法中,它会变得非常大和丑陋(难以维护)非常快。基于答案:
您可以在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);
}
很高兴这有帮助,请务必阅读我添加的最后一点,因为它将帮助您改进代码,使您的代码在将来更易于维护和使用。它非常有帮助,谢谢!同时也感谢您提供关于我的代码的一般提示,尽量做到优雅。用case/switch方法提问,是否有一种方法,在用户选择“1”,输入所有变量(拍卖ID等)后,代码将自动返回给他们再次选择菜单?希望这是有意义的。这是有意义的-您可能希望使用循环来实现这一点。考虑在调用“方法CUSER()”的过程中设置一个循环。根据您想要停止的时间,您需要从do、for或while中选择适当的循环。例如,我想你可以在菜单上添加一个退出选项。那太好了,谢谢。很抱歉打扰你,你能给我一些建议,如何从拍卖名单上删除一件物品吗?我需要从arraylist中删除整个对象(auctionID、startPrice和buyoutPrice),但只需输入与auctionID相关的对象。例如,用户出价,出价>=买断价格,因此需要删除特定对象。非常感谢您的帮助。您希望使用ArrayList移除方法。这将是itemSet.remove(selectedInfo)(这将删除他们选择的项目)。你可以在这里看到更多关于这方面的信息: