查找ArrayList <model>中元素的百分比

时间:2017-12-07 01:36:22

标签: java arraylist

所以我正在做一个项目,我想构建一个带有两个ArrayLists的库,其中一个是ArrayList'&lt;'Book'&gt;' BookList包含一个名为quantity的元素必须大于或等于零如果书的数量大于零,则BookList中另一个名为status的元素被设置为In-stock,如果它等于零则设置为借用。我正在尝试创建一个查看BookList的方法,并显示借来的书籍的百分比。我通过列表完成了这个,每次找到一本数量低于1的书,换句话说0计数器上升一个,所以最后我只是从BookList.size()中减去计数器,除了结果使用BookList.size(),将其乘以100并将其打印出来。

主类

public class Main {
    public static void main(String[] args) {
    Scanner keyb = new Scanner(System.in);
    int uinput;
    Library nag;
    try{
    nag = new Library();
    do{
        System.out.println("Type 1 to add a book.");
        System.out.println("Type 2 to show how many books are borrowed.");
        uinput = keyb.nextInt();
        if (uinput==1){
            nag.addBook();
        }
        if (uinput==2){
            nag.statistics();
        }
    }while (uinput > 0);
    }
    catch(Exception e){
        System.out.println("Invalid entry.");
    }
}//end of main
}//end of class

书类

public class Book {

    private String Title;
    private String Author;
    private String ISBN;
    private String Publisher;
    private String Publication_Date;
    private String Price;
    private int Quantity;
    private String Status;

    public Book(){
        Title= "";
        Author="";
        ISBN="";
        Publisher="";
        Publication_Date="";
        Price="";
        Quantity=1;
        Status="IN-STOCK";
    }

    //getters
    public String gettitle(){return Title;}
    public String getauthor(){return Author;}
    public String getisbn(){return ISBN;}
    public String getpublisher(){return Publisher;}
    public String getpublication_date(){return Publication_Date;}
    public String getprice(){return Price;}
    public int getquantity(){return Quantity;}
    public String getstatus(){return Status;}

    //setters
    public void settitle(String t){Title = t;}
    public void setauthor(String a){Author = a;}
    public void setisbn(String is){ISBN = is;}
    public void setpublisher(String p){Publisher = p;}
    public void setpublication_date(String pd){Publication_Date = pd;}
    public void setprice(String pr){Price = pr;}
    public void setquantity(int q){Quantity = q;}
    public void setstatus(String s){Status = s;}

}//end of class

图书馆类

public class Library {

private ArrayList<Book> BookList;

public Library(){
    BookList = new ArrayList<Book>();
}//end of constructor 1

public Library(ArrayList<Book> l) {
    BookList = l;
}//end of constructor 3

public void addBook(){
    try{
        Scanner keyb = new Scanner(System.in);
        Book bo = new Book();

        System.out.println("Type the title: ");
        String title_input;
        title_input = keyb.nextLine();
        bo.settitle(title_input);

        System.out.println("Type the author: ");
        String author_input;
        author_input = keyb.nextLine();
        bo.setauthor(author_input);

        System.out.println("Type the isbn: ");
        String isbn_input;
        isbn_input = keyb.nextLine();
        bo.setisbn(isbn_input);

        System.out.println("Type the publisher: ");
        String publisher_input;
        publisher_input = keyb.nextLine();
        bo.setpublisher(publisher_input);

        System.out.println("Type the publication date: ");
        String publication_date_input;
        publication_date_input = keyb.nextLine();
        bo.setpublication_date(publication_date_input);

        System.out.println("Type the price: ");
        String price_input;
        price_input = keyb.nextLine();
        bo.setprice(price_input);

        System.out.println("Type the quantity: ");
        int quantity_input = Integer.parseInt(keyb.nextLine());
        if (quantity_input >= 0){
            bo.setquantity(quantity_input);
            if (quantity_input > 0){
                bo.setstatus("IN_STOCK");
            }
            if(quantity_input == 0){
                bo.setstatus("BORROWED");
            }
            BookList.add(bo);
            System.out.println("Book added successfully.");
        }
    }catch(Exception e){
        System.out.println("Invalid entry");
    }//end of addBook() 

public void statistics(){
    Book bo = new Book();
    int counter = 0;
    for(int i=0; i < BookList.size();i++){
        bo= BookList.get(i);
        int holdquantity = bo.getquantity();
        if (holdquantity  < 1){
            counter++;
        }  
    }
    double substraction=BookList.size() - counter;
    double division= substraction/BookList.size();
    double percentage = division * 100;
    System.out.print(percentage + "%");
}//end of statistics()
}//end of class

问题在于,当我有一本零数量的书和另一本数量大于零的书时,它会保持100.0%的打印率。 所以我想知道问题是否存在于此代码中或其他地方。

1 个答案:

答案 0 :(得分:0)

假设您的Book类如下:

package com.company;

public class Book {

    private int quantity;

    public Book(int quantity) {
        this.quantity = quantity;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
}

您的主要内容如下:

package com.company;

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main (String[]args) {

        List<Book> bookList = new ArrayList<>(){{
            add(new Book(4));
            add(new Book(0));
            add(new Book(3));
            add(new Book(7));
            add(new Book(0));
            add(new Book(0));
            add(new Book(1));
            add(new Book(9));
            add(new Book(0));
            add(new Book(5));
        }};

        int booksOutOfStock = 0;

        for (int i = 0; i < bookList.size(); i++) {
            if (bookList.get(i).getQuantity() == 0)
                booksOutOfStock++;
        }

        double percentage = 100d / bookList.size() * booksOutOfStock;

        System.out.printf("Out of total %d books, %d are out of stock, which makes %.2f%%", bookList.size(), booksOutOfStock,
                                                                                                        percentage);
    }
}

请检查此代码,将其与您的代码进行比较,看看错误在哪里,或在此处发布完整代码,以便我们提供更多帮助。

上面的代码输出:

  

在总共10本书中,有4本缺货,这使得40.00%

好的,所以在你发布你的代码后我回顾了它,除了做一些艰难的事情,你的代码是“工作”,我所要做的就是在addBook的末尾在Library类中添加一个右括号catch块之后的()方法:

catch(Exception e){
        System.out.println("Invalid entry");
    } --> } <--- (added)

    //end of addBook()

我修正了你的百分比代码逻辑,你的代码会打印出非借用书籍的数量作为借用的百分比,这里是代码改变了:

    double division= (double)counter/BookList.size();
    double percentage = division * 100;
    System.out.printf("%.2f", percentage);