Reputation: 43
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!");
}
}
Upvotes: 1
Views: 1739
Reputation: 131346
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();
Upvotes: 1
Reputation: 12819
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());
}
Upvotes: 1
Reputation: 24671
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);
}
Upvotes: 1