Hello everyone, I am java noob so please bear with me, I have two classes for a program thats supposed to search for books but I am currently stuck!. Where I am stuck is at the bottom of the BookSearch class, and I am trying to essentially only search my array for the price and title, but, in my book class I declared the attributes of the book to have 3 parameters (see below). So the little tiny thing that I can't figure out is that if I want to compare 2 values given to me by the user (a title and a price) then I only want to compare those 2 parameters, but I have no idea how to go about it since my Book class has 3 parameters, so essentially I am asking how do I check through an array with 3 parameters but only want to check 2 of them for the entire array. I tried if bkArr[i].equals(pric) && bkArr[i].equals(titl) ... but it didn't work and I know it wouldn't work, it just dosen't look good. I am completely baffled by this and have spent 3 hours just staring at my screen trying to figure something which I think should be very easy to do. If anyone dosen't understand what I am trying to do please ask and Il try to explain again, thanks in advance !
class Book { //Attributes of Book private String title; private long ISBN; private double price; //Constructor of Book public Book() { System.out.println("Creating Book....."); title = "abc"; price = 25; ISBN = 1234; } public Book(String ti, long ib, double pr) { title = ti; price = pr; ISBN = ib; } //Methods of book public Book(Book bk) { //Copy Constructor System.out.println("Creating object with copy constructor....."); title = bk.title; price = bk.price; ISBN = bk.ISBN; } public void setPrice(double pr) {//sets the price of the book price = pr; } public double getPrice() {//gets the price of the book return price; } public void setTitle(String ti) {//sets the title of the book title = ti; } public String getTitle() {//gets the title of the book return title; } public void setISBN(long num) {//sets the ISBN of the book ISBN = num; } public long getISBN() {//gets the ISBN of the book return ISBN; } public String toString() {//returns book values System.out.println("The book title is " + title + ", the ISBN number is " + ISBN + " and the price is " + price +"$.\n\n"); return (title + " " + ISBN + " " + price); } }
import java.util.Scanner; public class BookSearch { public static void main (String[] args) { String titl, yn; int i, pric; //Create 10 books with the array Book[] bkArr = new Book[10]; //Create 10 book objects with values Book b1 = new Book ("hi",10, 1001); Book b2 = new Book ("hi",45, 1002); Book b3 = new Book ("hi",50, 1003); Book b4 = new Book ("hi",100, 1004); Book b5 = new Book ("hi",75, 1005); Book b6 = new Book ("hi",65, 1006); Book b7 = new Book ("hi",40, 1007); Book b8 = new Book ("hi",10, 1008); Book b9 = new Book ("hi",10, 1009); Book b10 = new Book ("hi",100,1010); bkArr[0] = b1; bkArr[1] = b2; bkArr[2] = b3; bkArr[3] = b4; bkArr[4] = b5; bkArr[5] = b6; bkArr[6] = b7; bkArr[7] = b8; bkArr[8] = b9; bkArr[9] = b10; Scanner kb = new Scanner(System.in); System.out.println("Please enter a book title, a price and either 'Yes' or 'No' for a combined search"); titl = kb.next(); pric = kb.nextInt(); yn = kb.next(); if (yn.equals("yes")) { for (i = 0; i < bkArr.length; i++) { if (bkArr[i]. } } if (yn.equals("No")) { } } }