图书馆管理_图书馆事业编制怎么考

(40) 2024-06-13 21:01:01

1.描述一个图书馆

2.进书,借书卡办理

3.借书,还书

4.查询某书的借出记录

5.查询借书卡的借出记录

6.显示图书列表(按借出次数排序)


Book类

package com.situ.Java01.Library; public class Book implements Comparable<Book>{ private String bookId; private String bookName; private int bookNum; private int BorrowedCount; public String getBookId() { return bookId; } public void setBookId(String bookId) { this.bookId = bookId; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public int getBookNum() { return bookNum; } public void setBookNum(int bookNum) { this.bookNum = bookNum; } public int getBorrowedCount() { return BorrowedCount; } public void setBorrowedCount(int borrowedCount) { BorrowedCount = borrowedCount; } public Book() { } @Override public String toString() { return "Book{" + "bookId='" + bookId + '\'' + ", bookName='" + bookName + '\'' + ", bookNum=" + bookNum + ", BorrowedCount=" + BorrowedCount + '}'; } public Book(String bookId, String bookName, int bookNum, int borrowedCount) { this.bookId = bookId; this.bookName = bookName; this.bookNum = bookNum; BorrowedCount = borrowedCount; } @Override public int compareTo(Book book) { if (this.getBorrowedCount()>book.getBorrowedCount()) { return -1; } else if (this.getBorrowedCount()<=book.getBorrowedCount()) { return 1; } else { return 0; } } } 

Card类

package com.situ.Java01.Library; public class Card implements Comparable<Card>{ private String cardId; private String studentId; public Card() { } public String getCardId() { return cardId; } public void setCardId(String cardId) { this.cardId = cardId; } public String getStudentId() { return studentId; } public void setStudentId(String studentId) { this.studentId = studentId; } @Override public String toString() { return "Card{" + "cardId='" + cardId + '\'' + ", studentId='" + studentId + '\'' + '}'; } public Card(String cardId, String studentId) { this.cardId = cardId; this.studentId = studentId; } @Override public int compareTo(Card c) { if (Integer.parseInt(this.getCardId())>Integer.parseInt(c.getCardId())) { return 1; } else if (Integer.parseInt(this.getCardId())<Integer.parseInt(c.getCardId())) { return -1; } else { return 0; } } } 

Borrow类

package com.situ.library; import java.util.Calendar; public class Record implements Comparable<Record> { private String bId; private String cId; private Calendar borrowTime;; // 借入还是借出 private Calendar returnTime; // 记录时间 public String getbId() { return bId; } public void setbId(String bId) { this.bId = bId; } public String getcId() { return cId; } public void setcId(String cId) { this.cId = cId; } public Calendar getBorrowTime() { return borrowTime; } public void setBorrowTime(Calendar borrowTime) { this.borrowTime = borrowTime; } public Calendar getReturnTime() { return returnTime; } public void setReturnTime(Calendar returnTime) { this.returnTime = returnTime; } @Override public String toString() { return "Record [bId=" + bId + ", cId=" + cId + ", borrowTime=" + borrowTime + ", returnTime=" + returnTime + "]"; } @Override public int compareTo(Record o) { int num=this.getcId().length()-o.getcId().length(); int num1=num==0?this.getcId().compareTo(o.getcId()):num; return num1; } } 

测试类

package com.situ.Java01.Library; import org.junit.Test; import java.util.ArrayList; import java.util.Scanner; import java.util.TreeSet; public class LibrarySystem { Scanner sc = new Scanner(System.in); private static TreeSet<Book> bookSet = new TreeSet<Book>(); private static TreeSet<Card> cardSet = new TreeSet<Card>(); private static TreeSet<Borrow> borrowSet = new TreeSet<>(); public static void main(String[] args) { LibrarySystem test = new LibrarySystem(); test.start(); } public void start() { while (true) { System.out.println("欢迎来到图书管理系统"); System.out.println("---1-添加书籍-----"); System.out.println("---2-添加借书卡---"); System.out.println("---3-借书--------"); System.out.println("---4-还书--------"); System.out.println("---5-查询某书的借阅记录---"); System.out.println("---6-查询某卡的借阅记录---"); System.out.println("---7-按借阅次数显示图书信息---"); System.out.println("---0-退出系统---"); int line = sc.nextInt(); if (0==line){ System.out.println("程序已退出!"); break; } switch (line){ case 1: addBooks(); break; case 2: addCard(); break; case 3: borrow(); break; case 4: Return(); break; case 5: BookBorrowedRecord(); break; case 6: cardBorrowRecord(); break; case 7: showSortByBorrowedCount(); break; default: System.out.println("无效操作"); break; } } } /* * 添加图书 */ public void addBooks() { while (true) { addBook(); System.out.println("是否继续(y/n)"); if ("n".equals(sc.next())) { break; } } } public void addBook() { Book book = new Book(); System.out.println("请输入书的编号:"); book.setBookId(sc.next()); System.out.println("请输入书的名称:"); book.setBookName(sc.next()); System.out.println("请输入图书的数量:"); book.setBookNum(sc.nextInt()); book.setBorrowedCount(0); bookSet.add(book); } /*办理借书卡 * */ public void addCard() { Card card = new Card(); // Random rand = new Random(2); System.out.println("请输入学号:"); card.setStudentId(sc.next()); // card.setCardId(rand.nextInt()); System.out.println("请输入卡号:"); card.setCardId(sc.next()); cardSet.add(card); System.out.println("你的卡号是:" + card.getCardId()); for (Card c : cardSet) { System.out.println("学号:" + c.getStudentId() + "卡号:" + c.getCardId()); } } /* * 借书 * */ @Test public void borrow() { // Book book1 =new Book(1,"zz",1,1); // Card card1 =new Card(1,1); // bookSet.add(book1); // cardSet.add(card1); System.out.println("请输入你的借书卡号:"); String cardId = sc.next(); Card card = null; for (Card c : cardSet) { if (cardId.equals(c.getCardId())) { card = c; break; } } if (card == null) { System.out.println("借书卡无效!"); return; } //查询是否有该图书 System.out.println("请输入图书编号:"); String bookN = sc.next(); Book book = null; for (Book b : bookSet) { if (bookN.equals(b.getBookId())) { book = b; break; } } if (book == null) { System.out.println("没有该图书!"); } //查询图书数量 if (book.getBookNum() == 0) { System.out.println("该书已全部被借走!"); } for (Book b : bookSet ) { System.out.println(b); } book.setBookNum(book.getBookNum() - 1); book.setBorrowedCount(book.getBorrowedCount() + 1); Borrow borrow = new Borrow(); borrow.setBookId(bookN); borrow.setCardId(cardId); borrowSet.add(borrow); for (Book b : bookSet ) { System.out.println(b); } } /* *还书 **/ public void Return() { // Book book1 = new Book(1, "zz", 1, 1); // Card card1 = new Card(1, 1); // Borrow borrow1 = new Borrow("1","1","zz"); // bookSet.add(book1); // cardSet.add(card1); // borrowSet.add(borrow1); System.out.println("请输入你的借书卡号:"); String cardId = sc.next(); Card card = null; for (Card c : cardSet) { if (cardId.equals(c.getCardId())) { card = c; break; } } if (card == null) { System.out.println("借书卡无效!"); return; } //查询是否有该图书 System.out.println("请输入图书编号:"); String bookId = sc.next(); Book book = null; for (Book b : bookSet) { if (bookId.equals(b.getBookId())) { book = b; break; } } if (book == null) { System.out.println("没有该图书!"); } Borrow borrow = null; for (Borrow b : borrowSet) { if (b.getCardId().equals(cardId) && b.getBookId().equals(bookId)) { borrow = b; break; } } if (borrow == null) { System.out.println("无记录"); return; } book.setBookNum(book.getBookNum() + 1); borrowSet.remove(borrow); System.out.println("还书成功!"); for (Book b : bookSet ) { System.out.println(b); } } public void BookBorrowedRecord() { Borrow borrow = null; System.out.println("请输入图书编号:"); String bookId = sc.next(); for (Borrow b : borrowSet) { if (b.getBookId().equals(bookId)) { borrow = b; System.out.println("图书编号:" + b.getBookId() + "卡号" + b.getCardId()); } } if (borrow == null) { System.out.println("没有该书的借阅记录!"); return; } } /* * 查询某卡的借阅记录 * */ public void cardBorrowRecord() { Borrow borrow = null; System.out.println("请输入图书编号:"); String cardId = sc.next(); for (Borrow b : borrowSet) { if (b.getCardId().equals(cardId)) { borrow = b; System.out.println("图书编号:" + b.getBookId() + "卡号" + b.getCardId()); } } if (borrow == null) { System.out.println("没有该书的借阅记录!"); return; } } /* * 按借阅次数排序,显示图书信息 * */ //将set集合转化为list集合,再进行排序 public void showSortByBorrowedCount() { ArrayList<Book> bookList = new ArrayList<>(bookSet); int len = bookList.size(); for (int i = 0; i < len; i++) { for (int j = 0; j < len - i - 1; j++) { Book book1 = bookList.get(j); Book book2 = bookList.get(j + 1); if (book1.getBorrowedCount() > book2.getBorrowedCount()) { Book book3 = new Book(); book1 = book3; book1 = book2; book2 = book3; book3.setBookId(book1.getBookId()); book3.setBookName(book1.getBookName()); book3.setBookNum(book1.getBookNum()); book3.setBorrowedCount(book1.getBorrowedCount()); book1.setBookId(book2.getBookId()); book1.setBookName(book2.getBookName()); book1.setBookNum(book2.getBookNum()); book1.setBorrowedCount(book2.getBorrowedCount()); book3.setBookId(book2.getBookId()); book3.setBookName(book2.getBookName()); book3.setBookNum(book2.getBookNum()); book3.setBorrowedCount(book2.getBorrowedCount()); } } } for (Book book : bookList) { System.out.println("图书号" + book.getBookId() + "借阅次数" + book.getBorrowedCount()); } } public void findBook() { for (Book b : bookSet ) { System.out.println(b); } } } 

THE END

发表回复