java - BlueJ how to get data from an array so that it can be compared with user input -
java - BlueJ how to get data from an array so that it can be compared with user input -
i'm messing around bluej , trying objects interact each other , stuff. have created business relationship , accountlist class. accountlist allows add together business relationship objects puts arraylist.
removing these accounts array index easy plenty loop position of each account, should remove business relationship account number specified parameter , can't work out how accountnumber array compare users input.
accountlist class
/** * creates accountlist store customers accounts * * @author ryan conway * @version 12/11/2014 */ import java.util.*; public class accountlist { private arraylist < business relationship > accounts; /** * create accountlist contain accounts. */ public accountlist() { accounts = new arraylist < business relationship >(); } /** * add together business relationship accountlist. */ public void addaccount(account newaccount) { accounts.add(newaccount); } /** * homecoming number of accounts stored in accountlist. */ public int getnumberofaccounts() { homecoming accounts.size(); } /** * prints out number of accounts terminal. * each loop used each account. */ public void getallaccounts() { for(account business relationship : accounts) { account.printaccountdetails(); } system.out.println("number of accounts: " + getnumberofaccounts()); } /** * print out details of business relationship * @param accountentry entry in list */ public void getaccount(int accountentry) { if(accountentry < 0) { system.out.println("negative entry :" + accountentry); } else if(accountentry < getnumberofaccounts()) { business relationship account = accounts.get(accountentry); account.printaccountdetails(); } else { system.out.println("no such entry :" + accountentry); } } /** * removes business relationship list * @param accountentry entry in list */ public void removeaccount(int accountentry) { if(accountentry < 0) { system.out.println("negative entry :" + accountentry); } else if(accountentry < getnumberofaccounts()) { accounts.remove(accountentry); } else { system.out.println("no such entry :" + accountentry); } } public boolean removeaccount(string accountnumber) { int index = 0; for(account business relationship : accounts) { if (accounts.equals(accountnumber)) system.out.println("found it!"); index++; } homecoming false; } public void printcollection() { system.out.println(accounts); } }
account class
/** * write description of class business relationship here. * * @author (your name) * @version (a version number or date) */ public class business relationship { private string firstname; private string lastname; private string accountnumber; private int pointsheld; private address address; /** * constructor objects of class account. * number of pointsheld should set zero. * * @param firstname business relationship holder's first name * @param lastname business relationship holder's lastly name * @param accnumber business relationship holder's business relationship number * @param street business relationship holder's street * @param town business relationship holder's town * @param postcode business relationship holder's postcode */ public account(string fname, string lname, string accnumber, string street, string town, string postcode) { firstname = fname; lastname = lname; accountnumber = accnumber; pointsheld = 0; address = new address(street, town, postcode); } /** * constructor objects of class account. * number of pointsheld should should set * supplied value. * * @param fname business relationship holder's first name * @param lname business relationship holder's lastly name * @param acctnumber business relationship number * @param thepoints pointsheld awarded when business relationship initialised * @param street business relationship holder's street * @param town business relationship holder's town * @param postcode business relationship holder's postcode */ public account(string fname, string lname, string acctnumber, int points, string street, string town, string postcode) { firstname = fname; lastname = lname; accountnumber = acctnumber; pointsheld = points; address = new address(street, town, postcode); } // accessors /** * business relationship holder's first name * * @return business relationship holder's first name */ public string getfirstname() { homecoming firstname; } /** * business relationship holder's lastly name * * @return business relationship holder's lastly name */ public string getlastname() { homecoming lastname; } /** * business relationship holder's business relationship number * * @return business relationship holder's business relationship number */ public string getaccountnumber() { homecoming accountnumber; } /** * number of points held * * @return number of points held */ public int getnoofpoints() { homecoming pointsheld; } /** * print out business relationship holder's details console window * */ public void printaccountdetails() { system.out.println( firstname + " " + lastname + "\n" + address.getfulladdress() + "\naccount number: " + accountnumber + "\nnumber of points: " + pointsheld); } /** * homecoming business relationship holder's address * * @return business relationship holder's address */ public string getaddress() { homecoming address.getfulladdress(); } // mutators /** * alter first name * * @param fname new first name * */ public void setfirstname(string fname) { firstname = fname; } /** * alter lastly name * * @param lname new lastly name * */ public void setlastname(string lname) { lastname = lname; } /** * increment number of points held given number * , output esage console window giving * revised number of points held. * * @param number of points add together total */ public void addpoints(int points) { pointsheld = pointsheld + points; system.out.println("points held: " + pointsheld); } /** * remove pointsheld given number , output * message console window giving revised number * of points held long number of points * not fall below 0 * - otherwise output message console window instead. * * @param number of pointsheld remove total. * */ public void removepoints (int points) { if ((pointsheld - points) >=0) { pointsheld = pointsheld - points; system.out.println("points held: " + pointsheld); } else { system.out.println("transaction refused: " + "insufficient points available."); } } /** * alter business relationship holder's address * * @param street street * @param town town * @postcode postcode */ public void setaddress(string street, string town, string postcode) { address.setfulladdress(street, town, postcode); } /** * print business relationship holder's address */ public void printaddress() { address.printaddress(); } } // end class
http://i.imgur.com/dcwt9ok.jpg
thanks.
try this:
public boolean removeaccount(string accountnumber) { iterator<account> iterator = accounts.iterator(); while (iterator.hasnext()) { if (accountnumber.equals(iterator.next().getaccountnumber())) { system.out.println("found it!"); iterator.remove(); homecoming true; } } homecoming false; }
removing item list while looping through not safe operation, iterator
class makes possible. more information: efficient equivalent removing elements while iterating collection
java arrays object bluej
Comments
Post a Comment