首页云计算【Java】图书管理系统,完整版+源代码!!!

【Java】图书管理系统,完整版+源代码!!!

时间2024-07-15 01:52:44发布ongwu分类云计算浏览49

1. 图书管理系统菜单

1.1 管理员菜单

查找图书新增图书删除图书显示图书退出系统

1.2普通用户菜单

查找图书借阅图书归还图书退出系统

2.基本框架的实现

首先我们要建立一个新的文件,在文件内建立三个包,分别命名为user(用户)、book(图书),operation(操作

先写book相关的代码。于是,我们先在book包中新建一个java类,名为:Book。

显示图书时,我们希望它能显示出所有图书的名称、作者、价格、类型和图书目前是否借出。 private String name;//书名 private String author;//作者 private int price;//价格(价格也可以定义为浮点型) private String type;//图书的种类 private boolean isLend;//图书是否被借出 12345

右击鼠标,选择Generate(这里我加了汉化),选择Getter and Setter 选项,点击shift,点最后一个,就能全选

自动生成: public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } 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; }
123456789101112131415161718192021222324252627282930313233343536

再写它们的构造方法:

当我们新增一本图书时,它默认就是未被借出的,所以不用构造方法。

快捷键:alt+insert。按ctrl,可以多选,直接生成下面代码块:

