1

So, I have a book inventory list class where I want to input the year of a book and the program should give me the percentage of books with that year. Now, when I run my code it gives me the last book I entered, not even a number. I don't know whats wrong with my code. Do I have my code wrong?

This is the Method I created that's in question.

    public void bookYear(int year) {
    int index = 0;
    for(Inventory listBook : list) {
        if(listBook.getYear() == year) {
            index = list.indexOf(listBook);
            index += index / list.size();
            System.out.println(list.get(index));
        }
    }
}

This is my Inventory class

package bookStore;

public class Inventory {

private int isbn;
private String title;
private int year;
private String author;
private double price;

public Inventory() {
    this.isbn = 0;
    this.title = "";
    this.year = 0;
    this.author = "";
    this.price = 0.0;
}

public Inventory(int isbn, 
        String title,
            int year, 
            String author, 
            double price) {
    this.isbn = isbn;
    this.title = title;
    this.year = year;
    this.author = author;
    this.price = price;
}

//Getters
public int getIsbn() {
    return this.isbn;
}
public String getTitle() {
    return this.title;
}
public int getYear() {
    return this.year;
}
public double getPrice() {
    return this.price;
}
public String getAuthor() {
    return this.author;
}

//Setters
public void setIsbn(int isbn) {
    this.isbn = isbn;
}
public void setTitle(String title) {
    this.title = title;
}
public void setYear(int year) {
    this.year = year;
}
public void setAuthor(String author) {
    this.author = author;
}
public void setPrice(double price) {
    this.price = price;
}

public String toString() {
    return ("ISBN: " + isbn + "\t" 
            + "Title: " + title + "\t"
            + "Year: " + year + "\t"
            + "Author: " + author + "\t"
            + "Price: " + price);
 }  
}

This is my InventoryList class(where I think my problem is)

package bookStore;

import java.util.ArrayList;

public class InventoryList {

int isbn = 0;
String title = "";
int year = 0;
String author = "";
double price = 0.0;

ArrayList<Inventory>list = new ArrayList<Inventory>();



//adding new books
public void addBook(int isbn, String title, 
        int year, String author, double price) {
        list.add(new Inventory(isbn, title, year, 
                    author, price));
}

//delete a book using its ISBN number
public void delete(int isbn) {
    int index = 0;
    for(Inventory listBook : list) {
        if(listBook.getIsbn() == isbn) {
            index = list.indexOf(listBook);
        }
    }
    list.remove(index);
}

//Searches for a book
public void searchBook(int isbn) {
    int index = 0;
    for(Inventory listBook : list) {
        if(listBook.getIsbn() == isbn) {
            index = list.indexOf(listBook);
            System.out.println(list.get(index));
        }
    }
}

//print out books of year chosen by user
public void bookYear(int year) {
    int index = 0;
    for(Inventory listBook : list) {
        if(listBook.getYear() == year) {
            index = list.indexOf(listBook);
            index += index / list.size();
            System.out.println(list.get(index));
        }
    }
}

//print out the sum of all books price
public double priceAll(double price) {
    int price1 = 0;
    for(Inventory listBook : list) {
        price1 += listBook.getPrice();
    }
    return price1;
}

//print out all books
public void listBooks(int isbn, String title, int year, 
        String author, double price) {

    for(Inventory listBook : list) {
        System.out.println(listBook);
    }
 }
}

This is my InventoryClient class

package bookStore;

import java.util.Scanner;

public class InventoryClient {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    int isbn = 0;
    String title = "";
    int year = 0;
    String author = "";
    double price = 0.0;
    int menu = 0;
    int isbn2 = 0;
    int isbn3 = 0;

    InventoryList book = new InventoryList();
    Scanner scan = new Scanner(System.in);

    do {
        System.out.println("\n1 - New Book");
        System.out.println("2 - Books By Year");
        System.out.println("3 - Total of Inventory Price");
        System.out.println("4 - Search Book");
        System.out.println("5 - Erase Book");
        System.out.println("6 - List of All Books");
        System.out.println("7 - Exit");
        System.out.print("\nEnter Number from Menu: ");
        menu = scan.nextInt();

        if(menu == 1) { //New Book
            System.out.print("Enter ISBN: ");
            isbn = scan.nextInt();
            System.out.print("Enter Title: ");
            title = scan.next();
            System.out.print("Enter Year: ");
            year = scan.nextInt();
            System.out.print("Enter Author: ");
            author = scan.next();
            System.out.print("Enter Price: ");
            price = scan.nextDouble();
            book.addBook(isbn, title, year, author, price);
        }
        if(menu == 2) { //Books by year
            System.out.println("Enter year: ");
            int year2 = scan.nextInt();
            book.bookYear(year);
        }
        if(menu == 3) { //Inventory Price   
            System.out.println(book.priceAll(price));
        }
        if(menu == 4) { //Search Book
            System.out.println("Enter ISBN of Book you wish to find: ");
            isbn3 = scan.nextInt();
            //System.out.print("The Book is ");
            book.searchBook(isbn3);
        }
        if(menu == 5) { //Erase Book
            System.out.println("Enter ISBN of Book you wish to delete: ");
            isbn2 = scan.nextInt();
            book.delete(isbn2);
            System.out.println("Book Deleted"); 
        }
        if(menu == 6) { //List of Books
            book.listBooks(isbn, title, year, author, price);
        }
    }while(menu != 7);//Exit
        System.out.println("\nGood Bye!");
 }
}
2
  • Well you're printing out an element of the List.. which is a book. Did you want: System.out.println(index);? Commented Dec 11, 2018 at 20:07
  • You aren't doing any counting. indexOf tells you the index in the list where the particular book exists. You need to count the number of books and then afterwards you print the count divided by the list size. Commented Dec 11, 2018 at 20:08

3 Answers 3

1

Like I said in my comment, you are using getIndex, which just gives you the index of a book in the list. To get a percentage, you need to count the number of books with matching year, and then after the loop you divide the count by the size of the list:

public void bookYear(int year) {
    int count = 0;
    for (Inventory listBook : list) {
        if (listBook.getYear() == year) {
            count++;
        }
    }
    double percentage = (double)count / list.size() * 100.0;
    System.out.println(percentage);
}
Sign up to request clarification or add additional context in comments.

Comments

1

In your method:

public void bookYear(int year) {
    int index = 0;
    for(Inventory listBook : list) {
        if(listBook.getYear() == year) {
            index = list.indexOf(listBook);
            index += index / list.size();
            System.out.println(list.get(index));
        }
    }
}

You are just adding setting the index to the index of the book. Note that:

index += index / list.size();

will be performing integer division, and will always result in zero as the size will always be more than the index. This means that you are always adding 0 to index. To get the average you need to count how many books have the year, and then divide it by the size. So something more like:

public void bookYear(int year) {
    double index = 0.0;
    for(Inventory listBook : list) {
       if(listBook.getYear() == year) {
           index++;
        }
    }
    System.out.println(index / (double)list.size());
}    

Comments

1

Your logic is flawed. You want to count the number of book for the year and compute the percentage after the loop and not inside the loop.
Besides generally you want to return the computed result to exploit it later (print or anything). Try something like :

public float bookYear(int year) {
    int count = 0;
    for(Inventory listBook : list) {
        if(listBook.getYear() == year) {
            count++;
        }
    }

   return (float)count / list.size();       
}

You could write such a logic in this way in Java 8 :

float prct = 
    list.stream()
        .filter(i -> i.getYear == year)
        .count() 
        / (float) list.size();

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.