Java实现图书管理系统的示例代码
作者:谦虚的荆南芒果
这篇文章将通过Java实现一个简答的图书管理系统,本图书管理系统用对象数组的方式来提供操作方法,比较特别,建议新手学习,这对理解Java面向对象有很大帮助
User类
首先创建一个抽象类User,属性为姓名,提供了构造方法和get方法(因为本系统不需要修改名字,所以没有设置set方法),有一个able方法,让子类管理员和普通用户实现不同的界面和操作。
public abstract class User { private String name; public User(String name){ this.name=name; } abstract void able(); public String getName() { return name; } }
管理员和普通用户类
这里定义了一个管理员和普通用户类继承User类,able方法里是菜单,可选择界面里的操作
public class AdminUser extends User{ public AdminUser(String name) { super(name); } @Override void able() { BookList bookList=new BookList(); while (true) { System.out.println("***********************"); System.out.println("欢迎 "+getName()+" 登录图书管理系统"); System.out.println("1.添加图书"); System.out.println("2.查看全部图书"); System.out.println("3.修改图书"); System.out.println("4.删除图书"); System.out.println("0.退出系统"); System.out.println("请输入您的选择"); Scanner sc=new Scanner(System.in); int i=sc.nextInt(); Operate.operates[i].work(bookList); } } }
public class normalUser extends User{ public normalUser(String name) { super(name); } @Override void able() { BookList bookList=new BookList(); while (true) { System.out.println("***********************"); System.out.println("欢迎 "+getName()+" 登录图书管理系统"); System.out.println("1.查找图书"); System.out.println("2.借出图书"); System.out.println("3.归还图书"); System.out.println("0.退出系统"); System.out.println("请输入您的选择"); Scanner sc=new Scanner(System.in); int i=sc.nextInt(); Operate.operates[i].work(bookList); } } }
Book类
书类,属性有:书名,价格,状态(已借出/未被借出)
告诉大家一个快速生成JavaBean的小技巧,点击右上角file->Settings-> Plugins->输入ptj,下载好之后,右键点击下面的PTJ to Javabean,就可以快速生成啦
package BOOK; public class Book { private String name; private int prize; private boolean isBorrowed; public Book() { } public Book(String name, int prize) { this.name = name; this.prize = prize; } /** * 获取 * @return name */ public String getName() { return name; } /** * 设置 * @param name */ public void setName(String name) { this.name = name; } /** * 获取 * @return prize */ public int getPrize() { return prize; } /** * 设置 * @param prize */ public void setPrize(int prize) { this.prize = prize; } /** * 获取 * @return isBorrowed */ public boolean IsBorrowed() { return isBorrowed; } public void setNoBorrowed(){ isBorrowed=false; } public void setYesBorrowed(){ isBorrowed=true; } /** * 设置 * @param isBorrowed */ public void setIsBorrowed(boolean isBorrowed) { this.isBorrowed = isBorrowed; } public String toString() { return "Book{name = " + name + ", prize = " + prize + ", isBorrowed = " + (isBorrowed==false?"未被借出":"已借出")+"}"; } }
BookList类
书架类,定义了一个能放10本书的数组,提供了一些操作方法,具体请看注释
package BOOK; public class BookList { private Book[] books=new Book[10]; private int size;//书的数量 \\构造方法,默认放三本书进去 public BookList() { books[0]=new Book("三国", 100000); books[1]=new Book("骆驼祥子", 10); books[2]=new Book("西游记", 90); this.size = 3; } \\添加方法 public void add(Book book){ books[size]=book; size++; } \\获取有多少本书 public int getSize(){ return size; } \\获取书架 public Book[] getBooks(){ return books; } \\返回书架上索引为index的书 public Book getBook(int index){ return books[index]; } \\往索引index插入已初始化的book public void setBook(int index,Book book){ books[index]=book; } \\删除书架上索引为index的书 public void removeBook(int index){ books[index]=null; } \\改变书的数量为i public void setSize(int i){ size=i; } }
Operate类
只是一个管理员的操作类,作为其他操作的父类,里面初始化一个操作数组,按照管理员类里输入的数字,再比照数组里操作的索引来实现不同的功能
work方法让子类去重写,来实现不同的功能
package Ac; import BOOK.BookList; public class Operate { static public Operate[] operates={ new exitOperate(),\\退出系统 new addOperate(),\\添加图书 new showOperate(),\\展示所有图书 new updateOperate(),\\修改某一本书 new removeOperate()\\删除图书 }; public void work(BookList bookList){ } }
只是一个普通用户的操作类,和上面的一样,不在此过多赘述
package Pc; import BOOK.BookList; public class Operate { static public Operate[] operates={ new exitOperate(),\\退出系统 new findOperate(),\\查找图书 new borrowBook(),\\借书 new returnBook()\\归还图书 }; public void work(BookList bookList){ } }
退出系统
我相信不用多讲,各位dddd
package Ac; import BOOK.BookList; public class exitOperate extends Operate{ @Override public void work(BookList bookList) { System.out.println("退出成功"); System.exit(0); } }
addOperate 添加书籍(管理员)
package Ac; import BOOK.Book; import BOOK.BookList; import java.util.Scanner; public class addOperate extends Operate{ @Override public void work(BookList bookList) { System.out.println("请输入书名"); Scanner sc=new Scanner(System.in); String name=sc.next(); \\查找书名,相同则录入失败 for (int i = 0; i < bookList.getSize(); i++) { Book[] books=bookList.getBooks(); if (bookList.getBook(i).getName().equals(name)){ System.out.println("已有此书,录入失败"); return; } } System.out.println("请输入价格"); int prize=sc.nextInt(); \\把书放入书架 Book book=new Book(name,prize); bookList.add(book); System.out.println("录入成功"); } }
展示所有图书(管理员)
很简单,遍历书架上所有书就完事了
package Ac; import BOOK.BookList; public class showOperate extends Operate{ @Override public void work(BookList bookList) { if (bookList.getSize()==0){ System.out.println("没有书"); } for (int i = 0; i < bookList.getSize(); i++) { System.out.println(bookList.getBook(i)); } } }
修改图书 (管理员)
package Ac; import BOOK.Book; import BOOK.BookList; import java.util.Scanner; public class updateOperate extends Operate{ @Override public void work(BookList bookList) { System.out.println("请输入您要修改的书的名字"); Scanner sc=new Scanner(System.in); String name=sc.next(); \\查找录入的书 int i=findBook(bookList,name); if (i!=-1){ \\录入书名,如果书名存在则录入失败 System.out.println("请输入修改后书名"); String newName=sc.next(); for (int j = 0; j < bookList.getSize(); j++) { Book[] books=bookList.getBooks(); if (bookList.getBook(j).getName().equals(newName)){ System.out.println("已有此书,录入失败"); return; } } System.out.println("请输入修改后价格"); int prize=sc.nextInt(); \\添加书到书架 Book book=new Book(newName,prize); bookList.setBook(i,book); System.out.println("修改成功"); return; } } public int findBook(BookList bookList,String name){ for (int i = 0; i < bookList.getSize(); i++) { Book[] books=bookList.getBooks(); if (bookList.getBook(i).getName().equals(name)){ return i; } } System.out.println("没有此书!!修改失败"); return -1; } }
删除图书(管理员)
与修改差不多,在此不过多赘述
package Ac; import BOOK.Book; import BOOK.BookList; import java.util.Scanner; public class removeOperate extends Operate{ @Override public void work(BookList bookList) { System.out.println("请输入要删除的图书"); Scanner sc=new Scanner(System.in); String name=sc.next(); int i=findBook(bookList,name); if(i!=-1){ for (int j = i; j < bookList.getSize()-1; j++) { Book book=bookList.getBook(j+1); bookList.setBook(j,book); } bookList.setSize(bookList.getSize()-1); bookList.removeBook(bookList.getSize()); System.out.println("删除成功"); } } public int findBook(BookList bookList,String name){ for (int i = 0; i < bookList.getSize(); i++) { Book[] books=bookList.getBooks(); if (bookList.getBook(i).getName().equals(name)){ return i; } } System.out.println("没有此书!!删除失败"); return -1; } }
查找某一本书(普通用户)
package Pc; import BOOK.Book; import BOOK.BookList; import java.util.Scanner; public class findOperate extends Operate{ @Override public void work(BookList bookList) { System.out.println("请输入您要查找的书名"); Scanner sc=new Scanner(System.in); String name=sc.next(); findBook(bookList,name); } public void findBook(BookList bookList,String name){ for (int i = 0; i < bookList.getSize(); i++) { Book[] books=bookList.getBooks(); if (bookList.getBook(i).getName().equals(name)){ System.out.println("找到了!!"); System.out.println(bookList.getBook(i)); return; } } System.out.println("没有此书!!"); } }
借出图书(普通用户)
借书原理很简单,芝士把某一本书拿出来修改状态再塞回书架
package Pc; import BOOK.Book; import BOOK.BookList; import java.util.Scanner; public class borrowBook extends Operate{ @Override public void work(BookList bookList) { System.out.println("请输入您要借书的书名"); Scanner sc=new Scanner(System.in); String name=sc.next(); int i=findBook(bookList,name); if (i!=-1){ bookList.getBook(i).setYesBorrowed();; System.out.println("成功借出"); return; } } public int findBook(BookList bookList,String name){ for (int i = 0; i < bookList.getSize(); i++) { Book[] books=bookList.getBooks(); if (bookList.getBook(i).getName().equals(name)){ return i; } } System.out.println("没有此书!!"); return -1; } }
归还图书(普通用户)
与上面的借书操作原理相同
package Pc; import BOOK.Book; import BOOK.BookList; import java.util.Scanner; public class returnBook extends Operate{ @Override public void work(BookList bookList) { System.out.println("请输入您要归还的书名"); Scanner sc=new Scanner(System.in); String name=sc.next(); int i=findBook(bookList,name); if (i!=-1){ bookList.getBook(i).setNoBorrowed();; System.out.println("成功归还"); return; } } public int findBook(BookList bookList,String name){ for (int i = 0; i < bookList.getSize(); i++) { Book[] books = bookList.getBooks(); if (bookList.getBook(i).getName().equals(name)) { return i; } } System.out.println("没有此书!!"); return -1; } }
到此这篇关于Java实现图书管理系统的示例代码的文章就介绍到这了,更多相关Java图书管理系统内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!