public Book(String name, String author, int price, String type) { this.name = name; this.author = author; this.price = price; this.type = type; // this.isLend = isLend;新生成的书籍默认是没有被借出的,所以省略 } 1234567

我们再右键选择Generate,选择ToString,全选,点OK,自动生成:

@Override public String toString() { return "Book{" + "name=" + name + \ + ", author=" + author + \ + ", price=" + price + ", type=" + type + \ + (isLend==true ? " 已借出" :" 未借出")+ //", isLend=" + isLend + }; } 1234567891011

此时,在book包中新建一个类:BookList(书架):

在这个书架中,我们希望能在里面固定的位置放书,并且能知道放了几本书: private Book[] books=new Book[10];//这个书架可以放十本书 private int useSize;//记录下当前book数组中有几本书 12

然后依旧是右键Generate ,getter and setter(先选useSize),自动生成:

public int getUseSize() { return useSize; } public void setUseSize(int useSize) { this.useSize = useSize; } 1234567

这边先按照思路把整体的框架下先实现出来,其中的细节之后再慢慢写出来

book包已经告一段落了,现在来写user包

我们在user包中新建三个类:AdminUser(管理员)、NormalUser(普通用户)、User

NormalUser、AdminUser,这也就是我们图书管理系统面对的两个对象了,管理员和普通用户,他们具有一些相同的属性。我们就可以写一个User类,让两个类去继承,来省一些代码

这里先写user类

package user; public class User { protected String name;//名字.这边的protect代表的是名字的权限。如果是private,它只能在同一个包的同一类使用。就不能让AdminUser类继承了。 //写public的话,权限太大了,不是很好。 public User(String name) {//构造方法 this.name = name; } } 123456789

所以在AdminUser类中:

package user; public class AdminUser extends User{ public AdminUser(String name) { super(name); } } 1234567

可以直接用快捷键,alt+insert,直接生成构造方法。

同理,NormalUser类中:

package user; public class NormalUser extends User{ public NormalUser(String name) { super(name); } } 12345678

现在书的属性和用户的属性的包和类已经完成差不多,现在要搭建用户功能上去

由于想要实现的功能操作基本上是针对图书的我们可以继续创一个包operation再创建一个接口命名为IOperation

package operation; import book.BookList; public interface IOperation { void work(BookList bookList); } 123456789

我们在opera(实现功能的包)中,创建两个类,AddOperation、FindOperation,这两个类。

我们会发现这两个类的实现也都要依靠BookList(书架)来实现。所以,在这两个类中,实现接口

FindOperation类中:

package operation; import book.BookList; public class FindOperation implements IOperation{//继承 @Override public void work(BookList bookList) {//重写IOPeration类中的work方法 System.out.println("查找图书!"); } } 123456789

AddOperation类中:

package operation; import book.BookList; public class AddOperation implements IOPeration { public void work(BookList bookList){ System.out.println("新增图书!"); } } 123456789

同理,新建类DelOperation(删除图书):

package operation; import book.BookList; public class DelOperation implements IOPeration{ @Override public void work(BookList bookList) { System.out.println("删除图书!"); } } 12345678910

新建一个类:ShowOperation(显示图书):

package operation; import book.BookList; public class ShowOperation implements IOPeration{ @Override public void work(BookList bookList) { System.out.println("打印所有图书!"); } } 12345678910

再新建一个类:ExitOperation(退出系统):

package operation; import book.BookList; public class ExitOperation implements IOPeration{ @Override public void work(BookList bookList) { System.out.println("退出系统!"); } } 12345678910

新建一个BrrowOperation类(借阅图书):

package operation; import book.BookList; public class BrrowOperation implements IOPeration{ @Override public void work(BookList bookList) { System.out.println("借阅图书!"); } } 12345678910

再新建一个类:ReturnOperation(归还图书):

package operation; import book.BookList; public class ReturnOperation implements IOPeration{ @Override public void work(BookList bookList) { System.out.println("归还图书!"); } } 12345678910

接着,我们来细化我们的类。

我们还没有给user添加菜单。

我们先给管理员添加菜单:

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("请输入你的操作:"); } 12345678910

再给普通用户添加菜单:

public int menu(){ System.out.println("******普通用户菜单******"); System.out.println("1.查找图书"); System.out.println("2.借阅图书"); System.out.println("3.归还图书"); System.out.println("0.退出系统"); System.out.println("*********************"); System.out.println("请输入你的操作:"); } } 12345678910

现在,我们写main方法。在src下新建一个类:Main:

import user.AdminUser; import user.NormalUser; import user.User; import java.util.Scanner; public class Main { //登录 public static User login(){ System.out.println("请输入你的姓名:"); Scanner scanner=new Scanner(System.in); String name=scanner.nextLine(); System.out.println("请选择你的身份:1->管理员 0->普通用户"); int choice =scanner.nextInt(); if(choice==1){ //说明是管理 //由于有返回值,所以我们的方法返回值就不能写void了。但是我们也无法确定返回值是什么,可能是管理员,可能是用户。所以,用向上转型,写User. return new AdminUser(name);//返回实例化一个管理员对象 }else { return new NormalUser(name);//返回实例化一个用户对象 } } public static void main(String[] args) { User user=login();//执行上面的login方法 user.menu();//实现打印菜单 } }
123456789101112131415161718192021222324252627

执行login方法时,我们是还不知道new的对象是管理员还是用户的。所以,在打印菜单时,我们也不能直接使用我们在AdminUser类和NormalUser类中写的menu()方法

所以,我们在我们的menu菜单中,需要输入我们的功能选择

Scanner scanner=new Scanner(System.in); int choice=scanner.nextInt(); return choice; 123

输入功能选择,我们就要通过这个选择就实现:

首先,我们要根据对象(管理员或用户),选择引用哪一个菜单方法:

NormalUser类中:

public NormalUser(String name) { super(name); this.iOperations=new IOperation[]{ new ExitOperation(), new FindOperation(), new BorrowOperation(), new ReturnOperation() }; } 123456789

AdminUser类中:

public AdminUser(String name) { super(name); this.iOperations=new IOperation[]{ new ExitOperation(), new FindOperation(), new AddOperation(), new DelOperation(), new ShowOperation() }; } 12345678910

当我们在构造方法中,写了这些,也就为它们分配了内存。当main类中login()方法下,new一个对象时,就会执行相应的构造方法,在这里就是开辟内存:

在User类中:

我们加上这样一段代码

public void doIoperations(int choice, BookList bookList){ this.iOperations[choice].work(bookList); } 123

this.ioPerations[choice]其实就是new了一个对象。后面的.work(bookList)是调用图书中对应的work方法。

由此一来,我们就可以根据我们一步步的选择,调用book包中对应的work方法。

为了让代码在输入0的时候退出系统,我们用while语句(在Main方法中修改):

while (true){ int choice=user.menu();//实现打印菜单 user.doWork(choice,bookList); } 1234

这下子基本框架就构造好了。

然后我们进行测试测试通过——>继续

3.实现业务

3.1打印所有图书

我们先在BookList类,也就是书架中先添加图书:

public BookList(){ books[0]=new Book("语文","张三",15,"教科书"); books[1]=new Book("数学","李四",14,"教科书"); books[2]=new Book("英语","王五",16,"教科书"); this.useSize=3; } public Book getBooks(int pos) { return books[pos]; } 1234567891011

然后,我们实现打印所有图书这个业务 。

在ShowOperation类中,添加打印所有图书的程序,利用for循环。

在ShowOperation类中:

package operation; import book.Book; import book.BookList; public class ShowOperation implements IOperation { public void work(BookList bookList){ System.out.println("显示图书___"); int currentSize=bookList.getUseSize(); for (int i = 0; i < currentSize; i++) { Book book=bookList.getBooks(i); System.out.println(book); } } }
12345678910111213141516

我们会发现一些小瑕疵,比如这边输出的false我们想把它改成已借出,未借出。

这时,我们要用到三目运算符。条件?符合输出:不符合输出

在book类中修改

@Override public String toString() { return "Book{" + "name=" + name + \ + ", author=" + author + \ + ", price=" + price + ", type=" + type + \ + (isLend==true ? " 已借出" :" 未借出")+ //", isLend=" + isLend + }; } 1234567891011

显示结果:

3.2 退出系统

退出系统很简单,只需要加上下面语句即可。0代表正常退出。

System.exit(0); 1

3.3 查找图书

查找图书是跟刚才打印图书差不多的思路。通过for循环,利用equal()进行比对,输出图书信息。

在FindOperation类中补充代码

package operation; import book.Book; import book.BookList; import java.util.Scanner; public class FindOperation implements IOperation{ public void work(BookList bookList){ System.out.println("查找图书___"); System.out.println("请输入你要查找的图书名:"); Scanner scanner=new Scanner(System.in); String bookName=scanner.nextLine(); int currentSize=bookList.getUseSize(); for (int i = 0; i < currentSize; i++) { Book book=bookList.getBooks(i); if (book.getName().equals(bookName)){ System.out.println("找到了这本书:"); System.out.println(book); return; } } System.out.println("没有你要找的书。"); } }
1234567891011121314151617181920212223242526

3.4 增加图书

我们要先输入新增图书信息,new一个对象,判断这本图书是否已经存在,如果不存在,则存入书架中。

AddOperation类:

package operation; import book.Book; import book.BookList; import java.util.Scanner; public class AddOperation implements IOperation{ 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(); System.out.println("请输入你要新增的图书的类型:"); String type=scanner.nextLine(); Book book=new Book(bookName,author,price,type); int currentSize= bookList.getUseSize(); bookList.setBook(currentSize,book); bookList.setUseSize(currentSize+1); System.out.println("新增图书成功"); } }
1234567891011121314151617181920212223242526272829303132333435363738394041

我在价格的地方,设置的是int类型。 int price=scanner.nextInt();这个语句执行完,它就不会执行后面的输入语句了。这是我疏忽的一个点。

查询了多种方法后,发现还是调换顺序可以更方便地解决

3.5删除图书

删除图书,我们要注意,不仅仅是把那本书删除就好了,还需要把后面的书往前挪。

DelOperation类:

package operation; import book.Book; import book.BookList; import java.util.Scanner; public class DelOperation implements IOperation { public void work(BookList bookList){ System.out.println("删除图书___"); System.out.println("请输入你要删除的图书名:"); Scanner scanner=new Scanner(System.in); String bookName=scanner.nextLine(); int currentSize=bookList.getUseSize(); int pos=-1; int i = 0; for (; i < currentSize; i++) { Book book=bookList.getBooks(i); if (book.getName().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.setBook(j,book); } //修改useSize bookList.setUseSize(currentSize-1); bookList.setBook(currentSize-1,null); System.out.println("删除成功"); } }
123456789101112131415161718192021222324252627282930313233343536373839404142434445

BookList类:

public void setBook(int pos,Book book){ books[pos]=book; } 123

我们可以注意到在BookList新添加的这个方法跟刚才在写增加图书时放置图书的方法相似。不过,我们可以注意到,增加图书,就是在最后面放一本书,就好了。而这里,是把后面的书一本本往前挪一个书位。

3.6借阅图书

思路:判断图书是否在书库中存在,判断图书是否已经被借出。满足条件,执行

3.7归还图书

思路:判断图书是否在书库中存在,判断书是否已被借出。

4.总代码

Book

package book; public class Book { private String name; private String author; private int price; private String type; private boolean isLend; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } 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; } public Book(String name, String author, int price, String type) { this.name = name; this.author = author; this.price = price; this.type = type; // this.isLend = isLend;新生成的书籍默认是没有被借出的,所以省略 } @Override public String toString() { return "Book{" + "name=" + name + \ + ", author=" + author + \ + ", price=" + price + ", type=" + type + \ + (isLend==true ? " 已借出" :" 未借出")+ //", isLend=" + isLend + }; } }
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869

BookList

package book; /** * 书架 */ public class BookList { private Book[] books=new Book[10]; private int useSize; public BookList(){ books[0]=new Book("语文","张三",15,"教科书"); books[1]=new Book("数学","李四",14,"教科书"); books[2]=new Book("英语","王五",16,"教科书"); this.useSize=3; } public Book getBooks(int pos) { return books[pos]; } public void setBook(int pos,Book book){ books[pos]=book; } public int getUseSize() { return useSize; } public void setUseSize(int useSize) { this.useSize = useSize; } public boolean isFull(){ if (this.useSize == books.length) { return true; } return false; } }
1234567891011121314151617181920212223242526272829303132333435363738

AddOperation

package operation; import book.Book; import book.BookList; import java.util.Scanner; public class AddOperation implements IOperation{ 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(); System.out.println("请输入你要新增的图书的类型:"); String type=scanner.nextLine(); Book book=new Book(bookName,author,price,type); int currentSize= bookList.getUseSize(); bookList.setBook(currentSize,book); bookList.setUseSize(currentSize+1); System.out.println("新增图书成功"); } }
12345678910111213141516171819202122232425262728293031323334353637383940

BorrowOperation

package operation; import book.Book; import book.BookList; import java.util.Scanner; public class BorrowOperation implements IOperation { @Override public void work(BookList bookList) { System.out.println("借阅图书___"); System.out.println("请输入你要借阅图书的书名:"); Scanner scanner=new Scanner(System.in); String bookName=scanner.nextLine(); int currentSize=bookList.getUseSize(); for (int i = 0; i < currentSize; i++) { Book book=bookList.getBooks(i); if (book.getName().equals(bookName)) { book.setLend(true); System.out.println("借阅成功"); return; } } System.out.println("借阅失败"); } }
1234567891011121314151617181920212223242526272829

DelOperation

package operation; import book.Book; import book.BookList; import java.util.Scanner; public class DelOperation implements IOperation { public void work(BookList bookList){ System.out.println("删除图书___"); System.out.println("请输入你要删除的图书名:"); Scanner scanner=new Scanner(System.in); String bookName=scanner.nextLine(); int currentSize=bookList.getUseSize(); int pos=-1; int i = 0; for (; i < currentSize; i++) { Book book=bookList.getBooks(i); if (book.getName().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.setBook(j,book); } //修改useSize bookList.setUseSize(currentSize-1); bookList.setBook(currentSize-1,null); System.out.println("删除成功"); } }
1234567891011121314151617181920212223242526272829303132333435363738394041424344

ExitOperation

package operation; import book.BookList; public class ExitOperation implements IOperation { public void work(BookList bookList){ System.out.println("退出系统___"); int currentSize=bookList.getUseSize(); for (int i = 0; i < currentSize; i++) { bookList.setBook(i,null); } System.exit(0); } }
12345678910111213141516

FindOperation

package operation; import book.Book; import book.BookList; import java.util.Scanner; public class FindOperation implements IOperation{ public void work(BookList bookList){ System.out.println("查找图书___"); System.out.println("请输入你要查找的图书名:"); Scanner scanner=new Scanner(System.in); String bookName=scanner.nextLine(); int currentSize=bookList.getUseSize(); for (int i = 0; i < currentSize; i++) { Book book=bookList.getBooks(i); if (book.getName().equals(bookName)){ System.out.println("找到了这本书:"); System.out.println(book); return; } } System.out.println("没有你要找的书。"); } }
1234567891011121314151617181920212223242526

IOperation

package operation; import book.BookList; public interface IOperation { void work(BookList bookList); } 1234567

ReturnOperation

package operation; import book.Book; import book.BookList; import java.util.Scanner; public class ReturnOperation implements IOperation { @Override public void work(BookList bookList) { System.out.println("归还图书___"); System.out.println("请输入你要归还图书的书名:"); Scanner scanner=new Scanner(System.in); String bookName=scanner.nextLine(); int currentSize=bookList.getUseSize(); for (int i = 0; i < currentSize; i++) { Book book=bookList.getBooks(i); if (book.getName().equals(bookName)) { book.setLend(false); System.out.println("归还成功"); return; } } System.out.println("归还失败"); } }
12345678910111213141516171819202122232425262728

ShowOperation

package operation; import book.Book; import book.BookList; public class ShowOperation implements IOperation { public void work(BookList bookList){ System.out.println("显示图书___"); int currentSize=bookList.getUseSize(); for (int i = 0; i < currentSize; i++) { Book book=bookList.getBooks(i); System.out.println(book); } } }
12345678910111213141516

AdminUser

package user; import operation.*; import java.util.Scanner; public class AdminUser extends User{ public AdminUser(String name) { super(name); this.iOperations=new IOperation[]{ new ExitOperation(), new FindOperation(), new AddOperation(), new DelOperation(), new ShowOperation() }; } 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; } }
123456789101112131415161718192021222324252627282930313233

NormalUser

package user; import operation.*; import java.util.Scanner; public class NormalUser extends User{ public NormalUser(String name) { super(name); this.iOperations=new IOperation[]{ new ExitOperation(), new FindOperation(), new BorrowOperation(), new ReturnOperation() }; } public int menu(){ System.out.println("******普通用户菜单******"); System.out.println("1.查找图书"); System.out.println("2.借阅图书"); System.out.println("3.归还图书"); System.out.println("0.退出系统"); System.out.println("*********************"); System.out.println("请输入你的操作:"); Scanner scanner=new Scanner(System.in); int choice=scanner.nextInt(); return choice; } }
1234567891011121314151617181920212223242526272829

User

package user; import book.BookList; import operation.IOperation; public abstract class User { protected String name; protected IOperation[] iOperations; public User(String name){ this.name=name; } public abstract int menu(); public void doIoperations(int choice, BookList bookList){ this.iOperations[choice].work(bookList); } }
1234567891011121314151617

Main

import book.BookList; import user.AdminUser; import user.NormalUser; import user.User; import java.util.Scanner; public class Main { public static User login(){ Scanner scanner=new Scanner(System.in); System.out.println("请输入你的姓名:"); String name= scanner.nextLine(); System.out.println("请输入你的身份:1.管理员 2.普通用户"); int choice= scanner.nextInt(); if (choice == 1) { AdminUser adminUser=new AdminUser(name); return adminUser; }else { NormalUser normalUser=new NormalUser(name); return normalUser; } } public static void main(String[] args) { BookList bookList=new BookList(); User user=login(); while (true){ int choice=user.menu(); user.doIoperations(choice,bookList); } } }
123456789101112131415161718192021222324252627282930313233

部分测试截图:

Ongwu博客 版权声明:以上内容未经允许不得转载!授权事宜或对内容有异议或投诉,请联系站长,将尽快回复您,谢谢合作!

展开全文READ MORE
解锁 JavaScript ES6:函数与对象的高级扩展功能 【C语言】函数执行背后的秘密:函数栈帧的创建和销毁超详解

游客 回复需填写必要信息