【发布时间】: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