Java ArrayList输出不正确

Java ArrayList输出不正确,java,Java,因此,我的类名为InventoryList,它有一个Arraylist,并有添加新书、删除一本书、获取所有书籍价格等方法 现在,它都可以工作了,但当我运行它并试图输出我添加的书籍列表时,结果都是一样的。因此,如果我输入以下内容: ISBN:1111------ISBN:2222 泰尔:酷------泰尔:为什么 年份:2018年------年份:2018年 作者:骆驼——作者:骆驼 价格:20.00----价格:20.00 当我把它列成一张单子的时候 2222为什么2018美洲驼20.00

因此,我的类名为
InventoryList
,它有一个Arraylist,并有添加新书、删除一本书、获取所有书籍价格等方法

现在,它都可以工作了,但当我运行它并试图输出我添加的书籍列表时,结果都是一样的。因此,如果我输入以下内容:

  • ISBN:1111------ISBN:2222
  • 泰尔:酷------泰尔:为什么
  • 年份:2018年------年份:2018年
  • 作者:骆驼——作者:骆驼
  • 价格:20.00----价格:20.00
当我把它列成一张单子的时候

2222为什么2018美洲驼20.00

2222为什么2018美洲驼20.00

我不知道怎么了,我到处都找遍了

我的代码有什么问题

这是
库存类
,我在其中存储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及其方法编辑的
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());
        
    }
}
}

不要试着懒洋洋地添加你的书。这个

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);  
}
存货账簿=新存货(isbn、标题、年份、作者、价格);
ArrayList=新的ArrayList();
//添加新书
public void addBook(整数isbn,字符串标题,整数年,字符串作者,双倍价格){
增加(书籍);
图书.setIsbn(isbn);
书籍。书名(书名);
书籍。设置年份(年);
书籍。作者(作者);
图书。定价(价格);
}
应该像

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));
}
List List=new ArrayList();
//添加新书
public void addBook(整数isbn,字符串标题,整数年,字符串作者,双倍价格){
添加(新库存(isbn、标题、年份、作者、价格);
}

不要尝试懒洋洋地添加书籍。这个

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);  
}
存货账簿=新存货(isbn、标题、年份、作者、价格);
ArrayList=新的ArrayList();
//添加新书
public void addBook(整数isbn,字符串标题,整数年,字符串作者,双倍价格){
增加(书籍);
图书.setIsbn(isbn);
书籍。书名(书名);
书籍。设置年份(年);
书籍。作者(作者);
图书。定价(价格);
}
应该像

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));
}
List List=new ArrayList();
//添加新书
public void addBook(整数isbn,字符串标题,整数年,字符串作者,双倍价格){
添加(新库存(isbn、标题、年份、作者、价格);
}
这是:

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() {
    for(Inventory listBook : list) {

    System.out.println(listBook.getIsbn() + "\t"
                + listBook.getTitle() + "\t"
                + listBook.getYear() + "\t"
                + listBook.getAuthor() + "\t"
                + listBook.getPrice());
    }
}
而且,这:

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 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() {
    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();
}
首先添加带有空值的book,然后将值分配给变量。 你应该这样说:

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

应该是:

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() {
    for(Inventory listBook : list) {

    System.out.println(listBook.getIsbn() + "\t"
                + listBook.getTitle() + "\t"
                + listBook.getYear() + "\t"
                + listBook.getAuthor() + "\t"
                + listBook.getPrice());
    }
}
而且,这:

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 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() {
    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();
}
首先添加带有空值的book,然后将值分配给变量。 你应该这样说:

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

您的代码有两个bug,让我们逐一修复

1) 将书本对象添加到列表的步骤

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

}

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

您的代码有两个bug,让我们逐一修复

1) 将书本对象添加到列表的步骤

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

}

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

您的
listBooks()
方法没有任何意义。它根本不使用您的列表。您的
listBooks()
方法没有任何意义。它根本不使用您的列表。我这样做了,但现在我在运行它时得到了这个信息“0”“0”“0.0”“1111 aaaa 2018 sarid 20.0”您可以用main方法更新完整的代码以及您要添加的内容吗@SaridRuiz@SaridRuiz您的if(menu==1)块不好。检查我的回答我这样做了,但现在我在运行它时得到了这个'0”“0”“0.0”“1111 aaaa 2018 sarid 20.0'您可以用main方法更新完整的代码以及您要添加的内容吗@SaridRuiz@SaridRuiz您的if(menu==1)块不好。检查我的回答我不敢相信是主电路出了问题!非常感谢你!我不敢相信这是主要的错误!非常感谢你!