图书管理系统(java 代码实现)

1.Book和BookList类的实现


  先创建一个book包,里面包含,Book和BookList(书架)类。

Book 类里面有属性 书名,作者,价格,类型,是否被借出。 

BookList 类里面有 Book类型的数组,用来存放各种书籍。还有总共有几本书用 countlist表示。


代码:

package book;

public class Book {
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean isborrow;

    public Book(String name, String author, int price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
    }

    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 isIsborrow() {
        return isborrow;
    }

    public void setIsborrow(boolean isborrow) {
        this.isborrow = isborrow;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ( (isborrow == true) ? ",已借出" : ",未借出" )+
                '}';
    }
}//重写toString 方便后续打印。
package book;

public class BookList {
    private Book[] books;
    private int usedSize;
    public BookList(){
        this.books = new Book[10];
        this.books[0] = new Book("三国演义","罗贯中",10,"小说");
        this.books[1] = new Book("西游记","吴承恩",9,"小说");
        this.books[2] = new Book("红楼梦","罗曹雪芹",15,"小说");
        this.usedSize = 3;
    }


    public Book getBooks(int pos) {
        return books[pos];
    }//获取图书的getBooks成员方法.

    public void setBooks(Book books , int pos) {
        this.books[pos] = books;
    }//存放或修改图书的setBooks方法。

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }
}

2.创建登入界面


  登入界面分为 管理员界面和普通用户界面,分别用AdminUser和Normal表示,都放到user包里面。

还有一个抽象类User,为了方便使用管理员和普通用户的共同属性,在主方法里面存有进行选择是管理员身份还是普通用户的方法。

如代码:

package user;


public abstract class User{

    public String name;
    public int usedSize;

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }

    public User(String name) {
        this.name = name;
    }
    public abstract int menu();
}
import java.util.Scanner;
public class AdminUser extends User{
    public AdminUser(String name){
        super(name);
}
    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 NormalUser extends User{
    public NormalUser(String name){
        super(name);
  }

    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;
    }
}
import java.util.Scanner;

public class Main {
    public static User login(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入姓名:");
        String name = sc.nextLine();
        System.out.println("********1.管理员   2.普通用户*********");
        int choice = sc.nextInt();
        if(choice == 1){
            return new AdminUser(name);//返回了管理员的引用
        }
        else{
            return new NormalUser(name);//返回了普通用户的调用
        }
    }
    public static void main(String[] args) {
        BookList bookList = new BookList();
        User user = login();//调用login()方法。
        while (true) {
            int choice = user.menu();
        }
    }
}

3.建立各种功能的基本框架


功能分别有:

1. 新增书籍 AddOperation

2. 删除书籍 DelOperation

3. 显示书籍 ShowOperation

4. 借阅书籍 BorrowOperation

5. 归还书籍 ReturnOperation

6. 退出系统 Exit

这些功能我们都放在 operation 包里面,同时我们需要创一个接口,目的为了类型统一,用接口的类型定义一个数组,来存放各种类型的引用。

  并且定义抽象的 work 方法,也是为了统一,当对象引用该方法时,会发生多态,这样可以使代码更简单。

如代码:

public interface IOpreation {
    public void work(BookList bookList);
}
public class AddOperation implements IOpreation {
    public void work(BookList bookList){
       System.out.println("新增书籍");
 }
}
public class FindOperation implements IOpreation{
    public void work(BookList bookList){
        System.out.println("查询书籍");
 }
}
public class DelOperation implements IOpreation{
    public void work(BookList bookList){
        System.out.println("删除书籍");
 }
}
public class BorrowOpreation implements IOpreation{
    public void work(BookList bookList){
        System.out.println("借阅书籍");
 }
}
public class ReturnOpreation implements IOpreation{
    public void work(BookList bookList){
        System.out.println("归还书籍");
 }
}
public class ShowOpreation implements IOpreation{
    public void work(BookList bookList){
        System.out.println("显示书籍");
 }
}
public class ExitOpreation implements IOpreation{
    public void work(BookList bookList){
        System.out.println("退出系统");
 }
}

4.构建一个数组,来存放各项功能,进而调用。

  我们菜单已经做好了,我们在调用各类方法的时候肯定是从菜单中调用的,所以我们定义数组的时候肯定在User里面定义。

  然后子类再进行初始化。

如代码:

​
public abstract class User{

    public String name;
    public IOpreation [] iOpreations;
    public int usedSize;

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }

    public User(String name) {
        this.name = name;
    }
    public abstract int menu();

    public void doOpration(int choice,BookList bookList){
        this.iOpreations[choice].work(bookList);
    }//调用数组内部的引用方法。
}

