网上书店系统java代码
时间: 2023-09-17 07:00:45 浏览: 151
网上书店java代码
网上书店系统是一个基于Java开发的在线书店系统。下面是一个简单的示例代码,用于实现网上书店的基本功能:
```java
import java.util.ArrayList;
import java.util.List;
public class OnlineBookStore {
private List<Book> books;
public OnlineBookStore() {
books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
}
public void removeBook(Book book) {
books.remove(book);
}
public List<Book> searchBook(String keyword) {
List<Book> matchingBooks = new ArrayList<>();
for (Book book : books) {
if (book.getTitle().contains(keyword) || book.getAuthor().contains(keyword)) {
matchingBooks.add(book);
}
}
return matchingBooks;
}
public static void main(String[] args) {
OnlineBookStore bookStore = new OnlineBookStore();
Book book1 = new Book("Java Programming", "John Smith");
Book book2 = new Book("Introduction to Python", "Jane Doe");
Book book3 = new Book("Data Structures in C", "David Williams");
bookStore.addBook(book1);
bookStore.addBook(book2);
bookStore.addBook(book3);
List<Book> searchResult = bookStore.searchBook("Java");
for (Book book : searchResult) {
System.out.println(book.getTitle() + " by " + book.getAuthor());
}
}
}
class Book {
private String title;
private String author;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
}
```
上述代码中,OnlineBookStore类实现了网上书店的主要功能,包括添加书籍、移除书籍和搜索书籍。Book类表示一本书的信息,包括书名和作者。在main方法中,演示了添加几本书,然后搜索包含关键字"Java"的书籍,并打印搜索结果。
当然,这只是一个简单的示例代码,真实的网上书店系统还需要实现更多功能,如用户登录、购买图书、管理订单等。
阅读全文