Java ArrayList / RMI
Asked Answered
T

3

6

I've built a simple item class;

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

I've created an ArrayList;

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

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

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.

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.

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.

Tuberculosis answered 7/11, 2014 at 17:19 Comment(0)
T
4

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

}

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

As you are learning, here are a few things to make your code a bit nicer:

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

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

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- 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.

Thagard answered 7/11, 2014 at 17:32 Comment(5)
Glad this helped, please do read the last bit which i've added as it will help you to improve make your code more maintainable and easier to work with in the future.Thagard
It helped so much, thank you! Also thanks for the general tips on my code, trying to be as elegant as possible. Just to ask with the case/switch method, is there a way that after a user has selected "1", inputted all of the variables (auctionID etc..), the code will automatically go back to giving them the menu choice again? Hope that makes sense.Tuberculosis
That does make sense - You probably want to use a loop to do that. Consider putting a loop around the call to "methodChoice()". Depending on when you want to stop, you will want to select an appropriate loop from do, for or while. I would think, for example, that you may add to your menu an option to quit.Thagard
That's great thanks. Sorry to pester, but could you give me some advice on a way to remove an item from the auction list? I would need to remove the entire object (auctionID, startPrice and buyoutPrice) from the arraylist but only those related to the auctionID entered. For example a user makes a bid, bid is >= buyout price therefore that specific object needs to be removed. Any help greatly appreciated.Tuberculosis
Yep. You want to use the arraylists remove method. So that would be itemSet.remove(selectedInfo) (That would remove the one they selected). You can see more about that here: docs.oracle.com/javase/7/docs/api/java/util/…Thagard
L
2

Building on ThePerson's answer:

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

You can use toString() on your itemInfo class.

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

then the for loop becomes

for(ItemInfo info : itemSet){
    System.out.println(info);
}
Louque answered 7/11, 2014 at 17:51 Comment(0)
R
0

You can override toString() method to return itemInfo with 3 getters to return 3 stored vaules. Each vaule must be turned into String because getters will return ints.

Reverent answered 7/11, 2014 at 17:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.