​
public class AdminUser extends User{
    public AdminUser(String name){
        super(name);
            this.iOpreations = new IOpreation[]{
                    new ExitOpreation(),
                    new FindOperation(),
                    new AddOperation(),
                    new DelOperation(),
                    new ShowOpreation()
            };
        }
}
public class NormalUser extends User{
    public NormalUser(String name){
        super(name);
            this.iOpreations = new IOpreation[]{
                    new ExitOpreation(),
                    new FindOperation(),
                    new BorrowOpreation(),
                    new ReturnOpreation()
            };
        }
}
public class Main {
    public static User login(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入姓名:");
        String name = sc.nextLine();
        System.out.println("********1.管理员   2.普通用户*********");
        int choice = sc.nextInt();
        if(choice == 1){
            return new AdminUser(name);
        }
        else{
            return new NormalUser(name);
        }
    }
    public static void main(String[] args) {
        BookList bookList = new BookList();
        User user = login();
        while (true) {
            int choice = user.menu();
            user.doOpration(choice,bookList);//选择要调用的方法。
        }
    }
}

5.各项功能的具体实现

1.新增图书

  首先我们需要输入,相应的属性,并在创建一个对象,用来存放。

  其次遍历一遍原本的数组,看有没有重的,如果有直接返回。

  最后我们需要调用 BookList 中的 bookList.setBooks() 方法 存放该书籍,书的个数相应加一。

如代码:

public class AddOperation implements IOpreation {
    public void work(BookList bookList){

        System.out.println("新增书籍");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入新增书籍的书名:");
        String name = sc.nextLine();
        System.out.println("请输入新增书籍的作者:");
        String author = sc.nextLine();
        System.out.println("请输入新增书籍的价格:");
        int price = sc.nextInt();
        System.out.println("请输入新增书籍的类型:");
        String str = sc.nextLine();//载入上一个回车。
        String type = sc.nextLine();
        Book book = new Book(name,author,price,type);
        int countSize = bookList.getUsedSize();
        int i = 0;
        for(; i < countSize; i++){
            if(bookList.getBooks(i).getName().equals(name)){
                System.out.println("该书已存在");
                return;
            }
        }
        bookList.setBooks(book,countSize);
        bookList.setUsedSize(countSize+1);
        System.out.println("新增成功");
    }
}

2.删除图书

  首先需要遍历一遍数组,找出所要删除的图书的下标,记录下表,对数组改下标后面的元素,向前挪一位。

  然后对最后那个位置赋值为空。

  最后书的总数减一。

如代码:

public class DelOperation implements IOpreation{
    public void work(BookList bookList){
        System.out.println("删除书籍");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除书籍名称:");
        String name = sc.nextLine();
        int countSize = bookList.getUsedSize();
        int i = 0;
        for( ; i <= countSize; i++){
            if(bookList.getBooks(i).getName().equals(name)){
                break;
            }
        }
        if(i >= countSize) {
            System.out.println("没有你要删除的图书!");
            return;
        }
        for (int j = i; j < countSize-1; j++) {
            Book book = bookList.getBooks(j+1);
            bookList.setBooks(book,j);
        }

        bookList.setBooks(null,countSize-1);

        bookList.setUsedSize(countSize-1);
        System.out.println("删除成功!");
    }
   }

以上介绍了两个功能,剩下的功能都比较简单,大家可以试着写一下。


6.总代码

项目名称及其各类名称:


Book类:

package book;

public class Book {
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean isborrow;

    public Book(String name, String author, int price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
    }

    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 isIsborrow() {
        return isborrow;
    }

    public void setIsborrow(boolean isborrow) {
        this.isborrow = isborrow;
    }

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

BookList类:

package book;

public class BookList {
    private Book[] books;
    private int usedSize;
    public BookList(){
        this.books = new Book[10];
        this.books[0] = new Book("三国演义","罗贯中",10,"小说");
        this.books[1] = new Book("西游记","吴承恩",9,"小说");
        this.books[2] = new Book("红楼梦","罗曹雪芹",15,"小说");
        this.usedSize = 3;
    }


    public Book getBooks(int pos) {
        return books[pos];
    }

    public void setBooks(Book books , int pos) {
        this.books[pos] = books;
    }

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }
}

AddOperation类:

package operation;
import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOpreation {
    public void work(BookList bookList){

        System.out.println("新增书籍");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入新增书籍的书名:");
        String name = sc.nextLine();
        System.out.println("请输入新增书籍的作者:");
        String author = sc.nextLine();
        System.out.println("请输入新增书籍的价格:");
        int price = sc.nextInt();
        System.out.println("请输入新增书籍的类型:");
        String str = sc.nextLine();
        String type = sc.nextLine();
        Book book = new Book(name,author,price,type);
        int countSize = bookList.getUsedSize();
        int i = 0;
        for(; i < countSize; i++){
            if(bookList.getBooks(i).getName().equals(name)){
                System.out.println("该书已存在");
                return;
            }
        }
        bookList.setBooks(book,countSize);
        bookList.setUsedSize(countSize+1);
        System.out.println("新增成功");
    }
}

