Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ArrayList / RMI

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.

like image 698
Michael_GG Avatar asked Nov 07 '14 17:11

Michael_GG


2 Answers

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.

like image 189
ThePerson Avatar answered Oct 21 '22 05:10

ThePerson


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);
}
like image 29
Zaphod Avatar answered Oct 21 '22 04:10

Zaphod