【问题标题】:Adding Books to ArrayList Inventory Class将书籍添加到 ArrayList 库存类
【发布时间】:2018-12-07 21:58:28
【问题描述】:

所以,我有一个名为 InventoryList 的类,它有一个 Arraylist。我必须制定方法来添加新书、删除书、获取所有书的价格等。我记下了一些,但我不知道如何添加带有 ISBN、标题、年份、作者和价格的书?

这是我存储 ISBN、标题、年份、作者和价格的 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);
 }
}

这是带有 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(books);

        books.setIsbn(isbn);
        books.setTitle(title);
        books.setYear(year);
        books.setAuthor(author);
        books.setPrice(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);
        } 
    }
}

//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 int priceAll(int 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() {
    for(Inventory listBook : list) {
        System.out.println(books.getIsbn() + "\t"
                    + books.getTitle() + "\t"
                    + books.getYear() + "\t"
                    + books.getAuthor() + "\t"
                    + books.getPrice());
        //return listBook;
    }
    //return books.getIsbn();
 }
}

这里的这部分是否已经涵盖了这一点?就像添加图书的 ISBN、标题、年份、作者和价格一样。

public void addBook(Inventory book) {
    for(Inventory listBook : list) {
        list.add(book);
    }
}

【问题讨论】:

  • Remove Inventory book = new Inventory(); / 使用 en 迭代器从列表中删除元素,或 .removeIf() 在 SO 或 google 上查找详细信息 / 添加,只添加一次,没有循环

标签: java


【解决方案1】:
public final class Book {
    private final int isbn;
    private final String title;
    private final int year;
    private final String author;
    private final double price;

    // getters
}

public final class BookStore {
    private final Map<Integer, Book> books = new HashMap<>();

    public void addBook(Book book) {
        if(book != null)
            books.put(book.getIsbn(), book);
    }

    public Book getBook(int isbn) {
        return books.get(isbn);
    }

    public Book removeBook(int isbn) {
        return books.remove(isbn);
    }

    public double getAllBooksPrice() {
        return books.values().stream().map(Book::getPrice()).sum();
    }
}

【讨论】:

  • 答案旁边应该总是有文字,没有它就没有vzlue
【解决方案2】:

不,它不包括您的目标。当您的InventoryList 创建时,您的list 为空,因此循环中的代码将不会运行。您的addBook 方法是否想将一本书添加到您的列表中?它只需删除循环并将其更改为list.add(book)book 参数构造与此构造函数Inventory(int isbn, String title, int year, String author, double price)

【讨论】:

  • addBook 是为了让用户可以自己在客户端类中添加书籍
  • 为您的用户想要添加的每本书致电addBook
猜你喜欢
  • 1970-01-01
  • 2022-01-16
  • 2013-10-21
  • 1970-01-01
  • 2021-04-30
  • 2015-01-20
  • 2013-05-02
  • 1970-01-01
  • 2015-03-30
相关资源
最近更新 更多