因此,我有一个名为InventoryList
的类,它具有一个Arraylist。我必须设法添加新书,删除书,获取所有书的价格等。我有一些下来,但我不知道如何添加ISBN,书名,年份,作者和价格的书?
这是Inventory
类,用于存储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(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);
}
}
答案 0 :(得分: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();
}
}
答案 1 :(得分:0)
不,它不能满足您的目标。创建InventoryList
时,您的list
为空,因此循环中的代码将无法运行。您的addBook
方法是否要将一本书添加到列表中?只需删除循环,并使用此构造函数list.add(book)
book
参数构造将其更改为Inventory(int isbn, String title, int year, String author, double price)