Java ArrayList / RMI

I created a simple class item;

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

I created an ArrayList;

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

I also have a method that allows the user to create an element (the method is incomplete, I only tried to implement the 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); } } 

I am stuck in building a method that will allow the user to view the auction list of the current item, basically a way to print an itemSet ArrayList.

I studied the use of toString (), but I'm not sure how to get it to return more than one value, for example, auction ID, startPrice, buyPrice.

Ideally, I would like the user to choose an option such as "view current auctions" and then the method for printing the entire ArrayList array in the format "Auction ID: **** Starting price: **** Buyout Price: *** * ", it is obvious that **** is the number entered by the user.

+6
source share
3 answers

Like an ItemSet, is an ArrayList of itemInfo objects, you can skip 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 out completely. Perhaps, as you include the identifier, you can ask the user to enter the next identifier, and then you can extract it from the arraylist. You can do this by going through them all and comparing their ID with the ID entered by the user. For instance:

 // 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 learn, here are a few things to make your code a little nicer:

1- Class names must begin with capital letters, you must change itemInfo as ItemInfo.

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

3. You should probably use a switch, not if (scanner.next (). Equals ("1")). Also, if you finish writing else if (scanner.next (). Equals ("2")), you will run into a problem, because every time scanner.next () is called, it expects input, so it was expecting would be input for each if. Instead, you should have scanner.next () outside your switch, and then use the value that is read. For instance:

 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 separate the functionality for handling each of these menu options to separate methods. If you put all this into this method, it will be very fast and ugly (hard to maintain) very fast.

+4
source

Based on ThePerson answer :

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

You can use toString () in your class itemInfo.

 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); } 
+2
source

You can override the toString () method to return itemInfo with 3 getters to return 3 saved vaules. Each vaule must be converted to String because getters will return ints.

0
source

Source: https://habr.com/ru/post/977888/


All Articles