Wednesday 15 August 2012

Java ArrayList / RMI -



Java ArrayList / RMI -

i've built simple item class;

class iteminfo{ int auctionid; int startprice; int buyoutprice; }

i've created arraylist;

arraylist<iteminfo> itemset = new arraylist<iteminfo>();

i have method here allows user create item (the method incomplete, i've tried implementing selection == 1 far!);

public void auctionchoice(){ system.out.println("---- do? ----\n"); system.out.println("1: list item auction\n"); system.out.println("2: bid on existing item\n"); system.out.println("3: remove item auction\n"); if(scanner.next().equals("1")){ iteminfo createditem = new iteminfo(); system.out.println("----enter auctionid----"); createditem.auctionid = scanner.nextint(); system.out.println("----enter item startprice----"); createditem.startprice = scanner.nextint(); system.out.println("----enter buyoutprice----"); createditem.buyoutprice = scanner.nextint(); system.out.println("auction id:" +createditem.auctionid+ "\nstartprice:" +createditem.startprice+ "\nbuyoutprice:" +createditem.buyoutprice); itemset.add(createditem); } }

what stuck on building method allow user view list of current item auctions, way print out itemset arraylist.

i have looked using tostring() unsure of how homecoming more 1 value, i.e auctionid, startprice, buyoutprice.

ideally user select selection such "view current auctions" , method print entire arraylist in format such "auction id: **** start price: **** buyout price: ****" **** beingness number user inputted.

as itemset, arraylist of iteminfo objects, can loop through them this:

for(iteminfo info : itemset){ system.out.println(info.actionid); system.out.println(info.auctionprice); system.out.println(info.buyoutprice); }

this print them out. perhaps, include id, can inquire user type in id next, , can retrieve 1 arraylist. can looping through them , comparing id id user entered. example:

// id int auctionid = scanner.nextint(); iteminfo selectedinfo; // find item for(iteminfo info : itemset){ if(info.auctionid = auctionid){ selectedinfo = info; break; } } if(selectedinfo == null){ // id not valid! // handle case. } else { system.out.println(selectedinfo.auctionid); system.out.println(selectedinfo.auctionprice); system.out.println(selectedinfo.buyoutprice); }

as learning, here few things create code bit nicer:

1- class names should start uppercase, should alter iteminfo iteminfo.

2- should utilize getters , setters, instead of using selectedinfo.auctionid, should utilize selectedinfo.getauctionid() , selectedinfo.setauctionid(x);

3- should consider using switch rather if(scanner.next().equals("1")). also, if end writng else if(scanner.next().equals("2")) run problem, each time scanner.next() called, expects input, hence expect input every if. instead, should have scanner.next() outside of switch, , utilize value read in. example:

int menuselection = scanner.nextint(); switch(menuselection){ case 1: // stuff break; case 2: // else break; default: // handle input isn't menu alternative }

4- finally, should split functionality handling each of these menu options in separate methods. if set method it's going big , ugly (hard maintain) fast.

java arraylist rmi tostring

No comments:

Post a Comment