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?