Reputation: 19
I am trying to solve my problem where i have to look if when inputing a value into an arraylist this value already been added, and if so I want to display a message along the lines of name already exists
import java.util.*;
class Bank{
void command(){
CommandReader commandReader=new CommandReader(this);
commandReader.run();
}
ArrayList <Account> accounts=new ArrayList <Account>();
public static void main(String[] args){
new Bank().command();
}
void print(String name){
System.out.println("Account of "+name);
int b=0;
for(int i=0; i<accounts.size(); i++){
b=accounts.get(i).balance(name);
break;
}
System.out.println("Balance: "+b);//dit moet naar account verwjizen op een of andere manier zodat hij weet waar hij balance() moet zoeken
}
void printAll(){
/*
for(int i =0; i<accounts.size()-1;i++){
print(accounts[i].name);//klopt niet, weet niet wat er wel moet staan
}*/
}
void enroll(String name){
Account account=new Account(name,0);
accounts.add(account);
}
void deposit(String name, int amount){
for(int i=0; i<accounts.size(); i++){
accounts.get(i).deposit(amount, name);
}
}
void withdraw(String name, int amount){//hiervoor moet je denk ik een methode in account maken zodat e met balance kan rekenen?
for(int i=0; i<accounts.size(); i++){
int x;
x=accounts.get(i).balance(name);
if (x>-1000){
amount = amount*-1;
accounts.get(i).deposit(amount, name);
}
}
}
void printRed(){
}
void interest(double rate){
for(int i=0; i<accounts.size(); i++){
accounts.get(i).inter(rate);
}
}
}
class Account{
String name;
int balance;
void inter(double rate){
balance+=rate
/100*balance;
}
Account(String n, int b){//weet niet of dit nodig is, denk het wel
this.name=n;
this.balance=b;
}
String name(){
return this.name;
}
int balance(String name){
if(name.equals(this.name)){
return this.balance;
}
return 0;
}
void deposit(int b, String name){
if(name.equals(this.name)){
this.balance+=b;
}
}
}
is my way to add values into the array via methods.(the zero can be ignored)
Upvotes: 0
Views: 787
Reputation: 85779
You can do it in two ways:
Write a comparison yourself. Then go through all the array elements to verify if the object already exists.
Let your Account
class override equals
and hashCode
methods, then use List#contains
to verify if the object is already present in the list.
A short example of this:
Account account = new Account(name,0);
//this assumes that Account class has already overridden equals and hashCode methods
if (!accounts.contains(account)) {
accounts.add(account);
}
If you're going to use the latter, it would be better using a Set
instead of a List
since Set
s already handle that the object should be unique in the collection.
From your edit, the current error is that Account
class must override equals
and hashCode
methods. When you do this, the code will behave as expected.
Providing a basic implementation for both:
class Account {
String name;
int balance;
void inter(double rate) {
balance+=rate
/100*balance;
}
Account(String n, int b) {
this.name=n;
this.balance=b;
}
String name() {
return this.name;
}
int balance(String name) {
if(name.equals(this.name)) {
return this.balance;
}
return 0;
}
void deposit(int b, String name){
if(name.equals(this.name)){
this.balance+=b;
}
}
//from here, this is the code you need
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o instanceof Account) {
Account another = (Account)o;
return this.name.equals(another.name);
}
return false;
}
@Override
public int hashCode() {
//if working with Java 7, uncomment below LoC
//return java.util.Objects.hash(name, balance);
//if using Java SE 6 or less
return java.util.Arrays.hashCode(new Object[]{ name, balance });
}
}
Upvotes: 3
Reputation: 240898
override equals()
and hashcode()
, Use Set
instead and its add()
returns boolean
depending on if it has ignored
Upvotes: 0