Reputation: 1
Good morning, I have the following specification:
"The searchAccount () method must verify the existence of the current account passed as a parameter in the set of current currents existing in the bank and stored in the vector."
I have implemented the following solution.
public boolean searchAccount(BankAccount ba) {
boolean found=false;
int count=0;
while(count<=accounts.length-1) {
if(accounts[count]!=null &&(accounts[count].getCode().equals(ba.getCode())))
found=true;
else
found=false;
count++;
}
return found;
}
Note that accounts is an array defined inside the class where the searchAccount () method is present. However, this solution gives me problems at runtime. Everything is fine when compiling.
In particular given the following main () method:
public static void main(String[] args) {
// print a large HTML header
System.out.println("<h4>-- Bank account exercise --</h4>");
// create 5 objects of type BankAccount
BankAccount c1 = new BankAccount("001",5);
BankAccount c2 = new BankAccount("002",10);
BankAccount c3 = new BankAccount("003",15);
BankAccount c4 = new BankAccount("004",20);
BankAccount c5 = new BankAccount("005",25);
Bank b = new Bank("B001");
b.addAccount(c1);
b.addAccount(c2);
b.addAccount(c3);
b.addAccount(c4);
b.addAccount(c5);
System.out.println("Search bank account having code " +
c4.getCode() + ": ");
System.out.println((b.searchAccount(c4))?"Found":"Not found");
System.out.println("c1.equals(c2)?" + (c1.equals(c2)));
BankAccount c6 = new BankAccount("001",29);
System.out.println("c1.equals(c6)?" + (c1.equals(c6)));
//System.out.println(b);
}
By running I get:
<h4>-- Bank account exercise --</h4>
Search bank account having code 004:
Not found
c1.equals(c2)?false
c1.equals(c6)?true
I don't know why the searchAccount () method doesn't find the C4 object.
Upvotes: 0
Views: 101
Reputation: 1017
As the other answers mention, you need to break the search when the account is found.
Here are two other alternatives to the search.
Using for-loop:
public boolean searchAccount2(BankAccount ba) {
for(BankAccount account : accounts) {
if(account != null && account.getCode().equals(ba.getCode())) {
return true;
}
}
return false;
}
Using streams:
public boolean searchAccount3(BankAccount ba) {
return Arrays.asList(accounts).stream().filter(Objects::nonNull).anyMatch(b -> b.getCode().equals(ba.getCode()));
}
Update
To test the above code I implemented rudimentary Bank and BankAccount classes and ran your code above, but adding searchAccount2 and searchAccount3:
//...
System.out.println((b.searchAccount(c4))?"Found":"Not found");
System.out.println((b.searchAccount2(c4))?"Found":"Not found");
System.out.println((b.searchAccount3(c4))?"Found":"Not found");
//...
Accounts are assumed to be an array in the Bank class:
private BankAccount[] accounts
This is the output:
<h4>-- Bank account exercise --</h4>
Search bank account having code 004:
Not found
Found
Found
c1.equals(c2)?false
c1.equals(c6)?false
EDIT - addAccount()
Here is your addAccount (as posted in the comment) formatted:
public void addAccount2(BankAccount ba) {
//Il metodo addAccount() deve aggiungere il conto corrente passato
// come parametro al vettore dei conti correnti presente nella banca,
// se non e' già presente.
if (!searchAccount(ba)) {
for (int i = 0; i < accounts.length; ++i) {
if (accounts[i] == null) {
accounts[i] = ba;
// PROBLEM HERE!!!
}
}
}
}
This method will add the given account (ba) to every position in the array because you do not stop the loop at any time.
You need a return statement where I indicated the "PROBLEM HERE!!!":
public void addAccount2(BankAccount ba) {
if (!searchAccount(ba)) {
for (int i = 0; i < accounts.length; ++i) {
if (accounts[i] == null) {
accounts[i] = ba;
return;
}
}
}
}
Otherwise, after you add the first account (to every position) there is no more room to add further accounts.
Upvotes: 0
Reputation: 1
Ok, so maybe it's my addAccount () method that's wrong. I'll bring it back here:
public void addAccount(BankAccount ba) {
if(!searchAccount(ba))
for(int i=0;i<=accounts.length-1;i++)
if(accounts[i]==null)
accounts[i]=ba;
}
Upvotes: 0
Reputation: 100
The better option for this task will be usage of Map for store your accounts objects. Something like this:
public class Bank {
private Map<String,Account> accounts = new HashMap<>();
public void addAccount(Account account){
accounts.put(account.getCode(),account);
}
public boolean searchAccount(Account account){
return accounts.containsKey(account.getCode());
}
}
Upvotes: 0
Reputation: 17890
What happens when you find a match? You set found
to true
. Then, you continue searching. When there are more elements, you'll again set (reset) found
to false
.
You must break and return the result when you find a match (short-circuiting the search).
while(count <= accounts.length-1) {
if(accounts[count] != null &&(accounts[count].getCode().equals(ba.getCode()))) {
found = true;
break;
}
}
return found;
Or, you can remove the found
variable and just return.
while(count <= accounts.length-1) {
if(accounts[count] != null &&(accounts[count].getCode().equals(ba.getCode()))) {
return true;
}
}
return false;
Upvotes: 2
Reputation: 2005
Try returning the true
value instead of saving it using found
variable. So write return true;
Your searchAccount
method actually finds the target account, but just continues with the loop, so the next iteration will cause found
to be false.
Upvotes: 1