Manager.java:文件
public
class
Manager {
//定义一个集合来存储图书信息
private
ArrayList<Book> list =
new
ArrayList<>();
/**
* 添加图书
* @param book
*/
public
void
add(Book book){
list.add(book);
}
/**
* 修改--根据书名修改价格
* @param name
* @param price
*/
public
void
update(String name,
double
price){
for
(
int
i =
0
; i < list.size(); i++) {
Book book = list.get(i);
if
(name.equals(book.getName())){
book.setPrice(price);
System.out.println(
"修改成功:"
+book);
}
}
}
/**
* 根据id删除图书
* @param id
*/
public
void
delete(
int
id){
for
(
int
i =
0
; i < list.size(); i++) {
Book book = list.get(i);
if
(id==book.getId()){
list.remove(book);
System.out.println(
"删除成功"
);
}
}
}
/**
* 查询--根据id查询图书信息
* @param id
*/
public
Book query(
int
id){
for
(
int
i =
0
; i < list.size(); i++) {
Book book = list.get(i);
if
(id==book.getId()){
System.out.println(book);
return
book;
}
}
return
null
;
}