实现创建模型

1.在app下创建名为model的包,model中建名为Book的class:
package models;

import java.util.HashSet;
import java.util.Set;

public class Book {
public Integer id;
public String title;
public Integer price;
public String author;

public Book(Integer id,String title,Integer price,String author){
this.id = id;
this.title =title;
this.price = price;
this.author =author;
}

private static Set<Book> books;
static{
books = new HashSet<>();
books.add(new Book(1,"C++",20,"leiyuxing"));
books.add(new Book(2,"JAVA",21,"yangjianwen"));
books.add(new Book(3,"C",22,"wangshiyu"));
}

public static Set<Book> allBooks(){
return books;
}

//通过id查找方法
public static Book findById(Integer id){
for(Book book:books){
if(id.equals(book.id)){
return book;
}
}
return null;
}

//添加书籍
public static void add(Book book){
books.add(book);
}

//删除书籍
public static boolean remove(Book book){
return books.remove(book);
}
}

发布了132 篇原创文章 · 获赞 35 · 访问量 13万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章