dimanche 25 avril 2021

Java function cannot iterate all objects to return the correct value [closed]

I have this Book class:

import java.util.Scanner;
import java.util.ArrayList;

public class Book {

private String name;

private String title;

private String ISBN;

private double price;

private int copies;

public Book() 
{


} 
public Book(String name,String title,String ISBN,double price,int copies) 
{
this.name = name;

this.title = title;

this.ISBN = ISBN;

this.price = price;

this.copies = copies;

}

public void setName(String name) 
{

this.name = name;
}
public String getName() 
{
return name;

}
public void setTitle(String title) 
{
this.title = title;

}
public String getTitle() 
{
return title;

}

public void setISBN(String ISBN) 
{

this.ISBN = ISBN;
}
public String getISBN() 
{
return ISBN;

}
public void setPrice(double price) 
{
this.price = price;

}
public double getPrice() {  return price;}

public void setCopies(int copies) { this.copies = copies;}

public int getCopies() { return copies;}

public void Read() 
{
Scanner input = new Scanner(System.in);

System.out.println("Author name:");

name = input.nextLine();

System.out.println("Book title:");

title = input.nextLine();

System.out.println("ISBN:");

ISBN = input.nextLine();

double prc;

do
{
 System.out.println("Price of book:");
 
 prc = input.nextDouble();
 
 price = prc;
 
 
  
}
while(prc<0);

int cps;

do
  
{
  
  System.out.println("Copies:");
  
  cps = input.nextInt();
  
  copies = cps;
 }
 while(cps<0);




}

public String toString() 
{

return name+" "+title+" "+ISBN+" "+" "+price+" "+copies;

} 
    


}

I have an ArrayList of Book objects(books ) and I have to make a function search(String ISBN) which will return -1 if there isnt a book object in books with that ISBN and to return the number of copies of the book if there is a book with that ISBN.

This is what I have done so far:I know i have to use an if inside a for loop

 public int search(String ISBN) 
 {
 for(int i=0;i<books.size();i++) 
 {
    if(books.get(i).getISBN()==ISBN) 
    {
        
    }
    
 }


}

but i am stuck.What should I do fron now on?

Aucun commentaire:

Enregistrer un commentaire