用Java语言开发一个spring框架的图书馆系统
时间: 2023-11-12 13:06:42 浏览: 41
好的,下面是一个简单的使用Spring框架开发的图书馆系统的Java代码示例,包括书籍类(Book)、用户类(User)、借阅记录类(BorrowRecord)、书籍服务类(BookService)和用户服务类(UserService):
```
// 书籍类
public class Book {
private String title;
private String author;
private boolean isBorrowed;
public Book(String title, String author) {
this.title = title;
this.author = author;
this.isBorrowed = false;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
}
// 用户类
public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
// 借阅记录类
public class BorrowRecord {
private Book book;
private User user;
private Date borrowDate;
private Date returnDate;
public BorrowRecord(Book book, User user, Date borrowDate) {
this.book = book;
this.user = user;
this.borrowDate = borrowDate;
}
public Book getBook() {
return book;
}
public User getUser() {
return user;
}
public Date getBorrowDate() {
return borrowDate;
}
public Date getReturnDate() {
return returnDate;
}
public void setReturnDate(Date returnDate) {
this.returnDate = returnDate;
}
}
// 书籍服务类
@Service
public class BookService {
private List<Book> books = new ArrayList<>();
public void addBook(Book book) {
books.add(book);
}
public List<Book> getAvailableBooks() {
return books.stream().filter(book -> !book.isBorrowed()).collect(Collectors.toList());
}
public Book getBookByTitle(String title) {
return books.stream().filter(book -> book.getTitle().equals(title)).findFirst().orElse(null);
}
}
// 用户服务类
@Service
public class UserService {
private List<User> users = new ArrayList<>();
public void addUser(User user) {
users.add(user);
}
public User getUserByName(String name) {
return users.stream().filter(user -> user.getName().equals(name)).findFirst().orElse(null);
}
}
// 图书馆类
@RestController
public class LibraryController {
@Autowired
private BookService bookService;
@Autowired
private UserService userService;
private List<BorrowRecord> borrowRecords = new ArrayList<>();
@GetMapping("/books")
public List<Book> listBooks() {
return bookService.getAvailableBooks();
}
@PostMapping("/borrow")
public String borrowBook(@RequestParam String title, @RequestParam String userName) {
Book book = bookService.getBookByTitle(title);
if (book == null) {
return "对不起," + title + " 不是本图书馆的书籍!";
}
if (book.isBorrowed()) {
return "对不起," + title + " 已被借出!";
}
User user = userService.getUserByName(userName);
if (user == null) {
return "对不起," + userName + " 不是本图书馆的用户!";
}
book.setBorrowed(true);
BorrowRecord borrowRecord = new BorrowRecord(book, user, new Date());
borrowRecords.add(borrowRecord);
return user.getName() + " 借阅了 " + book.getTitle();
}
@PostMapping("/return")
public String returnBook(@RequestParam String title) {
Book book = bookService.getBookByTitle(title);
if (book == null) {
return "对不起," + title + " 不是本图书馆的书籍!";
}
if (!book.isBorrowed()) {
return "对不起," + title + " 未被借出!";
}
BorrowRecord borrowRecord = borrowRecords.stream().filter(record -> record.getBook().equals(book) && record.getReturnDate() == null).findFirst().orElse(null);
if (borrowRecord == null) {
return "对不起," + title + " 不是本图书馆的书籍或未被借出!";
}
borrowRecord.setReturnDate(new Date());
book.setBorrowed(false);
return "归还 " + book.getTitle() + " 成功!";
}
}
// 测试
@SpringBootApplication
public class LibraryApplication {
public static void main(String[] args) {
SpringApplication.run(LibraryApplication.class, args);
}
}
```
这个图书馆系统使用了Spring框架,包括了书籍类、用户类、借阅记录类、书籍服务类和用户服务类,同时使用了RestController注解来创建一个RESTful的Web服务,可以通过HTTP请求进行借阅和归还书籍操作。在这个示例中,使用了内存存储来保存书籍、用户和借阅记录信息,可以根据实际情况替换为数据库存储。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)