ArrayList无法正确输出

时间:2018-12-10 01:20:34

标签: java

因此,我的名为InventoryList的类具有Arraylist,并且具有添加新书籍,删除书籍,获取所有书籍价格等的方法。

现在,所有内容都可以正常运行,但是当我运行它并尝试输出我添加的图书清单时,所有内容都是一样的。因此,如果我输入以下内容:

  
      
  • ISBN:1111 ----- ISBN:2222
  •   
  • Ttile:很酷------- Ttile:为什么
  •   
  • 年份:2018 -----年份:2018
  •   
  • 作者:llama-作者:llama
  •   
  • 价格:20.00 ----价格:20.00
  •   

当我把它列为列表时,我会得到

  

2222为什么2018骆驼20.00

     

2222为什么2018骆驼20.00

我不知道出什么问题了,我已经到处搜索了。

我的代码有什么问题?

这是Inventory class,用于存储ISBN,标题,年份,作者和价格。

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

这是具有ArrayList及其方法的 Edited InventoryList类。

package bookStore;

import java.util.ArrayList;

public class InventoryList {

private int isbn;
private String title;
private int year;
private String author;
private double price;
Inventory books = new Inventory(isbn, title, year, author, price);
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
//given by professor
public void delete(int isbn) {
    int index = 0;
    for(Inventory listBook : list) {
        if(books.getIsbn() == isbn) {
            index = list.indexOf(listBook);
            delete(index);
        } 
    }
}

//Searches for a book
public int searchBook(int isbn) {
    int index = 0;
    for(Inventory listBook : list) {
        if(books.getIsbn() == isbn) {
            index = list.indexOf(listBook);
        }
    }
    return index;
}

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

//print out the sum of all books price
public double priceAll(double price) {
    int price1 = 0;
    for(Inventory listBook : list) {
        if(books.getPrice() == price) {
            list.indexOf(listBook);
            price1 += price;
        }
    }
    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.getIsbn() + "\t"
                + listBook.getTitle() + "\t"
                + listBook.getYear() + "\t"
                + listBook.getAuthor() + "\t"
                + listBook.getPrice());

    }
}
}

编辑:这是我正在使用的主要

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) {
            book.addBook(isbn, title, year, author, price);
            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();
        }
        if(menu == 2) {

        }
        if(menu == 3) {

            System.out.println(book.priceAll(price));

        }
        if(menu == 4) {
            System.out.println("Enter ISBN of Book you wish to find: ");
            isbn3 = scan.nextInt();
            book.searchBook(isbn3);
        }
        if(menu == 5) {
            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) {
            book.listBooks(isbn, title, year, author, price);
        }
    }while(menu != 7);//Exit
        System.out.println("\nGood Bye!");
 }
}

3 个答案:

答案 0 :(得分:2)

不要尝试懒惰地添加书本。这个

Inventory books = new Inventory(isbn, title, year, author, price);
ArrayList<Inventory>list = new ArrayList<Inventory>();

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

        books.setIsbn(isbn);
        books.setTitle(title);
        books.setYear(year);
        books.setAuthor(author);
        books.setPrice(price);  
}

应该像

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

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

答案 1 :(得分:1)

此:

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

    books.setIsbn(isbn);
    books.setTitle(title);
    books.setYear(year);
    books.setAuthor(author);
    books.setPrice(price);  
}

应该是:

public void addBook(int isbn, String title, int year, String author, double price) {
    books.setIsbn(isbn);
    books.setTitle(title);
    books.setYear(year);
    books.setAuthor(author);
    books.setPrice(price);

    list.add(books);
}

而且,这个:

public void listBooks(int isbn, String title, int year, String author, double price) {
    for(Inventory listBook : list) {

        books.setIsbn(isbn);
        books.setTitle(title);
        books.setYear(year);
        books.setAuthor(author);
        books.setPrice(price);

        System.out.println(books.getIsbn() + "\t"
                + books.getTitle() + "\t"
                + books.getYear() + "\t"
                + books.getAuthor() + "\t"
                + books.getPrice());
        //return listBook;
    }
//return books.getIsbn();
}

应该是:

public void listBooks() {
    for(Inventory listBook : list) {

    System.out.println(listBook.getIsbn() + "\t"
                + listBook.getTitle() + "\t"
                + listBook.getYear() + "\t"
                + listBook.getAuthor() + "\t"
                + listBook.getPrice());
    }
}

编辑。这在您的主机内部是错误的。

if(menu == 1) {
    book.addBook(isbn, title, year, author, price);
    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();
}

您首先要添加带有空值的书,然后将值分配给变量。 您应该这样写:

if(menu == 1) {
    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();

    // this should be at end, after you assign values to variables
    book.addBook(isbn, title, year, author, price);
}

答案 2 :(得分:1)

您的代码有几个错误,让我们一一解决

1)将Book对象添加到列表中

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

}

OR

 //adding new books
public void addBook(int isbn, String title, int year, String author, double price) {
   Inventory books = new Inventory();
   books.setIsbn(isbn);
    books.setTitle(title);
    books.setYear(year);
    books.setAuthor(author);
    books.setPrice(price); 
    list.add(books);

}

2)要显示列表中的所有书籍,只需打印书籍属性

//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.getIsbn() + "\t"
                + listBook.getTitle() + "\t"
                + listBook.getYear() + "\t"
                + listBook.getAuthor() + "\t"
                + listBook.getPrice());
}