-1

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.