Java基础之图书管理系统一:建立信息表为图书管理人员编写一个图书管理系统,图书管理系统的设计主要是实现对图书的管理和相关操作,包括3个表: 读者信息表——存储读者的基本信息,包括学号、姓名、学院、专业班级等。 图书借阅表——存储读者借书的相关信息,包括学号、姓名、书号、书名、借阅日期、应还日期、归还日期等。 新建类: Book类: package book; import javax.print.DocFlavor; import java.io.Serializable; public class Book implements Serializable { private int bookid; private String bookname; private String author; private String publisher;//出版社 private String publication_date;//出版日期 private int booknumber=0;//数量 private String price;//定价 public void setBookid(int bookid) { this.bookid = bookid; } public void setBookname(String bookname) { this.bookname = bookname; } public void setAuthor(String author) { this.author = author; } public void setPublisher(String publisher) { this.publisher = publisher; } public void setPublication_date(String publication_date) { this.publication_date = publication_date; } public void setBooknumber(int booknumber) { this.booknumber = booknumber; } public void setPrice(String price) { this.price = price; } public String getBookname(){ return bookname; } public int getBookid() { return bookid; } public int getBooknumber() { return booknumber; } public String getPrice() { return price; } public String getAuthor() { return author; } public String getPublication_date() { return publication_date; } public String getPublisher() { return publisher; } @Override public String toString() { return "book{" + "bookid=" + bookid + ", bookname=" + bookname + + ", author=" + author + + ", publisher=" + publisher + + ", publication_date=" + publication_date + + ", booknumber=" + booknumber + ", price=" + price + }; } public Book(){ this.bookid=bookid; this.bookname=bookname; this.author=author; this.publisher=publisher; this.publication_date=publication_date; this.booknumber=booknumber; this.price=price; } } 读者类: package book; import javax.print.DocFlavor; import java.io.Serializable; public class Reader implements Serializable { private long manid;//学号 private String manname;//姓名 private String college;//学院 private String major_class;//专业 public long getManid() { return manid; } public void setManid(long manid) { this.manid = manid; } public String getManname() { return manname; } public void setManname(String manname) { this.manname = manname; } public String getCollege() { return college; } public void setCollege(String college) { this.college = college; } public String getMajor_class() { return major_class; } public void setMajor_class(String major_class) { this.major_class = major_class; } @Override public String toString() { return "reader{" + "manid=" + manid + ", manname=" + manname + + ", college=" + college + + ", major_class=" + major_class + + }; } public Reader(){ this.manid=manid; this.manname=manname; this.college=college; this.major_class=major_class; } } 借阅者类: package book; import java.io.Serializable; public class BookBorrow implements Serializable { private long borrowerid=0;//学号 private String borrowername=null;//姓名 private int bookid=0;//书号 private String bookname=null;//书名 private String borrowtime=null;//借阅日期 private String duedate=null;//应还日期 private String returntime=null;//归还日期 // @Override // public String toString() { // return "BookBorrow{" + // "borrowerid=" + borrowerid + // ", borrowername=" + borrowername + + // ", bookid=" + bookid + // ", bookname=" + bookname + + // ", borrowtime=" + borrowtime + + // ", duedate=" + duedate + + // ", returntime=" + returntime + + // }; // } public long getBorrowerid() { return borrowerid; } public void setBorrowerid(long borrowerid) { this.borrowerid = borrowerid; } public String getBorrowername() { return borrowername; } public void setBorrowername(String borrowername) { this.borrowername = borrowername; } public int getBookid() { return bookid; } public void setBookid(int bookid) { this.bookid = bookid; } public String getBookname() { return bookname; } public void setBookname(String bookname) { this.bookname = bookname; } public String getBorrowtime() { return borrowtime; } public void setBorrowtime(String borrowtime) { this.borrowtime = borrowtime; } public String getDuedate() { return duedate; } public void setDuedate(String duedate) { this.duedate = duedate; } public String getReturntime() { return returntime; } public void setReturntime(String returntime) { this.returntime = returntime; } public BookBorrow() { this.borrowerid = borrowerid; this.borrowername = borrowername; this.bookid = bookid; this.bookname = bookname; this.borrowtime = borrowtime; this.duedate = duedate; this.returntime = returntime; } } 二,用菜单选择方式完成下列功能:3.图书信息排序:按书号、书名等按升序进行排序。 4.图书信息的修改、删除:按书号或书名进行图书的修改和删除。 5.读者信息添加功能:包括学号、姓名、学院、专业班级等。 6.读者信息查询:分别按学号、姓名、专业班级等进行查询。 7.读者信息排序:按学号、学院等按升序进行排序。 8.读者信息的修改、删除:按学号+姓名进行读者信息的修改和删除。 9.图书借阅:输入学号+书号,如果该书图书信息表中的存馆数量大于0,则可以借出,借出相应数量后修改图书信息表中的存馆数量,在图书借阅表添加该同学的借阅。 10.图书归还:输入学号+书号,修改图书信息表中的存馆数量,在图书借阅表中记录该同学的归还时间。 11.图书借阅查询:分别按学号、书名、学院等进行查询。 ------------------------------------------------------------------------------------------------------------------------------ 先看一下菜单选择: public static void main(String[] args) { //创建集合对象,用于存储数据 ArrayList<Book> array = new ArrayList<Book>(); ArrayList<Reader> arr = new ArrayList<Reader>(); ArrayList<BookBorrow> ar = new ArrayList<BookBorrow>(); //循环 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("8.读者信息排序"); System.out.println("9.读者信息的修改"); System.out.println("10.读者信息的删除"); System.out.println("11.图书借阅"); System.out.println("12.图书归还"); System.out.println("13.图书借阅查询"); System.out.println("14.退出"); System.out.println("请输入对应序号:"); Scanner sc = new Scanner(System.in); String line = sc.nextLine(); //用switch语句完成操作的选择 switch (line) { case "1": System.out.println(""); addBook(array); break; case "2": System.out.println(""); findBook(array); break; case "3": System.out.println(""); sortingBook(array); break; case "4": System.out.println(""); reviseBook(array); break; case "5": System.out.println(""); deleteBook(array); break; case "6": System.out.println(""); addReader(arr); break; case "7": System.out.println(""); findReader(arr); break; case "8": System.out.println(""); sortingReader(arr); break; case "9": System.out.println(""); reviseReader(arr); break; case "10": System.out.println(""); deleteReader(arr); break; case "11": System.out.println(""); borrowingBook(ar, arr, array); break; case "12": System.out.println(""); returnBook(ar, arr, array); break; case "13": System.out.println(""); findBookborrow(ar,arr,array); break; case "14": System.out.println("谢谢使用"); System.exit(0);//JVM退出 default: System.out.println("请输入正确的序号"); } } } 这里创建三个集合对象来接那三个类,由于不知道一个类中会有几个对象,所以用ArrayList方便接收。 3.图书信息排序:按书号、书名等按升序进行排序。 //定义图书排序(冒泡排序) public static void sortingBook(ArrayList<Book> array) { // int[] arr = new int[100]; // for (int i = 0; i < array.size(); i++) { // Book b = array.get(i); // arr[i] = b.getBookid(); // } //冒泡 for (int i1 = 0; i1 < array.size() - 1; i1++) { for (int j = 0; j < array.size() - 1 - i1; j++) { Book book1 = array.get(j); Book book2 = array.get(j+1); if (book1.getBookid()>book2.getBookid()) { // int temp=arr[j]; // arr[j]=arr[j+1]; // arr[j+1]=temp; Collections.swap(array, j, j + 1); } } }//书号分别是98765时排序正确(56789),但书号分别是342时排序结果却是324 // 答案:if语句括号里哪两个对象比较,就应该哪两个对象互换,而不是比较这两个去换那两个 System.out.println("已完成排序"); //查看排序后的图书 for (int i = 0; i < array.size(); i++) { Book b = array.get(i); System.out.println(b.getBookid() + "," + b.getBookname() + "," + b.getAuthor() + "," + b.getPublisher() +","+b.getPublication_date()+ "," + b.getBooknumber() + "," + b.getPrice()); } } 4.图书信息的修改、删除:按书号或书名进行图书的修改和删除。 5.读者信息添加功能:包括学号、姓名、学院、专业班级等。 //定义添加读者信息 public static void addReader(ArrayList<Reader> arr) { Scanner sc = new Scanner(System.in); System.out.println("请输入学号:"); long sreaderkid = Integer.parseInt(sc.nextLine()); System.out.println("请输入姓名:"); String sreadername = sc.nextLine(); System.out.println("请输入学院:"); String scollege = sc.nextLine(); System.out.println("请输入专业班级:"); String smajor_class = sc.nextLine(); //创建读者对象,将键盘录入的数据赋值给读者对象的成员变量 Reader readers = new Reader(); readers.setManid(sreaderkid); readers.setManname(sreadername); readers.setCollege(scollege); readers.setMajor_class(smajor_class); arr.add(readers); System.out.println("添加学生信息成功"); } 6.读者信息查询:分别按学号、姓名、专业班级等进行查询。 //定义查看读者信息 public static void findReader(ArrayList<Reader> arr) { System.out.println("1.根据学号查找"); System.out.println("2.根据姓名查找"); System.out.println("3.根据专业班级查找"); Scanner scanner = new Scanner(System.in); //将整型数据Integer转换为基本数据类型int int a = Integer.parseInt(scanner.nextLine()); if (!(a == 1 || a == 2 || a == 3)) { System.out.println("输入错误,请重新输写"); } else { switch (a) { case 1: System.out.println("请输入学号:"); long name1; name1 = Long.parseLong(scanner.next()); if (arr.size() == 0) { System.out.println("您还没有添加读者"); //结束方法 return; } else { System.out.println("学号 姓名 学院 专业班级"); for (int i = 0; i < arr.size(); i++) { Reader b = arr.get(i); if (name1 == b.getManid()) { System.out.println(b.getManid() + "," + b.getManname() + "," + b.getCollege() + "," + b.getMajor_class()); } } } break; case 2: System.out.println("请输入姓名:"); String name2; name2 = scanner.next(); if (arr.size() == 0) { System.out.println("您还没有添加读者"); //结束方法 return; } else { System.out.println("学号 姓名 学院 专业班级"); for (int i = 0; i < arr.size(); i++) { Reader b = arr.get(i); if (name2.equals(b.getManname())) { System.out.println(b.getManid() + "," + b.getManname() + "," + b.getCollege() + "," + b.getMajor_class()); } } } break; case 3: System.out.println("请输入专业班级:"); String name3; name3 = scanner.next(); if (arr.size() == 0) { System.out.println("您还没有添加读者"); //结束方法 return; } else { System.out.println("学号 姓名 学院 专业班级"); for (int i = 0; i < arr.size(); i++) { Reader b = arr.get(i); if (name3.equals(b.getMajor_class())) { System.out.println(b.getManid() + "," + b.getManname() + "," + b.getCollege() + "," + b.getMajor_class()); } } } break; } } } 7.读者信息排序:按学号、学院等按升序进行排序。 //定义冒泡排序 public static void sortingReader(ArrayList<Reader> arr) { // long[] ar = new long[100]; // for (int i = 0; i < arr.size(); i++) { // Reader r = arr.get(i); // ar[i] = Math.toIntExact(r.getManid()); // } //冒泡 for (int i = 0; i < arr.size() - 1; i++) { for (int j = 0; j < arr.size() - 1 - i; j++) { Reader reader1=arr.get(j); Reader reader2=arr.get(j+1); if (reader1.getManid()>reader2.getManid()) { Collections.swap(arr, j, j + 1); } } } System.out.println("已完成排序"); //查看排序后的图书 System.out.println("学号 姓名 学院 专业班级"); for (int i = 0; i < arr.size(); i++) { Reader r = arr.get(i); System.out.println(r.getManid() + "," + r.getManname() + "," + r.getCollege() + "," + r.getMajor_class()); } } (因为学院是String类型的,所以这里只按学号进行升序排序) 8.读者信息的修改、删除:按学号+姓名进行读者信息的修改和删除。 //定义读者信息修改 public static void reviseReader(ArrayList<Reader> arr) { Scanner scanner = new Scanner(System.in); System.out.println("请输入学号"); long a = Integer.parseInt(scanner.nextLine()); System.out.println("请输入姓名"); String b = scanner.next(); if (arr.size() == 0) { System.out.println("您还没有添加读者"); //结束方法 return; } else { for (int i = 0; i < arr.size(); i++) { Reader r = arr.get(i); if (a == r.getManid() && b.equals(r.getManname())) { long a1; String a2; System.out.println("请输入修改后的学号"); a1 = scanner.nextLong(); r.setManid(a1); System.out.println("请输入修改后的姓名"); a2 = scanner.next(); r.setManname(a2); System.out.println("请输入修改后的学院"); a2 = scanner.next(); r.setCollege(a2); System.out.println("请输入修改后的专业班级"); a2 = scanner.next(); r.setMajor_class(a2); System.out.println("已完成读者信息的修改"); } } } } //定义读者信息删除 public static void deleteReader(ArrayList<Reader> arr) { Scanner sc = new Scanner(System.in); System.out.println("请输入该读者的学号"); long a1 = sc.nextLong(); System.out.println("请输入该读者的姓名"); String a2 = sc.next(); if (arr.size() == 0) { System.out.println("您还没有添加读者"); //结束方法 return; } else { for (int i = 0; i < arr.size(); i++) { Reader reader = arr.get(i); if (a1 == reader.getManid() && a2.equals(reader.getManname())) { arr.remove(i); } } System.out.println("已删除该读者"); } } 9.图书借阅:输入学号+书号,如果该书图书信息表中的存馆数量大于0,则可以借出,借出相应数量后修改图书信息表中的存馆数量,在图书借阅表添加该同学的借阅。 //定义图书借阅 public static void borrowingBook(ArrayList<BookBorrow> ar, ArrayList<Reader> arr, ArrayList<Book> array) { Scanner sc = new Scanner(System.in); boolean panduan1 = false; boolean panduan2 = false; Reader reader3 = null;//确定读者类对象 System.out.println("请输入借阅者的学号(8位以内)"); long a1 = sc.nextLong(); System.out.println("请输入借阅的书号"); int a2 = sc.nextInt(); //判断输入的学号是否存在 for (int i = 0; i < arr.size(); i++) { Reader reader1 = arr.get(i); if (a1 == reader1.getManid()) { panduan1 = true; } } //判断书号是否存在 for (int j = 0; j < array.size(); j++) { Book book1 = array.get(j); if (a2 == book1.getBookid()) { panduan2 = true; } } if (panduan1 == true && panduan2 == true) { for (int i1 = 0; i1 < array.size(); i1++) { Book book2 = array.get(i1);//确定图书类对象 if (a2 == book2.getBookid() && book2.getBooknumber() >= 1) { for (int i2 = 0; i2 < arr.size(); i2++) { Reader reader2 = arr.get(i2); if (a1 == reader2.getManid()) { reader3 = reader2; } } System.out.println("该图书剩余:" + book2.getBooknumber() + "本"); System.out.println("请选择您要借出的数量"); int n1 = sc.nextInt(); if (n1 <= book2.getBooknumber()) { System.out.println("已成功借出"); //获取日历类对象 Calendar calendar = Calendar.getInstance(); String year = String.valueOf(calendar.get(Calendar.YEAR));//校正后 String nonth = String.valueOf(calendar.get(Calendar.MONTH) + 1); String day = String.valueOf(calendar.get(Calendar.DATE)); calendar.add(Calendar.MONTH, 3); String year1 = String.valueOf(calendar.get(Calendar.YEAR));//校正后 String nonth1 = String.valueOf(calendar.get(Calendar.MONTH) + 1); String day1 = String.valueOf(calendar.get(Calendar.DATE)); //修改图书数量 book2.setBooknumber(book2.getBooknumber() - n1); //读者reader3,图书book2 BookBorrow bookBorrow = new BookBorrow(); bookBorrow.setBookid(book2.getBookid()); bookBorrow.setBookname(book2.getBookname()); bookBorrow.setBorrowerid(reader3.getManid()); bookBorrow.setBorrowername(reader3.getManname()); bookBorrow.setBorrowtime(year + "年" + nonth + "月" + day + "日"); bookBorrow.setDuedate(year1 + "年" + nonth1 + "月" + day1 + "日"); //将图书借阅者对象添加到集合中 ar.add(bookBorrow); } } } } else { System.out.println("您输入的学号或书号有误"); } } 10.图书归还:输入学号+书号,修改图书信息表中的存馆数量,在图书借阅表中记录该同学的归还时间。 //定义图书归还 public static void returnBook(ArrayList<BookBorrow> ar, ArrayList<Reader> arr, ArrayList<Book> array) { Scanner scanner = new Scanner(System.in); System.out.println("请输入您的学号"); long a1 = scanner.nextLong(); System.out.println("请输入书号"); int a2 = scanner.nextInt(); boolean panduan1 = false; boolean panduan2 = false; //判断输入的学号是否存在 for (int i = 0; i < arr.size(); i++) { Reader reader1 = arr.get(i); if (a1 == reader1.getManid()) { panduan1 = true; } } //判断书号是否存在 for (int j = 0; j < array.size(); j++) { Book book1 = array.get(j); if (a2 == book1.getBookid()) { panduan2 = true; } } if (panduan1 == true && panduan2 == true) { for (int i = 0; i < array.size(); i++) { Book book1=array.get(i); if (a2==book1.getBookid()){ System.out.println("请输入您归还的数量"); int n; n= scanner.nextInt(); book1.setBooknumber(book1.getBooknumber()+n);//不知道如何引用借出的数量 Calendar c=Calendar.getInstance(); String year= String.valueOf(c.get(Calendar.YEAR)); String month= String.valueOf(c.get(Calendar.MONTH)+1); String date= String.valueOf(c.get(Calendar.DATE)); //找借阅者(学号加书号) for (int i1 = 0; i1 < ar.size(); i1++) { BookBorrow bookBorrow=ar.get(i1); if (bookBorrow.getBookid()==a2&&bookBorrow.getBorrowerid()==a1){ bookBorrow.setReturntime(year+"年"+month+"月"+date+"日"); System.out.println("图书归还成功!"); } } } } } else { System.out.println("您的输入有误"); } } 11.图书借阅查询:分别按学号、书名、学院等进行查询。 //定义图书借阅查询 public static void findBookborrow(ArrayList<BookBorrow> ar, ArrayList<Reader> arr, ArrayList<Book> array){ if (ar.size()==0){ System.out.println("您还没有添加借阅者信息"); return;} System.out.println("请选择您要查询的方式"); System.out.println("1.按学号"); System.out.println("2,桉书名"); System.out.println("3,按学院"); Scanner scanner=new Scanner(System.in); int n1= scanner.nextInt(); switch(n1){ case 1: System.out.println("请输入您想查询的学号"); long mid= scanner.nextLong(); System.out.println("学号 姓名 书号 书名 借阅日期 应还日期 归还日期"); for (int i = 0; i < ar.size(); i++) { BookBorrow bookBorrow=ar.get(i); if (bookBorrow.getBorrowerid()==mid){ System.out.println(bookBorrow.getBorrowerid()+","+bookBorrow.getBorrowername()+"," +bookBorrow.getBookid()+","+bookBorrow.getBookname()+","+bookBorrow.getBorrowtime()+"," +bookBorrow.getDuedate()+","+bookBorrow.getReturntime()); } } break; case 2: System.out.println("请输入您想查询的书名"); String bname= scanner.nextLine(); System.out.println("学号 姓名 书号 书名 借阅日期 应还日期 归还日期"); for (int i = 0; i < ar.size(); i++) { BookBorrow bookBorrow=ar.get(i); if (bname.equals(bookBorrow.getBookname())){ System.out.println(bookBorrow.getBorrowerid()+","+bookBorrow.getBorrowername()+"," +bookBorrow.getBookid()+","+bookBorrow.getBookname()+","+bookBorrow.getBorrowtime()+"," +bookBorrow.getDuedate()+","+bookBorrow.getReturntime()); } } break; case 3: System.out.println("请输入您想查询的学院"); String mcollege= scanner.nextLine(); System.out.println("学号 姓名 书号 书名 借阅日期 应还日期 归还日期"); for (int i = 0; i < arr.size(); i++) { Reader reader=arr.get(i); if (mcollege.equals(reader.getCollege())){ for (int i1 = 0; i1 < ar.size(); i1++) { BookBorrow bookBorrow=ar.get(i1); if (reader.getManid()== bookBorrow.getBookid()){ System.out.println(bookBorrow.getBorrowerid()+","+bookBorrow.getBorrowername()+"," +bookBorrow.getBookid()+","+bookBorrow.getBookname()+","+bookBorrow.getBorrowtime()+"," +bookBorrow.getDuedate()+","+bookBorrow.getReturntime()); } else { System.out.println("没有在借阅者信息表中找到此人!"); } } } else { System.out.println("读者中无人在此学院!"); } } break; default: System.out.println("您的输入有误!"); } } 总结:这个做的比较早,一开始的思路是等学完I/O流再做,后来发现用ArrayList也能实现,所以就用它磕磕绊绊的做出来了,目前还在学习中,希望以后能越做越快,越做越好,加油! |