gpt4 book ai didi

java - 将书籍添加到 ArrayList 库存类

转载 作者:行者123 更新时间:2023-12-02 10:29:47 24 4
gpt4 key购买 nike

所以,我有一个名为 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);
}
}

最佳答案

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

关于java - 将书籍添加到 ArrayList 库存类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53677307/

24 4 0