java - 在java中对arraylist中的元素索引求和

标签 java

所以,我有一个图书 list 类,我想在其中输入一本书的年份,程序应该给我那一年的图书百分比。现在,当我运行我的代码时,它会给我输入的最后一本书,甚至没有数字。我不知道我的代码有什么问题。我的代码有误吗?

这是我创建的有问题的方法。

    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));
        }
    }
}

这是我的 Inventory

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);
 }  
}

这是我的 InventoryList 类(我认为我的问题出在这里)

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);
    }
 }
}

这是我的 InventoryClient

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!");
 }
}

最佳答案

正如我在评论中所说,您正在使用 getIndex,它只为您提供列表中一本书的索引。要获得百分比,您需要计算匹配年份的书籍数量,然后循环之后将计数除以列表的大小:

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);
}

关于java - 在java中对arraylist中的元素索引求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53731576/

相关文章:

java - 逻辑 : equal with the contents of a hidden field

java - 如何在 main 方法中调用 wait() 方法?如果我使用它,它将给出 java.lang.IllegalMonitorStateException

java - 在 AS400 上显示 DB2 中的 BLOB 内容

java - 如何检查模式是否由 <jdbc :embedded-database> tag 创建

java - 引用的文件包含错误 (http ://java. sun.com/xml/ns/javaee/web-app.xsd)

java - 当属性为 null 时 Javers 抛出异常

java - 如何使用 JCache 创建缓存键?

java - 如何防止 CFEXECUTE 在 PrintStackTrace 之后挂起

java - 清除整个数据库(用于使用 Hibernate 进行单元测试)

java - 如何让玩家始终指向鼠标