BorrowOperation类:

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class BorrowOpreation implements IOpreation{
    public void work(BookList bookList){
        System.out.println("借阅书籍");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要借阅的书籍:");
        String name = sc.nextLine();
        int countSize = bookList.getUsedSize();
        int i = 0;
        for(; i < countSize; i++){
            if(bookList.getBooks(i).getName().equals(name)){
                break;
            }
        }
        Book tmp = bookList.getBooks(i);
        tmp.setIsborrow(true);
        bookList.setBooks(tmp,i);
        System.out.println("借阅成功");
    }
}

DelOperation类:

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOpreation{
    public void work(BookList bookList){
        System.out.println("删除书籍");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除书籍名称:");
        String name = sc.nextLine();
        int countSize = bookList.getUsedSize();
        int i = 0;
        for( ; i <= countSize; i++){
            if(bookList.getBooks(i).getName().equals(name)){
                break;
            }
        }
        if(i >= countSize) {
            System.out.println("没有你要删除的图书!");
            return;
        }
        for (int j = i; j < countSize-1; j++) {
            Book book = bookList.getBooks(j+1);
            bookList.setBooks(book,j);
        }

        bookList.setBooks(null,countSize-1);

        bookList.setUsedSize(countSize-1);
        System.out.println("删除成功!");
    }
    }

ExitOperation类:

package operation;

import book.BookList;

public class ExitOpreation implements IOpreation{
    public void work(BookList bookList){
        System.out.println("退出系统");
        System.exit(0);
    }
}

FindOperation类:

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class FindOperation implements IOpreation{
    public void work(BookList bookList){
        System.out.println("查询书籍");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查找图书的书名:");
        String name = sc.nextLine();
        int countSize = bookList.getUsedSize();
        int i = 0;
        for(; i < countSize; i++){
            if(bookList.getBooks(i).getName().equals(name)){
                System.out.println(bookList.getBooks(i));
                break;
            }
        }
        if(i >= countSize){
            System.out.println("你所找的书籍不存在");
        }
    }
}

IOperation接口:

package operation;

import book.BookList;

public interface IOpreation {
    public void work(BookList bookList);
}

ReturnOperation类:

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class ReturnOpreation implements IOpreation{
    public void work(BookList bookList){
        System.out.println("归还书籍");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要归还的书籍:");
        String name = sc.nextLine();
        int countSize = bookList.getUsedSize();
        int i = 0;
        for(; i <= countSize; i++){
            if(bookList.getBooks(i).getName().equals(name)){
                break;
            }
        }
        Book tmp = bookList.getBooks(i);
        tmp.setIsborrow(false);
        bookList.setBooks(tmp,i);
        System.out.println("归还成功");
    }
}

ShowOperation类:

package operation;

import book.BookList;

import java.util.Scanner;

public class ShowOpreation implements IOpreation{
    public void work(BookList bookList){
        System.out.println("显示书籍");
        Scanner sc = new Scanner(System.in);
        int countSize = bookList.getUsedSize();
        int i = 0;
        for(; i < countSize; i++){
            System.out.println(bookList.getBooks(i));
        }
    }
}

User类:

package user;

import book.BookList;
import operation.*;

public abstract class User{

    public String name;
    public IOpreation [] iOpreations;
    public int usedSize;

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }

    public User(String name) {
        this.name = name;
    }
    public abstract int menu();
    public void doOpration(int choice,BookList bookList){
        this.iOpreations[choice].work(bookList);
    }
}

AdminUser类:

package user;
import operation.*;

import java.util.Scanner;
public class AdminUser extends User{
    public AdminUser(String name){
        super(name);
            this.iOpreations = new IOpreation[]{
                    new ExitOpreation(),
                    new FindOperation(),
                    new AddOperation(),
                    new DelOperation(),
                    new ShowOpreation()
            };
        }
    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;
    }

}

NormalUser类:

package user;
import operation.*;

import java.util.Scanner;
public class NormalUser extends User{
    public NormalUser(String name){
        super(name);
            this.iOpreations = new IOpreation[]{
                    new ExitOpreation(),
                    new FindOperation(),
                    new BorrowOpreation(),
                    new ReturnOpreation()
            };
        }

    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;
    }
}

Main类:

import book.BookList;
import operation.IOpreation;
import user.AdminUser;
import user.NormalUser;
import user.User;

import java.nio.file.attribute.UserPrincipal;
import java.util.Scanner;

public class Main {
    public static User login(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入姓名:");
        String name = sc.nextLine();
        System.out.println("********1.管理员   2.普通用户*********");
        int choice = sc.nextInt();
        if(choice == 1){
            return new AdminUser(name);
        }
        else{
            return new NormalUser(name);
        }
    }
    public static void main(String[] args) {
        BookList bookList = new BookList();
        User user = login();
        while (true) {
            int choice = user.menu();
            user.doOpration(choice,bookList);
        }
    }
}

以上就是图书管理系统的实现。

谢谢大家。

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值