文章目录
图书管理系统
图书管理系统背景
在图书馆中有管理员和用户两个角色,不同的角色有着不同的权力,管理员可以为图书馆增加图书,删减图书等等等,而用户呢可以去图书馆借阅图书,可以去归还已经借阅的图书等等。今天小编将带大家一起实现图书管理系统。
书的代码实现
public class Book {
private String bookname;//书名
private String bookauthor;//书的作者
private int price;//书的价格
private String type;//书的类型
private boolean isLend;//书是否被借阅
//各个字段的构造方法
public Book(String bookname, String bookauthor, int price, String type) {
this.bookname = bookname;
this.bookauthor = bookauthor;
this.price = price;
this.type = type;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getBookauthor() {
return bookauthor;
}
public void setBookauthor(String bookauthor) {
this.bookauthor = bookauthor;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isLend() {
return isLend;
}
public void setLend(boolean lend) {
isLend = lend;
}
//重写toSting方法,便于输出
@Override
public String toString() {
return "Book{" +
"bookname='" + bookname + '\'' +
", bookauthor=" + bookauthor +
", price=" + price +
", type='" + type + '\'' +
", isLend='" + (isLend==true?"已借出":"未借出" )+
'}';
}
}
我们知道书肯定是存放于书架的,我们实现了书的模板,肯定需要实现书架
书架的代码实现
public class BookList {
private Book[] books =new Book[10] ;//最大限度是10本书;
private int uesdsize;//实际存放书的数量
public BookList(){
books[0]=new Book("三国演义","罗贯中",10,"名著");
books[1]=new Book("红楼梦","曹雪芹",15,"名著");
books[2]=new Book("西游记","吴承恩",18,"名著");
this.uesdsize=3;//目前书架上已经有了三本书;
}
public boolean isfull(){//用于管理员新增图书时判断书架是否已满
if(this.uesdsize==books.length){
return true;
}
return false;
}
public Book getBooks(int pos) {
return books[pos];//代表的是一本书;
}
public void setBooks(int pos,Book book) {
this.books[pos]=book;
}
public int getUesdsize() {
return uesdsize;
}
public void setUesdsize(int uesdsize) {//书架上书籍的变化
this.uesdsize = uesdsize;
}
}
现在有了书本和存放书本的苏书架,此时需要人来管理书本,这时我们需要引出管理员。
管理员的代码实现
public class extends User {
public Admuser(String name) {
super(name);
functions=new Functions[]{
new EixtSystem(),//退出系统
new Findbook(),//查找图书
new Addbook(),//增加图书
new Delbook(),//删除图书
new Showbook()//显示图书
};//上面实现了接口,在文章末尾有讲解到
}
@Override
public int menu() {
System.out.println("****管理员菜单****");//管理员可以选择想要进行的操作
System.out.println("1、查找图书");
System.out.println("2、新增图书");
System.out.println("3、删除图书");
System.out.println("4、显示图书");
System.out.println("0、退出系统");
System.out.println("****************");
System.out.println("请输入您要进行的操作:");//输出操作
Scanner scanner=new Scanner(System.in);
int choice=scanner.nextInt();
return choice;
}
}
有了管理员来整理书籍,那么肯定就会有用户来使用书籍。
普通用户的代码实现
public class Orduser extends User {
public Orduser(String name) {
super(name);//用户名称
functions=new Functions[]{
new EixtSystem(),//退出系统
new Showbook(),//显示图书
new Findbook(),//查找图书
new Brrowbook(),//借阅图书
new Returnbook()//归还图书
};//上面实现了接口,在文章末尾有讲解到
}
@Override
public int menu() {
System.out.println("****普通用户菜单****");
System.out.println("1、显示图书");
System.out.println("2、查找图书");
System.out.println("3、借阅图书");
System.out.println("4、归还图书");
System.out.println("0、退出系统");
System.out.println("****************");
System.out.println("请输入您要进行的操作:");
Scanner scanner=new Scanner(System.in);
int choice=scanner.nextInt();
return choice;
}
}
细心的友友应该已经发现,上面的管理员和用户都extends(继承)于User这个类,那么User到底有什么作用呢?先看代码,后细细道来。
User类的代码实现
public abstract class User {//抽象类
public String name;//用户名
public User(String name) {
this.name = name;
}
protected Functions []functions;
public void dofunctions(int choice,BookList bookList){
this.functions[choice].work(bookList);
}
public abstract int menu();
}
想要理清User、Orduser、Admuser三者的关系,我们还需要插入一个方法,就是main方法,借助于main方法我们便可以解释清晰以上这三者的关系。
main方法的代码实现
public class Main {
public static User login(){
System.out.println("请输入您的姓名:");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
System.out.println("请选择您的身份: 1、管理员 2、普通用户" );
int choice=scanner.nextInt();
if(choice==1){
Admuser admuser =new Admuser(name);//选择什么后,便实例化好什么对象了
return admuser;
}
else {
Orduser orduser=new Orduser(name);
return orduser;
}
}
public static void main(String[] args) {
User user=login();//向上转型
BookList bookList = new BookList();//书架
while(true) {
int choice = user.menu();
user.dofunctions(choice, bookList);
}
}
}
由上代码不难看出在main中发生了**向上转型**,向上转型是在为多态铺路,在login方法中,实例化对象,后再main方法中由User接收,发生了向上转型,main方法中user.menu()实现了多态,多态会触发动态绑定,当main方法中,login()的返回值被user接受时,user调用menu方法时,便会发生动态绑定,会调用相应用户的menu菜单。
由上面两张图可以发现,在选择时会容易发生混乱,比如当我们选择2时,到底时管理员对应的2选项还是普通用户中对应的2选项呢?此时遇见了一个小难点。此时不得不掏出法宝接口(接口的具体在小编的上两节中有进行详细的讲解)。具体的作用我们由代码来实现。
接下来我们将逐个实现管理员和用户可以实现的功能。首先我们来实现管理员的各种功能。
总接口
public interface Functions {
void work(BookList bookList);
}
查找图书的代码实现
public class Findbook implements Functions {
@Override
public void work(BookList bookList) {
System.out.println("正在查找图书……");
Scanner scanner=new Scanner(System.in);
System.out.println("请输入你要查找的书名:");
String bookname=scanner.nextLine();//需要查找的书名
int currentsize=bookList.getUesdsize();//目前书架的长度
for (int i = 0; i < currentsize; i++) {
Book book=bookList.getBooks(i);//将书架上的书找出来,一本一本的对照比
if(book.getBookname().equals(bookname)) {
System.out.println("找到了这本书!");
System.out.println(book);
return;
}
}
System.out.println("没有找得到!");
}
}
新增图书的代码实现
public class Addbook implements Functions {
@Override
public void work(BookList bookList) {
System.out.println("正在新增图书……");
if(bookList.isfull()){
System.out.println("书满了,不能在新增书籍了!");
return;
}
System.out.println("请输入您想要输入的书名:");
Scanner scanner=new Scanner(System.in);
String bookname=scanner.nextLine();
System.out.println("请输入书名的作者:");
String author=scanner.nextLine();
System.out.println("请输入书的价格:");
int price=scanner.nextInt();
String type1=scanner.nextLine();
System.out.println("请输入书的类型:");
String type=scanner.nextLine();
Book book=new Book(bookname,author,price,type);
int currentsize=bookList.getUesdsize();
bookList.setBooks(currentsize,book);
bookList.setUesdsize(currentsize+1);
System.out.println("新增图书成功!!");
}
}
删除图书的代码实现
public class Delbook implements Functions {
@Override
public void work(BookList bookList) {
System.out.println("正在删除图书……");
Scanner scanner=new Scanner(System.in);
System.out.println("请输入你要删除的书名:");
String bookname=scanner.nextLine();
int currentsize=bookList.getUesdsize();
int pos=-1;
int i = 0;
for (; i < currentsize; i++) {
Book book=bookList.getBooks(i);
if(book.getBookname().equals(bookname)) {
pos=i;
break;
}
}
if(i>=currentsize){
System.out.println("没有找到这本书");
return;
}
//开始删除
for (int j = pos; j < currentsize-1; j++) {
Book book=bookList.getBooks(j+1);
bookList.setBooks(j,book);
}
bookList.setBooks(currentsize-1,null);
bookList.setUesdsize(currentsize-1);
System.out.println("删除图书成功!!");
}
}
显示图书的代码实现
public class Showbook implements Functions {
@Override
public void work(BookList bookList) {
System.out.println("正在显示图书……");
for (int i = 0; i < bookList.getUesdsize(); i++) {
System.out.println(bookList.getBooks(i));
}
}
}
退出系统的代码实现
public class EixtSystem implements Functions {
@Override
public void work(BookList bookList) {
System.out.println("正在退出系统……");
int currentsize=bookList.getUesdsize();
for (int i = 0; i < currentsize; i++) {
bookList.setBooks(i,null);
}
System.out.println("退出系统成功过");
System.exit(0);
}
}
管理员的功能已实现完毕,接下来我们实现用户的功能。
借阅图书的代码实现
public class Brrowbook implements Functions {
@Override
public void work(BookList bookList) {
System.out.println("正在借阅图书……");
Scanner scanner=new Scanner(System.in);
System.out.println("请输入您要借阅的图书:");
String bookname=scanner.nextLine();
int currentsize=bookList.getUesdsize();
for (int i = 0; i < currentsize; i++) {
Book book=bookList.getBooks(i);
if(book.getBookname().equals(bookname)) {
book.setLend(true);
System.out.println("成功被借阅!");
return;
}
}
System.out.println("借阅未成功!");
}
}
归还图书的代码实现
public class Returnbook implements Functions {
@Override
public void work(BookList bookList) {
System.out.println("正在归还图书……");
Scanner scanner=new Scanner(System.in);
System.out.println("请输入您要借阅的图书:");
String bookname=scanner.nextLine();
int currentsize=bookList.getUesdsize();
for (int i = 0; i < currentsize; i++) {
Book book=bookList.getBooks(i);
if(book.getBookname().equals(bookname)) {
book.setLend(false);
System.out.println("成功被归还!");
return;
}
}
System.out.println("归还未成功!");
}
}
普通用户的其他功能和管理员是一样的,所以上面只写了普通用户特有的功能。
对于接口的应用很简单,当实现接口时,只要重写接口当中的代码,便可以利用此功能。上面的功能代码中有看见@Override的就代表重写了方法。通过调用便可以使用对应的功能。
以上就是本期分享的全部内容了,小编也在主页发布了整体运行的视频,供大家参考,希望本期内容能对屏幕前的你有收获!!