简体   繁体   中英

How to check if exists entities with the some values java

I have two entities Client and Movie and I have to rent movies so I need a client and a movie, that's why I created the class Rental. The problem is when I want to check if the id that I entered when I want to rent a movie exists in the Client file, if doesn't exist to print a message.But it doesn't work.

The class Client:

public class Client extends BaseEntity<Long> {
private String name;
private int age;
public Client(String name, int age){
    this.name = name;
    this.age = age;
}
public String getName(){return name;}
public int getAge(){return age;}
public void setName(String name){this.name=name;}
public void setAge(int age){this.age=age;}

@Override
public String toString(){
    return "Client:{ " + name + " "+age + "} " + super.toString();
}

}

The Movie is almost the same as client, the Rental class:

public class Rental extends BaseEntity<Long> {

private Long IdClient;
private Long IdMovie;

public Rental(Long IdClient,Long IdMovie){
    this.IdClient = IdClient;
    this.IdMovie = IdMovie;
}

public Long getIdClient() {return IdClient;}
public Long getIdMovie(){return IdMovie;}

public String toString(){
    return "Rental:{ " + IdClient + " "+ IdMovie + "} " + super.toString();
}
}

And the console, where I try to check if exists or not:

private Rental readRental(){
    printAllClients();
    printAllMovies();
    System.out.println("Enter the Rentals ID , Clients ID and the rented Movie ID: ");
    BufferedReader bufferR = new BufferedReader(new InputStreamReader(System.in));
    try{
        Long id=Long.valueOf(bufferR.readLine());
        Long id1=Long.valueOf(bufferR.readLine());
        Long id2=Long.valueOf(bufferR.readLine());

        Rental rental = new Rental(id1, id2);
        rental.setId(id);
        return rental;
    }catch (IOException e){
        e.getStackTrace();
    }
    return null;
}

private void addRentals(){
    Set<Client> clients = clientC.getAllClients();

    Rental rental = readRental();
    if (rental == null || rental.getId() < 0 ){
        return;
    }
    if (!clients.contains(rental.getIdClient())){   //!!!!!!!!!!
        System.out.println("Doesn't contains");
    }
    else{
        System.out.println("Contains");
    }

    try{
        rentalC.addRental(rental);

        System.out.println("A rental was added.");
    }
    catch (ValidatorException e){
        e.printStackTrace();

    }
}

When I introduce a client which exists it gives me "doesn't exist". why?

Your clients is a set of client objects. The rental.getIdClient() returns a Long object. A set of client object can never contain a Long (unless you hack some dirty code). Suggestion: keep your clients mapped in a HashMap instead of a Set: map the id of a client to a client object.

Additionally, in Java, if you wish to use Client objects in a Set, class Client must properly override the two methods from class Object:

  • public boolean equals (Object obj)
  • public int hashCode()

You should do the same if you wish to use Client objects as Keys in a Map.

Consider these two resources for more information:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM