图书管理系统
代码主要分为四个大部分,主函数,图书相关函数,操作,以及使用者。分为三个包:book、operation、user和一个单独的主函数。
主函数
import book.BookList;
import user.AdiminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User login() {
System.out.println("请输入姓名:");
Scanner scan=new Scanner(System.in);
String name=scan.nextLine();
System.out.println("请输入你的身份(1->管理员 0->用户):");
int id=scan.nextInt();
if(id==1){
return new AdiminUser(name);
}else{
return new NormalUser(name);
}
}
public static void main(String[] args) {
BookList bookList =new BookList();
while(true) {
User user=login();
int choice = user.menu();
user.doOperation(choice, bookList);//对图书进行相关的操作
}
}
}
图书相关操作(operation包)
操作接口
对于每一个操作都有相近的一部分,刚好可以用接口,在User(使用者)中利用接口类型的数组来进行相关的操作。在下面的User中会具体说。
package operation;
import book.Book;
import book.BookList;
public interface IOpreation {
public void work(BookList booklist);
}
增加图书
增加图书只需要在数组的最后添加即可,于是要利用booklist.getUsedsize();得到图书数组的使用长度,决定要增加的图书应该所在的位置。
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOpreation{
@Override
public void work(BookList booklist) {
System.out.println("新增图书");
int size=booklist.getUsedsize();
Scanner scan=new Scanner(System.in);
System.out.println("请输入图书名称:");
String name=scan.nextLine();
System.out.println("请输入图书作者:");
String anuthor=scan.nextLine();
System.out.println("请输入图书价格:");
int price=scan.nextInt();
System.out.println("请输入图书种类:");
String type=scan.nextLine();
Book book=new Book(name,anuthor,price,type);
booklist.setBooklist(size,book);
System.out.println("新增书籍成功");
booklist.setUsedsize(size+1);
}
}
借阅图书
对于借阅图书只需要将图书信息中的布尔型变量isBorrowed变量变为true即可,首先还是要遍历booklist找到该书。
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOpreation {
public void work(BookList booklist){
System.out.println("借阅图书");
Scanner scan=new Scanner(System.in);
System.out.println("请输入要借阅的书籍名称:");
String name=scan.nextLine();
int size=booklist.getUsedsize();
for (int i = 0; i <size ; i++) {
Book book=booklist.getBook(i);
if(book.getName().equals(name)){
book.setBorrowed(true);
System.out.println("借阅成功");
System.out.println("该书籍的信息为:");
System.out.println(book);
return;
}
}
System.out.println("该书籍不存在");
}
}
删除书籍
类似于借阅书籍,首先要先找到你要删除的书籍,因此设置一个中间变量,index来判断是否存在该书籍,对于书籍同样也是要遍历booklist,对于删除操作只需要用后一个覆盖前一个的内容即利用for循环前移即可。
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DeleteOperation implements IOpreation{
@Override
public void work(BookList booklist) {
System.out.println("删除图书");
Scanner scan=new Scanner(System.in);
System.out.println("请输入要删除的图书名称:");
String name= scan.nextLine();
int size=booklist.getUsedsize();
int index=-1;
for (int i = 0; i <size ; i++) {
Book book=booklist.getBook(i);
if(book.getName().equals(name)){
index=i;
break;
}
}
if(index==-1){
System.out.println("不存在你想要删除的书籍");
}
//删除操作
for (int i = index; i <size-1 ; i++) {
Book book=booklist.getBook(i+1);
booklist.setBooklist(i,book);
}
System.out.println("删除成功");
booklist.setUsedsize(size-1);
}
}
显示书籍
同样利用booklist.getUsedsize();得到数组的使用长度,然后利用for循环将每本书依次打印出来即可。
package operation;
import book.Book;
import book.BookList;
import java.util.Arrays;
public class DisplayOperation implements IOpreation{
@Override
public void work(BookList booklist) {
System.out.println("显示图书");
int size=booklist.getUsedsize();
for (int i = 0; i < size; i++) {
Book book=booklist.getBook(i);
System.out.println(book);
}
}
}
退出系统
package operation;
import book.Book;
import book.BookList;
public class ExitOperation implements IOpreation{
@Override
public void work(BookList booklist) {
System.out.println("退出系统");
System.exit(0);
}
}
查找图书
主要是比较booklist中图书的名字与输入的名字之间是否相同,要先将数组中的某一本图书赋给一个Book类的变量book然后才能进行比较(Book book=booklist.getBook(i);)book.getName().equals(name)。
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOpreation{
@Override
public void work(BookList booklist) {
System.out.println("查找图书");
Scanner scan=new Scanner(System.in);
System.out.println("请输入要查找的书籍名称:");
String name=scan.nextLine();
int size=booklist.getUsedsize();
for (int i = 0; i <size ; i++) {
Book book=booklist.getBook(i);
if(book.getName().equals(name)){
System.out.println("查找成功,该书籍的信息为:");
System.out.println(book);
return;
}
}
System.out.println("不存在你要查找的书籍");
}
}
归还书籍
和借阅图书进行对比可只,借阅图书是将isBorrowed变量由false变为true,那么归还图书就是将isBorrowed变量由true变为false.
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOpreation{
@Override
public void work(BookList booklist) {
System.out.println("归还图书");
Scanner scan=new Scanner(System.in);
System.out.println("请输入要归还的书籍名称:");
String name=scan.nextLine();
int size=booklist.getUsedsize();
for (int i = 0; i <size ; i++) {
Book book=booklist.getBook(i);
if(book.getName().equals(name)){
book.setBorrowed(false);
System.out.println("归还成功");
System.out.println("该书籍的信息为:");
System.out.println(book);
break;
}
}
}
}
图书及“书架”(book包)
图书
需要说明的是在重写的toString函数中(toString函数在Object类中,它被认为是所有类的父类)(isBorrowed == true ? " 已借出 “:” 未借出 ") 将true和false转换为文字。
package book;
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean isBorrowed;
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
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;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\t' +
", author='" + author + '\t' +
", price=" + price +
", type='" + type + '\t' +
(isBorrowed == true ? " 已借出 ":" 未借出 ") +
'}';
}
}
“书架”
书架就可以看成以Book为基本数据类型的一个数组,要设立数组使用长度,便于后续操作。
package book;
public class BookList {
private Book[] booklist=new Book[10];
private int usedsize;
public BookList(){
this.booklist[0]=new Book("三国演义","罗贯中",28,"小说");
this.booklist[1]=new Book("水浒传","施耐庵",36,"小说");
this.booklist[2]=new Book("红楼梦","曹雪芹",56,"小说");
this.booklist[3]=new Book("西游记","吴承恩",35,"小说");
this.usedsize=4;
}
public int getUsedsize() {
return usedsize;
}
public void setUsedsize(int usedsize) {
this.usedsize = usedsize;
}
public void setBooklist(int pos,Book book){
booklist[pos]=book;
}
public Book getBook(int pos){
return booklist[pos];
}
}
使用人群(user包)
user父类
这里有一个protected IOpreation[] iOpreations;即接口类型的数组,主要是在它的子类中,在构造函数中需要根据它们各自的功能实例化各个操作。
package user;
import book.BookList;
import operation.IOpreation;
public abstract class User {
protected String name;
protected IOpreation[] iOpreations;
public User(String name){
this.name=name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList){
this.iOpreations[choice].work(bookList);
}
}
普通使用人员
上面说的 IOpreation[] iOpreations,在这里就实例化了
this.iOpreations=new IOpreation[]{
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
new ExitOperation()
对于选择的操作序号我们也要返回,从而在主函数中调用User类中的doOperation操作,要注意在上面对操作进行实例化时要与下面菜单的顺序一致。文章来源:https://www.toymoban.com/news/detail-492918.html
package user;
import operation.*;
import java.util.Scanner;
public class NormalUser extends User {
public NormalUser(String name) {
super(name);
this.iOpreations=new IOpreation[]{
new FindOperation(),
new BorrowOperation(),
new ReturnOperation(),
new ExitOperation()
};
}
public int menu(){
System.out.println("********普通人员菜单*********");
System.out.println("欢迎"+name+"进入图书借阅系统");
System.out.println("0 查找书籍");
System.out.println("1 借阅书籍");
System.out.println("2 归还书籍");
System.out.println("3 退出系统");
Scanner scan=new Scanner(System.in);
int choice=scan.nextInt();
return choice;
}
}
管理员
与上面的普通人员除了操作不同之外,其他都是同样的道理。文章来源地址https://www.toymoban.com/news/detail-492918.html
package user;
import operation.*;
import java.util.Scanner;
public class AdiminUser extends User{
public AdiminUser(String name) {
super(name);
this.iOpreations=new IOpreation[]{
new DisplayOperation(),
new DeleteOperation(),
new AddOperation(),
new FindOperation(),
new ExitOperation()
};
}
public int menu() {
System.out.println("********管理员菜单**********");
System.out.println("欢迎"+name+"进入管理员菜单");
System.out.println("0 显示书籍");
System.out.println("1 删除书籍");
System.out.println("2 增加书籍");
System.out.println("3 查找书籍");
System.out.println("4 退出系统");
System.out.println("***************************");
Scanner scan = new Scanner(System.in);
int choice = scan.nextInt();
return choice;
}
}
到了这里,关于JAVA图书管理系统详细代码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!