编写三个类,图书类、学生类和测试类,可以处理简单的书籍借阅情况,包括借书和还书等。图书类的数据成员包括书名、书号和借书学生等;方法包括借书、还书和显示书籍信息等。学生类的数据成员包括姓名、学号和所借书籍等;方法包括显示学生信息等。测试类是一个应用程序,在该程序中创建图书类和学生类对象,完成图书的借阅和归还。
package priv.lhw.bookborrowing;
import java.util.Objects;
public class Book {
/** 书名 */
private String name;
/** 书号 */
private String id;
/** 借阅此书的学生 */
private Student student;
/** 学生借阅此书时,此书在学生所借图书列表中的序号 */
private int indexInStudentBorrowList;
/**
* 带参构造函数
* @param name 书名
* @param id 书号
*/
public Book(String name, String id){
this.name = name;
this.id = id;
student = null;
indexInStudentBorrowList = 0;
}
/**
* 获取书名
* @return 书名
*/
public String getName(){
return name;
}
/**
* 获取借阅此书的学生
* @return 借书的学生
*/
public Student getStudent(){
return student;
}
/**
* 获取学生借阅此书时,此书在学生所借图书列表中的序号
* @return 图书所在列表中的序号
*/
public int getIndexInStudentBorrowList(){
return indexInStudentBorrowList;
}
/**
* 图书被某个学生借走
* @param student 借阅此书的学生
*/
public void borrowBy(Student student){
if(this.student != null){
System.out.println(this.name + "已被" + this.student.getName() + "借阅!");
}
this.student = student;
indexInStudentBorrowList = student.getCount();
student.borrow(this);
}
/**
* 图书被借阅此书的学生归还
* @param student 借阅此书的学生
*/
public void giveBackBy(Student student){
if(!Objects.equals(this.student.getId(), student.getId())){
System.out.println(student.getName() + "没有借阅" + this.name + "!");
return;
}
student.giveBack(this);
this.student = null;
}
@Override
public String toString(){
String string = "书名: " + name + "\n" + "书号: " + id + "\n" + "借书学生: ";
string += student == null ? "无" : student.getName();
return string;
}
}
package priv.lhw.bookborrowing;
import java.util.ArrayList;
public class Student {
/** 学生姓名 */
private final String name;
/** 学生学号 */
private final String id;
/** 学生手中图书数量 */
private int count;
/** 学生借书列表 */
private ArrayList<Book> borrowList;
/**
* 带参构造函数
* @param name 学生姓名
* @param id 学生学号
*/
public Student(String name, String id){
this.name = name;
this.id = id;
count = 0;
borrowList = new ArrayList<>();
}
/**
* 获取学生姓名
* @return 姓名
*/
public String getName(){
return name;
}
/**
* 获取学生学号
* @return 学号
*/
public String getId(){
return id;
}
/**
* 获取学生手中图书数量
* @return 学生手中图书数量
*/
public int getCount(){
return count;
}
public String[] getBorrowList(){
String[] books = new String[count + 1];
int index = 0;
for (Book book : borrowList){
books[index++] = book.getName();
}
return books;
}
/**
* 学生借书
* @param book 所借图书
*/
public void borrow(Book book) {
count++;
borrowList.add(book);
}
/**
* 学生还书
* @param book 所还图书
*/
public void giveBack(Book book){
borrowList.remove(book.getIndexInStudentBorrowList());
count--;
}
@Override
public String toString(){
StringBuilder string = new StringBuilder("姓名: " + name + "\n" +
"学号: " + id + "\n" +
"已借图书: ");
for (Book book : borrowList){
string.append(book.getName()).append("; ");
}
return string.toString();
}
}
package priv.lhw.bookborrowing;
import javax.swing.*;
import java.util.Objects;
public class Test {
static Book java = new Book("Java程序设计", "ISBN 978-7-5193-0612-0");
static Book c = new Book("C语言程序设计", "ISBN 978-7-5682-8878-1");
static Book python = new Book("Python程序设计", "ISBN 978-7-5620-9481-4");
static Student zhang = new Student("张三", "2022170041");
static Student li = new Student("李四", "2022170040");
static Student wang = new Student("王五", "2022170038");
static Student zhao = new Student("赵六", "2012170037");
static String[] books = {java.getName(), c.getName(), python.getName()};
static Book[] allBook = {java, c, python};
static Student[] students = {zhang, li, wang, zhao};
/**
* 查找图书
* @param s 书名
* @return 返回找到的图书未找到返回null
*/
public static Book findBook(String s){
Book curBook = null;
for (Book book : allBook){
if(Objects.equals(book.getName(), s)){
curBook = book;
}
}
return curBook;
}
/**
* 查找学生
* @param id 学号
* @return 返回查找到的学生未找到返回null
*/
public static Student findStudent(String id){
Student curStudent = null;
for (Student student : students){
if(Objects.equals(student.getId(), id)){
curStudent = student;
}
}
return curStudent;
}
/**
* 输入学号
* @return 返回学号对应的学生,不存在则返回null
*/
public static Student inputId(boolean isBorrow){
JPanel jPanel = new JPanel();
JTextField id = new JTextField(10);
jPanel.add(new JLabel("请输入学号: "));
jPanel.add(id);
String title = isBorrow ? "图书借阅" : "图书归还";
Student student;
while (true){
int option = JOptionPane.showConfirmDialog(null, jPanel, title, JOptionPane.OK_CANCEL_OPTION);
//是否点击取消按钮
if (option == JOptionPane.CANCEL_OPTION){
System.exit(0);
}
//判断输入是否为数字
if (id.getText().matches("^[0-9]*$")){
student = findStudent(id.getText());
if (student != null){
break;
}
}
JOptionPane.showMessageDialog(null, "您输入的学号有误或无此学生!", "温馨提示",
JOptionPane.INFORMATION_MESSAGE);
}
return student;
}
/**
* 初始界面
* @return 0代表借书,1代表还书
*/
public static int init(){
Object[] options = {"借书", "还书"};
return JOptionPane.showOptionDialog(null, "欢迎使用图书借阅系统!", "图书借阅系统",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,null, options, options[0]);
}
/**
* 借书
*/
public static void borrow(){
String s = (String) JOptionPane.showInputDialog(null,"请选择您要借阅的图书: ","图书借阅",
JOptionPane.PLAIN_MESSAGE, new ImageIcon("icon.png"), books, books[0]);
//是否点击取消按钮
if (s == null){
System.exit(0);
}
Book book = findBook(s);
if (book.getStudent() != null){
JOptionPane.showMessageDialog(null, "此书已被" + book.getStudent().getName() + "借走!",
"温馨提示", JOptionPane.INFORMATION_MESSAGE);
return;
}
Student student = inputId(true);
book.borrowBy(student);
showResult(student, book, true);
}
/**
* 还书
*/
public static void giveBack(){
Student student = inputId(false);
if (student.getBorrowList()[0] == null){
JOptionPane.showMessageDialog(null, "您没有未归还的图书!",
"温馨提示", JOptionPane.INFORMATION_MESSAGE);
return;
}
String bookName = (String) JOptionPane.showInputDialog(null,"请选择您要归还的图书: ",
"归还图书", JOptionPane.PLAIN_MESSAGE, new ImageIcon("icon.png"), student.getBorrowList(),
student.getBorrowList()[0]);
if (bookName == null){
System.exit(0);
}
Book book = findBook(bookName);
book.giveBackBy(student);
showResult(student, book, false);
}
/**
* 显示操作结果
* @param student 操作对应的学生
* @param book 操作对应的图书
* @param isBorrow true代表借书反之代表还书
*/
public static void showResult(Student student, Book book, boolean isBorrow){
String suffix = isBorrow ? "借阅成功!" : "归还成功!";
JOptionPane.showMessageDialog(null, book.toString() + "\n" + student.toString() + "\n" +
book.getName() + suffix);
}
public static void main(String[] args){
c.borrowBy(zhang);
python.borrowBy(zhang);
if (init() == 0){
borrow();
}else {
giveBack();
}
System.exit(0);
}
}